def test_tx_inandout(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual(t, helpers.P2PKH1['ser']['tx']['signed']) with self.assertRaises(ValueError) as context: tx_ins = [] tx.Tx(self.version, self.none_flag, tx_ins, self.tx_outs, None, self.lock_time) self.assertIn('Too few inputs or outputs. Stop that.', str(context.exception)) with self.assertRaises(ValueError) as context: tx_ins = [1] tx.Tx(self.version, self.none_flag, tx_ins, self.tx_outs, None, self.lock_time) self.assertIn('Invalid TxIn. Expected instance of TxIn. Got int', str(context.exception)) with self.assertRaises(ValueError) as context: tx_outs = [1] tx.Tx(self.version, self.none_flag, self.tx_ins, tx_outs, None, self.lock_time) self.assertIn('Invalid TxOut. Expected instance of TxOut. Got int', str(context.exception))
def test_tx_witness(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual(t, helpers.P2PKH1['ser']['tx']['signed']) with self.assertRaises(ValueError) as context: tx.Tx(self.version, b'\x00\x00', self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertIn('Invald segwit flag. Expected None or ', str(context.exception)) with self.assertRaises(ValueError) as context: tx.Tx(self.version, None, self.tx_ins, self.tx_outs, self.tx_witnesses, self.lock_time) self.assertIn('Got witnesses but no segwit flag.', str(context.exception)) with self.assertRaises(ValueError) as context: stack = self.stack + [self.stack[0]] witness = tx.InputWitness(stack) tx.Tx(self.version, self.segwit_flag, self.tx_ins, self.tx_outs, witness, self.lock_time) self.assertIn('Witness and TxIn lists must be same length. ', str(context.exception)) with self.assertRaises(ValueError) as context: tx.Tx(self.version, self.segwit_flag, self.tx_ins, self.tx_outs, [1 for _ in self.tx_witnesses], self.lock_time) self.assertIn( 'Invalid InputWitness. Expected instance of InputWitness.', str(context.exception))
def test_presegwit_sighashes(self): ''' all, all anyonecanpay, single, single_anyonecanpay. Marks transaction as pre- or non-segwit in a segwit network. ''' t = tx.Tx(self.version, None, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual( t.sighash_all( 0, helpers.P2PKH1['ser']['ins'][0]['pk_script'], ), helpers.P2PKH1['ser']['sighash']['all']) self.assertEqual( t.sighash_all(0, helpers.P2PKH1['ser']['ins'][0]['pk_script'], anyone_can_pay=True), helpers.P2PKH1['ser']['sighash']['all_anyonecanpay']) self.assertEqual( t.sighash_single(0, helpers.P2PKH1['ser']['ins'][0]['pk_script']), helpers.P2PKH1['ser']['sighash']['single']) self.assertEqual( t.sighash_single(0, helpers.P2PKH1['ser']['ins'][0]['pk_script'], anyone_can_pay=True), helpers.P2PKH1['ser']['sighash']['single_anyonecanpay'])
def test_everything(self): version = utils.i2le_padded(1, 4) outpoint_index = utils.i2le_padded(0, 4) outpoint_tx_id = bytearray( bytearray.fromhex('10399b3f20cbdd4b5ac3f823afdba28b' '9f70e21437a59b312a1b62c42c5cd101'))[::-1] outpoint = tx.Outpoint(outpoint_tx_id, outpoint_index) sequence = utils.i2le_padded(0, 4) script = bytearray( bytearray.fromhex( '473044022000e02ea97289a35181a9bfabd324f12439410db11c4e94978cdade6a665bf1840220458b87c34d8bb5e4d70d01041c7c2d714ea8bfaca2c2d2b1f9e5749c3ee17e3d012102ed0851f0b4c4458f80e0310e57d20e12a84642b8e097fe82be229edbd7dbd53920f6665740b1f950eb58d646b1fae9be28cef842da5e51dc78459ad2b092e7fd6e514c5163a914bb408296de2420403aa79eb61426bb588a08691f8876a91431b31321831520e346b069feebe6e9cf3dd7239c670400925e5ab17576a9140d22433293fe9652ea00d21c5061697aef5ddb296888ac' )) # noqa: E501 tx_in = tx.TxIn(outpoint, script, bytearray(), sequence) tx_ins = [tx_in] tx_outs = [ tx.TxOut( value=bytearray(utils.i2le_padded(2000, 8)), output_script=bytearray( bytearray.fromhex( '76a914f2539f42058da784a9d54615ad074436cf3eb85188ac') )) # noqa: E501 ] lock_time = utils.i2le_padded(0, 4) res = tx.Tx(version, None, tx_ins, tx_outs, None, lock_time) self.assertEqual(res, helpers.RAW_P2SH_TO_P2PKH)
def test_sighash_single_bug(self): with self.assertRaises(NotImplementedError) as context: t = tx.Tx(self.version, self.none_flag, self.tx_ins * 3, self.tx_outs, self.none_witnesses, self.lock_time) t.sighash_single(2, helpers.P2PKH1['ser']['ins'][0]['pk_script']) self.assertIn('I refuse to implement the SIGHASH_SINGLE bug.', str(context.exception))
def test_copy(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) t_copy = t.copy() self.assertEqual(t, t_copy) self.assertIsNot(t, t_copy)
def test_sighash_none(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) with self.assertRaises(NotImplementedError) as context: t.sighash_none() self.assertIn('SIGHASH_NONE is a bad idea.', str(context.exception))
def test_is_witness(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertFalse(t.is_witness()) t = tx.Tx.from_bytes(helpers.P2WSH['ser']['tx']['signed']) self.assertTrue(t.is_witness())
def test_tx_inout_mutation(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) with self.assertRaises(TypeError, msg='That\'s immutable, honey'): t.tx_ins = t.tx_ins + (1, ) with self.assertRaises(TypeError, msg='That\'s immutable, honey'): t.tx_outs = t.tx_outs + (1, )
def make_tx(version, tx_ins, tx_outs, lock_time, expiry=None, value_balance=None, tx_shielded_spends=None, tx_shielded_outputs=None, tx_witnesses=None, tx_joinsplits=None, joinsplit_pubkey=None, joinsplit_sig=None, binding_sig=None): ''' int, list(TxIn), list(TxOut), int, list(InputWitness) -> Tx ''' n = riemann.get_current_network_name() if 'decred' in n: return tx.DecredTx( version=utils.i2le_padded(version, 4), tx_ins=tx_ins, tx_outs=tx_outs, lock_time=utils.i2le_padded(lock_time, 4), expiry=utils.i2le_padded(expiry, 4), tx_witnesses=[tx_witnesses]) if 'sprout' in n and tx_joinsplits is not None: return tx.SproutTx( version=version, tx_ins=tx_ins, tx_outs=tx_outs, lock_time=utils.i2le_padded(lock_time, 4), tx_joinsplits=tx_joinsplits if tx_joinsplits is not None else [], joinsplit_pubkey=joinsplit_pubkey, joinsplit_sig=joinsplit_sig) if 'overwinter' in n: return tx.OverwinterTx( tx_ins=tx_ins, tx_outs=tx_outs, lock_time=utils.i2le_padded(lock_time, 4), expiry_height=utils.i2le_padded(expiry, 4), tx_joinsplits=tx_joinsplits if tx_joinsplits is not None else [], joinsplit_pubkey=joinsplit_pubkey, joinsplit_sig=joinsplit_sig) if 'sapling' in n: return tx.SaplingTx( tx_ins=tx_ins, tx_outs=tx_outs, lock_time=utils.i2le_padded(lock_time, 4), expiry_height=utils.i2le_padded(expiry, 4), value_balance=utils.i2le_padded[value_balance], tx_shielded_spends=(tx_shielded_spends if tx_shielded_spends is not None else []), tx_shielded_outputs=(tx_shielded_outputs if tx_shielded_outputs is not None else []), tx_joinsplits=tx_joinsplits if tx_joinsplits is not None else [], joinsplit_pubkey=joinsplit_pubkey, joinsplit_sig=joinsplit_sig, binding_sig=binding_sig) flag = riemann.network.SEGWIT_TX_FLAG \ if tx_witnesses is not None else None return tx.Tx(version=utils.i2le_padded(version, 4), flag=flag, tx_ins=tx_ins, tx_outs=tx_outs, tx_witnesses=tx_witnesses, lock_time=utils.i2le_padded(lock_time, 4))
def test_sighash_forkid_all(self): riemann.select_network('bitcoin_cash_main') t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) sighash = t.sighash_all( index=0, script=helpers.P2PKH1['ser']['ins'][0]['pk_script'], prevout_value=helpers.P2PKH1['ser']['ins'][0]['value']) self.assertEqual(sighash, helpers.SIGHASH_FORKID['all'])
def test_tx_inandout(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual(t, helpers.P2PKH1['ser']['tx']['signed']) with self.assertRaises(ValueError) as context: tx_ins = [] tx.Tx(self.version, self.none_flag, tx_ins, self.tx_outs, None, self.lock_time) self.assertIn('Too few inputs or outputs. Stop that.', str(context.exception)) with self.assertRaises(ValueError) as context: tx_ins = [1] tx.Tx(self.version, self.none_flag, tx_ins, self.tx_outs, None, self.lock_time) self.assertIn('Invalid TxIn. Expected instance of TxIn. Got int', str(context.exception)) with self.assertRaises(ValueError) as context: tx_outs = [1] tx.Tx(self.version, self.none_flag, self.tx_ins, tx_outs, None, self.lock_time) self.assertIn('Invalid TxOut. Expected instance of TxOut. Got int', str(context.exception)) with self.assertRaises(ValueError) as context: tx_in = self.tx_ins[0].copy(stack_script=b'\x00' * 1616, redeem_script=None) tx_ins = [tx_in for _ in range(255)] tx_outs = [self.tx_outs[0] for _ in range(255)] tx.Tx(self.version, self.none_flag, tx_ins, tx_outs, None, self.lock_time) self.assertIn('Tx is too large. Expect less than 100kB. Got: ', str(context.exception))
def test_calculate_fee(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual(t.calculate_fee([10**8]), 57534406)
def test_tx_id(self): t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs, self.none_witnesses, self.lock_time) self.assertEqual(t.tx_id, helpers.P2PKH1['ser']['tx']['hash']) self.assertEqual(t.tx_id_le, helpers.P2PKH1['ser']['tx']['hash_le'])