Bitcoin Forum
May 03, 2024, 11:58:36 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Generating a Bitcoin address with Node JS **UPDATED**  (Read 153 times)
madeindreams (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
December 05, 2020, 08:16:30 PM
Last edit: December 06, 2020, 12:25:51 AM by madeindreams
 #1

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

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/

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;

 
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!





1714737516
Hero Member
*
Offline Offline

Posts: 1714737516

View Profile Personal Message (Offline)

Ignore
1714737516
Reply with quote  #2

1714737516
Report to moderator
1714737516
Hero Member
*
Offline Offline

Posts: 1714737516

View Profile Personal Message (Offline)

Ignore
1714737516
Reply with quote  #2

1714737516
Report to moderator
Once a transaction has 6 confirmations, it is extremely unlikely that an attacker without at least 50% of the network's computation power would be able to reverse it.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714737516
Hero Member
*
Offline Offline

Posts: 1714737516

View Profile Personal Message (Offline)

Ignore
1714737516
Reply with quote  #2

1714737516
Report to moderator
Entontothekeseczi
Newbie
*
Offline Offline

Activity: 6
Merit: 9


View Profile
December 06, 2020, 05:25:13 PM
Last edit: December 06, 2020, 05:39:13 PM by Entontothekeseczi
 #2

Code:
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 base58check.

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!