ain.wallet module

class ain.wallet.Wallet(ain: Ain, chainId: int)

Bases: object

Class for the AIN Blockchain wallet.

accountToV3Keystore(address: str, password: str, options: ~ain.utils.v3keystore.V3KeystoreOptions = <ain.utils.v3keystore.V3KeystoreOptions object>) V3Keystore

Converts an account into a v3 Keystore and encrypts it with a password.

Parameters:
  • address – The AIN blockchain address.

  • password – The password of the v3 keystore.

  • options (V3KeystoreOptions) – The options for the v3 keystore. Defaults to no options.

Returns:

The v3 Keystore of the account.

Return type:

V3Keystore

add(privateKey: str | bytes) str

Adds a new account to the wallet, from the given private key.

Parameters:

privateKey (Union[str, bytes]) – The private key of the new account.

Returns:

The address of the new account.

Return type:

str

addAndSetDefaultAccount(privateKey: str | bytes) str

Adds a new account to the wallet, from the given private key. Then, sets the new account as the default account.

Parameters:

privateKey (Union[str, bytes]) – The private key of the new account.

Returns:

The address of the new account.

Return type:

str

addFromHDWallet(mnemonic: str, index: int = 0) str

Adds an account to the wallet, from a seed phrase. Only the account at the given index will be added.

Parameters:
  • mnemonic (str) – The mnemonic of account.

  • index (int) – The index of account. Defaults to 0.

Returns:

The address of the new account.

Return type:

str

addFromV3Keystore(v3Keystore: V3Keystore | str, password: str) str

Adds an account to the wallet, from the v3 keystore.

Parameters:
  • mnemonic (Union[V3Keystore, str]) – The v3 keystore of account.

  • index (int) – The password of your v3 keystore.

Returns:

The address of the new account.

Return type:

str

clear()

Clears the wallet, remove all account information.

create(numberOfAccounts: int)

Creates numberOfAccounts new accounts, and adds them to the wallet.

Parameters:

numberOfAccounts (int) – The number of account that you want to create. If this is not positive, raises ValueError.

async getBalance(address: str = None) int

Gets an AIN balance of the given account.

Parameters:

address (str, Optional) – The address of the account. If address is None, returns the balance of the default account address. Defaults to None. If address is None and default account is not set, raises ValueError.

Returns:

The AIN balance of the given account.

Return type:

int

getHashStrFromSig(signature: str) str

Gets a hash from the signature.

Parameters:

signature (str) – The signature.

Returns:

The hex prefixed hash from the signature.

getImpliedAddress(address: str = None) str

Gets an implied address.

Parameters:

address (str, Optional) – The address of the account. If address is not given, returns the default account address. Defaults to None. If address is not given and default account is not set, raises ValueError. If such address is not added to the wallet, raises ValueError.

Returns:

The implied address.

Return type:

str

async getNonce(address: str = None, source: str = None) int

Fetches an account’s nonce value, which is the current transaction count of the account.

Parameters:
  • address (str, Optional) – The address of the account. If address is None, it uses the default account address. Defaults to None. If address is None and default account is not set, raises ValueError.

  • source (str, Optional) – The source of the data. It could be either the pending transaction pool (“pending”) or the committed blocks (“committed”). The default value is “committed”.

Returns:

The nonce of the given account.

Return type:

int

getPublicKey(address: str) str

Gets a full public key of the given address.

Parameters:

address (str) – The address of the account.

Returns:

The full public key of the given address.

If such address is not in the wallet, returns empty string.

Return type:

str

async getTimestamp(address: str = None, source: str = None) int

Fetches an account’s timestamp value, which is the timestamp of the last transaction signed by the account with nonce = -2.

Parameters:
  • address (str, Optional) – The address of the account. If address is None, it uses the default account address. Defaults to None. If address is None and default account is not set, raises ValueError.

  • source (str, Optional) – The source of the data. It could be either the pending transaction pool (“pending”) or the committed blocks (“committed”). The default value is “committed”.

Returns:

The timestamp of the given account.

Return type:

int

isAdded(address: str) bool

Returns whether the address has already been added to the wallet.

Parameters:

address (str) – The address of the account.

Returns:

True, if the address has already been added to the wallet,

False, if not.

Return type:

bool

recover(signature: str) str

Recovers an address of the account that was used to create the signature.

Parameters:

signature (str) – The signature.

Returns:

The address of the account that was used to create the signature.

remove(address: str)

Removes the account from the wallet. If such account doesn’t exist, raises ValueError.

Parameters:

address (str) – The address of the account.

removeDefaultAccount()

Removes a default account from the wallet, and sets it to None.

setDefaultAccount(address: str)

Sets the default AIN Blockchain account address as address. If such address is not added to the wallet, raises ValueError.

Parameters:

address (str) – The address of the account.

sign(data: str, address: str = None) str

Signs a string data with the private key of the given address.

Parameters:
  • data (str) – The string data.

  • address (str, Optional) – The AIN blockchain address that wants to use for signing. Defaults to None, use the default account of the current wallet.

Returns:

The signature of the data.

Return type:

str

signTransaction(tx: TransactionBody, address: str = None) str

Signs a transaction data with the private key of the given address.

Parameters:
  • tx (TransactionBody) – The transaction data.

  • address (str, Optional) – The AIN blockchain address that wants to use for signing. Defaults to None, use the default account of the current wallet.

Returns:

The signature of the transaction.

Return type:

str

toV3Keystore(password: str, options: ~ain.utils.v3keystore.V3KeystoreOptions = <ain.utils.v3keystore.V3KeystoreOptions object>) List[V3Keystore]

Saves the accounts in the wallet as v3 Keystores, locking them with the password.

Parameters:
  • password (str) – The password of the v3 keystores.

  • options (V3KeystoreOptions) – The options for the v3 keystores. Defaults to no options.

Returns:

The list of the v3 Keystores.

Return type:

List[V3Keystore]

async transfer(toAddress: str, value: float, fromAddress: str = None, nonce: int = None, gas_price: int = None, isDryrun=False)

Sends a transfer transaction to the network.

Parameters:
  • toAddress (str) – The AIN blockchain address that wants to transfer AIN to.

  • value (float) – The amount of the transferring AIN.

  • fromAddress (str, Optional) – The AIN blockchain address that wants to transfer AIN from. Defaults to None, transfer from the default account of the current wallet.

  • nonce (int, Optional) – The nonce of the transfer transaction. Defaults to None.

  • gas_price (int, Optional) – The gas price of the transfer transaction. Defaults to None.

  • isDryrun (bool) – Dryrun option.

Returns:

The transaction result.

verifySignature(data: Any, signature: str, address: str) bool

Verifies the given signature.

Parameters:
  • data – The data that has made the signature.

  • signature (str) – The signature of the given data.

  • address (str) – The address of who created the signature.

Returns:

True, if the signature is valid.

False, if not.

Return type:

bool

accounts: Dict[str, Account]

The set of the accounts. Keys are the addresses of the accounts.

ain: Ain

The Ain instance.

chainId: int

The chain ID of the provider.

defaultAccount: Account | None

The default account of the wallet.

property length: int