Skip to content

Instantly share code, notes, and snippets.

@juscamarena
Last active August 30, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juscamarena/8b3928599f4c9658a39cf30f632eea95 to your computer and use it in GitHub Desktop.
Save juscamarena/8b3928599f4c9658a39cf30f632eea95 to your computer and use it in GitHub Desktop.
Generating Segwit Addresses bip49
const bitcoin = require('bitcoinjs-lib')
const assert = require('assert')
// example mnemonic: bless slide survey easy fossil river audit excess cry kingdom very valid country coffee addict
// acount extended pub key m/49'/0'/0'/0
const XPUB = 'xpub6DSniLEmH3haqnDkeV8vEbCaJeWn3NybeiHajgPY2TktB7DovU1ba6JoDn3JDYZNj8hSAKNCdAMMcrDPYGYa4Rg1ZGtB6p9Yv5EJqLwSPn5';
index = 0
let hdNode = bitcoin.HDNode.fromBase58(XPUB);
let pubKey = hdNode.derivePath("0/" + index).keyPair.getPublicKeyBuffer();
let witnessScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(witnessScript))
//console.log(scriptPubKey);
let address = bitcoin.address.fromOutputScript(scriptPubKey)
console.log(address);
assert.strictEqual(address, "3N7vBWpEY2kzUAdUuGFZUtcwGbMn5xzBNk");
@juscamarena
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment