Greetings.
So i'm generating an bitcoin address with NodeJS. I got this tuttorial here
https://medium.com/bitcraft/so-you-want-to-build-a-bitcoin-hd-wallet-part-1-8b27aa001ac3 (https://medium.com/bitcraft/so-you-want-to-build-a-bitcoin-hd-wallet-part-1-8b27aa001ac3)
I'm wondering why the author did not put the extra 4 lines of missing step in his code.
I am also using ian coleman's app to validate my code.
https://iancoleman.io/bip39/ (https://iancoleman.io/bip39/)
I manage to come up with the same bip32 as the test but for some reason the adress is to long
I am trying to code a HD wallet using NodeJS.
This is my code;
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 app;
// 1HaGGkWmUDTKExFxyXaiGHHPxYNaCefQrj
})
}
I don't get why I manage to get valid BIP32 keys but the address won't come out right.
also to get the BIP32 Key right but had to remove the last /0 from the root.
I hope someone can help me i want to move on to the next step!
const hdkey = HDkey.fromMasterSeed(seed)
const addrnode = hdkey.derive("m/44'/0'/0'/0")
console.log("\x1b[32m%s\x1b[0m",'PRIVATE EXTENDED : ', addrnode.privateExtendedKey)
const child = addrnode.derive("m/0");
const step1 = child._publicKey
console.log("\x1b[32m%s\x1b[0m",'PUBLIC EXTENDED : ', step1.toString('hex'))
//SHA256 of the public key
const step2 = createHash('sha256').update(step1).digest()
// RIPEDMD-160 of the SHA256 Hash
const step3 = createHash('rmd160').update(step2).digest()
// He we must add the network byte in front of that RIPEDMD result
// 0x00 for mainnet and 0x6f for testnet
var step4 = Buffer.allocUnsafe(21)
step4.writeUInt8(0x00, 0)
step3.copy(step4, 1)
// Return the bitcoins address
var step9 = bs58check.encode(step4)
resolve(step9)
The additional steps are already handled by base58
check.
Honest remark: Be careful to understand and test everything before you release a product or better let professionals with much experience take care of this as there are many fragile aspects to this kind of code and people might lose real money when there are bugs.