chainside / btcpy

A Python3 SegWit-compliant library which provides tools to handle Bitcoin data structures in a simple fashion.
https://www.chainside.net
GNU Lesser General Public License v3.0
270 stars 73 forks source link

Efficient script matching #32

Open SimoneBronzini opened 6 years ago

SimoneBronzini commented 6 years ago

At the moment ScriptBuilder tries to match scripts by running their verify() method, which in turn tries to both match a script template and return the pushed values (e.g. public key hash, script hash and so on). It would be useful to have a faster way to match script types, for instance having scripts declare fast matching conditions (e.g. P2SH must start with HASH160, followed by a 20-byte push operation, followed by EQUAL) to be used for template matching and verify() logics can be used to return the pushed data once the fast matching succeeds.

To be clear, an example of P2SH efficient matching might be:

def match(script_bytes):
    return all([script_bytes[0] == 169,
                script_bytes[1] == 20,
                script_bytes[-1] == 135,
                len(script_bytes) == 23])