Account Management

How to manage accounts addresses

Overview

Flow accounts are not derived from a private key. This creates an issues for testing, since you need to create actors in a specific order to use their addresses properly.

To reduce this friction, getAccountAddress, allows you to access a specific address using an alias. This way you can think about actual actors -- for example Alice and Bob -- without needing to know their Flow addresses.

It also helps you to write tests in a sequential or non-sequential way. Calling this method for the first time will create a new account and return the address. Calling it a second time with the same alias again will return the Flow address for that account, without creating new account.

Private Key Management

Universal private key

By default, accounts created and consumed by the Flow JS Testing library will use a universal private key for signing transactions. Generally, this alleviates the burden of any low-level key management and streamlines the process of testing cadence code.

Custom private keys

However, under some circumstances the user may wish to create accounts (see: createAccount) or sign for accounts (see: sendTransaction) using custom private keys (i.e. private key value, hashing algorithm, signing algorithm, etc.) - this functionality is facilitated by the aforementioned methods.

getAccountAddress

Resolves name alias to a Flow address (0x prefixed) under the following conditions:

  • If an account with a specific name has not been previously accessed, the framework will create a new one and then store it under the provided alias.
  • Next time when you call this method, it will grab exactly the same account. This allows you to create several accounts up-front and then use them throughout your code, without worrying that accounts match or trying to store and manage specific addresses.

Arguments

NameTypeDescription
aliasstringThe alias to reference or create.

Returns

TypeDescription
Address0x prefixed address of aliased account

Usage

1
import {getAccountAddress} from "@onflow/flow-js-testing"
2
3
const main = async () => {
4
const Alice = await getAccountAddress("Alice")
5
console.log({Alice})
6
}
7
8
main()

createAccount({name, keys})

In some cases, you may wish to manually create an account with a particular set of private keys

Options

Pass in the following as a single object with the following keys.

KeyTypeRequiredDescription
namestringNohuman-readable name to be associated with created account (will be used for address lookup within getAccountAddress)
keys[KeyObject or PublicKey]NoAn array of KeyObjects or PublicKeys to be added to the account upon creation (defaults to the universal private key)

📣 if name field not provided, the account address will not be cached and you will be unable to look it up using getAccountAddress.

Returns

TypeDescription
Address0x prefixed address of created account