Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: naufragus on July 12, 2021, 07:07:41 PM



Title: Regex for Testnet WIF [solved]
Post by: naufragus on July 12, 2021, 07:07:41 PM
I am writing a script for dealing with bitcoin hashing and it would be useful to have a function to validate Testnet WIF , both uncompressed (starting with 9) and compressed (starting with c), such as:

Code:
92Pg46rUhgTT7romnV7iGW6W1gbGdeezqdbJCzShkCsYNzyyNcc
cNJFgo1driFnPcBdBX8BrJrpxchBWXwXCvNH5SoSkdcF6JXXwHMm
#https://en.bitcoin.it/wiki/List_of_address_prefixes

I have got regexes for most of other types of addresses, including regexes for mainet WIF types but they don't seem to match if I merely add [9c] to the prefix list.. I read somewhere that prefix and ending of testnet WIFs are different from those of mainet. Below is the regex for mainet WIFs I have got and which does not work.

Code:
[59KLc][1-9A-HJ-NP-Za-km-z]{50,51}
UPDATE: THIS ACTUALLY WORKS THO

If you guys know a regex that could work with testnet WIFs, please let me know. Or point to a page of testnet WIFs I can check and try something myself (could not find anything that helped me on Google yet)..

Cheers

UPDATE:
OK, I reckon that regex is working when adding the right prefixes in the character list.
[...]


Title: Re: Regex for Testnet WIF [solved]
Post by: pooya87 on July 13, 2021, 02:29:39 AM
a function to validate Testnet WIF ,
This is not how you should "validate" a WIF though. The string length and starting character are not important to the code (only for readability).

What you should do is decode the string using Base58check (validates its checksum too) then check the byte array result:
- Length: 33 byte long for uncompressed, 34 byte long compressed
- First byte: based on network type eg. 239 for regtest
- Last byte: only for compressed that must be 1
- Remaining 32 byte: that must be between 1 and N-1 inclusive

For example KwFAa6AumokBD2dVqQLPou42jHiVsvThY1n25HJ8Ji8REf1wxAQb is an invalid mainnet private key.


Title: Re: Regex for Testnet WIF [solved]
Post by: NotATether on July 16, 2021, 10:32:36 PM
This is something I found while browsing Binance API docs at work so I decided to share it with you. It's a regex that they return that matches mainnet (1,3, bc1) addresses:

Code:
^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$


Title: Re: Regex for Testnet WIF [solved]
Post by: kano on July 17, 2021, 04:47:24 AM
This is something I found while browsing Binance API docs at work so I decided to share it with you. It's a regex that they return that matches mainnet (1,3, bc1) addresses:

Code:
^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$
Though it's not actually correct for bc1...
Firstly it can be BC1 or bc1.
The requirement for the rest is matching the B/b all uppercase or all lowercase.
Of course an all uppercase BC1 address is the same address as the matching all lowercase bc1 address
and certain characters are not allowed: B/b, I/i, O/o, 1

i.e.
Code:
^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[ac-hj-np-z02-9]{39,59}$|^(BC1)[AC-HJ-NP-Z02-9]{39,59}$


Title: Re: Regex for Testnet WIF [solved]
Post by: pooya87 on July 17, 2021, 07:41:47 AM
Same arguments for addresses, again we should't verify the string like this but instead the data that the string represents. For example all the following examples are invalid bitcoin addresses that would be accepted by looking at the "string" alone:
Code:
bc1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq9e75rs   data is empty byte[]
bc1pqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqmmente   data is empty byte[]
bc1qremwsxv3jm29f9qughgm8ger79pnh4sk4q4te    data is byte[19]
bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh   BIP350 is used to encode the witver==0 address
bc1pw508d6qejxtdg4y5r3zarvary0c5xw7k8e76x7   BIP173 is used to encode a witver>0 address


Title: Re: Regex for Testnet WIF [solved]
Post by: kano on July 17, 2021, 10:30:35 AM
Actually, in my case on my pool I simply use the pattern match to filter them before asking bitcoind to verify them.

Few people enter an invalid address that passes the pattern match, but the pattern check is of course a much faster and simpler pre-filter.

So yes indeed the pattern match doesn't guarantee a valid bc1 address, it has to just be a pre-filter.


Title: Re: Regex for Testnet WIF [solved]
Post by: naufragus on July 21, 2021, 01:51:23 AM
a function to validate Testnet WIF ,
This is not how you should "validate" a WIF though. The string length and starting character are not important to the code (only for readability).

What you should do is [...]

Thanks, pooya87! I now understand the correct validation method of Private Addresses. That info is really useful but also made me aware I really only need a pattern check, as kano explained.

NotATether was nice, too. I never found that regex in Binance API docs but I did not read those docs thoroughly when I was messing with them. All the discussion about the pattern regex was interesting (I know you guys are making history here) and I was able to add the mainnet uppercase BC1 regex to my collection as that case was not covered yet.  Cheers!