def test_assert_time_exceeds(self): run = asyncio.get_event_loop().run_until_complete remote = make_client_server() puzzle_program = p2_delegated_conditions.puzzle_for_pk( public_key_bytes_for_index(8)) puzzle_hash = ProgramHash(puzzle_program) coin_1 = farm_spendable_coin(remote, puzzle_hash) now = run(remote.skip_milliseconds(ms=uint64(0).to_bytes(4, 'big'))) assert (type(now) == int) min_time = now + 1000 conditions_time_exceeds = [ make_assert_time_exceeds_condition(min_time) ] # try to spend coin_1 with limit set to age 1. Should fail solution_1 = p2_delegated_conditions.solution_for_conditions( puzzle_program, conditions_time_exceeds) spend_bundle_1 = build_spend_bundle(coin_1, solution_1) r = run(remote.push_tx(tx=spend_bundle_1)) assert r.args[0].startswith( "exception: (<Err.ASSERT_TIME_EXCEEDS_FAILED") # wait a second, should succeed r = run(remote.skip_milliseconds(ms=uint64(1001))) r = run(remote.push_tx(tx=spend_bundle_1)) assert r["response"].startswith("accepted")
async def new_chain_view(self, chain_view, spend_bundles, coinbase_puzzle_hash, fees_puzzle_hash, storage, unspent_db): block_number = chain_view.tip_index + 1 REWARD = int(1e9) timestamp = uint64(self._now / 1000) pool_public_key = get_pool_public_key() plot_public_key = get_plot_public_key() pos = ProofOfSpace(pool_public_key, plot_public_key) coinbase_coin, coinbase_signature = create_coinbase_coin_and_signature( block_number, coinbase_puzzle_hash, REWARD, pool_public_key) spend_bundle = SpendBundle.aggregate(spend_bundles) header, body = farm_new_block(chain_view.tip_hash, chain_view.tip_signature, block_number, pos, spend_bundle, coinbase_coin, coinbase_signature, fees_puzzle_hash, timestamp) header_signature = sign_header(header, plot_public_key) [await storage.add_preimage(bytes(_)) for _ in (header, body)] chain_view = await chain_view.augment_chain_view( header, header_signature, storage, unspent_db, REWARD, self._now) return chain_view
async def move_custody(wallet, ledger_api): print("Moving custody") print(f"Amount being moved: {wallet.cp_balance}") amount = wallet.cp_balance unlock_time = wallet.unlock_time pubkey_permission = wallet.pubkey_permission current_time = await ledger_api.skip_milliseconds( ms=uint64(0).to_bytes(4, 'big')) if unlock_time > current_time: new_pub = input("Enter pubkey of the new custodian wallet: ") print("Permission needed before moving funds: ") puzzle_hash = ProgramHash( wallet.cp_puzzle(new_pub, pubkey_permission, unlock_time)) approval = input("\nAdd authorization: ") approval = bytes.fromhex(approval) spend_bundle = wallet.cp_generate_signed_transaction_with_approval( puzzle_hash, amount, approval) _ = await ledger_api.push_tx(tx=spend_bundle) else: print("Permission not needed for moving funds") pubkey = input("Enter receiver pubkey: ") pub_bytes = bytes.fromhex(pubkey) reg_puzzle = wallet.puzzle_for_pk(pub_bytes) puzzle_hash = ProgramHash(reg_puzzle) spend_bundle = wallet.cp_generate_signed_transaction( puzzle_hash, amount) _ = await ledger_api.push_tx(tx=spend_bundle)
async def print_my_details(wallet, ledger_api): print() print(divider) print(" \u2447 Wallet Details \u2447") print() print("Name: " + wallet.name) pk = hexlify(wallet.get_next_public_key().serialize()).decode("ascii") print(f"New pubkey: {pk}") current_time = await ledger_api.skip_milliseconds( ms=uint64(1000).to_bytes(4, 'big')) print(f"Current time in milliseconds is: {current_time}") print(divider)
def test_cp_with_permission(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = CPWallet() wallet_b = CPWallet() wallet_c = CPWallet() wallets = [wallet_a, wallet_b, wallet_c] pub_a = hexbytes(wallet_a.get_next_public_key()) pub_b = hexbytes(wallet_b.get_next_public_key()) unlock_time = 5 wallet_b.pubkey_permission = pub_a wallet_b.unlock_time = unlock_time wallet_a.pubkey_approval = pub_a b_puzzle = wallet_b.cp_puzzle(pub_b, pub_a, unlock_time) b_puzzlehash = ProgramHash(b_puzzle) commit_and_notify(remote, wallets, wallet_a) # Set time to 4, time needs to exceed 5, unless there is approval from authorizer _ = run(remote.skip_milliseconds(ms=uint64(4).to_bytes(4, 'big'))) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0 assert wallet_c.current_balance == 0 spend_bundle = wallet_a.generate_signed_transaction(1000, b_puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999999000 assert wallet_b.cp_balance == 1000 assert wallet_c.current_balance == 0 puzzlehash_c = wallet_c.get_new_puzzlehash() amount = 100 mode = 2 transaction = wallet_b.cp_generate_unsigned_transaction( puzzlehash_c, amount, mode) solution = transaction[0][1].solution approval_a = wallet_a.cp_approval_signature_for_transaction(solution).sig spend_bundle = wallet_b.cp_generate_signed_transaction_with_approval( puzzlehash_c, amount, approval_a) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999999000 assert wallet_b.cp_balance == 900 assert wallet_c.current_balance == 100
def test_cp_send_solo(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = CPWallet() wallet_b = CPWallet() wallet_c = CPWallet() wallets = [wallet_a, wallet_b, wallet_c] pub_a = hexbytes(wallet_a.get_next_public_key()) pub_b = hexbytes(wallet_b.get_next_public_key()) wallet_b.pubkey_permission = pub_a wallet_b.unlock_time = 3 b_puzzle = wallet_b.cp_puzzle(pub_b, pub_a, 3) b_puzzlehash = ProgramHash(b_puzzle) # Set ledger api to _ = run(remote.skip_milliseconds(ms=uint64(4).to_bytes(4, 'big'))) commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0 assert wallet_c.current_balance == 0 spend_bundle = wallet_a.generate_signed_transaction(1000, b_puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999999000 assert wallet_b.cp_balance == 1000 assert wallet_c.current_balance == 0 puzzlehash_c = wallet_c.get_new_puzzlehash() spend_bundle = wallet_b.cp_generate_signed_transaction(puzzlehash_c, 100) _ = run(remote.push_tx(tx=spend_bundle)) commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999999000 assert wallet_b.cp_balance == 900 assert wallet_c.current_balance == 100
def test_recovery_with_wallclock_time(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete now = run(remote.skip_milliseconds(ms=uint64(0).to_bytes(4, 'big'))) duration = 1000 min_time = now + duration wallet_a = RecoverableWallet(Decimal('1.1'), min_time, DurationType.WALLCLOCK_TIME) wallet_b = RecoverableWallet(Decimal('1.1'), min_time, DurationType.WALLCLOCK_TIME) farmer = RecoverableWallet(Decimal('1.1'), min_time, DurationType.WALLCLOCK_TIME) wallets = [wallet_a, wallet_b, farmer] commit_and_notify(remote, wallets, wallet_a) commit_and_notify(remote, wallets, wallet_b) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 1000000000 recovery_string = wallet_a.get_backup_string() recovery_dict = recovery_string_to_dict(recovery_string) root_public_key_serialized = bytes(recovery_dict['root_public_key']) recovery_pubkey = bytes(recovery_dict['root_public_key'].public_child(0)) for coin in wallet_a.my_utxos.copy(): pubkey = wallet_b.find_pubkey_for_hash(coin.puzzle_hash, root_public_key_serialized, recovery_dict['stake_factor'], recovery_dict['escrow_duration'], recovery_dict['duration_type']) signed_transaction, destination_puzzlehash, amount = \ wallet_b.generate_signed_recovery_to_escrow_transaction(coin, recovery_pubkey, pubkey, recovery_dict['stake_factor'], recovery_dict['escrow_duration'], recovery_dict['duration_type']) child = Coin(coin.name(), destination_puzzlehash, amount) r = run(remote.push_tx(tx=signed_transaction)) assert(type(r) is not RemoteError) wallet_b.escrow_coins[recovery_string].add(child) commit_and_notify(remote, wallets, farmer) assert wallet_a.current_balance == 0 assert wallet_b.current_balance == 900000000 # check recovery too soon for recovery_string, coin_set in wallet_b.escrow_coins.items(): recovery_dict = recovery_string_to_dict(recovery_string) root_public_key = recovery_dict['root_public_key'] secret_key = recovery_dict['secret_key'] escrow_duration = recovery_dict['escrow_duration'] duration_type = recovery_dict['duration_type'] signed_transaction = wallet_b.generate_recovery_transaction(coin_set, root_public_key, secret_key, escrow_duration, duration_type) r = run(remote.push_tx(tx=signed_transaction)) assert type(r) is RemoteError # pass time run(remote.skip_milliseconds(ms=uint64(duration + 1).to_bytes(4, 'big'))) for recovery_string, coin_set in wallet_b.escrow_coins.items(): recovery_dict = recovery_string_to_dict(recovery_string) root_public_key = recovery_dict['root_public_key'] secret_key = recovery_dict['secret_key'] escrow_duration = recovery_dict['escrow_duration'] duration_type = recovery_dict['duration_type'] signed_transaction = wallet_b.generate_recovery_transaction(coin_set, root_public_key, secret_key, escrow_duration, duration_type) r = run(remote.push_tx(tx=signed_transaction)) assert type(r) is not RemoteError commit_and_notify(remote, wallets, farmer) assert wallet_a.current_balance == 0 assert wallet_b.current_balance == 2000000000