首页 > 解决方案 > 如何验证 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
      })
    }

标签: bitcoinhierarchicaldeterministic

解决方案


您的示例中有两个问题。

  • 在第 1 步中,您必须使用_publicKey代替publicExtendedKey; 如果需要 PUBLIC EXTENDED,则必须为此使用新变量。

  • 要生成地址,您不需要步骤 5-8。


推荐阅读