Skip to content

Commit

Permalink
Merge pull request #1282 from xchainjs/btc-taproot-example
Browse files Browse the repository at this point in the history
Bitcoin taproot example
  • Loading branch information
0xp3gasus authored Oct 14, 2024
2 parents 74292f0 + bc10cec commit df104e2
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/bitcoin/.codesandbox/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// These tasks will run in order when initializing your CodeSandbox project.
"setupTasks": [
{
"name": "Install Dependencies",
"command": "yarn install"
}
]
}
4 changes: 4 additions & 0 deletions examples/bitcoin/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Devcontainer",
"image": "ghcr.io/codesandbox/devcontainers/typescript-node:latest"
}
2 changes: 2 additions & 0 deletions examples/bitcoin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# xchainjs-bitcoin

16 changes: 16 additions & 0 deletions examples/bitcoin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Bitcoin

Bitcoin examples to show different use cases using its client

## Examples

### Address

#### Get Taproot address by index

Check out how you should get your Taproot account address at certain index in this [example](https://github.com/xchainjs/xchainjs-lib/blob/master/examples/solana/address.ts) or run it as

```sh
yarn taprootAddress phrase index
```

21 changes: 21 additions & 0 deletions examples/bitcoin/address-taproot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AddressFormat, Client, defaultBTCParams, tapRootDerivationPaths } from '@xchainjs/xchain-bitcoin'

const main = async () => {
const phrase = `${process.argv[2]}`
const index = process.argv[3] ? Number(process.argv[3]) : 0

const client = new Client({
...defaultBTCParams,
phrase,
rootDerivationPaths: tapRootDerivationPaths,
addressFormat: AddressFormat.P2TR,
})

const address = await client.getAddressAsync(index)

console.log(`Your Taproot account at index ${index} is ${address}`)
}

main()
.then(() => process.exit(0))
.catch((err) => console.error(err))
21 changes: 21 additions & 0 deletions examples/bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "xchainjs-bitcoin",
"private": true,
"version": "0.0.1",
"scripts": {
"taprootAddress": "npx ts-node address-taproot.ts",
"build": "tsc --noEmit"
},
"description": "Examples using Bitcoin client",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@xchainjs/xchain-bitcoin": "workspace:*",
"@xchainjs/xchain-util": "workspace:*"
},
"devDependencies": {
"@types/node": "20.11.28",
"ts-node": "10.9.2",
"typescript": "^5.0.4"
}
}
15 changes: 15 additions & 0 deletions examples/bitcoin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noEmitOnError": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": [
"es6",
"dom",
"es2016",
"es2017"
]
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13686,6 +13686,18 @@ __metadata:
languageName: unknown
linkType: soft

"xchainjs-bitcoin@workspace:examples/bitcoin":
version: 0.0.0-use.local
resolution: "xchainjs-bitcoin@workspace:examples/bitcoin"
dependencies:
"@types/node": "npm:20.11.28"
"@xchainjs/xchain-bitcoin": "workspace:*"
"@xchainjs/xchain-util": "workspace:*"
ts-node: "npm:10.9.2"
typescript: "npm:^5.0.4"
languageName: unknown
linkType: soft

"xchainjs-check-tx@workspace:examples/check-tx":
version: 0.0.0-use.local
resolution: "xchainjs-check-tx@workspace:examples/check-tx"
Expand Down

0 comments on commit df104e2

Please sign in to comment.