To understand the initialization process, some background on the underlying libraries are required. For the Schnorrkel sr25519 a WASM library is used. This means that before
sr25519 can be used, the WASM libraries needs to be loaded and initialized. This is not an issue when the keyring only uses
If you have been through the base keyring documentation, you should be familiar with this process.
With this in mind, we take care of the initialization before we load our keyring, yielding -
cryptoWaitReady() promise resolves as soon as the underlying WASM libraries have been made available. This is followed by
keyring.loadAll(...) which is the main initialization for the keyring, which
- initializes the underlying
- loads all available accounts, addresses band contracts previously stored
- sets up the default address format with
42(This is the substrate development default. When not specified, this defaults to
- defaults to
sr25519accounts (We can still add
ed25519accounts, more on this later. When not specified this defaults to
Since you would generally want the keyring available before rendering the UI (although it can be done elsewhere, showing some info while loading), the following pattern is used in our examples, both React and Vue shown here -
When using the keyring together with the Polkadot-js API, the above would be adapted since the API itself already has a check for the WASM availability. In this case, we can adapt the interface to follow the following pattern -
The above pattern is how initialization is done in some applications such as the polkadot-js apps UI.
In addition to the
type options (both optional), the following additional configuration options are available to the
loadAll(...) call -
filter?: (json: KeyringJson) => boolean- An optional filter that is executed on account loading. This allows us to check the loaded account and apply rules on it before including it in the keyring. Generally this would be used for advanced cases, any account that is filtered with
genesisHash?: Hash- The
genesisHashof accounts to should be loaded. The meta property of the account may contain a
genesisHash, when both are available and non-matching, the account will not be included. To populate this option, the easiest way is to pass the
isDevelopment?: boolean- This flag, when set to
trueloads the keyring in development mode. This means that the default development accounts such as "Alice", "Bob", ..., "Ferdie" are included in the keyring. Generally you would only specify this when actually connected to a development chain.
store?: KeyringStore- An optional store to be used. By default the
ui-keyringwill user browser localStorage, but with a specific store this behavior can be adjusted. For instance, for an extension we would default to the extension store (although localStorage is available) and for Electron apps, we may want to default to file storage.
With the keyring installed and ready for use, next we will dive into adding and managing accounts.