Bitcoin Forum
April 25, 2024, 08:55:14 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Wrong derived public address?  (Read 662 times)
ThatRandom8543 (OP)
Sr. Member
****
Offline Offline

Activity: 1330
Merit: 307


View Profile
April 24, 2017, 03:51:00 AM
Last edit: April 24, 2017, 04:01:16 AM by ThatRandom8543
 #1

Tonight I was messing around with bitcoin.js and decided to see if I can generate addresses using xpub only. So far, the code works, but the address it generated doesnt show up on electrum. I decided to send a couple of cents to the address and it have been received by the address but still have yet to show up in electrum or the ledger bitcoin app and dont know what went wrong. I did check blockchain.info to see if the address is listed there with the xpub but no luck either. Maybe someone can point out where I went wrong.

Code:
var bitcoin = require('bitcoinjs-lib');
var HDNode = bitcoin.HDNode;
var xpub = HDNode.fromBase58("xpub...");

for(i = 0;i<5;i++){
    console.log(xpub.derive(0).derive(i).getAddress());
}

I am mainly trying to figure this out so I do not have to worry about having an electrum daemon running to handle it.

EDIT: I think I figured it out... need to use xpub.derive when getting the address. Someone can correct me though if im wrong.

EDIT: Updated Code. I did compare it against the list of addresses listed on electrum and so far they match. Any reason why I must use "derive(0).derive(...)" instead of just "derive(...)"? I may go back and reread the document I saw earlier since I couldve missed something.
According to NIST and ECRYPT II, the cryptographic algorithms used in Bitcoin are expected to be strong until at least 2030. (After that, it will not be too difficult to transition to different algorithms.)
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714035314
Hero Member
*
Offline Offline

Posts: 1714035314

View Profile Personal Message (Offline)

Ignore
1714035314
Reply with quote  #2

1714035314
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6535


Just writing some code


View Profile WWW
April 24, 2017, 03:56:44 AM
Merited by ABCbits (1)
 #2

It looks like you are not actually deriving any addresses but rather getting the address associated with the xpub itself.

ThatRandom8543 (OP)
Sr. Member
****
Offline Offline

Activity: 1330
Merit: 307


View Profile
April 24, 2017, 04:02:57 AM
 #3

It looks like you are not actually deriving any addresses but rather getting the address associated with the xpub itself.

Yea. After experimenting a little I did update the code which does give me the correct address, but kind of confused on why I need to do it two times. Will be rereading bip32 on github in a bit.
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
April 24, 2017, 05:43:15 AM
 #4

I would think the part you're looking for is here: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation
and possibly the section above: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions

Electrum uses "m/0/n" doesn't it for BIP32 HD wallets and addresses: https://bitcointalk.org/index.php?topic=274182.0

So, I would think that the first derive(0) puts you at m/0 in the tree... and then the 2nd one starts generating the child keys that match up to the addresses.

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
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!