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. | ||||
""" | """ | ||||
import hashlib | import hashlib | ||||
import struct | import struct | ||||
import unittest | import unittest | ||||
from typing import List, Dict | |||||
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: Dict["CScriptOp", str] = {} | ||||
def hash160(s): | def hash160(s): | ||||
return hashlib.new('ripemd160', sha256(s)).digest() | return hashlib.new('ripemd160', sha256(s)).digest() | ||||
def bn2vch(v): | def bn2vch(v): | ||||
"""Convert number to bitcoin-specific little endian format.""" | """Convert number to bitcoin-specific little endian format.""" | ||||
# We need v.bit_length() bits, plus a sign bit for every nonzero number. | # We need v.bit_length() bits, plus a sign bit for every nonzero number. | ||||
n_bits = v.bit_length() + (v != 0) | n_bits = v.bit_length() + (v != 0) | ||||
# The number of bytes for that is: | # The number of bytes for that is: | ||||
n_bytes = (n_bits + 7) // 8 | n_bytes = (n_bits + 7) // 8 | ||||
# Convert number to absolute value + sign in top bit. | # Convert number to absolute value + sign in top bit. | ||||
encoded_v = 0 if v == 0 else abs(v) | ((v < 0) << (n_bytes * 8 - 1)) | encoded_v = 0 if v == 0 else abs(v) | ((v < 0) << (n_bytes * 8 - 1)) | ||||
# Serialize to bytes | # Serialize to bytes | ||||
return encoded_v.to_bytes(n_bytes, 'little') | return encoded_v.to_bytes(n_bytes, 'little') | ||||
_opcode_instances = [] | _opcode_instances: List["CScriptOp"] = [] | ||||
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): | ||||
▲ Show 20 Lines • Show All 728 Lines • Show Last 20 Lines |