@trezor/suite-desktop-api
Private package providing strongly typed DesktopApi used in @trezor/suite and @trezor/suite-desktop. DesktopApi handles inter-process comumunication inside Electron between the main context, i.e. native processes running in NodeJS, and renderer context, i.e. browser-like processes running on Chromium.
Exported modules:
main(default) used in@trezor/suite-desktop/srcin main (NodeJS) context.renderer(browser) used in@trezor/suiteand@trezor/suite-desktop-uiin renderer context.
export function getDesktopApi(ipcRenderer?: Electron.IpcRenderer): DesktopApi;
export const desktopApi: DesktopApi;
Usage examples in main process
-
Overload
electrontypes. See typed-electron.ts -
Create
DesktopApiinstance and expose it to rendererwindow.desktopApiobject. See preload.ts -
Receive invoke messages from renderer. See metadata module
-
Receive event messages from renderer. See theme module
-
Send event messages to renderer. See
mainWindow.webContents.sendin autoupdater module
Usage examples in renderer process
-
DesktopApi.invoke. See metadata module -
DesktopApi.on. See AutoUpdater component
How to add new method/channel
To invoke a method on the main process and return an asynchronous result to the renderer process
- add a channel to
./src/api.ts InvokeChannels - add a channel to validChannels in
./src/validation.ts - add a method to
./src/api.ts DesktopApiasDesktopApiInvoke<'your-new-channel'> - process incoming request in
@trezor/suite-desktop/src/modules/*usingipcMain.handle('your-new-channel', (arg?: string) => { return 1; }) - trigger it from
@trezor/suiteusingconst r = await desktopApi.yourNewFunction()
To receive an asynchronous event in renderer process
- add a channel to
./src/api.ts RendererChannels - add a channel to validChannels in
./src/validation.ts - set a listener in
@trezor/suiteusingawait desktopApi.on('your-new-channel', (payload) => {}) - trigger an event from
@trezor/suite-desktop/src/modules/*usingmainWindow.webContents.send('your-new-channel', { foo: 'bar' })
To receive an asynchronous event in main process
- add a channel to
./src/api.ts MainChannels - add a channel to validChannels in
./src/validation.ts - add a method to
./src/api.ts DesktopApiasDesktopApiSend<'your-new-channel'> - set a listener in
@trezor/suite-desktop/src/modules/*usingipcMain.on('your-new-channel', (_, { foo }) => {}) - trigger an event from
@trezor/suiteusingdesktopApi.yourNewFunction({ foo: 'bar' })