def txn_params(context, fee, fv, lv, gh, to, close, amt, gen, note): context.fee = int(fee) context.fv = int(fv) context.lv = int(lv) context.gh = gh context.to = to context.amt = int(amt) if context.fee == 0: context.params = transaction.SuggestedParams(context.fee, context.fv, context.lv, context.gh, gen, flat_fee=True) else: context.params = transaction.SuggestedParams(context.fee, context.fv, context.lv, context.gh, gen) if close == "none": context.close = None else: context.close = close if note == "none": context.note = None else: context.note = base64.b64decode(note) if gen == "none": context.gen = None else: context.gen = gen
def test_file_read_write(self): # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create transaction txn = transaction.PaymentTxn(self.account_0, sp, self.account_0, 1000) # get private key w = wallet.Wallet(wallet_name, wallet_pswd, self.kcl) private_key = w.export_key(self.account_0) # sign transaction stx = txn.sign(private_key) # write to file dir_path = os.path.dirname(os.path.realpath(__file__)) transaction.write_to_file([txn, stx], dir_path + "/raw.tx") # read from file txns = transaction.retrieve_from_file(dir_path + "/raw.tx") # check that the transactions are still the same self.assertEqual(encoding.msgpack_encode(txn), encoding.msgpack_encode(txns[0])) self.assertEqual(encoding.msgpack_encode(stx), encoding.msgpack_encode(txns[1])) # delete the file os.remove("raw.tx")
def keyreg_txn_params(context, fee, fv, lv, gh, votekey, selkey, votefst, votelst, votekd, gen, note): context.fee = int(fee) context.fv = int(fv) context.lv = int(lv) context.gh = gh context.votekey = votekey context.selkey = selkey context.votefst = int(votefst) context.votelst = int(votelst) context.votekd = int(votekd) if gen == "none": context.gen = None else: context.gen = gen context.params = transaction.SuggestedParams(context.fee, context.fv, context.lv, context.gh, context.gen) if note == "none": context.note = None else: context.note = base64.b64decode(note) if gen == "none": context.gen = None else: context.gen = gen
def app_call_txn(): local_sp = transaction.SuggestedParams(fee= 2100, first=6002000, last=6003000, gen="testnet-v1.0", gh="SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",flat_fee=True) txn = algosdk.future.transaction.ApplicationCallTxn(sender="YK54TGVZ37C7P76GKLXTY2LAH2522VD3U2434HRKE7NMXA65VHJVLFVOE4", sp=local_sp, index=68, foreign_apps=[55], foreign_assets=[31566704], accounts=["7PKXMJB2577SQ6R6IGYRAZQ27TOOOTIGTOQGJB3L5SGZFBVVI4AHMKLCEI","NWBZBIROXZQEETCDKX6IZVVBV4EY637KCIX56LE5EHIQERCTSDYGXWG6PU"], app_args=[b'\x00\x00\0x00\x00',b'\x02\x00\0x00\x00'], on_complete=transaction.OnComplete.NoOpOC.real ) return txn
def sample_txn(cls, sender, txn_type): """ Helper function for creation Transaction for dryrun """ sp = transaction.SuggestedParams(int(1000), int(1), int(100), "", flat_fee=True) if txn_type == payment_txn: txn = transaction.Transaction(sender, sp, None, None, payment_txn, None) elif txn_type == appcall_txn: txn = transaction.ApplicationCallTxn(sender, sp, 0, 0) else: raise ValueError("unsupported src object") return txn
def test_auction(self): # get the default wallet wallets = self.kcl.list_wallets() wallet_id = None for w in wallets: if w["name"] == wallet_name: wallet_id = w["id"] # get a new handle for the wallet handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd) # generate account with kmd account_1 = self.kcl.generate_key(handle, False) # get self.account_0 private key private_key_0 = self.kcl.export_key(handle, wallet_pswd, self.account_0) # create bid bid = auction.Bid(self.account_0, 10000, 260, "bid_id", account_1, "auc_id") sb = bid.sign(private_key_0) nf = auction.NoteField(sb, constants.note_field_type_bid) # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create transaction txn = transaction.PaymentTxn(self.account_0, sp, account_1, 100000, note=base64.b64decode( encoding.msgpack_encode(nf))) # sign transaction with account signed_account = txn.sign(private_key_0) # send transaction send = self.acl.send_transaction(signed_account) self.assertEqual(send, txn.get_txid()) del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1) self.assertTrue(del_1)
def test_transaction_group(self): # get the default wallet wallets = self.kcl.list_wallets() wallet_id = None for w in wallets: if w["name"] == wallet_name: wallet_id = w["id"] # get a new handle for the wallet handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd) # get private key private_key_0 = self.kcl.export_key(handle, wallet_pswd, self.account_0) # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create transaction txn = transaction.PaymentTxn(self.account_0, sp, self.account_0, 1000) # calculate group id gid = transaction.calculate_group_id([txn]) txn.group = gid # sign using kmd stxn1 = self.kcl.sign_transaction(handle, wallet_pswd, txn) # sign using transaction call stxn2 = txn.sign(private_key_0) # check that they are the same self.assertEqual(encoding.msgpack_encode(stxn1), encoding.msgpack_encode(stxn2)) try: send = self.acl.send_transactions([stxn1]) self.assertEqual(send, txn.get_txid()) except error.AlgodHTTPError as ex: self.assertIn( "TransactionPool.Remember: transaction groups not supported", str(ex))
def app_create_txn(): approve_app = b'\x02 \x05\x00\x05\x04\x02\x01&\x07\x04vote\tVoteBegin\x07VoteEnd\x05voted\x08RegBegin\x06RegEnd\x07Creator1\x18"\x12@\x00\x951\x19\x12@\x00\x871\x19$\x12@\x00y1\x19%\x12@\x00R1\x19!\x04\x12@\x00<6\x1a\x00(\x12@\x00\x01\x002\x06)d\x0f2\x06*d\x0e\x10@\x00\x01\x00"2\x08+c5\x005\x014\x00A\x00\x02"C6\x1a\x016\x1a\x01d!\x04\x08g"+6\x1a\x01f!\x04C2\x06\'\x04d\x0f2\x06\'\x05d\x0e\x10C"2\x08+c5\x005\x012\x06*d\x0e4\x00\x10A\x00\t4\x014\x01d!\x04\tg!\x04C1\x00\'\x06d\x12C1\x00\'\x06d\x12C\'\x061\x00g1\x1b$\x12@\x00\x01\x00\'\x046\x1a\x00\x17g\'\x056\x1a\x01\x17g)6\x1a\x02\x17g*6\x1a\x03\x17g!\x04C' clear_pgm = b'\x02 \x01\x01' # the approve_app is a compiled Tealscript taken from https://pyteal.readthedocs.io/en/stable/examples.html#periodic-payment # we truncated the pgm because of the memory limit on the Ledeger approve_app = approve_app[:128] local_ints = 2 local_bytes = 5 global_ints = 24 global_bytes = 1 global_schema = transaction.StateSchema(global_ints, global_bytes) local_schema = transaction.StateSchema(local_ints, local_bytes) local_sp = transaction.SuggestedParams(fee= 2100, first=6002000, last=6003000, gen="testnet-v1.0", gh="SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",flat_fee=True) txn = algosdk.future.transaction.ApplicationCreateTxn(sender="YK54TGVZ37C7P76GKLXTY2LAH2522VD3U2434HRKE7NMXA65VHJVLFVOE4", sp=local_sp, approval_program=approve_app, on_complete=transaction.OnComplete.NoOpOC.real,clear_program= clear_pgm, global_schema=global_schema, foreign_apps=[55], foreign_assets=[31566704], accounts=["7PKXMJB2577SQ6R6IGYRAZQ27TOOOTIGTOQGJB3L5SGZFBVVI4AHMKLCEI", "NWBZBIROXZQEETCDKX6IZVVBV4EY637KCIX56LE5EHIQERCTSDYGXWG6PU"], app_args=[b'\x00\x00\0x00\x00',b'\x02\x00\0x00\x00'], local_schema=local_schema ) return txn
# Example: accepting assets from algosdk import account from algosdk.future import transaction private_key, address = account.generate_account() fee_per_byte = 10 first_valid_round = 1000 last_valid_round = 2000 genesis_hash = "SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=" receiver = address # to start accepting assets, set receiver to sender amount = 0 # to start accepting assets, set amount to 0 index = 1234 # identifying index of the asset # create the asset accept transaction sp = transaction.SuggestedParams(fee_per_byte, first_valid_round, last_valid_round, genesis_hash) txn = transaction.AssetTransferTxn(address, sp, receiver, amount, index) # sign the transaction signed_txn = txn.sign(private_key)
def test_wallet(self): # initialize wallet w = wallet.Wallet(wallet_name, wallet_pswd, self.kcl) # get master derivation key mdk = w.export_master_derivation_key() # get mnemonic mn = w.get_mnemonic() # make sure mnemonic can be converted back to mdk self.assertEqual(mdk, mnemonic.to_master_derivation_key(mn)) # generate account with account and check if it's valid private_key_1, account_1 = account.generate_account() # import generated account import_key = w.import_key(private_key_1) self.assertEqual(import_key, account_1) # check that the account is in the wallet keys = w.list_keys() self.assertIn(account_1, keys) # generate account with kmd account_2 = w.generate_key() private_key_2 = w.export_key(account_2) # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create transaction txn = transaction.PaymentTxn(self.account_0, sp, account_1, 100000) # sign transaction with wallet signed_kmd = w.sign_transaction(txn) # get self.account_0 private key private_key_0 = w.export_key(self.account_0) # sign transaction with account signed_account = txn.sign(private_key_0) # check that signing both ways results in the same thing self.assertEqual(encoding.msgpack_encode(signed_account), encoding.msgpack_encode(signed_kmd)) # create multisig account and transaction msig = transaction.Multisig(1, 2, [account_1, account_2]) txn = transaction.PaymentTxn(msig.address(), sp, self.account_0, 1000) # import multisig account msig_address = w.import_multisig(msig) # check that the multisig account is listed msigs = w.list_multisig() self.assertIn(msig_address, msigs) # export multisig account exported = w.export_multisig(msig_address) self.assertEqual(len(exported.subsigs), 2) # create multisig transaction mtx = transaction.MultisigTransaction(txn, msig) # sign the multisig using kmd msig_1 = w.sign_multisig_transaction(account_1, mtx) signed_kmd = w.sign_multisig_transaction(account_2, msig_1) # sign the multisig offline mtx1 = transaction.MultisigTransaction(txn, msig) mtx1.sign(private_key_1) mtx2 = transaction.MultisigTransaction(txn, msig) mtx2.sign(private_key_2) signed_account = transaction.MultisigTransaction.merge([mtx1, mtx2]) # check that they are the same self.assertEqual(encoding.msgpack_encode(signed_account), encoding.msgpack_encode(signed_kmd)) # delete accounts del_1 = w.delete_key(account_1) del_2 = w.delete_key(account_2) del_3 = w.delete_multisig(msig_address) self.assertTrue(del_1) self.assertTrue(del_2) self.assertTrue(del_3) # test renaming the wallet w.rename(wallet_name + "1") self.assertEqual(wallet_name + "1", w.info()["wallet"]["name"]) w.rename(wallet_name) self.assertEqual(wallet_name, w.info()["wallet"]["name"]) # test releasing the handle w.release_handle() self.assertRaises(error.KMDHTTPError, self.kcl.get_wallet, w.handle) # test handle automation w.info()
def test_multisig(self): # get the default wallet wallets = self.kcl.list_wallets() wallet_id = None for w in wallets: if w["name"] == wallet_name: wallet_id = w["id"] # get a new handle for the wallet handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd) # generate two accounts with kmd account_1 = self.kcl.generate_key(handle, False) account_2 = self.kcl.generate_key(handle, False) # get their private keys private_key_1 = self.kcl.export_key(handle, wallet_pswd, account_1) private_key_2 = self.kcl.export_key(handle, wallet_pswd, account_2) # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create multisig account and transaction msig = transaction.Multisig(1, 2, [account_1, account_2]) txn = transaction.PaymentTxn(msig.address(), sp, self.account_0, 1000) # check that the multisig account is valid msig.validate() # import multisig account msig_address = self.kcl.import_multisig(handle, msig) # export multisig account exported = self.kcl.export_multisig(handle, msig_address) self.assertEqual(len(exported.subsigs), 2) # create multisig transaction mtx = transaction.MultisigTransaction(txn, msig) # sign using kmd msig_1 = self.kcl.sign_multisig_transaction(handle, wallet_pswd, account_1, mtx) signed_kmd = self.kcl.sign_multisig_transaction( handle, wallet_pswd, account_2, msig_1) # sign offline mtx1 = transaction.MultisigTransaction(txn, msig) mtx1.sign(private_key_1) mtx2 = transaction.MultisigTransaction(txn, msig) mtx2.sign(private_key_2) signed_account = transaction.MultisigTransaction.merge([mtx1, mtx2]) # check that they are the same self.assertEqual(encoding.msgpack_encode(signed_account), encoding.msgpack_encode(signed_kmd)) # delete accounts del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1) del_2 = self.kcl.delete_key(handle, wallet_pswd, account_2) del_3 = self.kcl.delete_multisig(handle, wallet_pswd, msig_address) self.assertTrue(del_1) self.assertTrue(del_2) self.assertTrue(del_3)
def test_transaction(self): # get the default wallet wallets = self.kcl.list_wallets() wallet_id = None for w in wallets: if w["name"] == wallet_name: wallet_id = w["id"] # get a new handle for the wallet handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd) # generate account and check if it's valid private_key_1, account_1 = account.generate_account() self.assertTrue(encoding.is_valid_address(account_1)) # import generated account import_key = self.kcl.import_key(handle, private_key_1) self.assertEqual(import_key, account_1) # generate account with kmd account_2 = self.kcl.generate_key(handle, False) # get suggested parameters and fee gh = self.acl.versions()['genesis_hash_b64'] rnd = int(self.acl.status()['lastRound']) sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh) # create transaction txn = transaction.PaymentTxn(self.account_0, sp, account_1, 100000) # sign transaction with kmd signed_kmd = self.kcl.sign_transaction(handle, wallet_pswd, txn) # get self.account_0 private key private_key_0 = self.kcl.export_key(handle, wallet_pswd, self.account_0) # sign transaction with account signed_account = txn.sign(private_key_0) txid = txn.get_txid() # check that signing both ways results in the same thing self.assertEqual(encoding.msgpack_encode(signed_account), encoding.msgpack_encode(signed_kmd)) # send the transaction send = self.acl.send_transaction(signed_account) self.assertEqual(send, txid) # get transaction info in pending transactions self.assertEqual(self.acl.pending_transaction_info(txid)["tx"], txid) # wait for transaction to send self.acl.status_after_block(sp.first + 2) # get transaction info two different ways info_1 = self.acl.transactions_by_address(self.account_0, sp.first - 2, sp.first + 2) info_2 = self.acl.transaction_info(self.account_0, txid) self.assertIn("transactions", info_1) self.assertIn("type", info_2) # delete accounts del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1) del_2 = self.kcl.delete_key(handle, wallet_pswd, account_2) self.assertTrue(del_1) self.assertTrue(del_2)