From 0b97fb7f988ecab29f3bdc91c724f62a4bf525e5 Mon Sep 17 00:00:00 2001 From: hzx Date: Sat, 16 May 2020 19:27:50 +0800 Subject: [PATCH 1/2] recoginze segrate witness script 1. add try catch in operations function. 2. add function to recognize p2wpkh(pay to witness public key hash) script. 3. add function to recoginze p2wsh(pay to witness script hash) script. --- blockchain_parser/script.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/blockchain_parser/script.py b/blockchain_parser/script.py index ac0a6b1..e570896 100644 --- a/blockchain_parser/script.py +++ b/blockchain_parser/script.py @@ -70,8 +70,12 @@ def operations(self): """ if self._operations is None: # Some coinbase scripts are garbage, they could not be valid - self._operations = list(self.script) - + try: + self._operations = list(self.script) + except CScriptTruncatedPushDataError: + self._operations = "[INVALID]" + except CScriptInvalidError: + self._operations = "[INVALID]" return self._operations @property @@ -133,3 +137,17 @@ def is_unknown(self): return not self.is_pubkeyhash() and not self.is_pubkey() \ and not self.is_p2sh() and not self.is_multisig() \ and not self.is_return() + + # add by hzx + def is_p2wpkh(self): + if len(self.operations) == 2: + a = bytes(self.operations[0]) + b = bytes(self.operations[1]) + return len(a)==0 and len(b) == 20 + + # add by hzx + def is_p2wsh(self): + if len(self.operations) == 2: + a = bytes(self.operations[0]) + b = bytes(self.operations[1]) + return len(a)==0 and len(b) == 32 From ea7245ba721b7810b5413b25935cdde6c4fcac71 Mon Sep 17 00:00:00 2001 From: hzx Date: Sat, 16 May 2020 19:44:08 +0800 Subject: [PATCH 2/2] recoginze_segrate_witness script no extra comment --- blockchain_parser/script.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/blockchain_parser/script.py b/blockchain_parser/script.py index e570896..f7fc2c4 100644 --- a/blockchain_parser/script.py +++ b/blockchain_parser/script.py @@ -136,7 +136,8 @@ def is_multisig(self): def is_unknown(self): return not self.is_pubkeyhash() and not self.is_pubkey() \ and not self.is_p2sh() and not self.is_multisig() \ - and not self.is_return() + and not self.is_return() and not self.is_p2wpkh()\ + and not self.is_p2wsh() # add by hzx def is_p2wpkh(self): @@ -144,6 +145,8 @@ def is_p2wpkh(self): a = bytes(self.operations[0]) b = bytes(self.operations[1]) return len(a)==0 and len(b) == 20 + + return False # add by hzx def is_p2wsh(self): @@ -151,3 +154,6 @@ def is_p2wsh(self): a = bytes(self.operations[0]) b = bytes(self.operations[1]) return len(a)==0 and len(b) == 32 + + return False +