From 2e27e3fbdb65d145ffb14709a9713bff726ef6c6 Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Sat, 21 Sep 2024 17:17:17 +0200 Subject: [PATCH] Bitcoin taproot example --- examples/bitcoin/.codesandbox/tasks.json | 9 ++++++++ .../bitcoin/.devcontainer/devcontainer.json | 4 ++++ examples/bitcoin/CHANGELOG.md | 2 ++ examples/bitcoin/README.md | 16 ++++++++++++++ examples/bitcoin/address-taproot.ts | 21 +++++++++++++++++++ examples/bitcoin/package.json | 21 +++++++++++++++++++ examples/bitcoin/tsconfig.json | 15 +++++++++++++ yarn.lock | 12 +++++++++++ 8 files changed, 100 insertions(+) create mode 100644 examples/bitcoin/.codesandbox/tasks.json create mode 100644 examples/bitcoin/.devcontainer/devcontainer.json create mode 100644 examples/bitcoin/CHANGELOG.md create mode 100644 examples/bitcoin/README.md create mode 100644 examples/bitcoin/address-taproot.ts create mode 100644 examples/bitcoin/package.json create mode 100644 examples/bitcoin/tsconfig.json diff --git a/examples/bitcoin/.codesandbox/tasks.json b/examples/bitcoin/.codesandbox/tasks.json new file mode 100644 index 000000000..fff1c37f8 --- /dev/null +++ b/examples/bitcoin/.codesandbox/tasks.json @@ -0,0 +1,9 @@ +{ + // These tasks will run in order when initializing your CodeSandbox project. + "setupTasks": [ + { + "name": "Install Dependencies", + "command": "yarn install" + } + ] +} \ No newline at end of file diff --git a/examples/bitcoin/.devcontainer/devcontainer.json b/examples/bitcoin/.devcontainer/devcontainer.json new file mode 100644 index 000000000..9cef189a7 --- /dev/null +++ b/examples/bitcoin/.devcontainer/devcontainer.json @@ -0,0 +1,4 @@ +{ + "name": "Devcontainer", + "image": "ghcr.io/codesandbox/devcontainers/typescript-node:latest" +} \ No newline at end of file diff --git a/examples/bitcoin/CHANGELOG.md b/examples/bitcoin/CHANGELOG.md new file mode 100644 index 000000000..d25e9d9a8 --- /dev/null +++ b/examples/bitcoin/CHANGELOG.md @@ -0,0 +1,2 @@ +# xchainjs-bitcoin + diff --git a/examples/bitcoin/README.md b/examples/bitcoin/README.md new file mode 100644 index 000000000..6eed350d4 --- /dev/null +++ b/examples/bitcoin/README.md @@ -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 +``` + diff --git a/examples/bitcoin/address-taproot.ts b/examples/bitcoin/address-taproot.ts new file mode 100644 index 000000000..44cc78e12 --- /dev/null +++ b/examples/bitcoin/address-taproot.ts @@ -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)) diff --git a/examples/bitcoin/package.json b/examples/bitcoin/package.json new file mode 100644 index 000000000..a11793f66 --- /dev/null +++ b/examples/bitcoin/package.json @@ -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" + } +} \ No newline at end of file diff --git a/examples/bitcoin/tsconfig.json b/examples/bitcoin/tsconfig.json new file mode 100644 index 000000000..7b9e0e966 --- /dev/null +++ b/examples/bitcoin/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noEmitOnError": true, + "resolveJsonModule": true, + "esModuleInterop": true, + "lib": [ + "es6", + "dom", + "es2016", + "es2017" + ] + } +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 520fe79ab..8b5264647 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13668,6 +13668,18 @@ __metadata: languageName: node linkType: hard +"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"