Skip to content

Instantly share code, notes, and snippets.

@clarkmoody
Last active April 16, 2024 17:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save clarkmoody/0a788d2e012ffe339bb7d3873e47c081 to your computer and use it in GitHub Desktop.
Save clarkmoody/0a788d2e012ffe339bb7d3873e47c081 to your computer and use it in GitHub Desktop.
const bitcoinjs = require('bitcoinjs-lib') // @3.3.2
const bip39 = require('bip39')
const clone = require('lodash.clonedeep')
const networks = bitcoinjs.networks
const script = bitcoinjs.script
/*
Bitcoin 0x0488b21e - xpub 0x0488ade4 - xprv P2PKH or P2SH
Bitcoin 0x049d7cb2 - ypub 0x049d7878 - yprv P2WPKH in P2SH
Bitcoin 0x0295b43f - Ypub 0x0295b005 - Yprv P2WSH in P2SH
Bitcoin 0x04b24746 - zpub 0x04b2430c - zprv P2WPKH
Bitcoin 0x02aa7ed3 - Zpub 0x02aa7a99 - Zprv P2WSH
*/
// Default is BIP-44
networks.bitcoin.bip32.outputScript = (pubkey) => {
return script.pubKeyHash.output.encode(
bitcoinjs.crypto.hash160(pubkey))
}
// BIP-49 is P2PKH-in-P2SH
networks.bip49 = clone(networks.bitcoin)
networks.bip49.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))
}
}
// BIP-84 is P2WPKH with bech32-encoded addresses
networks.bip84 = clone(networks.bitcoin)
networks.bip84.bip32 = {
public: 0x04b24746,
private: 0x04b2430c,
outputScript: (pubkey) => {
return script.witnessPubKeyHash.output.encode(
bitcoinjs.crypto.hash160(pubkey))
}
}
// Entropy: 0x00
const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
const seedHex = bip39.mnemonicToSeedHex(mnemonic)
function printScheme(purpose, network) {
const path = `m/${purpose}'/0'/0'`
const rootNode = bitcoinjs.HDNode.fromSeedHex(seedHex, network)
const accountNode = rootNode.derivePath(path)
const pubkey = accountNode.derive(0).derive(0).getPublicKeyBuffer()
const address = bitcoinjs.address.fromOutputScript(
network.bip32.outputScript(pubkey))
console.log(`\n${path}`)
console.log(accountNode.toBase58())
console.log(accountNode.neutered().toBase58())
console.log(`${path}/0/0 address:`)
console.log(address)
}
printScheme(44, networks.bitcoin)
printScheme(49, networks.bip49)
printScheme(84, networks.bip84)
@Falehf
Copy link

Falehf commented Feb 1, 2024

Trust

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