Bitcoin Forum
June 27, 2024, 04:00:06 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Regex for public keys (production and testnet)?  (Read 1326 times)
cloverme (OP)
Legendary
*
Offline Offline

Activity: 1512
Merit: 1057


SpacePirate.io


View Profile WWW
November 25, 2015, 02:12:55 PM
Merited by ABCbits (1)
 #1

I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/

DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4680



View Profile
November 25, 2015, 02:51:41 PM
Last edit: November 25, 2015, 03:04:15 PM by DannyHamilton
 #2

I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/

There appears to be a bug in your regex.

When I test it against the following:
Code:
1O!@#$%^&*()-=_+[]{};':",./<>?`~

It claims that it is a match.

Perhaps consider something like the following to match mainnet addresses instead:

/^[13][1-9A-HJ-NP-Za-km-z]{26,35}/

If you want to find testnet addresses you could try:

/^[2mn][1-9A-HJ-NP-Za-km-z]{26,35}/

If you want to match both testnet AND bitcoin addresses, you could try:

/^[123mn][1-9A-HJ-NP-Za-km-z]{26,35}/

Note that these REGEX that I've supplied should match all the addresses indicated, but they will also match some strings that are NOT valid addresses.  If you really want to be certain that you are finding actual addresses, you'll need to verify the checksum.
Shibashi Dogemoto
Hero Member
*****
Offline Offline

Activity: 840
Merit: 1000


View Profile
November 25, 2015, 02:56:52 PM
 #3

I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/

There appears to be a bug in your regex.

When I test it against the following:
Code:
1A!@#$%^&*()-=_+[]{};':",./<>?`~

It claims that it is a match.

I'm not a regex guru, but if i read this correctly, the regex says:
Start with a 1,3 or n
Then a number between 1 and 9 or any letter (upper or lowercase)
Then between 20 and 40 characters, any character will do except OI and l...

So, basically, your test is supposed to match (starts with 1, then an A, then between 20 and 40 random characters (but no O,I or l)

If somebody points me to the correct defenition of a mainnet and testnet key, i can take a stab at writing a regex (no promises tough)
cloverme (OP)
Legendary
*
Offline Offline

Activity: 1512
Merit: 1057


SpacePirate.io


View Profile WWW
November 26, 2015, 02:40:41 AM
 #4


There appears to be a bug in your regex.

When I test it against the following:
Code:
1O!@#$%^&*()-=_+[]{};':",./<>?`~

If you want to match both testnet AND bitcoin addresses, you could try:

/^[123mn][1-9A-HJ-NP-Za-km-z]{26,35}/

Note that these REGEX that I've supplied should match all the addresses indicated, but they will also match some strings that are NOT valid addresses.  If you really want to be certain that you are finding actual addresses, you'll need to verify the checksum.

This did the trick for me... Agreed on checksum too.  Although, I can rely on the validateaddress api call to bitcoind as well. Mainly, I wanted the regex to keep people from spamming a bitcoin address in chat.

Much appreciated for your help sir!
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!