Initialization
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 ed25519
.
If you have been through the base keyring documentation, you should be familiar with this process.
#
CreateWith this in mind, we take care of the initialization before we load our keyring, yielding -
The 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
@polkadot/keyring
- 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 to42
) - defaults to
sr25519
accounts (We can still added25519
accounts, more on this later. When not specified this defaults tosr25519
)
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 -
#
Using with the APIWhen 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.
#
Additional create optionsIn addition to the ss58Format
and 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 withfalse
won't appear.genesisHash?: Hash
- ThegenesisHash
of accounts to should be loaded. The meta property of the account may contain agenesisHash
, when both are available and non-matching, the account will not be included. To populate this option, the easiest way is to pass theapi.genesisHash
property.isDevelopment?: boolean
- This flag, when set totrue
loads 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 theui-keyring
will 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.
#
Managing accountsWith the keyring installed and ready for use, next we will dive into adding and managing accounts.