Load Accounts

The following example shows how to load all accounts from the keyring as pairs and convert them into JSON format.

/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-var-requires */
// Import Keyring class and utility function
const Keyring = require('@polkadot/keyring').default;
const stringToU8a = require('@polkadot/util/string/toU8a').default;

async function main () {
  // Create account seed for Alice and Bob
  const ALICE_SEED = 'Alice'.padEnd(32, ' ');
  const BOB_SEED = 'Bob'.padEnd(32, ' ');

  // Create an instance of the Keyring
  const keyring = new Keyring();

  // Create their pairs with account seeds. Add to keyring pair dictionary
  keyring.addFromSeed(stringToU8a(ALICE_SEED));
  keyring.addFromSeed(stringToU8a(BOB_SEED));

  // Note that we did not use `addFromAddress` since it does not add the
  // secret key to memory so we cannot later retrieve it with `getPairs`

  // Retrieve all keyring pairs from the keyring pair dictionary
  keyring
    .getPairs()
    .forEach((pair, index) => {
      const { address } = pair;

      console.log(`\nAccount with index #${index} has json: `, JSON.stringify(keyring.toJson(address), null, 2));
    });
}

main().catch(console.error).finally(() => process.exit());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33