Bitcoin Forum
November 09, 2024, 02:36:07 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Nbitcoin master private key with mnemonic (cannot import in Electrum)  (Read 166 times)
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 26, 2021, 02:17:17 PM
Merited by ABCbits (1), Pmalek (1)
 #1

Hi,

For the fun I tried to generate a master private key from a seed (with Nbitcoin). But I cannot import it to electrum (it works but no tx).
But when I import the seed in electrum, txs are here... I m missing something for sure.

Mnemonic mnemo = new Mnemonic("my 12 words here", Wordlist.English);
ExtKey extKey = mnemo.DeriveExtKey();
var masterKey = extKey.GetWif(Network.Main);

Or with a derivation path like this :
ExtKey extKey = mnemo.DeriveExtKey().Derive(new KeyPath("m/84'/0'/0'"));

Without success.

And if I get the master pub key (zpub)
var pubKey = extKey.Neuter();
var pubKeyWif = pubKey.GetWif(Network.Main);

It doesn't match with the one I have in Electrum Wallet Info when I import the seed directly.

Maybe someone can point me where I m dumb Smiley
Thx
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1694
Merit: 8328


Fiatheist


View Profile WWW
November 26, 2021, 02:31:09 PM
Merited by pooya87 (2), ABCbits (2), NotATether (2), Pmalek (1)
 #2

NBitcoin gives a BIP39 mnemonic, while Electrum has its own standard. You should import it as a BIP39 seed, it's a checkbox that must be ticked during importation. If you knew this, then something else goes wrong.

Have you tried it on iancoleman to see if it's fine there? Also, could you give us a master public key generated using NBitcoin and the expected address of m/84'/0'/0'/0/0 ?

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 26, 2021, 02:47:41 PM
 #3

Yes when I import the seed (directly) I use BIP39 and the correct derivation path and I found my tx in electrum.

It's when I try to generate the masterkey in Nbitcoin that I have the issue (from the same seed). But I think Nbitcoin use BIP39 when he generates from the seed. So I don't understand.
var masterKey = extKey.GetWif(Network.Main);

I have a correct format but it doesn't fall on the same wallet.

Cannot really provide the exemple... it's an old wallet I used with 0 balance but linked to an exchange ( Smiley ) and with some personnal txs



BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1694
Merit: 8328


Fiatheist


View Profile WWW
November 26, 2021, 03:06:59 PM
 #4

Yes when I import the seed (directly) I use BIP39 and the correct derivation path and I found my tx in electrum.
Which exactly derivation path do you use? Is it m/84'/0'/0'? Again, have you confirmed it on iancoleman? What does it return you? If you don't feel comfortable of doing it on your browser, download the source files and open up them on an airgap machine.

Cannot really provide the exemple... it's an old wallet I used with 0 balance but linked to an exchange ( Smiley ) and with some personnal txs
How did you create your personal wallet? Just generate another wallet that way and post the seed, the expected zpub and the resulted zpub. I just want to understand the exact procedure.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 26, 2021, 03:22:59 PM
 #5

With :
Mnemonic mnemo = new Mnemonic("laptop laptop laptop laptop laptop laptop laptop laptop laptop laptop laptop laptop", Wordlist.English);

When I use normal ExtKey (without derivation path in Nbitcoin)
ExtKey extKey = mnemo.DeriveExtKey();

I obtain :
master priv : xprv9s21ZrQH143K41GwNx7T5yVeTpJN1GSptcLZ2B2ddfbHqKhBAkERuba2gs4m3LZKU17Nr8EFmtQ jhix3cnKWrn12aRKG4NksxjVZCWtZxmc
master pub : xpub661MyMwAqRbcGVMQUyeTT7SP1r8rQjAgFqG9pZSFC18Gi82KiHYgTPtWYATJeoPpBrjuufRWnPH pCCfb8Mzy7DYZroiMBTYQkDouM5mfM1E

When I use derivation :
ExtKey extKey = mnemo.DeriveExtKey().Derive(new KeyPath("m/84'/0'/0'"));
master priv : xprv9y4Jnspoiifv534Thtq5jsooccAGJAXNNCaD1jXdcKRMghvvqPp4bREXZ66a77QGKkBsjXfEBPU LX1keNQSLbkBFV8PKLe89fVuNGutZHgH
master pub : xpub6C3fCPMhZ6EDHX8vovN671kYAdzkhdFDjRVop7wFAexLZWG5Nw8K9DZ1QNSdsDG95tiFVzGCqJn 9tXCpVTBhA2Q1D4kVgqDXbos4RPxHiGn

When I import the seed in electrum under BIP39 :
master pub : zpub6qiBoihXrTKAz7XAUdwLXBwYWaHeasEDZeYFNuj1vfi6fhtXtFTSPLsHSnMos2ZyuAwrzwTKkdV Ff6Rwvr1ikVmCwk9LrerW9FzMCWYw5xQ

