Extend message/request ids as string (#48)
* Extend message/request ids as string * Only decode the current method (as meta available) * Dep-dup id generation
This commit is contained in:
parent
9d91f681a1
commit
a94836977a
@ -12,7 +12,7 @@ import { ActionBar, Button, Icon, IconBox, Link, Tip, defaults, withOnAction } f
|
||||
import { approveAuthRequest, rejectAuthRequest } from '../../messaging';
|
||||
|
||||
type Props = {
|
||||
authId: number,
|
||||
authId: string,
|
||||
className?: string,
|
||||
isFirst: boolean,
|
||||
onAction: OnActionFromCtx,
|
||||
|
||||
@ -11,6 +11,7 @@ import { Metadata, Method } from '@polkadot/types';
|
||||
type Props = {
|
||||
className?: string,
|
||||
genesisHash: string,
|
||||
isDecoded: boolean,
|
||||
method: string,
|
||||
nonce: string,
|
||||
url: string
|
||||
@ -39,7 +40,7 @@ function renderMethod (data: string, meta?: Metadata | null) {
|
||||
);
|
||||
}
|
||||
|
||||
function Details ({ className, genesisHash, method, nonce, url }: Props) {
|
||||
function Details ({ className, genesisHash, isDecoded, method, nonce, url }: Props) {
|
||||
const chain = findChain(genesisHash);
|
||||
|
||||
return (
|
||||
@ -57,7 +58,7 @@ function Details ({ className, genesisHash, method, nonce, url }: Props) {
|
||||
<td className='label'>nonce</td>
|
||||
<td className='data'>{nonce}</td>
|
||||
</tr>
|
||||
{renderMethod(method, chain ? chain.meta : null)}
|
||||
{renderMethod(method, (chain && isDecoded) ? chain.meta : null)}
|
||||
</tbody>
|
||||
</table>
|
||||
);
|
||||
|
||||
@ -16,7 +16,7 @@ type Props = {
|
||||
isFirst: boolean,
|
||||
onAction: OnActionFromCtx,
|
||||
request: MessageExtrinsicSign,
|
||||
signId: number,
|
||||
signId: string,
|
||||
url: string
|
||||
};
|
||||
|
||||
@ -33,6 +33,7 @@ function Request ({ isFirst, onAction, request: { address, genesisHash, method,
|
||||
<Address address={address}>
|
||||
<Details
|
||||
genesisHash={genesisHash}
|
||||
isDecoded={isFirst}
|
||||
method={method}
|
||||
nonce={nonce}
|
||||
url={url}
|
||||
|
||||
@ -10,7 +10,7 @@ import extension from 'extensionizer';
|
||||
import { PORT_POPUP } from '@polkadot/extension/defaults';
|
||||
|
||||
type Handlers = {
|
||||
[index: number]: {
|
||||
[index: string]: {
|
||||
resolve: (data: any) => void,
|
||||
reject: (error: Error) => void
|
||||
}
|
||||
@ -40,7 +40,7 @@ port.onMessage.addListener((data) => {
|
||||
|
||||
function sendMessage (message: MessageTypes, request: any = {}): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = ++idCounter;
|
||||
const id = `${Date.now()}.${++idCounter}`;
|
||||
|
||||
handlers[id] = { resolve, reject };
|
||||
|
||||
@ -64,11 +64,11 @@ export async function getAuthRequests (): Promise<Array<AuthorizeRequest>> {
|
||||
return sendMessage('authorize.requests');
|
||||
}
|
||||
|
||||
export async function rejectAuthRequest (id: number): Promise<boolean> {
|
||||
export async function rejectAuthRequest (id: string): Promise<boolean> {
|
||||
return sendMessage('authorize.reject', { id });
|
||||
}
|
||||
|
||||
export async function approveAuthRequest (id: number): Promise<boolean> {
|
||||
export async function approveAuthRequest (id: string): Promise<boolean> {
|
||||
return sendMessage('authorize.approve', { id });
|
||||
}
|
||||
|
||||
@ -76,11 +76,11 @@ export async function getSignRequests (): Promise<Array<SigningRequest>> {
|
||||
return sendMessage('signing.requests');
|
||||
}
|
||||
|
||||
export async function cancelSignRequest (id: number): Promise<boolean> {
|
||||
export async function cancelSignRequest (id: string): Promise<boolean> {
|
||||
return sendMessage('signing.cancel', { id });
|
||||
}
|
||||
|
||||
export async function approveSignRequest (id: number, password: string): Promise<boolean> {
|
||||
export async function approveSignRequest (id: string, password: string): Promise<boolean> {
|
||||
return sendMessage('signing.approve', { id, password });
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import extension from 'extensionizer';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
type AuthRequest = {
|
||||
id: number,
|
||||
id: string,
|
||||
idStr: string,
|
||||
request: MessageAuthorize,
|
||||
resolve: (result: boolean) => void,
|
||||
@ -27,7 +27,7 @@ type AuthUrls = {
|
||||
};
|
||||
|
||||
type SignRequest = {
|
||||
id: number,
|
||||
id: string,
|
||||
request: MessageExtrinsicSign,
|
||||
resolve: (result: MessageExtrinsicSign$Response) => void,
|
||||
reject: (error: Error) => void,
|
||||
@ -36,11 +36,15 @@ type SignRequest = {
|
||||
|
||||
let idCounter = 0;
|
||||
|
||||
function getId (): string {
|
||||
return `${Date.now()}.${++idCounter}`;
|
||||
}
|
||||
|
||||
export default class State {
|
||||
// at the moment, we are keeping the list in memory - this should be persisted
|
||||
private _authUrls: AuthUrls = {};
|
||||
private _authRequests: { [index: number]: AuthRequest } = {};
|
||||
private _signRequests: { [index: number]: SignRequest } = {};
|
||||
private _authRequests: { [index: string]: AuthRequest } = {};
|
||||
private _signRequests: { [index: string]: SignRequest } = {};
|
||||
|
||||
get hasAuthRequests (): boolean {
|
||||
return this.numAuthRequests === 0;
|
||||
@ -70,7 +74,7 @@ export default class State {
|
||||
.map(({ id, request, url }) => [id, request, url]);
|
||||
}
|
||||
|
||||
private authComplete = (id: number, fn: Function) => {
|
||||
private authComplete = (id: string, fn: Function) => {
|
||||
return (result: boolean | Error): void => {
|
||||
const isAllowed = result === true;
|
||||
const { idStr, request: { origin }, url } = this._authRequests[id];
|
||||
@ -90,7 +94,7 @@ export default class State {
|
||||
};
|
||||
}
|
||||
|
||||
private signComplete = (id: number, fn: Function) => {
|
||||
private signComplete = (id: string, fn: Function) => {
|
||||
return (result: MessageExtrinsicSign$Response | Error): void => {
|
||||
delete this._signRequests[id];
|
||||
this.updateIcon();
|
||||
@ -129,7 +133,7 @@ export default class State {
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = ++idCounter;
|
||||
const id = getId();
|
||||
|
||||
this._authRequests[id] = {
|
||||
id,
|
||||
@ -153,16 +157,16 @@ export default class State {
|
||||
return true;
|
||||
}
|
||||
|
||||
getAuthRequest (id: number): AuthRequest {
|
||||
getAuthRequest (id: string): AuthRequest {
|
||||
return this._authRequests[id];
|
||||
}
|
||||
|
||||
getSignRequest (id: number): SignRequest {
|
||||
getSignRequest (id: string): SignRequest {
|
||||
return this._signRequests[id];
|
||||
}
|
||||
|
||||
signQueue (url: string, request: MessageExtrinsicSign): Promise<MessageExtrinsicSign$Response> {
|
||||
const id = ++idCounter;
|
||||
const id = getId();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this._signRequests[id] = {
|
||||
|
||||
@ -15,7 +15,7 @@ const state = new State();
|
||||
const extension = new Extension(state);
|
||||
const tabs = new Tabs(state);
|
||||
|
||||
function createSubscription (id: number, port: chrome.runtime.Port): (data: any) => void {
|
||||
function createSubscription (id: string, port: chrome.runtime.Port): (data: any) => void {
|
||||
// FIXME We are not handling actual unsubscribes yet, this is an issue
|
||||
return (subscription: any) => {
|
||||
port.postMessage({ id, subscription });
|
||||
|
||||
@ -6,31 +6,31 @@ import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
|
||||
export type MessageTypes = 'authorize.approve' | 'authorize.reject' | 'authorize.requests' | 'authorize.tab' | 'accounts.create' | 'accounts.edit' | 'accounts.forget' | 'accounts.list' | 'accounts.subscribe' | 'extrinsic.sign' | 'seed.create' | 'seed.validate' | 'signing.approve' | 'signing.cancel' | 'signing.requests';
|
||||
|
||||
export type AuthorizeRequest = [number, MessageAuthorize, string];
|
||||
export type AuthorizeRequest = [string, MessageAuthorize, string];
|
||||
|
||||
export type SigningRequest = [number, MessageExtrinsicSign, string];
|
||||
export type SigningRequest = [string, MessageExtrinsicSign, string];
|
||||
|
||||
export type MessageAuthorize = {
|
||||
origin: string
|
||||
};
|
||||
|
||||
export type MessageAuthorizeApprove = {
|
||||
id: number;
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type MessageAuthorizeReject = {
|
||||
id: number;
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type MessageRequest = {
|
||||
id: number,
|
||||
id: string,
|
||||
message: MessageTypes,
|
||||
request: any
|
||||
};
|
||||
|
||||
export type MessageResponse = {
|
||||
error?: string,
|
||||
id: number,
|
||||
id: string,
|
||||
response?: any,
|
||||
subscription?: any
|
||||
};
|
||||
@ -52,12 +52,12 @@ export type MessageAccountForget = {
|
||||
};
|
||||
|
||||
export type MessageExtrinsicSignApprove = {
|
||||
id: number,
|
||||
id: string,
|
||||
password: string
|
||||
};
|
||||
|
||||
export type MessageExtrinsicSignCancel = {
|
||||
id: number
|
||||
id: string
|
||||
};
|
||||
|
||||
export type MessageExtrinsicSign = {
|
||||
@ -69,7 +69,7 @@ export type MessageExtrinsicSign = {
|
||||
};
|
||||
|
||||
export type MessageExtrinsicSign$Response = {
|
||||
id: number,
|
||||
id: string,
|
||||
signature: string
|
||||
};
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ import Injected from './Injected';
|
||||
// - resolves/rejects the promise with the result (or sub data)
|
||||
|
||||
type Handlers = {
|
||||
[index: number]: {
|
||||
[index: string]: {
|
||||
resolve: (data: any) => void,
|
||||
reject: (error: Error) => void,
|
||||
subscriber?: (data: any) => void
|
||||
@ -31,7 +31,7 @@ let idCounter = 0;
|
||||
// resolve once the event is resolved (by the response listener just below this)
|
||||
function sendMessage (message: MessageTypes, request: any = null, subscriber?: (data: any) => void): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = ++idCounter;
|
||||
const id = `${Date.now()}.${++idCounter}`;
|
||||
|
||||
handlers[id] = { resolve, reject, subscriber };
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user