Skip to content

Instantly share code, notes, and snippets.

@cryptoskillz
Last active April 22, 2022 17:46
Show Gist options
  • Save cryptoskillz/460eb7be8e2c4426da82fbd4953b1919 to your computer and use it in GitHub Desktop.
Save cryptoskillz/460eb7be8e2c4426da82fbd4953b1919 to your computer and use it in GitHub Desktop.
generate a Bitcoin address from a xpub using bitcoinJs
//test data
//xpub converted from electrum using https://jlopp.github.io/xpub-converter/
const xpub =
'xpub67yMUMbr2gAnBgnYvXcbJq8iUBe54Ev2dbUYKGN8jGY21AHJFeR7mnZqhbUNze4UbpRE9S1fWvmFCsFN4EvU1rWdqegW7dzoa7vZmYCLAAy';
//xpub from electrum https://www.poof.io/help/wallet/electrum
const zpub = "zpub6mdt5gwgL3FjtHAnbFBqj1Kip7vxwUu2TpWyt49uVHHn7MukkxkF1ut7k1PYzTNKR6eqePCnSFULySUVVdkVcKsqaL5MHTdn7a3rYdSEc2K";
//different xpubs for testing taking from https://github.com/peli-pro/coldcard_address_generator/blob/master/coldcard_address_generator_node.js
const xpub_44h_0h_0h = 'xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhawA7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj';
const xpub_49h_0h_0h = 'xpub6C6nQwHaWbSrzs5tZ1q7m5R9cPK9eYpNMFesiXsYrgc1P8bvLLAet9JfHjYXKjToD8cBRswJXXbbFpXgwsswVPAZzKMa1jUp2kVkGVUaJa7';
const xpub_84h_0h_0h = 'xpub6CatWdiZiodmUeTDp8LT5or8nmbKNcuyvz7WyksVFkKB4RHwCD3XyuvPEbvqAQY3rAPshWcMLoP2fMFMKHPJ4ZeZXYVUhLv1VMrjPC7PW6V';
//end test data
//number of addresse
const n = 20;
//index
const index = 1;
const bitcoin = require('bitcoinjs-lib');
//these 3 lines are what makes it work, do not ask me why!
const ecc = require('tiny-secp256k1')
const { BIP32Factory } = require('bip32')
// You must wrap a tiny-secp256k1 compatible implementation
const bip32 = BIP32Factory(ecc)
const network = bitcoin.networks.mainnet // spot the difference
const node = bip32.fromBase58(xpub)
var c;
child_44h_0h_0h = bip32.fromBase58(xpub_44h_0h_0h,network);
child_49h_0h_0h = bip32.fromBase58(xpub_49h_0h_0h);
child_84h_0h_0h = bip32.fromBase58(xpub_84h_0h_0h);
/////////// BIP 44
console.log("\n\nBIP 44 derivation m/44'/0'/0' receive/external and change/internal addresses: ");
console.log("Derivation from this key:", child_44h_0h_0h.toBase58())
console.log("receive/external addresses:");
for (c = 0; c <= n; c++) {
address = bitcoin.payments.p2pkh({pubkey: child_44h_0h_0h.derive(0).derive(c).publicKey}).address;
console.log("m/44'/0'/0'/".concat("0", "/", c, ": ", address))
}
console.log("change/internal addresses:");
for (c = 0; c <= n; c++) {
address = bitcoin.payments.p2pkh({pubkey: child_44h_0h_0h.derive(1).derive(c).publicKey}).address;
console.log("m/44'/0'/0'/".concat("1", "/", c, ": ", address))
}
////////// BIP49
console.log("\n\nBIP 49 derivation m/49'/0'/0' receive/external and change/internal addresses: ");
console.log("Derivation from this key:", child_49h_0h_0h.toBase58())
console.log("receive/external addresses:");
for (c = 0; c <= n; c++) {
//address = bitcoin.payments.p2pkh({pubkey: child_44h_0h_0h.derive(0).derive(c).publicKey}).address;
address = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey: child_49h_0h_0h.derive(0).derive(c).publicKey
}),
}).address;
console.log("m/49'/0'/0'/".concat("0", "/", c, ": ", address))
}
console.log("change/internal addresses:");
for (c = 0; c <= n; c++) {
//address = bitcoin.payments.p2pkh({pubkey: child_44h_0h_0h.derive(0).derive(c).publicKey}).address;
address = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey: child_49h_0h_0h.derive(1).derive(c).publicKey
}),
}).address;
console.log("m/49'/0'/0'/".concat("1", "/", c, ": ", address))
}
//////////// BIP84
console.log("\n\nBIP 84 derivation m/84'/0'/0' receive/external and change/internal addresses: ");
console.log("Derivation from this key:", child_84h_0h_0h.toBase58())
console.log("receive/external addresses:");
for (c = 0; c <= n; c++) {
address = bitcoin.payments.p2wpkh({pubkey: child_84h_0h_0h.derive(0).derive(c).publicKey}).address;
console.log("m/44'/0'/0'/".concat("0", "/", c, ": ", address))
}
console.log("change/internal addresses:");
for (c = 0; c <= n; c++) {
address = bitcoin.payments.p2wpkh({pubkey: child_84h_0h_0h.derive(1).derive(c).publicKey}).address;
console.log("m/44'/0'/0'/".concat("1", "/", c, ": ", address))
}
@cryptoskillz
Copy link
Author

This is node.js obvioulsy.

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