Bitcoin Forum
April 19, 2024, 10:07:36 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Generate addresses independently of Electrum  (Read 286 times)
anon4250158 (OP)
Sr. Member
****
Offline Offline

Activity: 266
Merit: 256


View Profile
July 03, 2019, 10:46:33 PM
 #1

I'm trying to make a version of https://iancoleman.io/bip39/ in java for my own use (mostly to learn about HD wallets).  I seem to have gotten it down, except for Electrum-generated addresses.  Here's what I know about Electrum's differences:

1. Electrum uses a version system that only allows certain mnemonics
2. When generating the seed, Electrum concats the passphrase to "electrum" instead of "mnemonic" to generate the PBKDF2 HMAC

It took a few minutes, but I found a 3-word mnemonic that is both BIP39 and Electrum (standard) compatible: "friend deposit glove".  If I go to New/Restore wallet and choose BIP39 (no passphrase), it generates the same as Ian Coleman and mine: 1BPEpJbnex8z19J9eDfbMakD7d5b9fa5KX (m/44'/0'/0'/0 index 0), etc.  However, if I just use Electrum standard with no passphrase, it generates 1N3UPoVAkX7pQanefb8aAAZGLrMpxC1EYX.  I'm not getting that with any of the following derivation paths: m/0, m/0', m/44'/0'/0'/0.

So, I have 2 questions:
1. What are the default derivation paths Electrum uses?
2. Are there any other internal differences for generating addresses like #2 above?

I'm trying to follow the source code, but it's a lot easier to write code than read it.
1713564456
Hero Member
*
Offline Offline

Posts: 1713564456

View Profile Personal Message (Offline)

Ignore
1713564456
Reply with quote  #2

1713564456
Report to moderator
1713564456
Hero Member
*
Offline Offline

Posts: 1713564456

View Profile Personal Message (Offline)

Ignore
1713564456
Reply with quote  #2

1713564456
Report to moderator
1713564456
Hero Member
*
Offline Offline

Posts: 1713564456

View Profile Personal Message (Offline)

Ignore
1713564456
Reply with quote  #2

1713564456
Report to moderator
"With e-currency based on cryptographic proof, without the need to trust a third party middleman, money can be secure and transactions effortless." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713564456
Hero Member
*
Offline Offline

Posts: 1713564456

View Profile Personal Message (Offline)

Ignore
1713564456
Reply with quote  #2

1713564456
Report to moderator
1713564456
Hero Member
*
Offline Offline

Posts: 1713564456

View Profile Personal Message (Offline)

Ignore
1713564456
Reply with quote  #2

1713564456
Report to moderator
anon4250158 (OP)
Sr. Member
****
Offline Offline

Activity: 266
Merit: 256


View Profile
July 04, 2019, 04:51:58 AM
 #2

Well this is dumb.  My code was generating the uncompressed addresses, not the proper compressed ones.  m/0 is the proper derivation path for standard p2pkh addresses.  

I'd still like to know what derivation path Electrum uses for segwit bech32 p2wpkh addresses.
Abdussamad
Legendary
*
Offline Offline

Activity: 3598
Merit: 1560



View Profile
July 04, 2019, 07:55:10 AM
Merited by pooya87 (1)
 #3

it uses bip84 for bech32 addresses. only one account is used (0').

https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki

keystore.py has code relevant to derivation paths but it's a bit of a mess really. there's no one function or set of constants AFAICT. it's spread out.
anon4250158 (OP)
Sr. Member
****
Offline Offline

Activity: 266
Merit: 256


View Profile
July 04, 2019, 09:01:43 PM
 #4

I've tried m/84'/0'/0' , m/84'/0'/0'/0' , and m/84'/0'/0'/0 with valid Electrum segwit and BIP39 mnemonic "abandon about razor", but I'm not getting the same result as Electrum (bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe for index 0).  Everything matches the results from BIP84 in Ian Coleman, so I'm not sure what's going wrong here.
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
July 05, 2019, 06:19:04 AM
 #5

Check out the project here: https://github.com/FarCanary/ElectrumSeedTester

Using that it generates the Address: bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe from the mnemonic "abandon about razor"

BIP32 Root Key: zprvAWgYBBk7JR8Gjbwr8fxvpq5zF33z4wyGYduJVPhcab4iUm8CfePDbKcf2u1pTmgsUwNGGMQdPTN XGkjj9iNqTsRoSQde254shHJJtXe8Weh
BIP32 Derivation Path used: m/0'/0
BIP32 extended private key: zprvAc8htgxrZRryygy2zcbcRFb9C7ybGYhmfJ4PX34YXpfSoorUJugPgUNmdBuNmDgUodvCXMcu3iy 8AMdTCmLZx42hTjvpLcQ74mLdegJzBzC

Quote
m/0'/0/0   bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe   02cd91d2ca04f60a727bbb86bd5869135069f5ec516da31ef8fc5edd4ba39af240   KyiVJaSALAurEwB9BzoCe7cGR6gQc96f1B8Y3eCmNKuBBDyyvKwf

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


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Abdussamad
Legendary
*
Offline Offline

Activity: 3598
Merit: 1560



View Profile
July 05, 2019, 01:59:29 PM
Merited by xandry (2), bones261 (2), LoyceV (1), ABCbits (1), o_e_l_e_o (1)
 #6

I've tried m/84'/0'/0' , m/84'/0'/0'/0' , and m/84'/0'/0'/0 with valid Electrum segwit and BIP39 mnemonic "abandon about razor", but I'm not getting the same result as Electrum (bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe for index 0).  Everything matches the results from BIP84 in Ian Coleman, so I'm not sure what's going wrong here.

First of all that mnemonic is not valid bip39 according to electrum but it is according to the ian coleman site. Regardless you can still create a bip39 wallet with it in electrum. The way to do this is to make sure to click on options and choose bip39 in the seed entry step and in the next step choose native segwit as the derivation path. If you do this you will get addresses that match the bip84 derivation path option on iancoleman's site (m/84'/0'/0'/0).

That mnemonic is also a valid electrum seed so if you don't click on options and check bip39 electrum will use a different method to convert to the binary seed (2 in the op) and as a result you get different addresses.

So overall you have to pay attention to details like these.
anon4250158 (OP)
Sr. Member
****
Offline Offline

Activity: 266
Merit: 256


View Profile
July 05, 2019, 10:04:13 PM
 #7

Check out the project here: https://github.com/FarCanary/ElectrumSeedTester

Using that it generates the Address: bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe from the mnemonic "abandon about razor"

BIP32 Root Key: zprvAWgYBBk7JR8Gjbwr8fxvpq5zF33z4wyGYduJVPhcab4iUm8CfePDbKcf2u1pTmgsUwNGGMQdPTN XGkjj9iNqTsRoSQde254shHJJtXe8Weh
BIP32 Derivation Path used: m/0'/0
BIP32 extended private key: zprvAc8htgxrZRryygy2zcbcRFb9C7ybGYhmfJ4PX34YXpfSoorUJugPgUNmdBuNmDgUodvCXMcu3iy 8AMdTCmLZx42hTjvpLcQ74mLdegJzBzC

Quote
m/0'/0/0   bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe   02cd91d2ca04f60a727bbb86bd5869135069f5ec516da31ef8fc5edd4ba39af240   KyiVJaSALAurEwB9BzoCe7cGR6gQc96f1B8Y3eCmNKuBBDyyvKwf

Thanks for this.  My code returns the same address now that I have the proper derivation path.


I've tried m/84'/0'/0' , m/84'/0'/0'/0' , and m/84'/0'/0'/0 with valid Electrum segwit and BIP39 mnemonic "abandon about razor", but I'm not getting the same result as Electrum (bc1qp0efh7jms7f580e7nnywjg5vzm90njm0rdvqpe for index 0).  Everything matches the results from BIP84 in Ian Coleman, so I'm not sure what's going wrong here.

First of all that mnemonic is not valid bip39 according to electrum but it is according to the ian coleman site. Regardless you can still create a bip39 wallet with it in electrum. The way to do this is to make sure to click on options and choose bip39 in the seed entry step and in the next step choose native segwit as the derivation path. If you do this you will get addresses that match the bip84 derivation path option on iancoleman's site (m/84'/0'/0'/0).

