Skip to content

Commit 8f6bedf

Browse files
committed
functional framework support for CScriptNum decode
1 parent 823b909 commit 8f6bedf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

test/functional/test_framework/script.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,12 @@ def __init__(self, d=0):
368368

369369
@staticmethod
370370
def encode(obj):
371+
val = obj.value
371372
r = bytearray(0)
372-
if obj.value == 0:
373+
if val == 0:
373374
return bytes(r)
374-
neg = obj.value < 0
375-
absvalue = -obj.value if neg else obj.value
375+
neg = val < 0
376+
absvalue = -val if neg else val
376377
while (absvalue):
377378
r.append(absvalue & 0xff)
378379
absvalue >>= 8
@@ -382,6 +383,22 @@ def encode(obj):
382383
r[-1] |= 0x80
383384
return bytes([len(r)]) + r
384385

386+
@staticmethod
387+
def decode(vch):
388+
# We assume valid push_size and minimal encoding
389+
value = vch[1:]
390+
# Mask for all but the highest result bit
391+
num_mask = (2**(len(value)*8) - 1) >> 1
392+
if len(value) == 0:
393+
return 0
394+
else :
395+
result = 0
396+
for i in range(len(value)):
397+
result |= int(value[i]) << 8*i
398+
if value[-1] >= 0x80:
399+
result &= num_mask
400+
result *= -1
401+
return result
385402

386403
class CScript(bytes):
387404
"""Serialized script

0 commit comments

Comments
 (0)