ghost-keychain/packages/extension/src/background/handlers/index.ts
Jaco Greeff f241e7191d
Introduce the enable() interface (prompts user) (#20)
* Introduce the enable() interface

* Provider

* Add auth requests to state

* injector comments

* Background extension list, spprove & reject

* Cleanups

* Untested, but the UI parts are there (test next)

* linting

* Not pretty, but seems to auth

* Small styling adjustments

* Adjust Box layout slightly

* Small styling adjustments

* signing -> transactions

* isDanger for links

* Styling defaults
2019-05-23 12:43:55 +02:00

39 lines
1.2 KiB
TypeScript

// Copyright 2019 @polkadot/extension-bg 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 { MessageRequest } from '../types';
import Extension from './Extension';
import State from './State';
import Tabs from './Tabs';
const state = new State();
const extension = new Extension(state);
const tabs = new Tabs(state);
export default function handler ({ id, message, request }: MessageRequest, { tab }: chrome.runtime.MessageSender, sendResponse: (response?: any) => void): boolean {
const source = `${tab ? tab.url : 'extension'}: ${id}: ${message}`;
console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`);
const promise = tab
? tabs.handle(message, request, tab.url)
: extension.handle(message, request);
promise
.then((response) => {
console.log(`[out] ${source}`); // :: ${JSON.stringify(response)}`);
sendResponse({ id, response });
})
.catch((error) => {
console.log(`[err] ${source}:: ${error.message}`);
sendResponse({ id, error: error.message });
});
// return true to indicate we are sending the respionse async
return true;
}