Created
August 23, 2024 08:10
-
-
Save akki91/f6b0c4c326398341f5e8f04e57206212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Run node get_btc_address.js | |
| const prompts = require("prompts"); | |
| const ecc = require("tiny-secp256k1"); | |
| const ecpair = require("ecpair"); | |
| const ECPair = ecpair.ECPairFactory(ecc); | |
| async function run() { | |
| const cliPrompts = [ | |
| { | |
| type: "text", | |
| name: "publicKey", | |
| message: | |
| "Generic public key from turnkey (hex-encoded, starts with 04, 66 characters long)", | |
| }, | |
| ]; | |
| const { publicKey } = await prompts(cliPrompts); | |
| const pair = ECPair.fromPublicKey(Buffer.from(publicKey, "hex")); | |
| const compressedPublicKey = pair.publicKey.toString("hex"); | |
| console.log("Compressed public key: " + compressedPublicKey); | |
| const p2shAddress = bitcoin.payments.p2sh({ | |
| redeem: bitcoin.payments.p2wpkh({ | |
| pubkey: pair.publicKey, | |
| network: bitcoin.networks.testnet, | |
| }), | |
| }).address; | |
| console.log("Testnet P2SH address: " + p2shAddress); | |
| // here's how can derive a P2PKH address (Pay-to-Public-Key-Hash) | |
| // P2PKH addresses are still in use in the Bitcoin ecosystem, but are | |
| // being phased out in favor of P2SH or native SegWit addresses. | |
| // ---- | |
| // let p2pkhAddress = bitcoin.payments.p2pkh({ | |
| // pubkey: pair.publicKey, | |
| // network: bitcoin.networks.testnet, | |
| // }).address; | |
| // console.log("Testnet P2PKH address: " + p2pkhAddress); | |
| } | |
| run().then((res) => console.log("...done!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment