def test_p2pkh_txweight(self): coin = coins.by_name('Bitcoin') seed = bip39.seed(' '.join(['all'] * 12), '') inp1 = TxInputType( address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e # amount=390000, prev_hash=unhexlify( 'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882' ), prev_index=0, amount=None, script_type=InputScriptType.SPENDADDRESS, sequence=None, multisig=None) out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 10000, script_type=OutputScriptType.PAYTOADDRESS, address_n=[], multisig=None) calculator = TxWeightCalculator(1, 1) calculator.add_input(inp1) calculator.add_output(output_derive_script(out1, coin)) serialized_tx = '010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000' tx_weight = len( serialized_tx) / 2 * 4 # non-segwit tx's weight is simple length*4 self.assertEqual(calculator.get_total(), tx_weight)
def test_bip143_outputs(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) for txo in [self.out1, self.out2]: txo_bin = TxOutputBinType() txo_bin.amount = txo.amount script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin, script_pubkey) outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double) self.assertEqual(hexlify(outputs_hash), b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83')
def test_native_p2wpkh_txweight(self): coin = coins.by_name('Testnet') seed = bip39.seed(' '.join(['all'] * 12), '') inp1 = TxInputType( # 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0], amount=12300000, prev_hash=unhexlify( '09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a' ), prev_index=0, script_type=InputScriptType.SPENDWITNESS, sequence=0xffffffff, multisig=None, ) out1 = TxOutputType( address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp', amount=5000000, script_type=OutputScriptType.PAYTOADDRESS, address_n=[], multisig=None, ) out2 = TxOutputType( address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu', script_type=OutputScriptType.PAYTOADDRESS, amount=12300000 - 11000 - 5000000, address_n=[], multisig=None, ) calculator = TxWeightCalculator(1, 2) calculator.add_input(inp1) calculator.add_output(output_derive_script(out1, coin)) calculator.add_output(output_derive_script(out2, coin)) self.assertEqual(calculator.get_total(), 566)
def test_p2wpkh_in_p2sh_txweight(self): coin = coins.by_name('Testnet') seed = bip39.seed(' '.join(['all'] * 12), '') inp1 = TxInputType( # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0], amount=123456789, prev_hash=unhexlify( '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337' ), prev_index=0, script_type=InputScriptType.SPENDP2SHWITNESS, sequence=0xffffffff, multisig=None, ) out1 = TxOutputType( address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC', amount=12300000, script_type=OutputScriptType.PAYTOADDRESS, address_n=[], multisig=None, ) out2 = TxOutputType( address='2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX', script_type=OutputScriptType.PAYTOADDRESS, amount=123456789 - 11000 - 12300000, address_n=[], multisig=None, ) calculator = TxWeightCalculator(1, 2) calculator.add_input(inp1) calculator.add_output(output_derive_script(out1, coin)) calculator.add_output(output_derive_script(out2, coin)) self.assertEqual(calculator.get_total(), 670)
def test_outputs(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) for txo in [self.out1, self.out2]: txo_bin = TxOutputBinType() txo_bin.amount = txo.amount script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin, script_pubkey) outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double) self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
def test_bip143_preimage_testdata(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) for txo in [self.out1, self.out2]: txo_bin = TxOutputBinType() txo_bin.amount = txo.amount script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin, script_pubkey) # test data public key hash result = bip143.hash143_preimage_hash(self.inp1, unhexlify('79091972186c449eb1ded22b78e40d009bdf0089')) self.assertEqual(hexlify(result), b'64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6')
def output_derive_script(self, txo: TxOutputType) -> bytes: if txo.address_n: # change output try: input_script_type = helpers.CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[ txo.script_type] except KeyError: raise SigningError(FailureType.DataError, "Invalid script type") node = self.keychain.derive(txo.address_n) txo.address = addresses.get_address(input_script_type, self.coin, node, txo.multisig) return scripts.output_derive_script(txo, self.coin)
def test_preimage_testdata(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) bip143.hash143_add_input(self.inp2) for txo in [self.out1, self.out2]: txo_bin = TxOutputBinType() txo_bin.amount = txo.amount script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin, script_pubkey) # test data public key hash # only for input 2 - input 1 is not segwit result = bip143.hash143_preimage_hash(self.inp2, unhexlify('1d0f172a0ecb48aee1be1f2687d2963ae33f71a1')) self.assertEqual(hexlify(result), b'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670')