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 | |||||
from .messages import ( | from .messages import ( | ||||
CTransaction, | CTransaction, | ||||
CTxOut, | CTxOut, | ||||
hash256, | hash256, | ||||
ser_string, | ser_string, | ||||
ser_uint256, | ser_uint256, | ||||
sha256, | sha256, | ||||
▲ Show 20 Lines • Show All 725 Lines • ▼ Show 20 Lines | def SignatureHashForkId(script, txTo, inIdx, hashtype, amount): | ||||
ss += ser_string(script) | ss += ser_string(script) | ||||
ss += struct.pack("<q", amount) | ss += struct.pack("<q", amount) | ||||
ss += struct.pack("<I", txTo.vin[inIdx].nSequence) | ss += struct.pack("<I", txTo.vin[inIdx].nSequence) | ||||
ss += ser_uint256(hashOutputs) | ss += ser_uint256(hashOutputs) | ||||
ss += struct.pack("<i", txTo.nLockTime) | ss += struct.pack("<i", txTo.nLockTime) | ||||
ss += struct.pack("<I", hashtype) | ss += struct.pack("<I", hashtype) | ||||
return hash256(ss) | return hash256(ss) | ||||
class TestFrameworkScript(unittest.TestCase): | |||||
def test_bn2vch(self): | |||||
self.assertEqual(bn2vch(0), bytes([])) | |||||
self.assertEqual(bn2vch(1), bytes([0x01])) | |||||
self.assertEqual(bn2vch(-1), bytes([0x81])) | |||||
self.assertEqual(bn2vch(0x7F), bytes([0x7F])) | |||||
self.assertEqual(bn2vch(-0x7F), bytes([0xFF])) | |||||
self.assertEqual(bn2vch(0x80), bytes([0x80, 0x00])) | |||||
self.assertEqual(bn2vch(-0x80), bytes([0x80, 0x80])) | |||||
self.assertEqual(bn2vch(0xFF), bytes([0xFF, 0x00])) | |||||
self.assertEqual(bn2vch(-0xFF), bytes([0xFF, 0x80])) | |||||
self.assertEqual(bn2vch(0x100), bytes([0x00, 0x01])) | |||||
self.assertEqual(bn2vch(-0x100), bytes([0x00, 0x81])) | |||||
self.assertEqual(bn2vch(0x7FFF), bytes([0xFF, 0x7F])) | |||||
self.assertEqual(bn2vch(-0x8000), bytes([0x00, 0x80, 0x80])) | |||||
self.assertEqual(bn2vch(-0x7FFFFF), bytes([0xFF, 0xFF, 0xFF])) | |||||
self.assertEqual(bn2vch(0x80000000), | |||||
bytes([0x00, 0x00, 0x00, 0x80, 0x00])) | |||||
self.assertEqual(bn2vch(-0x80000000), | |||||
bytes([0x00, 0x00, 0x00, 0x80, 0x80])) | |||||
self.assertEqual(bn2vch(0xFFFFFFFF), | |||||
bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00])) | |||||
self.assertEqual(bn2vch(123456789), | |||||
bytes([0x15, 0xCD, 0x5B, 0x07])) | |||||
self.assertEqual(bn2vch(-54321), | |||||
bytes([0x31, 0xD4, 0x80])) |