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

[WL-910] arc refactor, continued #449

Merged
merged 24 commits into from Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
81bcf37
refactor device module
fluctlight-kayaba Jan 22, 2024
c8cb705
chore: typed and colored logger
fluctlight-kayaba Jan 24, 2024
1efd138
feat: add Globe icon
fluctlight-kayaba Jan 24, 2024
3843c66
deps: add color2k dependency, for color manipulation
fluctlight-kayaba Jan 24, 2024
97b7348
feat: style util, optmize haptic feedback
fluctlight-kayaba Jan 24, 2024
bacb967
chore: use transparent icon for Extension build
fluctlight-kayaba Jan 24, 2024
ef8268a
feat: Browser route for browse feature
fluctlight-kayaba Jan 24, 2024
8a29817
refactor: decouple sdk/wallet-standard from other packages, only depe…
fluctlight-kayaba Jan 24, 2024
36fab3a
chore: use minimal import from storage/db in bridge
fluctlight-kayaba Jan 25, 2024
82ba471
chore: cleanup crypto module
fluctlight-kayaba Jan 26, 2024
6db4671
chore: isolate kernel, bridge and utils/storage
fluctlight-kayaba Jan 26, 2024
6118730
feat: native/mobile messaging module
fluctlight-kayaba Jan 28, 2024
3916dac
feat: more icons
fluctlight-kayaba Jan 28, 2024
e7198a1
chore: add fs, simulated broadcast-channel deps
fluctlight-kayaba Jan 28, 2024
02ae5ef
chore: messaging fallback for Mobile env
fluctlight-kayaba Jan 28, 2024
73ecc0e
chore: type import correction
fluctlight-kayaba Jan 28, 2024
17f22bf
feat: WebView abstraction for Browser
fluctlight-kayaba Jan 28, 2024
d348056
refactor: universal browser hook
fluctlight-kayaba Jan 28, 2024
59d11e5
feat: use messaging module in Mobile build
fluctlight-kayaba Jan 28, 2024
756fb2f
chore: alias kernel, bridge for mobile
fluctlight-kayaba Jan 28, 2024
4b3ecb1
feat: url helper
fluctlight-kayaba Jan 28, 2024
b102077
config: linking injection script for mobile, sync native deps
fluctlight-kayaba Jan 28, 2024
a75fde2
feat: BroadcastChannel shim for mobile
fluctlight-kayaba Jan 28, 2024
38716fe
feat: smart navigator component
fluctlight-kayaba Jan 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/wallet/android/link-assets-manifest.json
Expand Up @@ -52,6 +52,10 @@
{
"path": "assets/fonts/Rubik-SemiBoldItalic.ttf",
"sha1": "6a8ffebd4702be10b6e3fc980eace615b16634d9"
},
{
"path": "metacraft/injection.js",
"sha1": "f9d3a2e5e46cb50e09ccfb78dff5f8db8c6b803e"
}
]
}
4 changes: 3 additions & 1 deletion apps/wallet/babel.config.js
Expand Up @@ -14,6 +14,8 @@ module.exports = {
modals: './src/modals',
stacks: './src/stacks',
screens: './src/screens',
kernel: './browser/kernel',
bridge: './browser/bridge',
crypto: 'react-native-quick-crypto',
path: './vendor/path',
zlib: './vendor/zlib',
Expand All @@ -25,6 +27,6 @@ module.exports = {
},
],
['react-native-reanimated/plugin'],
'@babel/plugin-proposal-export-namespace-from',
['@babel/plugin-proposal-export-namespace-from'],
],
};
8 changes: 2 additions & 6 deletions apps/wallet/browser/bridge/reply.ts
@@ -1,9 +1,5 @@
import {
Channels,
PopupType,
RequestType,
ResponseCode,
} from '@walless/messaging';
import { PopupType, RequestType, ResponseCode } from '@walless/core';
import { Channels } from '@walless/messaging';

import type { PayloadOptions, PopupPayload } from './utils';
import { encryptedMessenger } from './utils';
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/browser/bridge/request.ts
@@ -1,6 +1,6 @@
import { Timeout } from '@walless/core';
import { RequestType, Timeout } from '@walless/core';
import type { PureMessagePayload } from '@walless/messaging';
import { Channels, RequestType } from '@walless/messaging';
import { Channels } from '@walless/messaging';
import * as bs58 from 'bs58';

