Author

Topic: Regex for Testnet WIF [solved] (Read 168 times)

newbie
Activity: 29
Merit: 50
July 20, 2021, 08:51:23 PM
#7
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!
legendary
Activity: 4592
Merit: 1851
Linux since 1997 RedHat 4
July 17, 2021, 05:30:35 AM
#6
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.
legendary
Activity: 3472
Merit: 10611
July 17, 2021, 02:41:47 AM
#5
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
legendary
Activity: 4592
Merit: 1851
Linux since 1997 RedHat 4
July 16, 2021, 11:47:24 PM
#4
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}$
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
July 16, 2021, 05:32:36 PM
#3
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}$
legendary
Activity: 3472
Merit: 10611
July 12, 2021, 09:29:39 PM
#2
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.
newbie
Activity: 29
Merit: 50
July 12, 2021, 02:07:41 PM
#1
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.
[...]
Jump to: