Bitcoin Forum
April 30, 2024, 11:12:55 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Regex for Testnet WIF [solved]  (Read 149 times)
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
July 12, 2021, 07:07:41 PM
Last edit: July 21, 2021, 01:20:11 AM by naufragus
Merited by HCP (5), ABCbits (2)
 #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.
[...]
1714518775
Hero Member
*
Offline Offline

Posts: 1714518775

View Profile Personal Message (Offline)

Ignore
1714518775
Reply with quote  #2

1714518775
Report to moderator
1714518775
Hero Member
*
Offline Offline

Posts: 1714518775

View Profile Personal Message (Offline)

Ignore
1714518775
Reply with quote  #2

1714518775
Report to moderator
1714518775
Hero Member
*
Offline Offline

Posts: 1714518775

View Profile Personal Message (Offline)

Ignore
1714518775
Reply with quote  #2

1714518775
Report to moderator
Each block is stacked on top of the previous one. Adding another block to the top makes all lower blocks more difficult to remove: there is more "weight" above each block. A transaction in a block 6 blocks deep (6 confirmations) will be very difficult to remove.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714518775
Hero Member
*
Offline Offline

Posts: 1714518775

View Profile Personal Message (Offline)

Ignore
1714518775
Reply with quote  #2

1714518775
Report to moderator
pooya87
Legendary
*
Offline Offline

Activity: 3430
Merit: 10519



View Profile
July 13, 2021, 02:29:39 AM
Merited by ABCbits (1), NotATether (1), naufragus (1)
 #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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
NotATether
Legendary
*
Offline Offline

Activity: 1582
Merit: 6717


bitcoincleanup.com / bitmixlist.org


View Profile WWW
July 16, 2021, 10:32:36 PM
Merited by ABCbits (1)
 #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}$

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
kano
Legendary
*
Offline Offline

Activity: 4480
Merit: 1800


Linux since 1997 RedHat 4


View Profile
July 17, 2021, 04:47:24 AM
Last edit: July 17, 2021, 05:01:00 AM by kano
Merited by ABCbits (1), naufragus (1)
 #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}$

Pool: https://kano.is - low 0.5% fee PPLNS 3 Days - Most reliable Solo with ONLY 0.5% fee   Bitcointalk thread: Forum
Discord support invite at https://kano.is/ Majority developer of the ckpool code - k for kano
The ONLY active original developer of cgminer. Original master git: https://github.com/kanoi/cgminer
pooya87
Legendary
*
Offline Offline

Activity: 3430
Merit: 10519



View Profile
July 17, 2021, 07: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

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
kano
Legendary
*
Offline Offline

Activity: 4480
Merit: 1800


Linux since 1997 RedHat 4


View Profile
July 17, 2021, 10:30:35 AM
Merited by pooya87 (1)
 #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.

Pool: https://kano.is - low 0.5% fee PPLNS 3 Days - Most reliable Solo with ONLY 0.5% fee   Bitcointalk thread: Forum
Discord support invite at https://kano.is/ Majority developer of the ckpool code - k for kano
The ONLY active original developer of cgminer. Original master git: https://github.com/kanoi/cgminer
naufragus (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 50


View Profile
July 21, 2021, 01:51:23 AM
 #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!
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!