plugins.aea-ledger-cosmos.aea_
ledger_
cosmos.cosmos
Cosmos module wrapping the public and private key cryptography and ledger api.
DataEncrypt Objects
class DataEncrypt()
Class to encrypt/decrypt data strings with password provided.
encrypt
| @classmethod
| encrypt(cls, data: bytes, password: str) -> bytes
Encrypt data with password.
bytes_
encode
| @staticmethod
| bytes_encode(data: bytes) -> str
Encode bytes to ascii friendly string.
bytes_
decode
| @staticmethod
| bytes_decode(data: str) -> bytes
Decode ascii friendly string to bytes.
decrypt
| @classmethod
| decrypt(cls, encrypted_data: bytes, password: str) -> bytes
Decrypt data with password provided.
CosmosHelper Objects
class CosmosHelper(Helper)
Helper class usable as Mixin for CosmosApi or as standalone class.
is_
transaction_
settled
| @staticmethod
| is_transaction_settled(tx_receipt: JSONLike) -> bool
Check whether a transaction is settled or not.
Arguments:
tx_receipt
: the receipt of the transaction.
Returns:
True if the transaction has been settled, False o/w.
get_
code_
id
| @staticmethod
| get_code_id(tx_receipt: JSONLike) -> Optional[int]
Retrieve the code_id
from a transaction receipt.
Arguments:
tx_receipt
: the receipt of the transaction.
Returns:
the code id, if present
get_
contract_
address
| @staticmethod
| get_contract_address(tx_receipt: JSONLike) -> Optional[str]
Retrieve the contract_address
from a transaction receipt.
Arguments:
tx_receipt
: the receipt of the transaction.
Returns:
the contract address, if present
is_
transaction_
valid
| @staticmethod
| is_transaction_valid(tx: JSONLike, seller: Address, client: Address, tx_nonce: str, amount: int) -> bool
Check whether a transaction is valid or not.
Arguments:
tx
: the transaction.seller
: the address of the seller.client
: the address of the client.tx_nonce
: the transaction nonce.amount
: the amount we expect to get from the transaction.
Returns:
True if the random_message is equals to tx['input']
generate_
tx_
nonce
| @staticmethod
| generate_tx_nonce(seller: Address, client: Address) -> str
Generate a unique hash to distinguish transactions with the same terms.
Arguments:
seller
: the address of the seller.client
: the address of the client.
Returns:
return the hash in hex.
get_
address_
from_
public_
key
| @classmethod
| get_address_from_public_key(cls, public_key: str) -> str
Get the address from the public key.
Arguments:
public_key
: the public key
Returns:
str
recover_
message
| @classmethod
| recover_message(cls, message: bytes, signature: str, is_deprecated_mode: bool = False) -> Tuple[Address, ...]
Recover the addresses from the hash.
Arguments:
message
: the message we expectsignature
: the transaction signatureis_deprecated_mode
: if the deprecated signing was used
Returns:
the recovered addresses
recover_
public_
keys_
from_
message
| @classmethod
| recover_public_keys_from_message(cls, message: bytes, signature: str, is_deprecated_mode: bool = False) -> Tuple[str, ...]
Get the public key used to produce the signature
of the message
Arguments:
message
: raw bytes used to produce signaturesignature
: signature of the messageis_deprecated_mode
: if the deprecated signing was used
Returns:
the recovered public keys
get_
hash
| @staticmethod
| get_hash(message: bytes) -> str
Get the hash of a message.
Arguments:
message
: the message to be hashed.
Returns:
the hash of the message.
is_
valid_
address
| @classmethod
| is_valid_address(cls, address: Address) -> bool
Check if the address is valid.
Arguments:
address
: the address to validate
load_
contract_
interface
| @classmethod
| load_contract_interface(cls, file_path: Path) -> Dict[str, str]
Load contract interface.
Arguments:
file_path
: the file path to the interface
Returns:
the interface
CosmosCrypto Objects
class CosmosCrypto(Crypto[SigningKey])
Class wrapping the Account Generation from Ethereum ledger.
__
init__
| __init__(private_key_path: Optional[str] = None, password: Optional[str] = None) -> None
Instantiate an ethereum crypto object.
Arguments:
private_key_path
: the private key path of the agentpassword
: the password to encrypt/decrypt the private key.
private_
key
| @property
| private_key() -> str
Return a private key.
Returns:
a private key string
public_
key
| @property
| public_key() -> str
Return a public key in hex format.
Returns:
a public key string in hex format
address
| @property
| address() -> str
Return the address for the key pair.
Returns:
a display_address str
load_
private_
key_
from_
path
| @classmethod
| load_private_key_from_path(cls, file_name: str, password: Optional[str] = None) -> SigningKey
Load a private key in hex format from a file.
Arguments:
file_name
: the path to the hex file.password
: the password to encrypt/decrypt the private key.
Returns:
the Entity.
sign_
message
| sign_message(message: bytes, is_deprecated_mode: bool = False) -> str
Sign a message in bytes string form.
Arguments:
message
: the message to be signedis_deprecated_mode
: if the deprecated signing is used
Returns:
signature of the message in string form
sign_
transaction
| sign_transaction(transaction: JSONLike) -> JSONLike
Sign a transaction in bytes string form.
Arguments:
transaction
: the transaction to be signed
Returns:
signed transaction
generate_
private_
key
| @classmethod
| generate_private_key(cls) -> SigningKey
Generate a key pair for cosmos network.
encrypt
| encrypt(password: str) -> str
Encrypt the private key and return in json.
Arguments:
private_key
: the raw private key.password
: the password to decrypt.
Returns:
json string containing encrypted private key.
decrypt
| @classmethod
| decrypt(cls, keyfile_json: str, password: str) -> str
Decrypt the private key and return in raw form.
Arguments:
keyfile_json
: json string containing encrypted private key.password
: the password to decrypt.
Returns:
the raw private key.
_
CosmosApi Objects
class _CosmosApi(LedgerApi)
Class to interact with the Cosmos SDK via a HTTP APIs.
__
init__
| __init__(**kwargs: Any) -> None
Initialize the Cosmos ledger APIs.
api
| @property
| api() -> Any
Get the underlying API object.
get_
balance
| get_balance(address: Address) -> Optional[int]
Get the balance of a given account.
get_
state
| get_state(callable_name: str, *args: Any, **kwargs: Any) -> Optional[JSONLike]
Call a specified function on the ledger API.
Based on the cosmos REST API specification, which takes a path (strings separated by '/'). The convention here is to define the root of the path (txs, blocks, etc.) as the callable_name and the rest of the path as args.
get_
deploy_
transaction
| get_deploy_transaction(contract_interface: Dict[str, str], deployer_address: Address, **kwargs: Any, ,) -> Optional[JSONLike]
Get the transaction to deploy the smart contract.
Dispatches to _get_storage_transaction and _get_init_transaction based on kwargs.
Arguments:
contract_interface
: the contract interface.deployer_address
: The address that will deploy the contract. :returns tx: the transaction dictionary.
get_
handle_
transaction
| get_handle_transaction(sender_address: Address, contract_address: Address, handle_msg: Any, amount: int, tx_fee: int, denom: Optional[str] = None, gas: int = 0, memo: str = "", chain_id: Optional[str] = None) -> Optional[JSONLike]
Create a CosmWasm HandleMsg transaction.
Arguments:
sender_address
: the sender address of the message initiator.contract_address
: the address of the smart contract.handle_msg
: HandleMsg in JSON format.amount
: Funds amount sent with transaction.tx_fee
: the tx fee accepted.denom
: the name of the denomination of the contract fundsgas
: Maximum amount of gas to be used on executing command.memo
: any string comment.chain_id
: the Chain ID of the CosmWasm transaction. Default is 1 (i.e. mainnet).
Returns:
the unsigned CosmWasm HandleMsg
execute_
contract_
query
| execute_contract_query(contract_address: Address, query_msg: JSONLike) -> Optional[JSONLike]
Execute a CosmWasm QueryMsg. QueryMsg doesn't require signing.
Arguments:
contract_address
: the address of the smart contract.query_msg
: QueryMsg in JSON format.
Returns:
the message receipt
get_
transfer_
transaction
| get_transfer_transaction(sender_address: Address, destination_address: Address, amount: int, tx_fee: int, tx_nonce: str, denom: Optional[str] = None, gas: int = 80000, memo: str = "", chain_id: Optional[str] = None, **kwargs: Any, ,) -> Optional[JSONLike]
Submit a transfer transaction to the ledger.
Arguments:
sender_address
: the sender address of the payer.destination_address
: the destination address of the payee.amount
: the amount of wealth to be transferred.tx_fee
: the transaction fee.tx_nonce
: verifies the authenticity of the txdenom
: the denomination of tx fee and amountgas
: the gas used.memo
: memo to include in tx.chain_id
: the chain ID of the transaction.
Returns:
the transfer transaction
send_
signed_
transaction
| send_signed_transaction(tx_signed: JSONLike) -> Optional[str]
Send a signed transaction and wait for confirmation.
Arguments:
tx_signed
: the signed transaction
Returns:
tx_digest, if present
is_
cosmwasm_
transaction
| is_cosmwasm_transaction(tx_signed: JSONLike) -> bool
Check whether it is a cosmwasm tx.
is_
transfer_
transaction
| @staticmethod
| is_transfer_transaction(tx_signed: JSONLike) -> bool
Check whether it is a transfer tx.
get_
transaction_
receipt
| get_transaction_receipt(tx_digest: str) -> Optional[JSONLike]
Get the transaction receipt for a transaction digest.
Arguments:
tx_digest
: the digest associated to the transaction.
Returns:
the tx receipt, if present
get_
transaction
| get_transaction(tx_digest: str) -> Optional[JSONLike]
Get the transaction for a transaction digest.
Arguments:
tx_digest
: the digest associated to the transaction.
Returns:
the tx, if present
get_
contract_
instance
| get_contract_instance(contract_interface: Dict[str, str], contract_address: Optional[str] = None) -> Any
Get the instance of a contract.
Arguments:
contract_interface
: the contract interface.contract_address
: the contract address.
Returns:
the contract instance
get_
last_
code_
id
| get_last_code_id() -> int
Get ID of latest deployed .wasm bytecode.
Returns:
code id of last deployed .wasm bytecode
get_
last_
contract_
address
| get_last_contract_address(code_id: int) -> str
Get contract address of latest initialised contract by its ID.
Arguments:
code_id
: id of deployed CosmWasm bytecode
Returns:
contract address of last initialised contract
update_
with_
gas_
estimate
| update_with_gas_estimate(transaction: JSONLike) -> JSONLike
Attempts to update the transaction with a gas estimate
Arguments:
transaction
: the transaction
Returns:
the updated transaction
CosmosApi Objects
class CosmosApi(_CosmosApi, CosmosHelper)
Class to interact with the Cosmos SDK via a HTTP APIs.
CosmosFaucetApi Objects
class CosmosFaucetApi(FaucetApi)
Cosmos testnet faucet API.
__
init__
| __init__(poll_interval: Optional[float] = None)
Initialize CosmosFaucetApi.
get_
wealth
| get_wealth(address: Address, url: Optional[str] = None) -> None
Get wealth from the faucet for the provided address.
Arguments:
address
: the address.url
: the url
Returns:
None :raises: RuntimeError of explicit faucet failures