-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.ts
83 lines (74 loc) · 2.66 KB
/
preload.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { contextBridge } from 'electron'
import { ipcRenderer } from 'electron'
import type {
ApiFileStoreService,
ApiKeystore,
ApiUrl,
AppUpdateRD,
IPCExportKeystoreParams,
StoreFileData,
StoreFileName
} from '../shared/api/types'
import { getStoreFilesIPCMessages } from '../shared/ipc/fileStore'
import { apiHDWallet } from './api/hdwallet'
import { apiLang } from './api/lang'
import IPCMessages from './ipc/messages'
// ContextBridge is used here to expose custom api objects on `window`
// to be accessable at `renderer` processes,
// even if `contextIsolation` is enabled
// https://www.electronjs.org/docs/api/context-bridge
/**
* When exposing anything to the real world do not forget to
* declare appropriate types for global Window interface
* at the src/shared/api/types.ts
*/
//
// `apiKeystore` object
//
const apiKeystore: ApiKeystore = {
// Note: `params` need to be encoded by `ipcKeystoreWalletsIO` before calling `saveKeystoreWallets` */
saveKeystoreWallets: (params: unknown) => ipcRenderer.invoke(IPCMessages.SAVE_KEYSTORE_WALLETS, params),
exportKeystore: (params: IPCExportKeystoreParams) => ipcRenderer.invoke(IPCMessages.EXPORT_KEYSTORE, params),
load: () => ipcRenderer.invoke(IPCMessages.LOAD_KEYSTORE),
initKeystoreWallets: () => ipcRenderer.invoke(IPCMessages.INIT_KEYSTORE_WALLETS)
}
contextBridge.exposeInMainWorld('apiKeystore', apiKeystore)
//
// `apiLang` object
//
contextBridge.exposeInMainWorld('apiLang', apiLang)
//
// `apiUrl` object
//
const apiUrl: ApiUrl = {
openExternal: (url: string) => ipcRenderer.invoke(IPCMessages.OPEN_EXTERNAL_URL, url)
}
contextBridge.exposeInMainWorld('apiUrl', apiUrl)
//
// `apiHDWallet` object
//
contextBridge.exposeInMainWorld('apiHDWallet', apiHDWallet)
//
// api for storage objects
//
const getFileStoreApi = <FileName extends StoreFileName>(
storeFileName: FileName
): ApiFileStoreService<StoreFileData<FileName>> => {
const ipcMessages = getStoreFilesIPCMessages(storeFileName)
return {
save: (data) => ipcRenderer.invoke(ipcMessages.SAVE_FILE, data),
remove: () => ipcRenderer.invoke(ipcMessages.REMOVE_FILE),
get: () => ipcRenderer.invoke(ipcMessages.GET_FILE),
exists: () => ipcRenderer.invoke(ipcMessages.FILE_EXIST)
}
}
contextBridge.exposeInMainWorld('apiCommonStorage', getFileStoreApi('common'))
contextBridge.exposeInMainWorld('apiUserNodesStorage', getFileStoreApi('userNodes'))
contextBridge.exposeInMainWorld('apiPoolsStorage', getFileStoreApi('pools'))
//
// api for update
//
const apiAppUpdate = {
checkForAppUpdates: (): Promise<AppUpdateRD> => ipcRenderer.invoke(IPCMessages.APP_CHECK_FOR_UPDATE)
}
contextBridge.exposeInMainWorld('apiAppUpdate', apiAppUpdate)