bitcoin - 如何验证 HD 钱包地址以匹配 BIP44
问题描述
我正在用 NodeJS 生成一个比特币地址。我从这个不完整的代码开始。
[https://medium.com/bitcraft/so-you-want-to-build-a-bitcoin-hd-wallet-part-1-8b27aa001ac3][1]
在下面你会找到我的版本。
问题
我最终得到了一个太长的地址。我也不确定根三和校验和连接。不确定,因为我不得不偏离这个例子,我还不能解释。
我的目标是从比特币开始创建一个高清钱包,但最终是为了更多或其他加密货币。
为了验证我使用这个应用程序的价值;
[https://iancoleman.io/bip39/][2]
我设法通过删除根 3 中的最后一个 0 来提出相同的 bip32 密钥
我从
hdkey.derive("m/44'/0'/0'/0/0")
即使派生的密钥接缝有效,我也无法在验证器的 BIP44 选项卡中匹配它。所以我决定删除最后一个 0,因为我注意到它不是验证器中的选项。
hdkey.derive("m/44'/0'/0'/0")
随着最后一个 0 的消失,BIP32 扩展的私钥和公钥都有效。但我最终还是得到了一个太长的地址。
这是我的代码;
var bip39 = require('bip39');
var HDkey = require('hdkey');
var createHash = require('create-hash');
var bs58check = require('bs58check');
exports.seed = async () =>{
return new Promise(resolve => {
const mnemonic = "drive captain sustain winner neutral anchor congress skirt buzz usage orient wood"
//const mnemonic = bip39.generateMnemonic(); //generates the string above
const seed = bip39.mnemonicToSeed(mnemonic); //creates seed buffer
resolve(seed)
})
}
exports.key = async (seed) =>{
return new Promise(resolve => {
const hdkey = HDkey.fromMasterSeed(Buffer.from(seed, 'hex'))
const masterPrivateKey = hdkey.privateExtendedKey
// This key Match iancoleman BIP32 ROOT KEY
console.log("\x1b[32m%s\x1b[0m",'PRIVATE KEY BIP32 ROOT: ', masterPrivateKey)
// this line will not give me valid BIP32 EXTENDED PRIVATE KEYS
//const addrnode = hdkey.derive("m/44'/0'/0'/0/0")
// This one will when I removed the last /0
const addrnode = hdkey.derive("m/44'/0'/0'/0")
// THESE 2 BIP32 Extended Key are valid on iancoleman's app
console.log("\x1b[32m%s\x1b[0m",'PRIVATE EXTENDED : ', addrnode.privateExtendedKey)
const step1 = addrnode.publicExtendedKey
console.log("\x1b[32m%s\x1b[0m",'PUBLIC EXTENDED : ', step1.toString('hex'))
// Here is what I could understand from the example
//SHA256 of the public key
const step2 = createHash('sha256').update(step1).digest()
// PIPEDMD-160 of the SHA256 Hash
const step3 = createHash('rmd160').update(step2).digest()
// He we must add the network byte in front of that PIPEDMD result
// 0x00 for mainnet and 0x6f for testnet
var step4 = Buffer.allocUnsafe(21)
step4.writeUInt8(0x00, 0)
step3.copy(step4,1)
//step3 now holds the Network ID + RIPEMD160 result
//we hash it twice
var step5 = createHash('sha256').update(step4).digest()
var step6 = createHash('sha256').update(step5).digest()
//checksum first 4 byte of second hash
var step7 = step6.slice(0,4)
console.log("\x1b[32m%s\x1b[0m",'CHECKSUM : ', step7.toString('hex'))
// Adding the checksum to the end of
var step8 = Buffer.concat([step4, step7]);
console.log("\x1b[32m%s\x1b[0m",'Base + CHECKSUM : ', step8.toString('hex'))
// Return the bitcoins address
var step9 = bs58check.encode(step8)
resolve(step9)
// The address generated by this code;
// 1WpGHR9UmDm7UiJuFu1H3zE7TmtK187D1yZStMQ
// The address generated with the same mnemonic on iancoleman
// 1HaGGkWmUDTKExFxyXaiGHHPxYNaCefQrj
})
}
解决方案
您的示例中有两个问题。
在第 1 步中,您必须使用
_publicKey
代替publicExtendedKey
; 如果需要 PUBLIC EXTENDED,则必须为此使用新变量。要生成地址,您不需要步骤 5-8。
推荐阅读
- javascript - 在两个兄弟组件之间传递一个数组
- java - 如何持久化在 docker 内的 tomcat 上运行的 Spring Boot 应用程序的会话
- r - 如何解决 f(x, ...) 中的错误:未使用的参数 (x)
- c - 我们可以在 printf() 中使用“+”运算符吗?
- prestashop - Preastashop hookHeader 显示在正文而不是
- javascript - 如何使用模态视图PHP获取mysql数据
- .net - 以低内存使用处理数千个请求和文件流
- javascript - Javascript Array - 检查 2 个数组中的匹配项
- java - 为什么 EXPAND_FACTOR 在 ImmutableCollections.java 中为 2
- data-warehouse - 如何从此数据库创建雪花模式?