-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from all commits
81bcf37
c8cb705
1efd138
3843c66
97b7348
bacb967
ef8268a
8a29817
36fab3a
82ba471
6db4671
6118730
3916dac
e7198a1
02ae5ef
73ecc0e
17f22bf
d348056
59d11e5
756fb2f
4b3ecb1
b102077
a75fde2
38716fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if we import |
||
|
||
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 ({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { logger, runtime } from '@walless/core'; | ||
import { configure } from '@walless/store'; | ||
import { storage } from 'utils/storage/db'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
There was a problem hiding this comment.
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?