def test_genesis_transaction(self): raw = unhexlify( '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04' 'ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e20' '6272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01' '000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4c' 'ef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000') tx = ledger_class.transaction_class(raw) self.assertEqual(tx.version, 1) self.assertEqual(tx.locktime, 0) self.assertEqual(len(tx.inputs), 1) self.assertEqual(len(tx.outputs), 1) coinbase = tx.inputs[0] self.assertEqual(coinbase.output_txhash, NULL_HASH) self.assertEqual(coinbase.output_index, 0xFFFFFFFF) self.assertEqual(coinbase.sequence, 4294967295) self.assertTrue(coinbase.is_coinbase) self.assertEqual(coinbase.script, None) self.assertEqual( coinbase.coinbase[8:], b'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks' ) out = tx.outputs[0] self.assertEqual(out.amount, 5000000000) self.assertEqual(out.index, 0) self.assertTrue(out.script.is_pay_pubkey) self.assertFalse(out.script.is_pay_pubkey_hash) self.assertFalse(out.script.is_pay_script_hash) tx._reset() self.assertEqual(tx.raw, raw)
async def create_tx_from_nothing(self, my_account, height): to_address = await my_account.receiving.get_or_create_usable_address() to_hash = ledger_class.address_to_hash160(to_address) tx = ledger_class.transaction_class(height=height, is_verified=True) \ .add_inputs([self.txi(self.txo(1, sha256(str(height).encode())))]) \ .add_outputs([self.txo(1, to_hash)]) await self.ledger.db.insert_transaction(tx) await self.ledger.db.save_transaction_io(tx, to_address, to_hash, '') return tx
def test_paying_from_my_account_to_my_account(self): tx = ledger_class.transaction_class() \ .add_inputs([get_input(300*CENT)]) \ .add_outputs([get_output(190*CENT, NULL_HASH), get_output(100*CENT, NULL_HASH)]) tx.inputs[0].txo_ref.txo.is_my_account = True tx.outputs[0].is_my_account = True tx.outputs[1].is_my_account = True self.assertEqual(tx.net_account_balance, -10 * CENT) # lost to fee
async def create_tx_to_nowhere(self, txo, height): from_hash = txo.script.values['pubkey_hash'] from_address = self.ledger.hash160_to_address(from_hash) to_hash = NULL_HASH tx = ledger_class.transaction_class(height=height, is_verified=True) \ .add_inputs([self.txi(txo)]) \ .add_outputs([self.txo(1, to_hash)]) await self.ledger.db.save_transaction_io('insert', tx, from_address, from_hash, '') return tx
async def create_tx_from_txo(self, txo, to_account, height): from_hash = txo.script.values['pubkey_hash'] from_address = self.ledger.hash160_to_address(from_hash) to_address = await to_account.receiving.get_or_create_usable_address() to_hash = ledger_class.address_to_hash160(to_address) tx = ledger_class.transaction_class(height=height, is_verified=True) \ .add_inputs([self.txi(txo)]) \ .add_outputs([self.txo(1, to_hash)]) await self.ledger.db.insert_transaction(tx) await self.ledger.db.save_transaction_io(tx, from_address, from_hash, '') await self.ledger.db.save_transaction_io(tx, to_address, to_hash, '') return tx
def create_utxos(self, amounts): utxos = [self.txo(amount) for amount in amounts] self.funding_tx = ledger_class.transaction_class() \ .add_inputs([self.txi(self.txo(sum(amounts)+0.1))]) \ .add_outputs(utxos) save_tx = 'insert' for utxo in utxos: yield self.ledger.db.save_transaction_io( save_tx, self.funding_tx, 1, True, self.ledger.hash160_to_address( utxo.script.values['pubkey_hash']), utxo.script.values['pubkey_hash'], '') save_tx = 'update' defer.returnValue(utxos)
async def create_utxos(self, amounts): utxos = [self.txo(amount) for amount in amounts] self.funding_tx = ledger_class.transaction_class(is_verified=True) \ .add_inputs([self.txi(self.txo(sum(amounts)+0.1))]) \ .add_outputs(utxos) await self.ledger.db.insert_transaction(self.funding_tx) for utxo in utxos: await self.ledger.db.save_transaction_io( self.funding_tx, self.ledger.hash160_to_address( utxo.script.values['pubkey_hash']), utxo.script.values['pubkey_hash'], '') return utxos
def test_coinbase_transaction(self): raw = unhexlify( '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4e03' '1f5a070473319e592f4254432e434f4d2f4e59412ffabe6d6dcceb2a9d0444c51cabc4ee97a1a000036ca0' 'cb48d25b94b78c8367d8b868454b0100000000000000c0309b21000008c5f8f80000ffffffff0291920b5d' '0000000017a914e083685a1097ce1ea9e91987ab9e94eae33d8a13870000000000000000266a24aa21a9ed' 'e6c99265a6b9e1d36c962fda0516b35709c49dc3b8176fa7e5d5f1f6197884b400000000' ) tx = ledger_class.transaction_class(raw) self.assertEqual(tx.version, 1) self.assertEqual(tx.locktime, 0) self.assertEqual(len(tx.inputs), 1) self.assertEqual(len(tx.outputs), 2) coinbase = tx.inputs[0] self.assertEqual(coinbase.output_txhash, NULL_HASH) self.assertEqual(coinbase.output_index, 0xFFFFFFFF) self.assertEqual(coinbase.sequence, 4294967295) self.assertTrue(coinbase.is_coinbase) self.assertEqual(coinbase.script, None) self.assertEqual(coinbase.coinbase[9:22], b'/BTC.COM/NYA/') out = tx.outputs[0] self.assertEqual(out.amount, 1561039505) self.assertEqual(out.index, 0) self.assertFalse(out.script.is_pay_pubkey) self.assertFalse(out.script.is_pay_pubkey_hash) self.assertTrue(out.script.is_pay_script_hash) self.assertFalse(out.script.is_return_data) out1 = tx.outputs[1] self.assertEqual(out1.amount, 0) self.assertEqual(out1.index, 1) self.assertEqual( hexlify(out1.script.values['data']), b'aa21a9ede6c99265a6b9e1d36c962fda0516b35709c49dc3b8176fa7e5d5f1f6197884b4' ) self.assertTrue(out1.script.is_return_data) self.assertFalse(out1.script.is_pay_pubkey) self.assertFalse(out1.script.is_pay_pubkey_hash) self.assertFalse(out1.script.is_pay_script_hash) tx._reset() self.assertEqual(tx.raw, raw)
def test_sign(self): account = self.ledger.account_class.from_seed( self.ledger, u"carbon smart garage balance margin twelve chest sword toast envelope bottom stomach ab" u"sent", u"torba") yield account.ensure_address_gap() address1 = (yield account.receiving.get_addresses())[0] address2 = (yield account.receiving.get_addresses())[0] pubkey_hash1 = self.ledger.address_to_hash160(address1) pubkey_hash2 = self.ledger.address_to_hash160(address2) tx = ledger_class.transaction_class() \ .add_inputs([ledger_class.transaction_class.input_class.spend(get_output(2*COIN, pubkey_hash1))]) \ .add_outputs([ledger_class.transaction_class.output_class.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)]) \ yield tx.sign([account]) self.assertEqual( hexlify(tx.inputs[0].script.values['signature']), b'3044022064cd6b95c9e0084253c10dd56bcec2bfd816c29aad05fbea490511d79540462b02201aa9d6f73' b'48bb0c76b28d1ad87cf4ffd51cf4de0b299af8bf0ecad70e3369ef201')
def get_transaction(txo=None): return ledger_class.transaction_class() \ .add_inputs([get_input()]) \ .add_outputs([txo or ledger_class.transaction_class.output_class.pay_pubkey_hash(CENT, NULL_HASH)])
def get_output(amount=CENT, pubkey_hash=NULL_HASH): return ledger_class.transaction_class() \ .add_outputs([ledger_class.transaction_class.output_class.pay_pubkey_hash(amount, pubkey_hash)]) \ .outputs[0]