aea.crypto.ethereum

Ethereum module wrapping the public and private key cryptography and ledger api.

EthereumCrypto

class EthereumCrypto(Crypto[Account])

Class wrapping the Account Generation from Ethereum ledger.

__init__

 | __init__(private_key_path: Optional[str] = None)

Instantiate an ethereum crypto object.

Arguments:

  • private_key_path: the private key path of the agent

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) -> Account

Load a private key in hex format from a file.

Arguments:

  • file_name: the path to the hex file.

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 signed
  • is_deprecated_mode: if the deprecated signing is used

Returns:

signature of the message in string form

sign_transaction

 | sign_transaction(transaction: Any) -> Any

Sign a transaction in bytes string form.

Arguments:

  • transaction: the transaction to be signed

Returns:

signed transaction

recover_message

 | recover_message(message: bytes, signature: str, is_deprecated_mode: bool = False) -> Tuple[Address, ...]

Recover the addresses from the hash.

Arguments:

  • message: the message we expect
  • signature: the transaction signature
  • is_deprecated_mode: if the deprecated signing was used

Returns:

the recovered addresses

generate_private_key

 | @classmethod
 | generate_private_key(cls) -> Account

Generate a key pair for ethereum network.

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

dump

 | dump(fp: BinaryIO) -> None

Serialize crypto object as binary stream to fp (a .write()-supporting file-like object).

Arguments:

  • fp: the output file pointer. Must be set in binary mode (mode='wb')

Returns:

None

EthereumApi

class EthereumApi(LedgerApi)

Class to interact with the Ethereum Web3 APIs.

__init__

 | __init__(address: str, gas_price: str = DEFAULT_GAS_PRICE)

Initialize the Ethereum ledger APIs.

Arguments:

  • address: the endpoint for Web3 APIs.

api

 | @property
 | api() -> Web3

Get the underlying API object.

get_balance

 | get_balance(address: Address) -> Optional[int]

Get the balance of a given account.

transfer

 | transfer(crypto: Crypto, destination_address: Address, amount: int, tx_fee: int, tx_nonce: str, chain_id: int = 1, **kwargs, ,) -> Optional[str]

Submit a transfer transaction to the ledger.

Arguments:

  • crypto: the crypto object associated to 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 tx
  • chain_id: the Chain ID of the Ethereum transaction. Default is 1 (i.e. mainnet).

Returns:

tx digest if present, otherwise None

send_signed_transaction

 | send_signed_transaction(tx_signed: Any) -> Optional[str]

Send a signed transaction and wait for confirmation.

Arguments:

  • tx_signed: the signed transaction

Returns:

tx_digest, if present

is_transaction_settled

 | is_transaction_settled(tx_digest: str) -> bool

Check whether a transaction is settled or not.

Arguments:

  • tx_digest: the digest associated to the transaction.

Returns:

True if the transaction has been settled, False o/w.

get_transaction_receipt

 | get_transaction_receipt(tx_digest: str) -> Optional[Any]

Get the transaction receipt for a transaction digest (non-blocking).

Arguments:

  • tx_digest: the digest associated to the transaction.

Returns:

the tx receipt, if present

generate_tx_nonce

 | generate_tx_nonce(seller: Address, client: Address) -> str

Generate a unique hash to distinguish txs with the same terms.

Arguments:

  • seller: the address of the seller.
  • client: the address of the client.

Returns:

return the hash in hex.

is_transaction_valid

 | is_transaction_valid(tx_digest: str, seller: Address, client: Address, tx_nonce: str, amount: int) -> bool

Check whether a transaction is valid or not (non-blocking).

Arguments:

  • tx_digest: the transaction digest.
  • 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']

EthereumFaucetApi

class EthereumFaucetApi(FaucetApi)

Ethereum testnet faucet API.

get_wealth

 | get_wealth(address: Address) -> None

Get wealth from the faucet for the provided address.

Arguments:

  • address: the address.

Returns:

None