def test_format_default(): """Test if default CosmosSDK transaction is correctly formated.""" account = CosmosCrypto() cc2 = CosmosCrypto() cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG) amount = 10000 transfer_transaction = cosmos_api.get_transfer_transaction( sender_address=account.address, destination_address=cc2.address, amount=amount, tx_fee=1000, tx_nonce="something", ) signed_transaction = cc2.sign_transaction(transfer_transaction) assert "tx" in signed_transaction assert "signatures" in signed_transaction["tx"] assert len(signed_transaction["tx"]["signatures"]) == 1 assert "pub_key" in signed_transaction["tx"]["signatures"][0] assert "value" in signed_transaction["tx"]["signatures"][0]["pub_key"] base64_pbk = signed_transaction["tx"]["signatures"][0]["pub_key"]["value"] assert "signature" in signed_transaction["tx"]["signatures"][0] signature = signed_transaction["tx"]["signatures"][0]["signature"] default_formated_transaction = cc2.format_default_transaction( transfer_transaction, signature, base64_pbk) # Compare default formatted transaction with signed transaction assert signed_transaction == default_formated_transaction
def test_format_cosmwasm(): """Test if CosmWasm transaction is correctly formated.""" cc2 = CosmosCrypto() # Dummy CosmWasm transaction wasm_transaction = { "account_number": "8", "chain_id": "agent-land", "fee": { "amount": [], "gas": "200000" }, "memo": "", "msgs": [{ "type": "wasm/execute", "value": { "sender": "cosmos14xjnl2mwwfz6pztpwzj6s89npxr0e3lhxl52nv", "contract": "cosmos1xzlgeyuuyqje79ma6vllregprkmgwgav5zshcm", "msg": { "create_single": { "item_owner": "cosmos1fz0dcvvqv5at6dl39804jy92lnndf3d5saalx6", "id": "1234", "path": "SOME_URI", } }, "sent_funds": [], }, }], "sequence": "25", } signed_transaction = cc2.sign_transaction(wasm_transaction) assert "value" in signed_transaction assert "signatures" in signed_transaction["value"] assert len(signed_transaction["value"]["signatures"]) == 1 assert "pub_key" in signed_transaction["value"]["signatures"][0] assert "value" in signed_transaction["value"]["signatures"][0]["pub_key"] base64_pbk = signed_transaction["value"]["signatures"][0]["pub_key"][ "value"] assert "signature" in signed_transaction["value"]["signatures"][0] signature = signed_transaction["value"]["signatures"][0]["signature"] wasm_formated_transaction = cc2.format_wasm_transaction( wasm_transaction, signature, base64_pbk) # Compare Wasm formatted transaction with signed transaction assert signed_transaction == wasm_formated_transaction
def test_construct_sign_and_submit_transfer_transaction(): """Test the construction, signing and submitting of a transfer transaction.""" account = CosmosCrypto() balance = get_wealth(account.address) assert balance > 0, "Failed to fund account." cc2 = CosmosCrypto() cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG) amount = 10000 assert amount < balance, "Not enough funds." transfer_transaction = cosmos_api.get_transfer_transaction( sender_address=account.address, destination_address=cc2.address, amount=amount, tx_fee=1000, tx_nonce="something", ) assert (isinstance(transfer_transaction, dict) and len(transfer_transaction) == 6), "Incorrect transfer_transaction constructed." signed_transaction = account.sign_transaction(transfer_transaction) assert (isinstance(signed_transaction, dict) and len(signed_transaction["tx"]) == 4 and isinstance(signed_transaction["tx"]["signatures"], list)), "Incorrect signed_transaction constructed." transaction_digest = cosmos_api.send_signed_transaction(signed_transaction) assert transaction_digest is not None, "Failed to submit transfer transaction!" not_settled = True elapsed_time = 0 while not_settled and elapsed_time < 20: elapsed_time += 1 time.sleep(2) transaction_receipt = cosmos_api.get_transaction_receipt( transaction_digest) if transaction_receipt is None: continue is_settled = cosmos_api.is_transaction_settled(transaction_receipt) not_settled = not is_settled assert transaction_receipt is not None, "Failed to retrieve transaction receipt." assert is_settled, "Failed to verify tx!" tx = cosmos_api.get_transaction(transaction_digest) is_valid = cosmos_api.is_transaction_valid(tx, cc2.address, account.address, "", amount) assert is_valid, "Failed to settle tx correctly!" assert tx == transaction_receipt, "Should be same!"