That mnemonic is also a valid electrum seed so if you don't click on options and check bip39 electrum will use a different method to convert to the binary seed (2 in the op) and as a result you get different addresses.

So overall you have to pay attention to details like these.

I'm guessing Electrum would find "abandon about razor" to be valid, but according to the source code, it will return a failed checksum if the mnemonic is less than 12 words before actually calculating the checksum.  I have next to no experience with Python, so following the code has been difficult.  Nowhere could I find the m/0'/0 derivation path that leads to p2wpkh addresses even after HCP demonstrated that was what is used.  I guess it would be nice if the derivation paths were clearly set as constants in the code.  Thanks for your help.
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
July 05, 2019, 10:24:59 PM
Last edit: July 05, 2019, 10:41:47 PM by HCP
Merited by Abdussamad (2)
 #8

That stuff seems to happen in bip32.py.

Check the derivation path related functions at the end of the file: https://github.com/spesmilo/electrum/blob/master/electrum/bip32.py#L276-L343


EDIT: Actually, it's probably more clear in the base_wizard.py: https://github.com/spesmilo/electrum/blob/fb76fcc886b7c999387a6676f479678df742fdaa/electrum/base_wizard.py#L397-L399

You can see if sets up the derivation points there based on user choice of type... this then uses the bip44_derivation() function in keystore.py to set the path: https://github.com/spesmilo/electrum/blob/fb76fcc886b7c999387a6676f479678df742fdaa/electrum/keystore.py#L791-L793

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
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!