Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/script.py
Show First 20 Lines • Show All 409 Lines • ▼ Show 20 Lines | def encode(obj): | ||||
r.append(absvalue & 0xff) | r.append(absvalue & 0xff) | ||||
absvalue >>= 8 | absvalue >>= 8 | ||||
if r[-1] & 0x80: | if r[-1] & 0x80: | ||||
r.append(0x80 if neg else 0) | r.append(0x80 if neg else 0) | ||||
elif neg: | elif neg: | ||||
r[-1] |= 0x80 | r[-1] |= 0x80 | ||||
return bytes([len(r)]) + r | 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): | class CScript(bytes): | ||||
"""Serialized script | """Serialized script | ||||
A bytes subclass, so you can use this directly whenever bytes are accepted. | A bytes subclass, so you can use this directly whenever bytes are accepted. | ||||
Note that this means that indexing does *not* work - you'll get an index by | Note that this means that indexing does *not* work - you'll get an index by | ||||
byte rather than opcode. This format was chosen for efficiency so that the | byte rather than opcode. This format was chosen for efficiency so that the | ||||
general case would not require creating a lot of little CScriptOP objects. | general case would not require creating a lot of little CScriptOP objects. | ||||
▲ Show 20 Lines • Show All 307 Lines • Show Last 20 Lines |