From 2012d4df23b7b065ccc1dbe596073d98e428fd3d Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Tue, 27 Nov 2018 10:06:19 -0500 Subject: Add CScriptNum decode python implementation in functional suite --- test/functional/test_framework/script.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test/functional/test_framework') diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py index 2fe44010ba..2c5ba24a6a 100644 --- a/test/functional/test_framework/script.py +++ b/test/functional/test_framework/script.py @@ -385,6 +385,22 @@ class CScriptNum: r[-1] |= 0x80 return bytes([len(r)]) + r + @staticmethod + def decode(vch): + result = 0 + # We assume valid push_size and minimal encoding + value = vch[1:] + if len(value) == 0: + return result + for i, byte in enumerate(value): + result |= int(byte) << 8*i + if value[-1] >= 0x80: + # Mask for all but the highest result bit + num_mask = (2**(len(value)*8) - 1) >> 1 + result &= num_mask + result *= -1 + return result + class CScript(bytes): """Serialized script -- cgit v1.2.3