mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-14 12:43:40 +02:00
functional framework support for CScriptNum decode
This commit is contained in:
parent
823b9093e9
commit
8f6bedfd4b
1 changed files with 20 additions and 3 deletions
|
|
@ -368,11 +368,12 @@ class CScriptNum():
|
|||
|
||||
@staticmethod
|
||||
def encode(obj):
|
||||
val = obj.value
|
||||
r = bytearray(0)
|
||||
if obj.value == 0:
|
||||
if val == 0:
|
||||
return bytes(r)
|
||||
neg = obj.value < 0
|
||||
absvalue = -obj.value if neg else obj.value
|
||||
neg = val < 0
|
||||
absvalue = -val if neg else val
|
||||
while (absvalue):
|
||||
r.append(absvalue & 0xff)
|
||||
absvalue >>= 8
|
||||
|
|
@ -382,6 +383,22 @@ class CScriptNum():
|
|||
r[-1] |= 0x80
|
||||
return bytes([len(r)]) + r
|
||||
|
||||
@staticmethod
|
||||
def decode(vch):
|
||||
# We assume valid push_size and minimal encoding
|
||||
value = vch[1:]
|
||||
# Mask for all but the highest result bit
|
||||
num_mask = (2**(len(value)*8) - 1) >> 1
|
||||
if len(value) == 0:
|
||||
return 0
|
||||
else :
|
||||
result = 0
|
||||
for i in range(len(value)):
|
||||
result |= int(value[i]) << 8*i
|
||||
if value[-1] >= 0x80:
|
||||
result &= num_mask
|
||||
result *= -1
|
||||
return result
|
||||
|
||||
class CScript(bytes):
|
||||
"""Serialized script
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue