Bitcoin Forum
April 19, 2024, 02:10:31 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: C# Bitcoin Address Validation [Legacy, Nested SegWit, Native SegWit] | OFFLINE  (Read 144 times)
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 19, 2021, 09:52:36 PM
Merited by vapourminer (1), ABCbits (1)
 #1

I am looking for a class or method in c# language to validate bitcoin address.(OFFLINE)
I want to validate [Legacy, Nested SegWit, Native SegWit] address types.

For this purpose i found this :  
determine-if-a-bitcoin-wallet-address-is-valid
But it validates Legacy addresses only.

I also found this :  
bitcoin-address-validation
But i think it is using an api for this purpose & language is not c#.

I also found this :  
bitcoin-address-validator
This is not in c# language. It's php & i am not familiar with php.

I also found this :
address_validation
Too old and only for Legacy addresses.

Please give me a solution on this.
1713535831
Hero Member
*
Offline Offline

Posts: 1713535831

View Profile Personal Message (Offline)

Ignore
1713535831
Reply with quote  #2

1713535831
Report to moderator
Transactions must be included in a block to be properly completed. When you send a transaction, it is broadcast to miners. Miners can then optionally include it in their next blocks. Miners will be more inclined to include your transaction if it has a higher transaction fee.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713535831
Hero Member
*
Offline Offline

Posts: 1713535831

View Profile Personal Message (Offline)

Ignore
1713535831
Reply with quote  #2

1713535831
Report to moderator
1713535831
Hero Member
*
Offline Offline

Posts: 1713535831

View Profile Personal Message (Offline)

Ignore
1713535831
Reply with quote  #2

1713535831
Report to moderator
1713535831
Hero Member
*
Offline Offline

Posts: 1713535831

View Profile Personal Message (Offline)

Ignore
1713535831
Reply with quote  #2

1713535831
Report to moderator
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
August 20, 2021, 02:07:50 AM
Merited by NeuroticFish (3), BlackHatCoiner (2), vapourminer (1), ABCbits (1), Pmalek (1)
 #2

I have a library called Bitcoin.Net that has a static address class that does validation like this.
You have to use the GetAddressType method with 2 overloads
AddressType GetAddressType(string address, NetworkType netType)
AddressType GetAddressType(string address, NetworkType netType, out byte[] data)

This both checks the address and return its type and it can decode and return the data (hash) encoded by that address.

If you want to check the address against a certain pubkey script type you can use bool VerifyType(string address, PubkeyScriptType scrType, out byte[] hash) method.

BTW "nested SegWit" address type is P2SH, there is no way of having any additional information by only seeing the address.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 20, 2021, 05:33:18 AM
 #3

Thanks for the answer,
But in this link bitcoin-address-validator
They are validating legacy, segwit and native segwit (bech32) Bitcoin addresses.
It's in php language.
So there should be a way for Nested SegWit (P2SH) too.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
August 20, 2021, 07:07:21 AM
Merited by ABCbits (2), vapourminer (1)
 #4

So there should be a way for Nested SegWit (P2SH) too.
The point I was making is that you can't know the script type that was hashed by seeing the address alone. In other words you can validate a P2SH address and get the 20-byte hash out but there is no way to tell from the address if the script type which is hashed is P2WPKH or P2WSH or a legacy redeem script or something else entirely (eg. P2TR-P2SH).

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 20, 2021, 08:46:14 AM
 #5

My goal is recognize that a string is bitcoin address or not.(Offline Mode)
So for this purpose introduce a good library.

Would you please show some examples of usage these methods :
Quote
AddressType GetAddressType(string address, NetworkType netType)
AddressType GetAddressType(string address, NetworkType netType, out byte[] data)
bool VerifyType(string address, PubkeyScriptType scrType, out byte[] hash)

What is NetworkType netType? Is this method working offline?
I need a method that returns TRUE or FALSE.
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 20, 2021, 10:47:24 AM
 #6

This link solved my issue.
regex-bitcoin-addresses
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
August 20, 2021, 01:06:55 PM
Merited by ABCbits (3), BlackHatCoiner (2), vapourminer (1)
 #7

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

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 20, 2021, 01:49:05 PM
 #8

Coding Enthusiast
Thanks a lot brother
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1498
Merit: 7235


Farewell, Leo


View Profile
August 20, 2021, 01:49:38 PM
Last edit: August 21, 2021, 07:19:07 AM by BlackHatCoiner
Merited by vapourminer (2), Pmalek (2), Coding Enthusiast (2)
 #9

Even if your issue is resolved, I'll try to clear out some things to you that you possibly don't know. Giving you a bunch of coding lines will only assist you temporarily.

I need a method that returns TRUE or FALSE.
Alright, maybe you aren't familiar that much with coding yet and Coding Enthusiast's scripts may be an overstatement to understand.

First off, it is required for you to know what's the background of a Bitcoin address, how it is structured etc. In this case, I'll explain you a legacy address' structure, but it works similarly for Native SegWit and Nested SegWit. This bitcoin wiki article is useful. Briefly, a Bitcoin address is a base58 encoding of a string that contains these stuff in the following order: [version prefix][160-bit hash][checksum]

The checksum is the first 4 bytes of the prefix plus a 160-bit hash that is hashed two times with SHA256. For instance, if someone gave you this 160-bit hash:

Code:
e01f8922bb5bbfac8d02e77b023f2561fd2fa26b

You could get the checksum as following:

Code:
SHA256("00e01f8922bb5bbfac8d02e77b023f2561fd2fa26b") = 5088bf915b5880a5423a4064266ff534b28d74d4e1dce6e68f3fbf35e4481e32
SHA256("5088bf915b5880a5423a4064266ff534b28d74d4e1dce6e68f3fbf35e4481e32") = 351ca7c467a8c5f19dfb8472f5d4c1f8ee1ed38e2e9254ed1b0f04fec79a19d4
Checksum = 351ca7c4

So, to be able to detect an address, you have to decode it and check if it has the above requirements; if the twice hashed prefix along with the next 40 hexadecimal characters begin with the last 4 bytes of the decoded address. You can google for a base58 decoder script in C#, but it shouldn't be hard to write it yourself if you firstly understand how it works.

In Bitcoin, you should never hash a message as a text, but rather as a byte array.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
Vort3x.Layers (OP)
Jr. Member
*
Offline Offline

Activity: 47
Merit: 11


View Profile
August 20, 2021, 02:20:26 PM
 #10

BlackHatCoiner
Thanks for the assist in this topic
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!