Remove extensionizer indirection (#309)

* Remove extensionizer indirection

* Slight duplication

* Chrome mock

* Update package.json

* chrome types, chrome import
This commit is contained in:
Jaco Greeff 2020-04-03 13:06:25 +02:00 committed by GitHub
parent 5c58be78db
commit 75f9109cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 66 additions and 44 deletions

View File

@ -29,4 +29,7 @@ const messagingFake = new MessagingFake();
chrome.runtime.connect.returns(messagingFake); chrome.runtime.connect.returns(messagingFake);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).chrome = chrome;
export default chrome; export default chrome;

View File

@ -12,9 +12,10 @@ module.exports = Object.assign({}, config, {
}, },
modulePathIgnorePatterns: [ modulePathIgnorePatterns: [
'<rootDir>/packages/extension/build', '<rootDir>/packages/extension/build',
'<rootDir>/packages/extension-ui/build', '<rootDir>/packages/extension-base/build',
'<rootDir>/packages/extension-chains/build', '<rootDir>/packages/extension-chains/build',
'<rootDir>/packages/extension-dapp/build',
'<rootDir>/packages/extension-inject/build', '<rootDir>/packages/extension-inject/build',
'<rootDir>/packages/extension-dapp/build' '<rootDir>/packages/extension-ui/build'
] ]
}); });

View File

@ -8,7 +8,6 @@
"@babel/runtime": "^7.9.2", "@babel/runtime": "^7.9.2",
"@polkadot/api": "^1.9.1", "@polkadot/api": "^1.9.1",
"@polkadot/keyring": "^2.7.1", "@polkadot/keyring": "^2.7.1",
"@polkadot/ui-keyring": "^0.52.0-beta.22", "@polkadot/ui-keyring": "^0.52.0-beta.22"
"extensionizer": "^1.0.1"
} }
} }

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../../__mocks__/chrome';
import keyring from '@polkadot/ui-keyring'; import keyring from '@polkadot/ui-keyring';
import ExtensionStore from '@polkadot/ui-keyring/stores/Extension'; import ExtensionStore from '@polkadot/ui-keyring/stores/Extension';
import { cryptoWaitReady } from '@polkadot/util-crypto'; import { cryptoWaitReady } from '@polkadot/util-crypto';

View File

