Dai.js supports the use of multiple accounts (i.e. private keys) with a single Maker instance. Accounts can be specified in the options for Maker.create or with the addAccount method.
Call useAccount to switch to using an account by its name, or useAccountWithAddress to switch to using an account by its address, and subsequent calls will use that account as the transaction signer.
When the Maker instance is first created, it will use the account named default if it exists, or the first account in the list otherwise.
constmaker=awaitMaker.create({ url:'http://localhost:2000', accounts: { other: {type: privateKey, key: someOtherKey}, default: {type: privateKey, key: myKey} }});awaitmaker.addAccount('yetAnother', {type: privateKey, key: thirdKey});constcdp1=awaitmaker.openCdp(); // owned by "default"maker.useAccount('other');constcdp2=awaitmaker.openCdp(); // owned by "other"maker.useAccount('yetAnother');constcdp3=awaitmaker.openCdp(); // owned by "yetAnother"awaitmaker.addAccount({type: privateKey, key:fourthAccount.key}); // the name argument is optionalmaker.useAccountWithAddress(fourthAccount.address);constcdp4=awaitmaker.openCdp(); //owned by the fourth account
You can check the current account with currentAccount and currentAddress:
In addition to the privateKey account type, there are two other built-in types:
provider: Get the first account from the provider (e.g. the value from getAccounts).
browser: Get the first account from the provider in the browser (e.g. MetaMask), even if the Maker instance is configured to use a different provider.
constmaker=awaitMaker.create({ url:'http://localhost:2000', accounts: {// this will be the first account from the provider at// localhost:2000 first: {type:'provider'},// this will be the current account in MetaMask, and it// will send its transactions through MetaMask second: {type:'browser'},// this account will send its transactions through the// provider at localhost:2000 third: {type:'privateKey', key: myPrivateKey} }})
Hardware wallets
Plugins can add additional account types. There are currently two such plugins for hardware wallet support: