def test_standard_spend(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 # wallet a send to wallet b pubkey_puz_string = "(0x%s)" % hexlify( wallet_b.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_a.generator_lookups[wallet_b.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) amount = 5000 spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert len(wallet_b.my_utxos) == 1 # wallet b sends back to wallet a pubkey_puz_string = "(0x%s)" % hexlify( wallet_a.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_b.generator_lookups[wallet_a.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) amount = 5000 spend_bundle = wallet_b.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0
def test_spend_failure(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] amount = 5000 # wallet a send to wallet b pubkey_puz_string = "(0x%s)" % hexlify( wallet_b.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_a.generator_lookups[wallet_b.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None commit_and_notify(remote, wallets, wallet_a) amount = 50000000000000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 999995000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 5000 amount = 6000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 4000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 1000 commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000 assert wallet_a.temp_balance == 1000
def test_invalid_payee(): # Only Wallet C is approved - now lets try to spend to Wallet D remote = make_client_server() run = asyncio.get_event_loop().run_until_complete # A gives B some money, but B can only send that money to C (and generate change for itself) wallet_a = Wallet() wallet_b = APWallet() wallet_c = Wallet() wallet_d = Wallet() wallets = [wallet_a, wallet_b, wallet_c, wallet_d] a_pubkey = wallet_a.get_next_public_key().serialize() b_pubkey = wallet_b.get_next_public_key().serialize() APpuzzlehash = ap_wallet_a_functions.ap_get_new_puzzlehash( a_pubkey, b_pubkey) wallet_b.set_sender_values(APpuzzlehash, a_pubkey) wallet_b.set_approved_change_signature( ap_wallet_a_functions.ap_sign_output_newpuzzlehash( APpuzzlehash, wallet_a, a_pubkey)) commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 # Wallet A locks up the puzzle with information regarding B's pubkey amount = 5000 spend_bundle = wallet_a.generate_signed_transaction(amount, APpuzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, wallet_d) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert len(wallet_b.my_utxos) == 1 approved_puzhashes = [wallet_c.get_new_puzzlehash()] signatures = [ ap_wallet_a_functions.ap_sign_output_newpuzzlehash( approved_puzhashes[0], wallet_a, a_pubkey) ] ap_output = [(wallet_d.get_new_puzzlehash(), 2000)] spend_bundle = wallet_b.ap_generate_signed_transaction( ap_output, signatures) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert wallet_b.temp_coin.amount == 5000 assert len(wallet_b.my_utxos) == 1
def test_future_utxos(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 amount = 5000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.current_balance == 1000000000 assert wallet_a.temp_balance == 999985000 commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999985000 assert wallet_b.current_balance == 15000 assert len(wallet_b.my_utxos) == 3 assert wallet_b.my_utxos.copy().pop().amount == 5000 # test wallet spend multiple coins in one spendbundle puzzlehash = wallet_a.get_new_puzzlehash() spend_bundle = wallet_b.generate_signed_transaction(15000, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0
def test_spend_failure(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] amount = 5000 # wallet a send to wallet b program = wallet_b.get_new_puzzle() puzzlehash = ProgramHash(program) spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None commit_and_notify(remote, wallets, wallet_a) amount = 50000000000000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 999995000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 5000 amount = 6000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 4000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 1000 commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000 assert wallet_a.temp_balance == 1000
def test_standard_spend(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 # wallet a send to wallet b program = wallet_b.get_new_puzzle() puzzlehash = ProgramHash(program) amount = 5000 spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert len(wallet_b.my_utxos) == 1 # wallet b sends back to wallet a puzzlehash = wallet_a.get_new_puzzlehash() amount = 5000 spend_bundle = wallet_b.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0
def test_AP_spend(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete # A gives B some money, but B can only send that money to C (and generate change for itself) wallet_a = Wallet() wallet_b = APWallet() wallet_c = Wallet() wallet_d = Wallet() wallets = [wallet_a, wallet_b, wallet_c, wallet_d] a_pubkey = wallet_a.get_next_public_key().serialize() b_pubkey = wallet_b.get_next_public_key().serialize() APpuzzlehash = ap_wallet_a_functions.ap_get_new_puzzlehash( a_pubkey, b_pubkey) wallet_b.set_sender_values(APpuzzlehash, a_pubkey) wallet_b.set_approved_change_signature( ap_wallet_a_functions.ap_sign_output_newpuzzlehash( APpuzzlehash, wallet_a, a_pubkey)) commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 # Wallet A locks up the puzzle with information regarding B's pubkey amount = 5000 spend_bundle = wallet_a.generate_signed_transaction(amount, APpuzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, wallet_d) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert len(wallet_b.my_utxos) == 1 # Wallet A sends more money into Wallet B using the aggregation coin aggregation_puzzlehash = ap_wallet_a_functions.ap_get_aggregation_puzzlehash( APpuzzlehash) # amount = 80 spend_bundle = wallet_a.generate_signed_transaction( 5000, aggregation_puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) spend_bundle = wallet_d.generate_signed_transaction( 3000, aggregation_puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999990000 assert wallet_b.temp_coin.amount == 13000 assert wallet_c.current_balance == 0 assert wallet_d.current_balance == 999997000 assert len(wallet_b.my_utxos) == 1 commit_and_notify(remote, wallets, Wallet()) assert wallet_b.current_balance == 13000 approved_puzhashes = [wallet_c.get_new_puzzlehash()] signatures = [ ap_wallet_a_functions.ap_sign_output_newpuzzlehash( approved_puzhashes[0], wallet_a, a_pubkey) ] ap_output = [(approved_puzhashes[0], 4000)] spend_bundle = wallet_b.ap_generate_signed_transaction( ap_output, signatures) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999990000 assert wallet_b.temp_coin.amount == 9000 assert wallet_c.current_balance == 4000 assert len(wallet_c.my_utxos) == 1 assert wallet_d.current_balance == 999997000 assert len(wallet_b.my_utxos) == 1 # Spend from wallet_c freely puzzlehash = wallet_a.get_new_puzzlehash() spend_bundle = wallet_c.generate_signed_transaction(2000, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_c.current_balance == 2000 assert len(wallet_c.my_utxos) == 1 assert wallet_a.current_balance == 999992000 # Test spend more money than AP wallet's amount ap_output = [(approved_puzhashes[0], 10000)] spend_bundle = wallet_b.ap_generate_signed_transaction( ap_output, signatures) assert spend_bundle is None ap_output = [(approved_puzhashes[0], 6000)] spend_bundle = wallet_b.ap_generate_signed_transaction( ap_output, signatures) _ = run(remote.push_tx(tx=spend_bundle)) # Try and over spend temp amount, but legal actual amount assert wallet_b.temp_coin.amount == 3000 assert wallet_b.current_balance == 9000 ap_output = [(approved_puzhashes[0], 6000)] spend_bundle = wallet_b.ap_generate_signed_transaction( ap_output, signatures) assert spend_bundle is None assert wallet_b.temp_coin.amount == 3000 assert wallet_b.current_balance == 9000