Closed bazooka70 closed 6 years ago
I think there's another way to do this. Maybe new BitcoinAddress() or BitcoinAddress.Parse() or BitcoinAddress.FromWif() or something like this, being familiar with Nicolas's patterns. If you cannot find it, I'll dig it up for you.
@nopara73 , I'm almost sure there will be no such capability b/c detecting all altcoins addresses now is a pain in the ass. But hopefully you might know better!
if I'm right I only hope that BitcoinAddress.Create(string, null)
will work as before for BTC, and wont be completely deprecated later. I can (try to) detect other alt coins with some effort.
Is it deprecated with expected Network provided, too?
@nopara73, No. as far as I can tell BitcoinAddress.Create(string, expectedNetwork)
parses any registered network as expected. but @NicolasDorier can confirm.
The difference is: if the expected network doesn't match, it throws an exception.
BitcoinAddress.Create(string)
is deprecated and you must use BitcoinAddress.Create(string, Network)
. If you want the same that you have with BitcoinAddress.Create(string)
, but you don't want the compiler to complain about deprecation, then simply do something like this:
public static class BitcoinAddressParser
{
public static BitcoinAddress Parse(string address)
{
try
{
return BitcoinAddress.Create(address, Network.Main);
}
catch
{
try
{
return BitcoinAddress.Create(address, Network.TestNet);
}
catch
{
return BitcoinAddress.Create(address, Network.RegTest);
}
}
}
}
Then call:
BitcoinAddressParser.Parse("12345678asdfghj");
@nopara73 , an easier way is to simply call
BitcoinAddress.Create(string, null)
The compiler won't complain and will do the same job, but only for BTC (i.e. Network.Main
, Network.TestNet
, Network.RegTest
) , not other alt coins Networks (exception will be thrown).
To do this for other alt coins your method could be used e.g.
public static class BitcoinAddressParser
{
public static BitcoinAddress Parse(string address)
{
try
{
// try BTC
return BitcoinAddress.Create(address, null);
}
catch
{
// try BCH
try
{
return BitcoinAddress.Create(address, BCash.Mainnet);
}
catch
{
// etc for BCash.Testnet, BCash.Regtest
// ...
// etc (LTC, DOGE...)
}
}
}
}
Very ugly :-P
Yes, but that's a bug, which you should expect later to be fixed and break your code.
Btw, there's also Network.Parse<BitcoinAddress>("xyz");
.
Why do you think it's a bug? (but i agree later it could break the code if Nicolas deprecate it completly not allowing null
as the second parameter...)
Internally BitcoinAddress.Create()
calls Network.Parse<BitcoinAddress>("xyz", expectedNetwork)
unless I missed something... (Line 96, and 110)
https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/BitcoinAddress.cs
Why do you think it's a bug?
Because that's what had been deprecated. Btw I'm not exactly sure if Network.Parse<BitcoinAddress>("xyz");
is correct way to do things. They may very well just forgot to deprecate this function without provided network, too.
It is obsolete for 4 reasons:
BitcoinAddress.Create(String, Network)
does)BitcoinAddress.Create(String)
it will break when you add another shitcoin down the road. So at the end of the day, there is no good reason to use BitcoinAddress.Create
and if you designed your code correctly, everytimes you need to parse a string, you probably already have the Network somewhere in the scope.
@NicolasDorier , You got very strong points there against using BitcoinAddress.Create
. but still it was a nice feature.
I will really try to get rid of all BitcoinAddress.Create(address)
in the backend (I used it mainly with QBitNinja).
Am I right about my assumption for BitcoinAddress.Create(address, null)
? will it work fine for BTC?
@bazooka70 QBitNinjaClient
know his Network
, so you can easily use this member to parse your address.
Yes it will work for bitcoin, .Create
always try first with bitcoin network, but I might remove this one day.
The same goes for ExtPubKey.Parse(string, expectedNetwork)
now?
yes
The main usage I think that I will be missing is when I send funds to a raw address, I could validate that the destination address is valid for my wallet network. think about it...
well you still can valid, this is the whole point.
Ahhh yes, I can see your point now! thanks. OK, I am going to close this now. I see no point in leaving this open unless you plan to un-deprecate it some day... ;)
It was kinda cool feature. I'm guessing this is b/c of the alt coins support? Is it true for BTC also? From my initial test there is no problem with it with BTC addresses.