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

[MS-191] feat: migrate messaging #561

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
53 changes: 33 additions & 20 deletions apps/wallet/browser/bridge/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PopupType, RequestType, ResponseCode, Timeout } from '@walless/core';
import { ChromeChannel } from '@metacraft/crab/chrome';
import type { RawRequest, Response } from '@metacraft/crab/core';
import type { UnknownObject } from '@walless/core';
import { PopupType, RequestType, Timeout } from '@walless/core';
import type { PureMessagePayload, ResponsePayload } from '@walless/messaging';
import { Channels } from '@walless/messaging';
import * as bs58 from 'bs58';
Expand All @@ -18,19 +21,25 @@ export const sendRequest = async (
};

export const handleRequestConnect = async (
requestId: string,
resolveId: string,
isApproved: boolean,
) => {
const payload: PopupPayload = {
const payload: RawRequest = {
from: PopupType.REQUEST_CONNECT_POPUP,
type: RequestType.REQUEST_CONNECT,
sourceRequestId: requestId,
type: RequestType.REQUEST_CONNECT as never,
resolveId: resolveId,
isApproved,
};

try {
const res = await encryptedMessenger.request(Channels.kernel, payload);
if (res.responseCode === ResponseCode.SUCCESS) {
const chromeChannel = new ChromeChannel(Channels.popup);
const res = await chromeChannel.request<Response<UnknownObject>>(
payload,
Timeout.sixtySeconds,
);
if (res.error) {
throw Error(res.error);
} else {
window.close();
}
} catch (error) {
Expand All @@ -56,36 +65,40 @@ export const handleRequestInstallLayout = async (
}
};

export const handleRequestSignature = async (
options: PayloadOptions,
type: RequestType,
) => {
const payload: PopupPayload = {
export const handleRequestSignature = async (options: PayloadOptions) => {
const { sourceRequestId } = options;
const payload: RawRequest = {
from: PopupType.SIGNATURE_POPUP,
type,
type: RequestType.RESOLVE_REQUEST_SIGNATURE as never,
resolveId: sourceRequestId,
...options,
};

try {
return await encryptedMessenger.request(Channels.kernel, payload, 10000);
const chromeChannel = new ChromeChannel(Channels.popup);
const res = await chromeChannel.request<Response<UnknownObject>>(
payload,
Timeout.sixtySeconds,
);
return res;
} catch (error) {
throw Error('Unable to handle sign message request');
}
};

export const getDataFromSourceRequest = async (
requestId: string,
resolveId: string,
from: string,
) => {
const payload: PopupPayload = {
const payload: RawRequest = {
from,
type: RequestType.REQUEST_PAYLOAD,
sourceRequestId: requestId,
type: RequestType.REQUEST_PAYLOAD as never,
resolveId: resolveId,
};

try {
const res = await encryptedMessenger.request(
Channels.kernel,
const chromeChannel = new ChromeChannel(Channels.popup);
const res = await chromeChannel.request<Response<UnknownObject>>(
payload,
Timeout.sixtySeconds,
);
Expand Down
2 changes: 2 additions & 0 deletions apps/wallet/browser/bridge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Networks } from '@walless/core';
import { logger } from '@walless/core';
import type { PureMessagePayload } from '@walless/messaging';
import { createEncryptionKeyVault, createMessenger } from '@walless/messaging';
Expand All @@ -7,6 +8,7 @@ export interface PayloadOptions {
sourceRequestId: string;
isApproved?: boolean;
passcode?: string;
network?: Networks;
}

export type PopupPayload = PureMessagePayload & PayloadOptions;
Expand Down
23 changes: 18 additions & 5 deletions apps/wallet/browser/content/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createMessenger } from '@walless/messaging';

const messenger = createMessenger();
import { ChromeChannel } from '@metacraft/crab/chrome';
import type { Response } from '@metacraft/crab/core';
import { Timeout, type UnknownObject } from '@walless/core';
import { Channels } from '@walless/messaging';

const chromeChannel = new ChromeChannel(Channels.kernel);
export const initializeMessaging = async () => {
window.postMessage({ from: 'walless-content-script-loaded' });
window.addEventListener(
Expand All @@ -15,9 +17,20 @@ export const initializeMessaging = async () => {
await chrome.runtime.sendMessage(data);
} else {
// TODO: use timeout from sdk that include in data
const response = await messenger.request('kernel', data);
const newResponse = await chromeChannel.request<
Response<UnknownObject>
>(
{
...data,
id: data.requestId,
},
data.timeout || Timeout.sixtySeconds,
);

window.postMessage(response);
window.postMessage({
...newResponse,
from: 'walless@kernel',
});
}
}
},
Expand Down
63 changes: 37 additions & 26 deletions apps/wallet/browser/kernel/handlers/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { ChromeKernel } from '@metacraft/crab/chrome';
import type { Middleware } from '@metacraft/crab/core';
import { PublicKey } from '@solana/web3.js';
import type { SuiPublicKey } from '@walless/core';
import type { RequestType, SuiPublicKey } from '@walless/core';
import { Networks } from '@walless/core';
import { ResponseCode } from '@walless/core';
import type { ConnectOptions } from '@walless/sdk';
import type { Channels } from '@walless/messaging';
import type { PublicKeyDocument, TrustedDomainDocument } from '@walless/store';
import { selectors } from '@walless/store';
import { encode } from 'bs58';
Expand All @@ -13,12 +15,10 @@ import {
checkInstalledExtensionById,
} from '../utils/helper';
import { openPopup } from '../utils/popup';
import { getRequestRecord, respond } from '../utils/requestPool';
import { respond } from '../utils/requestPool';
import type { HandleMethod } from '../utils/types';

export const connect: HandleMethod<{ options?: ConnectOptions }> = async ({
payload,
}) => {
export const connect: Middleware = async (payload, respond) => {
if (!payload.options) throw new Error('No connection options provided');

const connectOptions = payload.options;
Expand Down Expand Up @@ -63,12 +63,14 @@ export const connect: HandleMethod<{ options?: ConnectOptions }> = async ({
})
.filter((k) => !!k);

respond(payload.requestId, ResponseCode.SUCCESS, { publicKeys });
respond({
publicKeys,
requestId: payload.id,
responseCode: ResponseCode.SUCCESS,
});
};

export const disconnect: HandleMethod<{
options?: ConnectOptions;
}> = async ({ payload }) => {
export const disconnect: Middleware = async (payload, respond) => {
if (!payload.options) throw Error('No disconnection options provided');

const connectOptions = payload.options;
Expand All @@ -82,24 +84,33 @@ export const disconnect: HandleMethod<{
);
}

respond(payload.requestId, ResponseCode.SUCCESS);
respond({
...payload,
responseCode: ResponseCode.SUCCESS,
});
};

export const requestPayload: HandleMethod<{
sourceRequestId?: string;
}> = ({ payload }) => {
if (!payload.sourceRequestId) {
throw Error('Not sourceRequestId provided');
}

const { sourceRequestId, requestId } = payload;
const { payload: sourcePayload, channel: sourceChannel } =
getRequestRecord(sourceRequestId);

respond(requestId, ResponseCode.SUCCESS, {
...sourceChannel,
...sourcePayload,
});
export const requestPayload = (
kernel: ChromeKernel<Channels, RequestType>,
): Middleware => {
return async (payload, respond) => {
if (!payload.resolveId) {
throw Error('Not sourceRequestId provided');
}

const { resolveId, id: requestId } = payload;
const response = kernel.getRequestByResolveId(resolveId);

if ('error' in response) {
respond({ error: response.error });
} else {
respond({
requestId,
responseCode: ResponseCode.SUCCESS,
...response.request,
});
}
};
};

export type LayoutPayload = {
Expand Down
Loading
Loading