<s> import io <EOL> import unittest <EOL> from pycoin . block import Block <EOL> from pycoin import ecdsa <EOL> from pycoin . encoding import public_pair_to_sec , public_pair_to_bitcoin_address , wif_to_secret_exponent <EOL> from pycoin . serialize import h2b <EOL> from pycoin . tx import Tx , SIGHASH_ALL <EOL> from pycoin . tx . TxIn import TxIn <EOL> from pycoin . tx . TxOut import TxOut , standard_tx_out_script <EOL> from pycoin . tx . exceptions import SolvingError <EOL> from pycoin . tx . pay_to import build_hash160_lookup <EOL> block_80971_cs = h2b ( '<STR_LIT>' ) <EOL> block_80971_data = h2b ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' ) ; <EOL> block_80971 = Block . parse ( io . BytesIO ( block_80971_data ) ) <EOL> COINBASE_PUB_KEY_FROM_80971 = h2b ( "<STR_LIT>" "<STR_LIT>" ) <EOL> COINBASE_BYTES_FROM_80971 = h2b ( "<STR_LIT>" ) <EOL> def standard_tx ( coins_from , coins_to ) : <EOL> txs_in = [ ] <EOL> unspents = [ ] <EOL> for h , idx , tx_out in coins_from : <EOL> txs_in . append ( TxIn ( h , idx ) ) <EOL> unspents . append ( tx_out ) <EOL> txs_out = [ ] <EOL> for coin_value , bitcoin_address in coins_to : <EOL> txs_out . append ( TxOut ( coin_value , standard_tx_out_script ( bitcoin_address ) ) ) <EOL> version , lock_time = <NUM_LIT:1> , <NUM_LIT:0> <EOL> tx = Tx ( version , txs_in , txs_out , lock_time ) <EOL> tx . set_unspents ( unspents ) <EOL> return tx <EOL> class BuildTxTest ( unittest . TestCase ) : <EOL> def test_signature_hash ( self ) : <EOL> compressed = False <EOL> exponent_2 = int ( "<STR_LIT>" , <NUM_LIT:16> ) <EOL> bitcoin_address_2 = public_pair_to_bitcoin_address ( <EOL> ecdsa . public_pair_for_secret_exponent ( ecdsa . generator_secp256k1 , exponent_2 ) , <EOL> compressed = compressed ) <EOL> exponent = wif_to_secret_exponent ( "<STR_LIT>" ) <EOL> public_key_sec = public_pair_to_sec ( ecdsa . public_pair_for_secret_exponent ( ecdsa . generator_secp256k1 , exponent ) , compressed = compressed ) <EOL> the_coinbase_tx = Tx . coinbase_tx ( public_key_sec , int ( <NUM_LIT:50> * <NUM_LIT> ) , COINBASE_BYTES_FROM_80971 ) <EOL> coins_from = [ ( the_coinbase_tx . hash ( ) , <NUM_LIT:0> , the_coinbase_tx . txs_out [ <NUM_LIT:0> ] ) ] <EOL> coins_to = [ ( int ( <NUM_LIT:50> * <NUM_LIT> ) , bitcoin_address_2 ) ] <EOL> unsigned_coinbase_spend_tx = standard_tx ( coins_from , coins_to ) <EOL> tx_out_script_to_check = the_coinbase_tx . txs_out [ <NUM_LIT:0> ] . script <EOL> idx = <NUM_LIT:0> <EOL> actual_hash = unsigned_coinbase_spend_tx . signature_hash ( tx_out_script_to_check , idx , hash_type = SIGHASH_ALL ) <EOL> self . assertEqual ( actual_hash , <NUM_LIT> ) <EOL> def test_standard_tx_out ( self ) : <EOL> coin_value = <NUM_LIT:10> <EOL> recipient_bc_address = '<STR_LIT>' <EOL> tx_out = standard_tx ( [ ] , [ ( coin_value , recipient_bc_address ) ] ) . txs_out [ <NUM_LIT:0> ] <EOL> s = str ( tx_out ) <EOL> self . assertEqual ( '<STR_LIT>' , s ) <EOL> def test_coinbase_tx ( self ) : <EOL> coinbase_bytes = h2b ( "<STR_LIT>" ) <EOL> tx = Tx . coinbase_tx ( COINBASE_PUB_KEY_FROM_80971 , int ( <NUM_LIT:50> * <NUM_LIT> ) , COINBASE_BYTES_FROM_80971 ) <EOL> s = io . BytesIO ( ) <EOL> tx . stream ( s ) <EOL> tx1 = s . getvalue ( ) <EOL> s = io . BytesIO ( ) <EOL> block_80971 . txs [ <NUM_LIT:0> ] . stream ( s ) <EOL> tx2 = s . getvalue ( ) <EOL> self . assertEqual ( tx1 , tx2 ) <EOL> def test_tx_out_bitcoin_address ( self ) : <EOL> coinbase_bytes = h2b ( "<STR_LIT>" ) <EOL> tx = Tx . coinbase_tx ( COINBASE_PUB_KEY_FROM_80971 , int ( <NUM_LIT:50> * <NUM_LIT> ) , COINBASE_BYTES_FROM_80971 ) <EOL> self . assertEqual ( tx . txs_out [ <NUM_LIT:0> ] . bitcoin_address ( ) , '<STR_LIT>' ) <EOL> def test_build_spends ( self ) : <EOL> TX_DB = { } <EOL> exponent = wif_to_secret_exponent ( "<STR_LIT>" ) <EOL> compressed = False <EOL> public_key_sec = public_pair_to_sec ( ecdsa . public_pair_for_secret_exponent ( ecdsa . generator_secp256k1 , exponent ) , compressed = compressed ) <EOL> the_coinbase_tx = Tx . coinbase_tx ( public_key_sec , int ( <NUM_LIT:50> * <NUM_LIT> ) , COINBASE_BYTES_FROM_80971 ) <EOL> TX_DB [ the_coinbase_tx . hash ( ) ] = the_coinbase_tx <EOL> compressed = False <EOL> exponent_2 = int ( "<STR_LIT>" , <NUM_LIT:16> ) <EOL> bitcoin_address_2 = public_pair_to_bitcoin_address ( <EOL> ecdsa . public_pair_for_secret_exponent ( ecdsa . generator_secp256k1 , exponent_2 ) , <EOL> compressed = compressed ) <EOL> self . assertEqual ( "<STR_LIT>" , bitcoin_address_2 ) <EOL> coins_from = [ ( the_coinbase_tx . hash ( ) , <NUM_LIT:0> , the_coinbase_tx . txs_out [ <NUM_LIT:0> ] ) ] <EOL> coins_to = [ ( int ( <NUM_LIT:50> * <NUM_LIT> ) , bitcoin_address_2 ) ] <EOL> unsigned_coinbase_spend_tx = standard_tx ( coins_from , coins_to ) <EOL> solver = build_hash160_lookup ( [ exponent ] ) <EOL> coinbase_spend_tx = unsigned_coinbase_spend_tx . sign ( solver ) <EOL> self . assertEqual ( coinbase_spend_tx . bad_signature_count ( ) , <NUM_LIT:0> ) <EOL> TX_DB [ coinbase_spend_tx . hash ( ) ] = coinbase_spend_tx <EOL> compressed = True <EOL> exponent_3 = int ( "<STR_LIT>" , <NUM_LIT:16> ) <EOL> bitcoin_address_3 = public_pair_to_bitcoin_address ( <EOL> ecdsa . public_pair_for_secret_exponent ( ecdsa . generator_secp256k1 , exponent_3 ) , <EOL> compressed = compressed ) <EOL> self . assertEqual ( "<STR_LIT>" , bitcoin_address_3 ) <EOL> coins_from = [ ( coinbase_spend_tx . hash ( ) , <NUM_LIT:0> , coinbase_spend_tx . txs_out [ <NUM_LIT:0> ] ) ] <EOL> unsigned_spend_tx = standard_tx ( coins_from , [ ( int ( <NUM_LIT:50> * <NUM_LIT> ) , bitcoin_address_3 ) ] ) <EOL> solver . update ( build_hash160_lookup ( [ exponent_2 ] ) ) <EOL> spend_tx = unsigned_spend_tx . sign ( solver ) <EOL> self . assertEqual ( spend_tx . bad_signature_count ( ) , <NUM_LIT:0> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
