WalletManager
This guide provides step-by-step instructions on how to use WalletManager
.
WalletManager
The WalletManager
constructor accepts an optional object to define its storage. The storage describes how and where the WalletManager
will store its vaults of wallets. If storage is not provided, the WalletManager
uses a default one that does not persist data.
For now, let's keep it simple and not worry about the storage. Later we will discuss it in more detail.
To instantiate a WalletManager
you can simply:
const walletManager = new WalletManager();
WalletManager
Password By default, a WalletManager
instance is locked when created. Before using it, you need to unlock it by setting a password. You can do this by calling the unlock
method.
const password = 'my-password';
await walletManager.unlock(password);
Once your WalletManager
is unlocked, it can manage your wallets.
A vault in WalletManager
serves as a secure container for wallets. The WalletManager
manages wallets by interacting with these vaults, supporting operations such as getAccounts
, which returns public information about all wallets stored in the vault, and exportAccount
, which exports a private key for a given wallet address.
To add a vault, we utilize the addVault
method. Here's how we can create a private key vault and add a private key from a wallet we own:
const myWallet = Wallet.generate({
provider,
});
const privateKey = myWallet.privateKey;
await walletManager.addVault({
type: 'privateKey',
secret: privateKey,
title: 'My first private key vault',
provider,
});
The addVault
method requires an object with three properties: type
, secret
, and title
. The WalletManager
currently supports two types of vaults: privateKeyVault
and mnemonicVault
. For the secret
, we use our wallet's private key, and for the title
, we can provide a custom name.
By running this code, WalletManager
creates a new vault instance of the type privateKey
and adds one account (our wallet) to this newly created vault.
A key feature of the WalletManager
is its ability to manage multiple vaults, even of the same type. This implies that if you run the addVault
method again, with the same parameters, WalletManager
will create another vault of the type privateKey
, holding the same wallet. Here's an example:
await walletManager.addVault({
type: 'privateKey',
secret: privateKey,
title: 'My second private key vault',
provider,
});
After executing this, you will find that your WalletManager
is managing two privateKey
vaults, both storing the same wallet.
Remember, both title
and secret
are optional when adding vaults, but providing a title
makes it easier to manage your vaults and wallets. If you add a vault without providing a secret
, this will result in one new account (wallet) being generated by the vault it self.
WalletManager
With your WalletManager
set up, you can now access your vaults and wallets. Here's how to retrieve the details of your vaults:
const vaults = walletManager.getVaults();
console.log(vaults);
This will output something like this:
[
{
title: 'My first private key vault',
type: 'privateKey',
vaultId: 0
},
{
title: 'My second private key vault',
type: 'privateKey',
vaultId: 1
}
]
As you can see, the WalletManager assigns unique vaultIds
for each vault. The first vault you added has a vaultId
of 0
, and the second one has a vaultId
of 1
.
Let's retrieve your wallet instance with the getWallet
method:
const retrievedWallet = walletManager.getWallet(myWallet.address);
This guide walked through the steps to instantiate a WalletManager, set up its first vault, and retrieve vault information. The following sections will explore more functionalities of WalletManager, and go deeper into the usage of its vaults and the details of its storage system.