@ -6,7 +6,6 @@ import { MetadataDef } from '@polkadot/extension-inject/types';
import { SubjectInfo } from '@polkadot/ui-keyring/observable/types'; import { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import { AccountJson, AuthorizeRequest, MessageTypes, MetadataRequest, RequestAccountCreateExternal, RequestAccountCreateSuri, RequestAccountEdit, RequestAccountExport, RequestAccountValidate, RequestAuthorizeApprove, RequestAuthorizeReject, RequestDeriveCreate, ResponseDeriveValidate, RequestMetadataApprove, RequestMetadataReject, RequestSigningApprovePassword, RequestSigningApproveSignature, RequestSigningCancel, RequestSeedCreate, RequestTypes, ResponseAccountExport, RequestAccountForget, ResponseSeedCreate, RequestSeedValidate, RequestDeriveValidate, ResponseSeedValidate, ResponseType, SigningRequest } from '../types'; import { AccountJson, AuthorizeRequest, MessageTypes, MetadataRequest, RequestAccountCreateExternal, RequestAccountCreateSuri, RequestAccountEdit, RequestAccountExport, RequestAccountValidate, RequestAuthorizeApprove, RequestAuthorizeReject, RequestDeriveCreate, ResponseDeriveValidate, RequestMetadataApprove, RequestMetadataReject, RequestSigningApprovePassword, RequestSigningApproveSignature, RequestSigningCancel, RequestSeedCreate, RequestTypes, ResponseAccountExport, RequestAccountForget, ResponseSeedCreate, RequestSeedValidate, RequestDeriveValidate, ResponseSeedValidate, ResponseType, SigningRequest } from '../types';
import extension from 'extensionizer';
import keyring from '@polkadot/ui-keyring'; import keyring from '@polkadot/ui-keyring';
import accountsObservable from '@polkadot/ui-keyring/observable/accounts'; import accountsObservable from '@polkadot/ui-keyring/observable/accounts';
import { TypeRegistry } from '@polkadot/types'; import { TypeRegistry } from '@polkadot/types';
@ -14,6 +13,7 @@ import { KeyringPair, KeyringPair$Meta } from '@polkadot/keyring/types';
import { assert, isHex } from '@polkadot/util'; import { assert, isHex } from '@polkadot/util';
import { keyExtractSuri, mnemonicGenerate, mnemonicValidate } from '@polkadot/util-crypto'; import { keyExtractSuri, mnemonicGenerate, mnemonicValidate } from '@polkadot/util-crypto';
import chrome from '../../chrome';
import State from './State'; import State from './State';
import { createSubscription, unsubscribe } from './subscriptions'; import { createSubscription, unsubscribe } from './subscriptions';
@ -270,8 +270,8 @@ export default class Extension {
} }
private windowOpen (): boolean { private windowOpen (): boolean {
extension.tabs.create({ chrome.tabs.create({
url: extension.extension.getURL('index.html') url: chrome.extension.getURL('index.html')
}); });
return true; return true;

View File

@ -7,11 +7,11 @@ import { ProviderMeta, MetadataDef } from '@polkadot/extension-inject/types';
import { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc-provider/types'; import { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc-provider/types';
import { AccountJson, AuthorizeRequest, MetadataRequest, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning, SigningRequest, RequestRpcUnsubscribe } from '../types'; import { AccountJson, AuthorizeRequest, MetadataRequest, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning, SigningRequest, RequestRpcUnsubscribe } from '../types';
import extension from 'extensionizer';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { assert } from '@polkadot/util'; import { assert } from '@polkadot/util';
import MetadataStore from './MetadataStore'; import chrome from '../../chrome';
import { MetadataStore } from '../stores';
interface AuthRequest { interface AuthRequest {
id: string; id: string;
@ -65,7 +65,7 @@ const WINDOW_OPTS = {
left: 150, left: 150,
top: 150, top: 150,
type: 'popup', type: 'popup',
url: extension.extension.getURL('index.html'), url: chrome.extension.getURL('index.html'),
width: 480 width: 480
}; };
@ -144,13 +144,13 @@ export default class State {
private popupClose (): void { private popupClose (): void {
this.#windows.forEach((id: number): void => this.#windows.forEach((id: number): void =>
extension.windows.remove(id) chrome.windows.remove(id)
); );
this.#windows = []; this.#windows = [];
} }
private popupOpen (): void { private popupOpen (): void {
extension.windows.create({ ...WINDOW_OPTS }, (window?: chrome.windows.Window): void => { chrome.windows.create({ ...WINDOW_OPTS }, (window?: chrome.windows.Window): void => {
if (window) { if (window) {
this.#windows.push(window.id); this.#windows.push(window.id);
} }
@ -215,7 +215,7 @@ export default class State {
: (signCount ? `${signCount}` : '') : (signCount ? `${signCount}` : '')
); );
extension.browserAction.setBadgeText({ text }); chrome.browserAction.setBadgeText({ text });
if (shouldClose && text === '') { if (shouldClose && text === '') {
this.popupClose(); this.popupClose();

View File

@ -4,23 +4,23 @@
import { MetadataDef } from '@polkadot/extension-inject/types'; import { MetadataDef } from '@polkadot/extension-inject/types';
import extension from 'extensionizer'; import chrome from '@polkadot/extension-base/chrome';
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
type StoreValue = Record<string, any>; type StoreValue = Record<string, any>;
const lastError = (type: string): void => { const lastError = (type: string): void => {
const error = extension.runtime.lastError; const error = chrome.runtime.lastError;
if (error) { if (error) {
console.error(`ChainStore.${type}:: runtime.lastError:`, error); console.error(`MetadataStore.${type}:: runtime.lastError:`, error);
} }
}; };
export default class Store { export default class MetadataStore {
public all (cb: (key: string, value: MetadataDef) => void): void { public all (cb: (key: string, value: MetadataDef) => void): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
extension.storage.local.get(null, (result: StoreValue): void => { chrome.storage.local.get(null, (result: StoreValue): void => {
lastError('all'); lastError('all');
Object.entries(result).forEach(([key, value]): void => { Object.entries(result).forEach(([key, value]): void => {
@ -30,7 +30,7 @@ export default class Store {
} }
public get (key: string, cb: (value: MetadataDef) => void): void { public get (key: string, cb: (value: MetadataDef) => void): void {
extension.storage.local.get([key], (result: StoreValue): void => { chrome.storage.local.get([key], (result: StoreValue): void => {
lastError('get'); lastError('get');
cb(result[key]); cb(result[key]);
@ -38,7 +38,7 @@ export default class Store {
} }
public remove (key: string, cb?: () => void): void { public remove (key: string, cb?: () => void): void {
extension.storage.local.remove(key, (): void => { chrome.storage.local.remove(key, (): void => {
lastError('remove'); lastError('remove');
cb && cb(); cb && cb();
@ -46,7 +46,7 @@ export default class Store {
} }
public set (key: string, value: MetadataDef, cb?: () => void): void { public set (key: string, value: MetadataDef, cb?: () => void): void {
extension.storage.local.set({ [key]: value }, (): void => { chrome.storage.local.set({ [key]: value }, (): void => {
lastError('set'); lastError('set');
cb && cb(); cb && cb();

View File

@ -0,0 +1,5 @@
// Copyright 2019 @polkadot/extension-chains authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as MetadataStore } from './Metadata';

View File

@ -0,0 +1,11 @@
// Copyright 2019 @polkadot/extension-chains authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const extension = typeof chrome !== 'undefined'
? chrome
: typeof browser !== 'undefined'
? browser
: null;
export default extension as typeof chrome;

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../../__mocks__/chrome';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';
import { configure, mount, ReactWrapper } from 'enzyme'; import { configure, mount, ReactWrapper } from 'enzyme';
import { themes, Theme } from '@polkadot/extension-ui/components'; import { themes, Theme } from '@polkadot/extension-ui/components';

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../../__mocks__/chrome';
import { AuthorizeRequest } from '@polkadot/extension-base/background/types'; import { AuthorizeRequest } from '@polkadot/extension-base/background/types';
import { configure, mount, ReactWrapper } from 'enzyme'; import { configure, mount, ReactWrapper } from 'enzyme';

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../../__mocks__/chrome';
import { configure, mount, ReactWrapper } from 'enzyme'; import { configure, mount, ReactWrapper } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';
import React from 'react'; import React from 'react';

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../__mocks__/chrome';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';
import { configure, mount, ReactWrapper } from 'enzyme'; import { configure, mount, ReactWrapper } from 'enzyme';

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../../__mocks__/chrome';
import React, { useState } from 'react'; import React, { useState } from 'react';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';

View File

@ -2,9 +2,11 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../__mocks__/chrome';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';
import { configure } from 'enzyme'; import { configure } from 'enzyme';
import extensionizer from 'extensionizer'; import chrome from '@polkadot/extension-base/chrome';
import { exportAccount } from './messaging'; import { exportAccount } from './messaging';
@ -14,7 +16,7 @@ describe('messaging sends message to background via extension port for', () => {
test('exportAccount', () => { test('exportAccount', () => {
const callback = jest.fn(); const callback = jest.fn();
extensionizer.runtime.connect().onMessage.addListener(callback); chrome.runtime.connect().onMessage.addListener(callback);
exportAccount('HjoBp62cvsWDA3vtNMWxz6c9q13ReEHi9UGHK7JbZweH5g5', 'passw0rd'); exportAccount('HjoBp62cvsWDA3vtNMWxz6c9q13ReEHi9UGHK7JbZweH5g5', 'passw0rd');
expect(callback).toHaveBeenCalledWith( expect(callback).toHaveBeenCalledWith(

View File

@ -6,8 +6,8 @@ import { AccountJson, AuthorizeRequest, SigningRequest, RequestTypes, MessageTyp
import { Chain } from '@polkadot/extension-chains/types'; import { Chain } from '@polkadot/extension-chains/types';
import { KeypairType } from '@polkadot/util-crypto/types'; import { KeypairType } from '@polkadot/util-crypto/types';
import extension from 'extensionizer';
import { PORT_EXTENSION } from '@polkadot/extension-base/defaults'; import { PORT_EXTENSION } from '@polkadot/extension-base/defaults';
import chrome from '@polkadot/extension-base/chrome';
import { findChain } from '@polkadot/extension-chains'; import { findChain } from '@polkadot/extension-chains';
interface Handler { interface Handler {
@ -20,7 +20,7 @@ interface Handler {
type Handlers = Record<string, Handler>; type Handlers = Record<string, Handler>;
const port = extension.runtime.connect({ name: PORT_EXTENSION }); const port = chrome.runtime.connect({ name: PORT_EXTENSION });
const handlers: Handlers = {}; const handlers: Handlers = {};
let idCounter = 0; let idCounter = 0;

View File

@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import '../../../../__mocks__/chrome';
import Adapter from 'enzyme-adapter-react-16'; import Adapter from 'enzyme-adapter-react-16';
import { configure, mount, ReactWrapper } from 'enzyme'; import { configure, mount, ReactWrapper } from 'enzyme';
import React from 'react'; import React from 'react';

View File

@ -2,11 +2,10 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import extension from 'extensionizer';
// Runs in the extension background, handling all keyring access // Runs in the extension background, handling all keyring access
import handlers from '@polkadot/extension-base/background/handlers'; import handlers from '@polkadot/extension-base/background/handlers';
import chrome from '@polkadot/extension-base/chrome';
import { PORT_CONTENT, PORT_EXTENSION } from '@polkadot/extension-base/defaults'; import { PORT_CONTENT, PORT_EXTENSION } from '@polkadot/extension-base/defaults';
import keyring from '@polkadot/ui-keyring'; import keyring from '@polkadot/ui-keyring';
import ExtensionStore from '@polkadot/ui-keyring/stores/Extension'; import ExtensionStore from '@polkadot/ui-keyring/stores/Extension';
@ -14,10 +13,10 @@ import { assert } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto'; import { cryptoWaitReady } from '@polkadot/util-crypto';
// setup the notification (same a FF default background, white text) // setup the notification (same a FF default background, white text)
extension.browserAction.setBadgeBackgroundColor({ color: '#d90000' }); chrome.browserAction.setBadgeBackgroundColor({ color: '#d90000' });
// listen to all messages and handle appropriately // listen to all messages and handle appropriately
extension.runtime.onConnect.addListener((port): void => { chrome.runtime.onConnect.addListener((port): void => {
// shouldn't happen, however... only listen to what we know about // shouldn't happen, however... only listen to what we know about
assert([PORT_CONTENT, PORT_EXTENSION].includes(port.name), `Unknown connection from ${port.name}`); assert([PORT_CONTENT, PORT_EXTENSION].includes(port.name), `Unknown connection from ${port.name}`);

View File

@ -2,11 +2,11 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import extension from 'extensionizer'; import chrome from '@polkadot/extension-base/chrome';
import { PORT_CONTENT } from '@polkadot/extension-base/defaults'; import { PORT_CONTENT } from '@polkadot/extension-base/defaults';
// connect to the extension // connect to the extension
const port = extension.runtime.connect({ name: PORT_CONTENT }); const port = chrome.runtime.connect({ name: PORT_CONTENT });
// send any messages from the extension back to the page // send any messages from the extension back to the page
port.onMessage.addListener((data): void => { port.onMessage.addListener((data): void => {
@ -26,7 +26,7 @@ window.addEventListener('message', ({ data, source }): void => {
// inject our data injector // inject our data injector
const script = document.createElement('script'); const script = document.createElement('script');
script.src = extension.extension.getURL('page.js'); script.src = chrome.extension.getURL('page.js');
script.onload = (): void => { script.onload = (): void => {
// remove the injecting tag when loaded // remove the injecting tag when loaded

View File

@ -1,11 +0,0 @@
// Copyright 2019-2020 @polkadot/extension authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
/// <reference types="chrome"/>
declare module 'extensionizer' {
const extensionizer: typeof chrome;
export default extensionizer;
}

View File

@ -2869,7 +2869,6 @@ __metadata:
"@polkadot/api": ^1.9.1 "@polkadot/api": ^1.9.1
"@polkadot/keyring": ^2.7.1 "@polkadot/keyring": ^2.7.1
"@polkadot/ui-keyring": ^0.52.0-beta.22 "@polkadot/ui-keyring": ^0.52.0-beta.22
extensionizer: ^1.0.1
languageName: unknown languageName: unknown
linkType: soft linkType: soft