import type { PopupPayload } from './utils';
Expand Down
3 changes: 1 addition & 2 deletions apps/wallet/browser/bridge/utils.ts
Expand Up @@ -2,7 +2,7 @@ import { logger } from '@walless/core';
import type { PureMessagePayload } from '@walless/messaging';
import { createEncryptionKeyVault, createMessenger } from '@walless/messaging';
import type { SettingDocument } from '@walless/store';
import { storage } from 'utils/storage';
import { storage } from 'utils/storage/db';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bridge is used for web app communication, can we use a separate implementation to decouple code from another place?


export interface PayloadOptions {
sourceRequestId: string;
Expand All @@ -13,7 +13,6 @@ export interface PayloadOptions {
export type PopupPayload = PureMessagePayload & PayloadOptions;

export const encryptionKeyVault = createEncryptionKeyVault(storage);

export const encryptedMessenger = createMessenger(encryptionKeyVault);

export const launchSignInTab = async () => {
Expand Down
6 changes: 3 additions & 3 deletions apps/wallet/browser/bundler/manifest.json
Expand Up @@ -8,9 +8,9 @@
"extension_pages": "script-src 'self'; object-src 'self'; frame-ancestors 'none';"
},
"icons": {
"16": "app-icons/16.png",
"48": "app-icons/48.png",
"128": "app-icons/128.png"
"16": "favicon.png",
"48": "favicon.png",
"128": "favicon.png"
},
"action": {
"default_popup": "popup.html",
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/browser/content/injection.ts
@@ -1,5 +1,5 @@
import { logger } from '@walless/core';
import Walless from '@walless/sdk';
import { Walless } from '@walless/sdk';
import { initialize } from '@walless/wallet-standard';

const configureWalletStandard = async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/browser/kernel/handlers/aptos.ts
@@ -1,4 +1,4 @@
import { ResponseCode } from '@walless/messaging';
import { ResponseCode } from '@walless/core';
import { aptosHandler } from '@walless/network';
import { Network, Provider } from 'aptos';

Expand Down
6 changes: 3 additions & 3 deletions apps/wallet/browser/kernel/handlers/common.ts
@@ -1,16 +1,16 @@
import type { ConnectOptions } from '@walless/core';
import { Networks } from '@walless/core';
import { ResponseCode } from '@walless/messaging';
import { ResponseCode } from '@walless/core';
import type { ConnectOptions } from '@walless/sdk';
import type { PublicKeyDocument, TrustedDomainDocument } from '@walless/store';
import { selectors } from '@walless/store';
import { storage } from 'utils/storage/db';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if we import utils/storagein browser. For kernel only, I created a storage instance in apps/wallet/browser/kernel/utils/storage.ts. I think we should keep platforms or environments separately


import {
addExtensionsById,
checkInstalledExtensionById,
} from '../utils/helper';
import { openPopup } from '../utils/popup';
import { getRequestRecord, respond } from '../utils/requestPool';
import { storage } from '../utils/storage';
import type { HandleMethod } from '../utils/types';

export const connect: HandleMethod<{ options?: ConnectOptions }> = async ({
Expand Down
3 changes: 1 addition & 2 deletions apps/wallet/browser/kernel/handlers/kernel.ts
@@ -1,6 +1,5 @@
import { Networks } from '@walless/core';
import { Networks, PopupType, RequestType, ResponseCode } from '@walless/core';
import type { MessengerCallback } from '@walless/messaging';
import { PopupType, RequestType, ResponseCode } from '@walless/messaging';

import { handle } from '../utils/handle';
import {
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/browser/kernel/handlers/solana.ts
@@ -1,5 +1,5 @@
import { clusterApiUrl, Connection } from '@solana/web3.js';
import { ResponseCode } from '@walless/messaging';
import { ResponseCode } from '@walless/core';
import { solanaHandler } from '@walless/network';
import { environment } from 'utils/config';

Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/browser/kernel/handlers/sui.ts
@@ -1,5 +1,5 @@
import { Connection, JsonRpcProvider } from '@mysten/sui.js';
import { ResponseCode } from '@walless/messaging';
import { ResponseCode } from '@walless/core';
import { suiHandler } from '@walless/network';

import { respond } from '../utils/requestPool';
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/browser/kernel/handlers/tezos.ts
@@ -1,5 +1,5 @@
import { TezosToolkit } from '@taquito/taquito';
import { ResponseCode } from '@walless/messaging';
import { ResponseCode } from '@walless/core';
import { tezosHandler } from '@walless/network';

import { respond } from '../utils/requestPool';
Expand Down
7 changes: 3 additions & 4 deletions apps/wallet/browser/kernel/index.ts
@@ -1,15 +1,14 @@
import { logger, runtime } from '@walless/core';
import { configure } from '@walless/store';
import { storage } from 'utils/storage/db';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as same as the above comment


import { keepBackgroundAlive } from './utils/extension';
import { initModules } from './utils/init';
import { initializeMessaging } from './messaging';
import { configurePWA } from './pwa';

logger.info('Initializing kernel..');

initModules().then(async () => {
await Promise.all([initializeMessaging()]);
});
configure(storage).then(initializeMessaging);

if (runtime.isExtension) {
keepBackgroundAlive();
Expand Down
114 changes: 0 additions & 114 deletions apps/wallet/browser/kernel/messaging.ts

This file was deleted.

84 changes: 84 additions & 0 deletions apps/wallet/browser/kernel/messaging/index.ext.ts
@@ -0,0 +1,84 @@
import {
PopupType,
ResponseCode,
ResponseMessage,
runtime,
} from '@walless/core';
import type {
EncryptedMessage,
MessagePayload,
MessengerCallback,
} from '@walless/messaging';
import { decryptMessage } from '@walless/messaging';

import { onKernelMessage } from '../handlers/kernel';
import { respond } from '../utils/requestPool';

import { encryptionKeyVault, initializeVaultKeys } from './shared';

/* Manually forward/coordinate communication using chrome.runtime.port messaging,
* included encryption support */
export const initializeMessaging = async (): Promise<void> => {
await initializeVaultKeys();

const callbackRegistry: Record<string, MessengerCallback> = {};

runtime.onConnect.addListener((port: chrome.runtime.Port) => {
const handleInComingMessage = async (
message: EncryptedMessage | MessagePayload,
) => {
const registeredCallback = callbackRegistry[port.name];
const isEncrypted = !!message?.iv;

if (registeredCallback) {
if (isEncrypted) {
const key = await encryptionKeyVault.get(port.name);
const decrypted = await decryptMessage(
message as EncryptedMessage,
key,
);
registeredCallback?.(decrypted, port);
} else {
registeredCallback?.(message as never, port);
}
}
};

const handleDisconnect = (port: chrome.runtime.Port) => {
if (port.name.includes('/')) {
const [id, requestId] = port.name.split('/');
if (id === PopupType.REQUEST_CONNECT_POPUP) {
try {
respond(requestId, ResponseCode.ERROR, {
error: ResponseMessage.REJECT_REQUEST_CONNECT,
});
} catch (error) {
return;
}
} else if (id === PopupType.SIGNATURE_POPUP) {
try {
respond(requestId, ResponseCode.ERROR, {
error: ResponseMessage.REJECT_COMMON_REQUEST,
});
} catch (error) {
return;
}
} else if (id === PopupType.REQUEST_INSTALL_LAYOUT_POPUP) {
try {
respond(requestId, ResponseCode.ERROR);
} catch (error) {
return;
}
}
}

port.onMessage.removeListener(handleInComingMessage);
port.onDisconnect.removeListener(handleDisconnect);
};

port.onMessage.addListener(handleInComingMessage);
port.onDisconnect.addListener(handleDisconnect);
});

callbackRegistry.kernel = onKernelMessage;
};
10 changes: 10 additions & 0 deletions apps/wallet/browser/kernel/messaging/index.ts
@@ -0,0 +1,10 @@
import { createMessenger } from '@walless/messaging';

import { onKernelMessage } from '../handlers/kernel';

/* quite similar to Browser/PWA version,
* but Mobile messaging talk to itself, no encryption ever needed! */
export const initializeMessaging = async (): Promise<void> => {
const messenger = createMessenger();
messenger.onMessage('kernel', onKernelMessage);
};
13 changes: 13 additions & 0 deletions apps/wallet/browser/kernel/messaging/index.web.ts
@@ -0,0 +1,13 @@
import { createMessenger } from '@walless/messaging';

import { onKernelMessage } from '../handlers/kernel';

import { encryptionKeyVault, initializeVaultKeys } from './shared';

/* Mostly use messaging module to communicate,
* support encrypted communication */
export const initializeMessaging = async (): Promise<void> => {
await initializeVaultKeys();
const encryptedMessenger = createMessenger(encryptionKeyVault);
encryptedMessenger.onMessage('kernel', onKernelMessage);
};