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 algosdkAccount
objectSigningAccount
- An abstraction aroundalgosdk.Account
that supports rekeyed accountsLogicSigAccount
- An in-built algosdkalgosdk.LogicSigAccount
objectMultisigAccount
- An abstraction aroundalgosdk.MultisigMetadata
,algosdk.makeMultiSigAccountTransactionSigner
,algosdk.multisigAddress
,algosdk.signMultisigTransaction
andalgosdk.appendSignMultisigTransaction
that supports multisig accounts with one or more signers presentTransactionSignerAccount
- An interface that provides a sender address alongside a transaction signer (e.g. for use withAtomicTransactionComposer
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:
algokit.getSenderAddress
- Returns the public address of the sender the account representsalgokit.getSenderTransactionSigner
- Returns aTransactionSigner
to represent the signer of the account
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
algokit.signTransaction
- Signs a singlealgosdk.Transaction
object with the given account
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 viaprocess.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
algokit.mnemonicAccount(mnemonicSecret)
- Returns an Algorand account (algosdk.Account
) with secret key loaded (i.e. that can sign transactions) by taking the mnemonic secret.algokit.multisigAccount(multisigParams, signingAccounts)
- Returns a multisig account with one or more signing keys loaded.algokit.rekeyedAccount(signer, sender)
- Returns aSigningAccount
representing the given rekeyed sender/signer combinationalgokit.transactionSignerAccount(signer, sender)
- Returns aTransactionSigner
along with its sender addressalgokit.randomAccount()
- Returns a new, cryptographically randomly generated account with private key loaded.
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 viaprocess.env.DISPENSER_MNEMONIC
(and optionallyprocess.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 rekeyedrekeyTo: SendTransactionFrom | string
- The address of the account that will be used to authorise transactions for the rekeyed account going forwardtransactionParams?: SuggestedParams
- The optional transaction parametersnote?: TransactionNote
- The transaction notelease?: 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...