【问题标题】:How to validate HD wallet address to match BIP44如何验证 HD 钱包地址以匹配 BIP44
【发布时间】:2021-03-17 16:48:16
【问题描述】:

我正在使用 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
      })
    }

【问题讨论】:

    标签: bitcoin hierarchical deterministic


    【解决方案1】:

    您的示例中有两个问题。

    • 在步骤 1 中,您必须使用 _publicKey 而不是 publicExtendedKey;如果需要 PUBLIC EXTENDED,则必须为此使用新变量。

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

    【讨论】:

    • 我明白了,但为什么要使用 _publicKey 而不是 publicExtended。我改变了它并得到完全相同的结果。为了解决这个问题,我最终将 bs58check 切换为 base58-encode,从而解决了我的地址问题。但它似乎没有延长或没有我得到相同的地址。你能解释一下吗?
    猜你喜欢
    • 2018-08-22
    • 2022-07-28
    • 1970-01-01
    • 2021-11-26
    • 2021-04-06
    • 2018-07-11
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多