Segregated-Witness

使用 Javascript,我想生成一個 segwit 私鑰 (zprv),類似於我生成舊版 base58 擴展私鑰的方式。如何?

  • May 13, 2021
let bip32 = require('bip32');
let bip39 = require('bip39');
let { bech32, bech32m } = require('bech32')

const seed = bip39.mnemonicToSeedSync('rural catch say tooth tell soul road pilot fee board goose wedding');
// ^randomly generated for this question

const node = bip32.fromSeed(seed, bitcoin.networks.testnet);

//legacy account extended private key
console.log("LEGACY: " + node.derivePath("m/44'/0/0").toBase58()); 
//tprv8giQNMeV5drQbXzAxSaVDj5Q1rAuTDZdGWoSx5oaLfuk9rupqVZXoziLgJSpkuauCToZw7BepygJjJaDBWKx5xQLimyg4KFtKnU2E7b99YH

//segwit account extended private key
//how do I take the same inputs, and get a segwit extended private key / zprv

這就是我獲得 xprv 的方式,我也想測試獲得 zprv,但我無法找到相關文件。我假設這將是使用 bech32 的東西。

使用 zpub/zprv 版本字節的 P2WPKH 的密鑰派生在 BIP 84 中定義。因此您可以使用bip84庫。

let bip84 = require('bip84');

const root = new bip84.fromSeed('rural catch say tooth tell soul road pilot fee board goose wedding')
const child0 = root.deriveAccount(0) // m/84'/0'/0'
console.log(child0)
// zprvAce8anvk7mGXLFgryEKx2b8tETAKuJRFXM1SRN4wuZDw2cwzxYuJ9PJdecA9MJAuAuchhh3cfLfN3SSL8agTL3tJARw6TmgvAKVgcfVcANm

引用自:https://bitcoin.stackexchange.com/questions/106266