Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/script.py
| # 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 Bitcoin Scripts. | Functionality to build Bitcoin Scripts. | ||||
| """ | """ | ||||
| import struct | import struct | ||||
| import unittest | import unittest | ||||
| from typing import Dict, List | from typing import Dict, List | ||||
| MAX_SCRIPT_ELEMENT_SIZE = 520 | MAX_SCRIPT_ELEMENT_SIZE = 520 | ||||
| OPCODE_NAMES: Dict["CScriptOp", str] = {} | OPCODE_NAMES: Dict["CScriptOp", str] = {} | ||||
| 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)) | ||||
| ▲ Show 20 Lines • Show All 622 Lines • Show Last 20 Lines | |||||