Skip to content

创建文章

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Account

Account management

Account management is one of the core capabilities provided by AlgoKit Utils. It allows you to create mnemonic, rekeyed, multisig, transaction signer, idempotent KMD and environment variable injected accounts that can be used to sign transactions as well as representing a sender address at the same time. This significantly simplifies passing around sender/signer within and into AlgoKit Utils.

SendTransactionFrom

Any AlgoKit Utils function that needs to sign/send a transaction will take a SendTransactionFrom object, which represents an account that combined a sender and signer and is a type union between the following types:

  • Account - An in-built algosdk Account object
  • SigningAccount - An abstraction around algosdk.Account that supports rekeyed accounts
  • LogicSigAccount - An in-built algosdk algosdk.LogicSigAccount object
  • MultisigAccount - An abstraction around algosdk.MultisigMetadata, algosdk.makeMultiSigAccountTransactionSigner, algosdk.multisigAddress, algosdk.signMultisigTransaction and algosdk.appendSignMultisigTransaction that supports multisig accounts with one or more signers present
  • TransactionSignerAccount - An interface that provides a sender address alongside a transaction signer (e.g. for use with AtomicTransactionComposer or useWallet)

The use of in-built algosdk types like Account, LogicSigAccount and TransactionSigner is aligned to the Modularity principle. Allowing you to co-exist non AlgoKit Utils code with AlgoKit Utils functions.

Using SendTransactionFrom

AlgoKit Utils provides a few helper methods to take one of these SendTransactionFrom objects:

Note

this is memoized so multiple calls to this for the same account will safely return the same TransactionSigner instance; this works nicely with AtomicTransactionComposer

Accounts

In order to get the accounts you can use the underlying algosdk methods where relevant, or you can use the following AlgoKit Utils functions (all of which return a type compatible with SendTransactionFrom):

  • algokit.mnemonicAccountFromEnvironment(account, algod, kmd?) - Returns an Algorand account with private key loaded by convention based on the given name identifier - either by idempotently creating the account in KMD or from environment variable via process.env['{NAME}_MNEMONIC'] and (optionally) process.env['{NAME}_SENDER'] (if account is rekeyed)
  • This allows you to have powerful code that will automatically create and fund an account by name locally and when deployed against TestNet/MainNet will automatically resolve from environment variables, without having to have different code

Note

account can either be a string name, or an object with {name: string, fundWith?: AlgoAmount}, where fundWith allows you to control how many ALGOs are seeded into an account created in KMD

Dispenser

  • algokit.getDispenserAccount(algod, kmd?) - Returns an account that can act as a dispenser to fund other accounts either via Kmd (when targeting LocalNet) or by convention from environment variable via process.env.DISPENSER_MNEMONIC (and optionally process.env.DISPENSER_SENDER if rekeyed)

Rekey account

One of the unique features of Algorand is the ability to change the private key that can authorise transactions for an account. This is called rekeying.

You can issue a transaction to rekey an account by using the algokit.rekeyAccount(rekey, algod) function. The rekey parameter is an AlgoRekeyParams object with the following properties:

  • All properties in SendTransactionParams
  • from: SendTransactionFrom - The account that will be rekeyed
  • rekeyTo: SendTransactionFrom | string - The address of the account that will be used to authorise transactions for the rekeyed account going forward
  • transactionParams?: SuggestedParams - The optional transaction parameters
  • note?: TransactionNote - The transaction note
  • lease?: string | Uint8Array: A lease to assign to the transaction to enforce a mutually exclusive transaction (useful to prevent double-posting and other scenarios)
// Example
await algokit.rekeyAccount(
  {
    from: account,
    rekeyTo: newAccount,
    // Optionally specify transactionParams, note, lease and transaction sending parameters
  },
  algod,
)

const rekeyedAccount = algokit.rekeyedAccount(newAccount, account.addr)
// rekeyedAccount can be used to sign transactions on behalf of account...