Created
July 8, 2022 09:12
-
-
Save rjmacarthy/4018a738d4ab81968a55ce9641cafa8c to your computer and use it in GitHub Desktop.
Generate a Bitcoin P2TR Address
This file contains 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
const ECPairFactory = require('ecpair').ECPairFactory | |
const ecurve = require('ecurve') | |
const secp256k1 = ecurve.getCurveByName('secp256k1') | |
const schnorr = require('bip-schnorr') | |
const bech32 = require('bech32').bech32 | |
const bech32m = require('bech32').bech32m | |
const tinysecp = require('tiny-secp256k1'); | |
const ECPair = ECPairFactory(tinysecp); | |
function getP2TRAddress(keyPair) { | |
const pubKey = ecurve.Point.decodeFrom(secp256k1, keyPair.publicKey) | |
const taprootPubkey = schnorr.taproot.taprootConstruct(pubKey) | |
const words = bech32.toWords(taprootPubkey) | |
words.unshift(1) | |
return bech32m.encode('bc',words) | |
} | |
const keyPair = ECPair.makeRandom(); | |
console.log(getP2TRAddress(keyPair)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment