packages/extension-app/src/background/index.ts
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import {
PORT_CONTENT,
PORT_EXTENSION,
} from '@polkadot/extension-base/defaults';
import { AccountsStore } from '@polkadot/extension-base/stores';
import chrome from '@polkadot/extension-inject/chrome';
import keyring from '@polkadot/ui-keyring';
import { assert } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { handlers } from './handlers';
// listen to all messages and handle appropriately
chrome.runtime.onConnect.addListener((port): void => {
// shouldn't happen, however... only listen to what we know about
assert(
[PORT_CONTENT, PORT_EXTENSION].includes(port.name),
`Unknown connection from ${port.name}`
);
// message and disconnect handlers
port.onMessage.addListener((data) => handlers(data, port));
port.onDisconnect.addListener(() =>
console.log(`Disconnected from ${port.name}`)
);
});
// initial setup
cryptoWaitReady()
.then(() => {
console.log('crypto initialized');
// load all the keyring data
keyring.loadAll({ store: new AccountsStore(), type: 'sr25519' });
console.log('initialization completed');
})
.catch((error) => {
console.error('initialization failed', error);
});