Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: dude1234321 on December 28, 2021, 12:32:17 PM



Title: check sum a base58 private key
Post by: dude1234321 on December 28, 2021, 12:32:17 PM
hi,

i want some code, preferably .net (c# or vb.net) to check sum base 58 (51 character) private keys to see if they are valid or not.

can anyone point me in the right direction?

thank you



Title: Re: check sum a base58 private key
Post by: PawGo on December 28, 2021, 12:39:59 PM
Take a look here:
https://learnmeabitcoin.com/technical/wif

Checksum is in fact double sha256. You will have to decode your private key, remove last 4 bytes (8 decoded characters), calculate double hash and compare first 4 bytes (8 characters) of result with checksum received after decoding WIF.

Maybe this is what you look for:
https://gist.github.com/CodesInChaos/3175971#file-base58encoding-cs


Title: Re: check sum a base58 private key
Post by: dude1234321 on December 28, 2021, 12:45:16 PM
thank you for the information.

is there no library out there ? all i want to do is run some code that does

print IsValidKey("testkey")

i.e. prints true or false

there must be some library out there already that does that?


Title: Re: check sum a base58 private key
Post by: NeuroticFish on December 28, 2021, 12:46:47 PM
Although it tells it's not optimized, you may find useful parts for your needs here: https://gist.github.com/CodesInChaos/3175971
Another one I've found is https://github.com/adamcaudill/Base58Check and you can get it as NuGet package.
Both are not my code, I've just found them on the internet.




Title: Re: check sum a base58 private key
Post by: PawGo on December 28, 2021, 12:48:07 PM
Please check carefully the code I sent you. There is method DecodeWithChecksum, which returns correctly decoded private key or throws exception if checksum is invalid, just like you want.


Title: Re: check sum a base58 private key
Post by: dude1234321 on December 28, 2021, 12:52:00 PM
Please check carefully the code I sent you. There is method DecodeWithChecksum, which returns correctly decoded private key or throws exception if checksum is invalid, just like you want.

oh yes just saw that, will try it out, thank you :)


Title: Re: check sum a base58 private key
Post by: BlackHatCoiner on December 28, 2021, 12:53:30 PM
Since you want it to be .NET, check Autarkysoft.Bitcoin/Encoders/Base58.cs (https://github.com/Autarkysoft/Denovo/blob/master/Src/Autarkysoft.Bitcoin/Encoders/Base58.cs) of Coding Enthusiast. The boolean you're searching is IsValid(string encoded) (https://github.com/Autarkysoft/Denovo/blob/b20e5cb79b17b6237284e24ed4e279bb5d3b570f/Src/Autarkysoft.Bitcoin/Encoders/Base58.cs#L44). Check his stuff, he has done some pretty good work.