Skip to content

Instantly share code, notes, and snippets.

@onyb
Created July 24, 2020 15:28
Show Gist options
  • Save onyb/c022bc1a35aae47a327ce5356f2c6a31 to your computer and use it in GitHub Desktop.
Save onyb/c022bc1a35aae47a327ce5356f2c6a31 to your computer and use it in GitHub Desktop.
SLIP32 test vector generator (using bitcoinjs)
// Based on https://gist.github.com/clarkmoody/0a788d2e012ffe339bb7d3873e47c081
// by Clark Moody.
const bitcoinjs = require('bitcoinjs-lib') // @3.3.2
const bip39 = require('bip39')
const bip32 = require('bip32')
const _ = require('lodash')
const script = bitcoinjs.script
const networks = {
mainnet: {
// xpub
bip44: {
...bitcoinjs.networks.bitcoin,
bip32: {
public: 0x0488b21e,
private: 0x0488ade4,
outputScript: (pubkey) =>
script.pubKeyHash.output.encode(bitcoinjs.crypto.hash160(pubkey)),
},
purpose: 44,
coinType: 0,
},
// ypub
bip49: {
...bitcoinjs.networks.bitcoin,
bip32: {
public: 0x049d7cb2,
private: 0x049d7878,
outputScript: (pubkey) => {
const spendScript = script.witnessPubKeyHash.output.encode(
bitcoinjs.crypto.hash160(pubkey)
)
return script.scriptHash.output.encode(
bitcoinjs.crypto.hash160(spendScript)
)
},
},
purpose: 49,
coinType: 0,
},
// zpub
bip84: {
...bitcoinjs.networks.bitcoin,
bip32: {
public: 0x04b24746,
private: 0x04b2430c,
outputScript: (pubkey) =>
script.witnessPubKeyHash.output.encode(
bitcoinjs.crypto.hash160(pubkey)
),
},
purpose: 84,
coinType: 0,
},
},
}
networks.testnet = _.cloneDeep(networks.mainnet)
// tpub
networks.testnet.bip44 = {
...networks.mainnet.bip44,
...bitcoinjs.networks.testnet,
bip32: {
...networks.mainnet.bip44.bip32,
public: 0x043587cf,
private: 0x04358394,
},
coinType: 1,
}
// upub
networks.testnet.bip49 = {
...networks.mainnet.bip49,
...bitcoinjs.networks.testnet,
bip32: {
...networks.mainnet.bip49.bip32,
public: 0x044a5262,
private: 0x044a4e28,
},
coinType: 1,
}
// vpub
networks.testnet.bip84 = {
...networks.mainnet.bip84,
...bitcoinjs.networks.testnet,
bip32: {
...networks.mainnet.bip84.bip32,
public: 0x045f1cf6,
private: 0x045f18bc,
},
coinType: 1,
}
// Entropy: 0x00
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
const seed = bip39.mnemonicToSeedSync(mnemonic)
function printScheme(network) {
// Modify these to customize the derivation path.
const accountIndex = 0
const chainIndex = 1 // 0 for external, 1 for internal.
const addressIndex = 1
const rootNode = bip32.fromSeed(seed, network)
const path = `m/${network.purpose}'/${network.coinType}'/${accountIndex}'`
const accountNode = rootNode.derivePath(path)
const addressNode = accountNode.derive(chainIndex).derive(addressIndex)
const address = bitcoinjs.address.fromOutputScript(
network.bip32.outputScript(addressNode.publicKey),
network
)
console.log(`Account derivation path: ${path}`)
console.log(`Extended private key: ${accountNode.toBase58()}`)
console.log(`Extended public key: ${accountNode.neutered().toBase58()}`)
console.log(`Address derivation path: ${path}/${chainIndex}/${addressIndex}`)
console.log(`Address: ${address}\n`)
}
printScheme(networks.testnet.bip44)
printScheme(networks.testnet.bip49)
printScheme(networks.testnet.bip84)
{
"name": "slip32 test vector generator",
"version": "1.0.0",
"main": "generate.js",
"dependencies": {
"bip39": "^3.0.2",
"bip32": "^2.0.5",
"bitcoinjs-lib": "3.3.2",
"lodash": "^4.17.19"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment