Merge #14816: Add CScriptNum decode python implementation in functional suite

2012d4df2 Add CScriptNum decode python implementation in functional suite (Gregory Sanders)

Pull request description:

  I needed this for reasons and thought it'd be good to upsteam it.

Tree-SHA512: 6ea89fa2a5f5a7759ba722f2b4ed5cd6423ebfff4e83ac8b8b5c935e6aa479684e626c5f41fa020816d2a9079a99af5564e30808594d5c13e3b51ec9b474926d
This commit is contained in:
Wladimir J. van der Laan
2018-12-04 11:28:43 +01:00
2 changed files with 26 additions and 2 deletions

View File

@@ -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