MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 844 forks source link

bip32 address for DASH #1113

Closed arahimi1996 closed 2 years ago

arahimi1996 commented 2 years ago

Hi, I am trying to generate HD addresses from a DASH wallet public key (xpub) using NBitcoin.

Here it is the code I use to successfully generate Bitcoin addresses from BTC wallet xpub:

var network = Network.Main; var pubkey = ExtPubKey.Parse(xpub, network); var address = pubkey.Derive(0).Derive(0).PubKey.GetAddress(pubkeyType, network).ToString();

Also it is the code I use to successfully generate dogeCoin addresses from DOGE wallet xpub:

var network = NBitcoin.Altcoins.AltNetworkSets.Dogecoin.Mainnet; var pubkey = ExtPubKey.Parse(xpub, network); var address = pubkey.Derive(0).Derive(0).PubKey.GetAddress(pubkeyType, network).ToString();

And using this code to generate Dash Address from DASH wallet xpub but an error occurs :

var network = NBitcoin.Altcoins.AltNetworkSets.Dash.Mainnet; var pubkey = ExtPubKey.Parse(xpub, network); var address = pubkey.Derive(0).Derive(0).PubKey.GetAddress(pubkeyType, network).ToString();

Here it is my sample and valid Dash wallet xpub:

var xpub = "drkvjJe5sJgqomjLnD39LKGdme64zzL4d38fDEhkveYsjvqDZzEZSoq6VEE5znetSRvSB6pYAxhTViXJdZ5QygKogD4nsa31hQ8aVuW6psczteC"

Here it is an error occurs:

Exception Message =>

Invalid base58 string

Exception Stack Trace =>

at NBitcoin.Network.Parse(String str, Type targetType) at NBitcoin.Network.Parse[T](String str) at NBitcoin.ExtPubKey.Parse(String wif, Network expectedNetwork)

Any suggestion about how to derive a DASH compatible address?

Thanks in advance.

kiarash3012 commented 2 years ago

I have the same exact problem, the error is the same as when you add the wrong network (like adding a bitcoin network for doge), can anyone help?

kiarash3012 commented 2 years ago

Ping @bazooka70 , @bdangh , @snogcel , somebody can fix it pls?

arahimi1996 commented 2 years ago

@NicolasDorier help me to fix this exception please

NicolasDorier commented 2 years ago

strange, checking @arahimi1996

NicolasDorier commented 2 years ago

the prefix for dash xpub is 0x04, 0x88, 0xB2, 0x1E and I see a different one 02 fe 52 f8.

Note we use the official https://github.com/dashpay/dash/blob/master/src/chainparams.cpp#L457

        // Dash BIP32 pubkeys start with 'xpub' (Bitcoin defaults)
        base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};

Workaround:

            var network = NBitcoin.Altcoins.AltNetworkSets.Dash.Mainnet;
            var xpub = "drkvjJe5sJgqomjLnD39LKGdme64zzL4d38fDEhkveYsjvqDZzEZSoq6VEE5znetSRvSB6pYAxhTViXJdZ5QygKogD4nsa31hQ8aVuW6psczteC";
            if (network.NetworkSet == Altcoins.AltNetworkSets.Dash)
            {
                var xpubBytes = Encoders.Base58Check.DecodeData(xpub);
                network.GetVersionBytes(Base58Type.EXT_PUBLIC_KEY, true).CopyTo(xpubBytes, 0);
                xpub = Encoders.Base58Check.EncodeData(xpubBytes);
            }
            var pubkey = ExtPubKey.Parse(xpub, network);

Note the workaround will accept a tpub rather than xpub even for mainnet

arahimi1996 commented 2 years ago

Thanks a lot @NicolasDorier.