Skip to content

Instantly share code, notes, and snippets.

@Anderson-Juhasc
Last active December 10, 2022 09:27
Show Gist options
  • Save Anderson-Juhasc/16a6cc19e5d59855ff6ee0fbbd7cbd94 to your computer and use it in GitHub Desktop.
Save Anderson-Juhasc/16a6cc19e5d59855ff6ee0fbbd7cbd94 to your computer and use it in GitHub Desktop.
Taproot(p2tr) with BIP38
const { ECPairFactory } = require('ecpair')
const bitcoin = require('bitcoinjs-lib')
const ecurve = require('ecurve')
const secp256k1 = ecurve.getCurveByName('secp256k1')
const schnorr = require('bip-schnorr')
const { bech32, bech32m } = require('bech32')
const bip38 = require('bip38')
const wif = require('wif')
const tinysecp = require('tiny-secp256k1')
const ECPair = ECPairFactory(tinysecp);
function getP2TRAddress(keyPair, network) {
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)
}
var myWifString = '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR'
const keyPair = new ECPair.fromWIF(myWifString)
console.log(getP2TRAddress(keyPair))
var password = 'TestingOneTwoThree'
var decoded = new ECPair.fromWIF(myWifString)
var encryptedKey = bip38.encrypt(decoded.privateKey, decoded.compressed, password)
console.log('encryptedKey', encryptedKey)
var encryptedKey = '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg'
var decryptedKey = bip38.decrypt(encryptedKey, password, function (status) {
//console.log(status.percent) // will print the percent every time current increases by 1000
})
console.log('decryptedKey', wif.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed))
{
"name": "taproot-paper-wallet",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx browserify src/index.js -o src/build.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bech32": "^2.0.0",
"bip-schnorr": "^0.6.6",
"bip38": "^3.1.1",
"bip39": "^3.0.4",
"bitcoinjs-lib": "^6.1.0",
"ecpair": "^2.1.0",
"ecurve": "^1.0.6",
"tiny-secp256k1": "^2.2.1",
"wif": "^2.0.6"
},
"devDependencies": {
"browserify": "^17.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment