Post
Topic
Board Development & Technical Discussion
Merits 6 from 3 users
Re: C# Bitcoin Address Validation [Legacy, Nested SegWit, Native SegWit] | OFFLINE
by
Coding Enthusiast
on 20/08/2021, 13:06:55 UTC
⭐ Merited by ETFbitcoin (3) ,BlackHatCoiner (2) ,vapourminer (1)
For future reference:
My goal is recognize that a string is bitcoin address or not.(Offline Mode)
I need a method that returns TRUE or FALSE.
You just have to check if the returned type is not Invalid (eg. wrong checksum, wrong length, etc.) or Unknown (eg. Bech32m with witness version 2+).
Code:
AddressType t = GetAddressType(...);
bool result = (t != AddressType.Invalid && t != AddressType.Unknown);

Would you please show some examples of usage these methods :
Bitcoin.Net has a huge number of tests that you could use as examples. Each test file is in the Test project with the same namespace name.
/Tests/Bitcoin/Encoders/AddressTests.cs

What is NetworkType netType?
3 different network types are defined in Bitcoin: MainNet, TestNet and RegTest and some of the prefixes used when encoding addresses (and other variables elsewhere) are different for these networks.

Is this method working offline?
There is no reason not to!

This link solved my issue.
regex-bitcoin-addresses
This is a very bad approach and will have a lot of false positives and false negatives.
  • Address length in Base58 is not fixed. It may be as small as 26 and as big as 35 (the article uses wrong values).
  • First character being 1 or 3 doesn't mean the address's first byte was correct. Eg. 3R2cuiTJNvFDn18H7i7h7pvwYuvaRTNaJq is an invalid address because it uses 6 as the first byte instead of 5
  • Regex doesn't validate the address checksum for either legacy or Bech32
  • Due to lack of checksum validation there is no way to reject Bech32 addresses for version 1+ witness programs
  • Certain edge cases aren't considered such as "bc1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqyqskt8xg" being an invalid Bech32 address