Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed all interfaces to make the extension work independently #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SignerPayloadJSON } from '@polkadot/types/types';

import { ResponseSigning } from '@cennznet/extension-base/background/types';
import { MetadataDef } from '@cennznet/extension-inject/types';

import { KeyringPair } from '@polkadot/keyring/types';
import { TypeRegistry } from '@polkadot/types';
import keyring from '@polkadot/ui-keyring';
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('Extension', () => {
const signatureExpected = registry
.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(pair);

tabs.handle('1615191860871.5', 'pub(extrinsic.sign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
tabs.handle('1615191860871.5', 'pub(extrinsic.cSign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
.then((result) => {
expect((result as ResponseSigning)?.signature).toEqual(signatureExpected.signature);
}).catch((err) => console.log(err));
Expand Down Expand Up @@ -258,7 +259,7 @@ describe('Extension', () => {
const signatureExpected = registry
.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(pair);

tabs.handle('1615191860771.5', 'pub(extrinsic.sign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
tabs.handle('1615191860771.5', 'pub(extrinsic.cSign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
.then((result) => {
expect((result as ResponseSigning)?.signature).toEqual(signatureExpected.signature);
}).catch((err) => console.log(err));
Expand Down Expand Up @@ -314,7 +315,7 @@ describe('Extension', () => {
const signatureExpected = registry
.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(pair);

tabs.handle('1615191860771.5', 'pub(extrinsic.sign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
tabs.handle('1615191860771.5', 'pub(extrinsic.cSign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
.then((result) => {
expect((result as ResponseSigning)?.signature).toEqual(signatureExpected.signature);
}).catch((err) => console.log(err));
Expand Down Expand Up @@ -381,7 +382,7 @@ describe('Extension', () => {
const signatureExpected = registry
.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(pair);

tabs.handle('1615191860771.5', 'pub(extrinsic.sign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
tabs.handle('1615191860771.5', 'pub(extrinsic.cSign)', payload, 'http://localhost:3000', {} as chrome.runtime.Port)
.then((result) => {
expect((result as ResponseSigning)?.signature).toEqual(signatureExpected.signature);
}).catch((err) => console.log(err));
Expand Down
7 changes: 3 additions & 4 deletions packages/extension-base/src/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { MetadataDef } from '@cennznet/extension-inject/types';
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@polkadot/keyring/types';
import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
import type { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import type {
AccountJson,
import type { AccountJson,
AllowedPath,
AuthorizeRequest,
MessageTypes,
Expand Down Expand Up @@ -45,11 +44,11 @@ import type {
ResponseSeedValidate,
ResponseSigningIsLocked,
ResponseType,
SigningRequest
} from '../types';
SigningRequest } from '../types';

import { ALLOWED_PATH, PASSWORD_EXPIRY_MS } from '@cennznet/extension-base/defaults';
import chrome from '@cennznet/extension-inject/chrome';

import { TypeRegistry } from '@polkadot/types';
import keyring from '@polkadot/ui-keyring';
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
Expand Down
10 changes: 5 additions & 5 deletions packages/extension-base/src/background/handlers/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { MetadataDef, ProviderMeta } from '@cennznet/extension-inject/types
import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc-provider/types';
import type { AccountJson, AuthorizeRequest, MetadataRequest, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning, SigningRequest } from '../types';

import { BehaviorSubject } from 'rxjs';

import { addMetadata, knownMetadata } from '@cennznet/extension-chains';
import chrome from '@cennznet/extension-inject/chrome';
import { BehaviorSubject } from 'rxjs';

import { assert } from '@polkadot/util';

import { MetadataStore } from '../../stores';
Expand Down Expand Up @@ -318,7 +318,7 @@ export default class State {
});
}

public ensureUrlAuthorized (url: string): boolean {
public ensureUrlAuthorized1 (url: string): boolean {
const entry = this.#authUrls[this.stripUrl(url)];

assert(entry, `The source ${url} has not been enabled yet`);
Expand Down Expand Up @@ -367,7 +367,7 @@ export default class State {
public rpcSend (request: RequestRpcSend, port: chrome.runtime.Port): Promise<JsonRpcResponse> {
const provider = this.#injectedProviders.get(port);

assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
assert(provider, 'Cannot call pub(rpc.cSubscribe) before provider is set');

return provider.send(request.method, request.params) as Promise<JsonRpcResponse>;
}
Expand Down Expand Up @@ -400,7 +400,7 @@ export default class State {
public rpcSubscribe ({ method, params, type }: RequestRpcSubscribe, cb: ProviderInterfaceCallback, port: chrome.runtime.Port): Promise<number | string> {
const provider = this.#injectedProviders.get(port);

assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
assert(provider, 'Cannot call pub(rpc.cSubscribe) before provider is set');

return provider.subscribe(type, method, params, cb);
}
Expand Down
41 changes: 21 additions & 20 deletions packages/extension-base/src/background/handlers/Tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { MessageTypes, RequestAccountList, RequestAuthorizeTab, RequestRpcS

import { PHISHING_PAGE_REDIRECT } from '@cennznet/extension-base/defaults';
import { canDerive } from '@cennznet/extension-base/utils';

import { checkIfDenied } from '@polkadot/phishing';
import keyring from '@polkadot/ui-keyring';
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
Expand Down Expand Up @@ -52,7 +53,7 @@ export default class Tabs {

// FIXME This looks very much like what we have in Extension
private accountsSubscribe (url: string, id: string, port: chrome.runtime.Port): boolean {
const cb = createSubscription<'pub(accounts.subscribe)'>(id, port);
const cb = createSubscription<'pub(accounts.cSubscribe)'>(id, port);
const subscription = accountsObservable.subject.subscribe((accounts: SubjectInfo): void =>
cb(transformAccounts(accounts))
);
Expand Down Expand Up @@ -112,8 +113,8 @@ export default class Tabs {
}

private async rpcSubscribe (request: RequestRpcSubscribe, id: string, port: chrome.runtime.Port): Promise<boolean> {
const innerCb = createSubscription<'pub(rpc.subscribe)'>(id, port);
const cb = (_error: Error | null, data: SubscriptionMessageTypes['pub(rpc.subscribe)']): void => innerCb(data);
const innerCb = createSubscription<'pub(rpc.cSubscribe)'>(id, port);
const cb = (_error: Error | null, data: SubscriptionMessageTypes['pub(rpc.cSubscribe)']): void => innerCb(data);
const subscriptionId = await this.#state.rpcSubscribe(request, cb, port);

port.onDisconnect.addListener((): void => {
Expand All @@ -125,8 +126,8 @@ export default class Tabs {
}

private rpcSubscribeConnected (request: null, id: string, port: chrome.runtime.Port): Promise<boolean> {
const innerCb = createSubscription<'pub(rpc.subscribeConnected)'>(id, port);
const cb = (_error: Error | null, data: SubscriptionMessageTypes['pub(rpc.subscribeConnected)']): void => innerCb(data);
const innerCb = createSubscription<'pub(rpc.cSubscribeConnected)'>(id, port);
const cb = (_error: Error | null, data: SubscriptionMessageTypes['pub(rpc.cSubscribeConnected)']): void => innerCb(data);

this.#state.rpcSubscribeConnected(request, cb, port);

Expand Down Expand Up @@ -170,48 +171,48 @@ export default class Tabs {
return this.redirectIfPhishing(url);
}

if (type !== 'pub(authorize.tab)') {
this.#state.ensureUrlAuthorized(url);
if (type !== 'pub(authorize.cennzTab)') {
this.#state.ensureUrlAuthorized1(url);
}

switch (type) {
case 'pub(authorize.tab)':
case 'pub(authorize.cennzTab)':
return this.authorize(url, request as RequestAuthorizeTab);

case 'pub(accounts.list)':
case 'pub(accounts.cList)':
return this.accountsList(url, request as RequestAccountList);

case 'pub(accounts.subscribe)':
case 'pub(accounts.cSubscribe)':
return this.accountsSubscribe(url, id, port);

case 'pub(bytes.sign)':
case 'pub(bytes.cSign)':
return this.bytesSign(url, request as SignerPayloadRaw);

case 'pub(extrinsic.sign)':
case 'pub(extrinsic.cSign)':
return this.extrinsicSign(url, request as SignerPayloadJSON);

case 'pub(metadata.list)':
case 'pub(metadata.cList)':
return this.metadataList(url);

case 'pub(metadata.provide)':
case 'pub(metadata.cProvide)':
return this.metadataProvide(url, request as MetadataDef);

case 'pub(rpc.listProviders)':
case 'pub(rpc.cListProviders)':
return this.rpcListProviders();

case 'pub(rpc.send)':
case 'pub(rpc.cSend)':
return this.rpcSend(request as RequestRpcSend, port);

case 'pub(rpc.startProvider)':
case 'pub(rpc.cStartProvider)':
return this.rpcStartProvider(request as string, port);

case 'pub(rpc.subscribe)':
case 'pub(rpc.cSubscribe)':
return this.rpcSubscribe(request as RequestRpcSubscribe, id, port);

case 'pub(rpc.subscribeConnected)':
case 'pub(rpc.cSubscribeConnected)':
return this.rpcSubscribeConnected(request as null, id, port);

case 'pub(rpc.unsubscribe)':
case 'pub(rpc.cUnsubscribe)':
return this.rpcUnsubscribe(request as RequestRpcUnsubscribe, port);

default:
Expand Down
26 changes: 13 additions & 13 deletions packages/extension-base/src/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ export interface RequestSignatures {
'pri(signing.requests)': [RequestSigningSubscribe, boolean, SigningRequest[]];
'pri(window.open)': [AllowedPath, boolean];
// public/external requests, i.e. from a page
'pub(accounts.list)': [RequestAccountList, InjectedAccount[]];
'pub(accounts.subscribe)': [RequestAccountSubscribe, boolean, InjectedAccount[]];
'pub(authorize.tab)': [RequestAuthorizeTab, null];
'pub(bytes.sign)': [SignerPayloadRaw, ResponseSigning];
'pub(extrinsic.sign)': [SignerPayloadJSON, ResponseSigning];
'pub(metadata.list)': [null, InjectedMetadataKnown[]];
'pub(metadata.provide)': [MetadataDef, boolean];
'pub(accounts.cList)': [RequestAccountList, InjectedAccount[]];
'pub(accounts.cSubscribe)': [RequestAccountSubscribe, boolean, InjectedAccount[]];
'pub(authorize.cennzTab)': [RequestAuthorizeTab, null];
'pub(bytes.cSign)': [SignerPayloadRaw, ResponseSigning];
'pub(extrinsic.cSign)': [SignerPayloadJSON, ResponseSigning];
'pub(metadata.cList)': [null, InjectedMetadataKnown[]];
'pub(metadata.cProvide)': [MetadataDef, boolean];
'pub(phishing.redirectIfDenied)': [null, boolean];
'pub(rpc.listProviders)': [void, ResponseRpcListProviders];
'pub(rpc.send)': [RequestRpcSend, JsonRpcResponse];
'pub(rpc.startProvider)': [string, ProviderMeta];
'pub(rpc.subscribe)': [RequestRpcSubscribe, number, JsonRpcResponse];
'pub(rpc.subscribeConnected)': [null, boolean, boolean];
'pub(rpc.unsubscribe)': [RequestRpcUnsubscribe, boolean];
'pub(rpc.cListProviders)': [void, ResponseRpcListProviders];
'pub(rpc.cSend)': [RequestRpcSend, JsonRpcResponse];
'pub(rpc.cStartProvider)': [string, ProviderMeta];
'pub(rpc.cSubscribe)': [RequestRpcSubscribe, number, JsonRpcResponse];
'pub(rpc.cSubscribeConnected)': [null, boolean, boolean];
'pub(rpc.cUnsubscribe)': [RequestRpcUnsubscribe, boolean];
}

export type MessageTypes = keyof RequestSignatures;
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-base/src/page/Accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default class Accounts implements InjectedAccounts {
}

public get (anyType?: boolean): Promise<InjectedAccount[]> {
return sendRequest('pub(accounts.list)', { anyType });
return sendRequest('pub(accounts.cList)', { anyType });
}

public subscribe (cb: (accounts: InjectedAccount[]) => unknown): Unsubcall {
sendRequest('pub(accounts.subscribe)', null, cb)
sendRequest('pub(accounts.cSubscribe)', null, cb)
.catch((error: Error) => console.error(error));

return (): void => {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-base/src/page/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default class Metadata implements InjectedMetadata {
}

public get (): Promise<InjectedMetadataKnown[]> {
return sendRequest('pub(metadata.list)');
return sendRequest('pub(metadata.cList)');
}

public provide (definition: MetadataDef): Promise<boolean> {
return sendRequest('pub(metadata.provide)', definition);
return sendRequest('pub(metadata.cProvide)', definition);
}
}
10 changes: 5 additions & 5 deletions packages/extension-base/src/page/PostMessageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class PostMessageProvider implements InjectedProvider {
}

public listProviders (): Promise<ProviderList> {
return sendRequest('pub(rpc.listProviders)', undefined);
return sendRequest('pub(rpc.cListProviders)', undefined);
}

/**
Expand All @@ -113,7 +113,7 @@ export default class PostMessageProvider implements InjectedProvider {
if (subscription) {
const { callback, type } = subscription;

const id = await sendRequest('pub(rpc.subscribe)', { method, params, type }, (res): void => {
const id = await sendRequest('pub(rpc.cSubscribe)', { method, params, type }, (res): void => {
subscription.callback(null, res);
});

Expand All @@ -122,7 +122,7 @@ export default class PostMessageProvider implements InjectedProvider {
return id;
}

return sendRequest('pub(rpc.send)', { method, params });
return sendRequest('pub(rpc.cSend)', { method, params });
}

/**
Expand All @@ -133,10 +133,10 @@ export default class PostMessageProvider implements InjectedProvider {
this.#isConnected = false;
this.#eventemitter.emit('disconnected');

const meta = await sendRequest('pub(rpc.startProvider)', key);
const meta = await sendRequest('pub(rpc.cStartProvider)', key);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
sendRequest('pub(rpc.subscribeConnected)', null, (connected) => {
sendRequest('pub(rpc.cSubscribeConnected)', null, (connected) => {
this.#isConnected = connected;

if (connected) {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-base/src/page/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class Signer implements SignerInterface {

public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
const id = ++nextId;
const result = await sendRequest('pub(extrinsic.sign)', payload);
const result = await sendRequest('pub(extrinsic.cSign)', payload);

// we add an internal id (number) - should have a mapping from the
// extension id (string) -> internal id (number) if we wish to provide
Expand All @@ -29,7 +29,7 @@ export default class Signer implements SignerInterface {

public async signRaw (payload: SignerPayloadRaw): Promise<SignerResult> {
const id = ++nextId;
const result = await sendRequest('pub(bytes.sign)', payload);
const result = await sendRequest('pub(bytes.cSign)', payload);

return {
...result,
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-base/src/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function sendMessage<TMessageType extends MessageTypes> (message: TMessag

// the enable function, called by the dapp to allow access
export async function enable (origin: string): Promise<Injected> {
await sendMessage('pub(authorize.tab)', { origin });
await sendMessage('pub(authorize.cennzTab)', { origin });

return new Injected(sendMessage);
}
Expand Down
454 changes: 229 additions & 225 deletions packages/extension-chains/src/config.ts

Large diffs are not rendered by default.

Loading