Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/script.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Functionality to build scripts, as well as SignatureHash(). | """Functionality to build scripts, as well as SignatureHash(). | ||||
This file is modified from python-bitcoinlib. | This file is modified from python-bitcoinlib. | ||||
""" | """ | ||||
from .bignum import bn2vch | |||||
import hashlib | import hashlib | ||||
import struct | import struct | ||||
from .messages import ( | from .messages import ( | ||||
CTransaction, | CTransaction, | ||||
CTxOut, | CTxOut, | ||||
hash256, | hash256, | ||||
ser_string, | ser_string, | ||||
ser_uint256, | ser_uint256, | ||||
sha256, | sha256, | ||||
uint256_from_str, | uint256_from_str, | ||||
) | ) | ||||
MAX_SCRIPT_ELEMENT_SIZE = 520 | MAX_SCRIPT_ELEMENT_SIZE = 520 | ||||
OPCODE_NAMES = {} | OPCODE_NAMES = {} | ||||
def hash160(s): | def hash160(s): | ||||
return hashlib.new('ripemd160', sha256(s)).digest() | return hashlib.new('ripemd160', sha256(s)).digest() | ||||
def bn2vch(v): | |||||
"""Convert number to bitcoin-specific little endian format.""" | |||||
# The top bit is used to indicate the sign of the number. If there | |||||
# isn't a spare bit in the bit length, add an extension byte. | |||||
have_ext = False | |||||
ext = bytearray() | |||||
if v.bit_length() > 0: | |||||
have_ext = (v.bit_length() & 0x07) == 0 | |||||
ext.append(0) | |||||
# Is the number negative? | |||||
neg = False | |||||
if v < 0: | |||||
neg = True | |||||
v = -v | |||||
# Convert the int to bytes | |||||
v_bin = bytearray() | |||||
bytes_len = (v.bit_length() + 7) // 8 | |||||
for i in range(bytes_len, 0, -1): | |||||
v_bin.append((v >> ((i - 1) * 8)) & 0xff) | |||||
# Add the sign bit if necessary | |||||
if neg: | |||||
if have_ext: | |||||
ext[0] |= 0x80 | |||||
else: | |||||
v_bin[0] |= 0x80 | |||||
v_bytes = ext + v_bin | |||||
# Reverse bytes ordering for LE | |||||
v_bytes.reverse() | |||||
return bytes(v_bytes) | |||||
_opcode_instances = [] | _opcode_instances = [] | ||||
class CScriptOp(int): | class CScriptOp(int): | ||||
"""A single script opcode""" | """A single script opcode""" | ||||
__slots__ = () | __slots__ = () | ||||
@staticmethod | @staticmethod | ||||
def encode_op_pushdata(d): | def encode_op_pushdata(d): | ||||
"""Encode a PUSHDATA op, returning bytes""" | """Encode a PUSHDATA op, returning bytes""" | ||||
if len(d) < 0x4c: | if len(d) < 0x4c: | ||||
# OP_PUSHDATA | # OP_PUSHDATA | ||||
return b'' + bytes([len(d)]) + d | return b'' + bytes([len(d)]) + d | ||||
elif len(d) <= 0xff: | elif len(d) <= 0xff: | ||||
# OP_PUSHDATA1 | # OP_PUSHDATA1 | ||||
return b'\x4c' + bytes([len(d)]) + d | return b'\x4c' + bytes([len(d)]) + d | ||||
elif len(d) <= 0xffff: | elif len(d) <= 0xffff: | ||||
return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2 | # OP_PUSHDATA2 | ||||
return b'\x4d' + struct.pack(b'<H', len(d)) + d | |||||
elif len(d) <= 0xffffffff: | elif len(d) <= 0xffffffff: | ||||
return b'\x4e' + struct.pack(b'<I', len(d)) + d # OP_PUSHDATA4 | # OP_PUSHDATA4 | ||||
return b'\x4e' + struct.pack(b'<I', len(d)) + d | |||||
else: | else: | ||||
raise ValueError("Data too long to encode in a PUSHDATA op") | raise ValueError("Data too long to encode in a PUSHDATA op") | ||||
@staticmethod | @staticmethod | ||||
def encode_op_n(n): | def encode_op_n(n): | ||||
"""Encode a small integer op, returning an opcode""" | """Encode a small integer op, returning an opcode""" | ||||
if not (0 <= n <= 16): | if not (0 <= n <= 16): | ||||
raise ValueError( | raise ValueError( | ||||
▲ Show 20 Lines • Show All 687 Lines • Show Last 20 Lines |