2

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]"

9
  • Not all signatures in the Bitcoin block chain are DER. OP_RETURN prefixed outputs don't have to be 80 bytes.
    – Claris
    Nov 26, 2015 at 10:13
  • 1
    [quote]Not all signatures in the Bitcoin block chain are DER.[/quote] Is there an example for non-DER signature in blockchain?
    – amaclin
    Nov 26, 2015 at 13:51
  • @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) Nov 26, 2015 at 15:13
  • 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.
    – Claris
    Nov 26, 2015 at 15:28
  • 1
    @amaclin Some altcoins also now contain the bytes to trigger the OpenSSL 32bit consensus failure.
    – Claris
    Nov 27, 2015 at 2:12

1 Answer 1

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

4
  • 1
    Ring 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! Nov 27, 2015 at 15:12
  • @WizardOfOzzie You should probably test the regex before accepting the answer.
    – Claris
    Nov 27, 2015 at 22:22
  • 1
    You 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
    Nov 28, 2015 at 1:18
  • 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])?''' Dec 7, 2015 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.