Regular Expressions for DER signature hex (also req, address, TxIDs)?

1

I'm using Python (2.7) and its re module for recognition of various Bitcoin data, namely:

addresses, DER sigs, OP Return hexdata, TxIDs

I've been using re.compile: for example, for a valid Tx hash (TxID):

RE_TXHASH = re.compile('^[0-9a-fA-F]{64}$')
if bool(re.match(RE_TXHASH, 'f'*64)):    # 'f'*63 would fail
    print 'valid tx hash!'
else:
    raise Exception("invalid tx hash!")

I'm looking for regex patterns for the following (or input on my current best attempt):

  1. DER signatures: General format "30[sig_size]02[r_size]02[s_size][sighash]"

  2. Bitcoin addresses: re.compile('^[123mn]{1}[a-km-zA-HJ-NP-Z0-9]{26,33}$')

  3. OP Return hex strings: re.compile('^(6a){1}[a-fA-F0-9]{0,80}$')

  4. TxIDs: re.compile('^[0-9a-fA-F]{64}$')

EDIT: to clarify, can someone help with a regex pattern for this? "30[sig_size]02[r_size]02[s_size][sighash]"

Wizard Of Ozzie

Posted 2015-11-26T05:38:03.563

Reputation: 4 535

Not all signatures in the Bitcoin block chain are DER. OP_RETURN prefixed outputs don't have to be 80 bytes.Anonymous 2015-11-26T10:13:31.877

1[quote]Not all signatures in the Bitcoin block chain are DER.[/quote] Is there an example for non-DER signature in blockchain?amaclin 2015-11-26T13:51:26.920

@Bitcoin I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern. Re OP_RETURN, isn't the regex saying between 0 and 80? (Honest question)Wizard Of Ozzie 2015-11-26T15:13:55.027

2@amaclin All of the ones with unnecessary amounts of padding on the values are BER not DER. You must use the smallest possible encoding for DER rules.Anonymous 2015-11-26T15:28:19.460

Yes the regex is saying between one and eighty, but that's not a requirement of the data type.Anonymous 2015-11-27T01:19:19.567

WRT the OP_RETURN: You're missing the data push byte. The data doesn't start immediately after the return. Also, 80 bytes is standard, but not required by consensus.Nick ODell 2015-11-27T01:50:41.523

@NickODell Expecting a certain data push type is ill advised, push types are malleable and not always used consistently. 80 bytes you could use 0x50 for a direct push, 0x4c50 for PUSHDATA1, or 0x4d0050 for PUSHDATA2, or 0x4e00000050 for PUSHDATA4. Regular expressions isn't ideal for this task, especially for BER signatures.Anonymous 2015-11-27T02:10:13.307

1@amaclin Some altcoins also now contain the bytes to trigger the OpenSSL 32bit consensus failure.Anonymous 2015-11-27T02:12:41.793

@Bitcoin How did we reach discussing altcoins, DER/BER and OP_RETURN ahead of my question on regexs? Me: I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern. seems pretty clear and independent of DER/BER classificationWizard Of Ozzie 2015-11-27T15:15:47.037

Answers

2

For DER sigs, it's possible for R or S to be very small.

  1. DER sigs: re.compile('^30[0-9a-fA-F]{72,136}02[0-9a-fA-F]{0,64}02[0-9a-fA-F]{0,64}[0-9a-fA-F]{8}')

Bitcoin addresses cannot have a 0. Also the {1} is superfluous

  1. Bitcoin addresses: re.compile('^[123mn][a-km-zA-HJ-NP-Z1-9]{26,33}$')

Again, {1} is superfluous for OP_RETURN. Also, the a might be capitalized.

  1. OP Return hex strings: re.compile('^6[aA][a-fA-F0-9]{0,80}$')

TXIDs are fine

Jimmy Song

Posted 2015-11-26T05:38:03.563

Reputation: 7 067

1Ring a ding ding. Thank you for answering the question: I get that it's SO and pedantic semantics are to be expected... +250 bounty headed your way for the concise answer!Wizard Of Ozzie 2015-11-27T15:12:29.790

@WizardOfOzzie You should probably test the regex before accepting the answer.Anonymous 2015-11-27T22:22:21.670

1You might want to consider using '([0-9a-fA-F][0-9a-fA-F]){X}' instead of '[0-9a-fA-F]{2X}', to capture the fact that the hex chars must come in pairs.morsecoder 2015-11-28T01:18:25.190

I ended up using: ur'''30(?P<siglen>[0-4][0-9a-f])02(?P<rlen>[0-2][0-9a-f])(?P<r>(?:00)?[a-f0-9]{2,64})02(?P<slen>[0-2][0-9a-f])(?P<s>(?:00)?[a-f0-9]{2,64})(?P<sighash>(0|8)[0-3])?'''Wizard Of Ozzie 2015-12-07T08:59:11.100