The seed is not correct (checksum failed : I don't know if it matters to correctly provide you an example).

It doesn't generate the same master keys (pub / priv)

The seed works in Exodus wallet to, so clearly I m doing something wrong in Nbitcoin.





BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1694
Merit: 8328


Fiatheist


View Profile WWW
November 26, 2021, 03:50:55 PM
Last edit: November 27, 2021, 11:14:08 AM by BlackHatCoiner
 #6

The seed is not correct
The seed is definitely incorrect unless you don't count a checksum. Otherwise, twelve laptops would give you the following:

0111110100001111101000011111010000111110100001111101000011111010000111110100001 11110100001111101000011111010000111110100001111101000

The green part is false, because, the first four bits of the hash of the blue bits is 0010, not 1000. A valid seed phrase would be:
Code:
laptop laptop laptop laptop laptop laptop laptop laptop laptop laptop laptop labor

(checksum failed : I don't know if it matters to correctly provide you an example).
Where does that Electrum tell you? I just tried to import it using BIP39 and it worked fine without any debug log messages.

When I use derivation :
ExtKey extKey = mnemo.DeriveExtKey().Derive(new KeyPath("m/84'/0'/0'"));
master priv : xprv9y4Jnspoiifv534Thtq5jsooccAGJAXNNCaD1jXdcKRMghvvqPp4bREXZ66a77QGKkBsjXfEBPU LX1keNQSLbkBFV8PKLe89fVuNGutZHgH
master pub : xpub6C3fCPMhZ6EDHX8vovN671kYAdzkhdFDjRVop7wFAexLZWG5Nw8K9DZ1QNSdsDG95tiFVzGCqJn 9tXCpVTBhA2Q1D4kVgqDXbos4RPxHiGn
It shouldn't return you xpub keys, but rather zpub as you're deriving from m/84 which gives SegWit Native, not Legacy.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 26, 2021, 04:04:49 PM
 #7

With this seed in iancoleman "shell immense right kitten shield acid human same guard panther alert thank dignity odor doctor"

I cannot obtain in Nbitcoin :
a "zpubxxxxx" type for the ext pub
it remains a "xpubxxxx"... (not BIP 84)

So i think I miss something to correctly use BIP 84 derivation path in nbitcoin.

It works with
ExtKey extKey = mnemo.DeriveExtKey().Derive(new KeyPath("m/44'/0'/0'/0"));
I fall on the same as iancoleman for extPriv and pub BIP 44.

Don't understand why when I put the BIP84 path in nbitcoin it continues to produce "xpub" and not "zpub".

Sorry mega noob here... maybe time to read a lot more. It seems I don't master the base concepts.. Wink


pooya87
Legendary
*
Offline Offline

Activity: 3626
Merit: 11027


Crypto Swap Exchange


View Profile
November 27, 2021, 03:58:55 AM
 #8

Don't understand why when I put the BIP84 path in nbitcoin it continues to produce "xpub" and not "zpub".
I can't find any indication of changing the first 4 bytes (version bytes) in ExtKey.cs whether during construction of the object or setting the derivation path or deriving the keys. Which is probably why you always get the default version (producing xprv/xpub) when you convert the ExtKey object to string.
I also couldn't find any way to change this.

Base58Type enum doesn't seem to have the type to fetch the version either.

P.S. I'm not familiar with this library and it is not the most readable code so you may want to spend more time yourself investigating how it works.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
NotATether
Legendary
*
Offline Offline

Activity: 1778
Merit: 7372


Top Crypto Casino


View Profile WWW
November 27, 2021, 05:39:30 AM
 #9

With this seed in iancoleman "shell immense right kitten shield acid human same guard panther alert thank dignity odor doctor"

I cannot obtain in Nbitcoin :
a "zpubxxxxx" type for the ext pub
it remains a "xpubxxxx"... (not BIP 84)

So i think I miss something to correctly use BIP 84 derivation path in bitcoin.

Did you check if it's really making a zpub but it's just appending the "xpub" string at the beginning instead of "zpub"?

Most likely, what is happening is that since you can't specify both a master private key and a derivation path at the same time in electrum, the MPK directly generated from the seed is looking at /0/1, /0/2, /0/3 etc. addresses instead of the ones that are under /84'/0'/0' master private key.

███████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████

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


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 27, 2021, 06:45:32 PM
 #10

Tried to only replace xprv by zprv in the master key wif.

But it is not working, Electrum don't let me continue to import.

So yes, I don't understand why i cannot derive or generate a master key for native segwit.

Very strange.
I will continue to search, Wink Thx for the help.

the MPK directly generated from the seed is looking at /0/1, /0/2, /0/3 etc. addresses instead of the ones that are under /84'/0'/0' master private key.
(seems strange too, normaly the derviation path is included in the master key ?) I think i don't understand nbitcoin correctly (I tried in debug with the source code, but no success for the moment)
pooya87
Legendary
*
Offline Offline

Activity: 3626
Merit: 11027


Crypto Swap Exchange


View Profile
November 28, 2021, 03:48:16 AM
 #11

Tried to only replace xprv by zprv in the master key wif.
The base58 string of an extended key contains a checksum so you can't just change characters because it would invalidate the existing checksum. You could decode it first using a base58 decoder, remove the checksum then change the first 4 bytes to get zprv and then encode it again with base58.

Quote
I will continue to search, Wink Thx for the help.
There is a chat room for the project on gitter you could use to ask others more familiar with the library: https://gitter.im/MetacoSA/NBitcoin

Quote
normaly the derviation path is included in the master key
No it is not.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 28, 2021, 12:38:27 PM
 #12

tomorrow I will try your base58 method. And thx for the forum link.
++
AsyncLuck (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 4


View Profile
November 29, 2021, 10:31:22 AM
Last edit: November 29, 2021, 10:55:56 AM by AsyncLuck
Merited by pooya87 (2)
 #13

Hi,

Thx  pooya87, I did exactly what you said but with this lib extension (toZprv) for Nbitcoin :
https://github.com/zkSNACKs/WalletWasabi/blob/abebff2b0703b0758ac8b97ba4b4f1cbd3a605eb/WalletWasabi/Extensions/NBitcoinExtensions.cs

An it works, can import my master key with BIP84 derivation path.

Thx a lot !!

PS : Now I need to understand why I cannot generate the good ScriptPubKey from my Extended Master Key, to query electrs (electrum rust server) server. SmileySmiley
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!