-
Notifications
You must be signed in to change notification settings - Fork 306
/
wallet.ts
36 lines (32 loc) · 1.06 KB
/
wallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import './prepare.js'
import type { api } from '@dimensiondev/mask-wallet-core/proto'
// How to interact with this worker:
export type Input = {
id: number
data: api.IMWRequest
}
export type Output = {
id: number
response: api.MWResponse
}
const promise = (async () => {
const { request } = await import('@dimensiondev/mask-wallet-core/bundle')
const { api } = await import('@dimensiondev/mask-wallet-core/proto')
return { request, api }
})()
self.addEventListener('message', async (ev: MessageEvent) => {
const { api, request } = await promise
const { id, data } = ev.data as Input
if (!id) return
try {
const payload = api.MWRequest.encode(data).finish()
const wasmResult = request(payload)
const response = api.MWResponse.decode(wasmResult)
self.postMessage({ id, response })
} catch (error) {
const out: Output = { id, response: { error: { errorMsg: String(error) } } }
self.postMessage(out)
}
})
// see: packages/shared-base/src/onDemandWorker/index.ts
self.postMessage('Alive')