Skip to content

Commit

Permalink
chore: improve typings from desktop folder (#2182)
Browse files Browse the repository at this point in the history
* chore: update moduleResolution

* fix: type issues in desktop
  • Loading branch information
Tuditi authored Mar 20, 2024
1 parent 0b7b156 commit eecd944
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 131 deletions.
47 changes: 28 additions & 19 deletions packages/desktop/lib/electron/apis/wallet.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import * as IotaSdk from '@iota/sdk'
import type { CreateAccountPayload, IAuth, SyncOptions } from '@iota/sdk'
import {
Wallet,
WalletOptions,
Account,
Client,
migrateStrongholdSnapshotV2ToV3,
SecretManager,
SyncOptions,
CreateAccountPayload,
IAuth,
} from '@iota/sdk'

import { bindMethodsAcrossContextBridge, bindSdkUtilsMethods } from '../utils/context-bridge.utils'
import { STRONGHOLD_V2_HASHING_ROUNDS, STRONGHOLD_V2_SALT } from '../constants/stronghold-v2-migration.constants'
Expand All @@ -12,7 +21,7 @@ interface PayloadType {
syncOptions: SyncOptions
}

const profileManagers: { [id: string]: IotaSdk.Wallet } = {}
const profileManagers: { [id: string]: Wallet } = {}

const sdkUtilsMethods = bindSdkUtilsMethods()

Expand All @@ -30,59 +39,59 @@ export default {
nodeInfo,
}
},
createWallet(id: string, options: unknown): IotaSdk.Wallet {
const manager = new IotaSdk.Wallet(options)
createWallet(id: string, options: WalletOptions): Wallet {
const manager = new Wallet(options)
manager['id'] = id
profileManagers[id] = manager
return bindMethodsAcrossContextBridge(IotaSdk.Wallet, manager)
return bindMethodsAcrossContextBridge(Wallet, manager)
},
async createAccount(managerId: string, payload: CreateAccountPayload): Promise<IotaSdk.Account> {
async createAccount(managerId: string, payload: CreateAccountPayload): Promise<Account> {
const manager = profileManagers[managerId]
const account = await manager.createAccount(payload)
return bindMethodsAcrossContextBridge(IotaSdk.Account, account)
return bindMethodsAcrossContextBridge(Account, account)
},
deleteWallet(id: string): void {
if (id && id in profileManagers) {
delete profileManagers[id]
}
},
async getAccount(managerId: string, index: number): Promise<IotaSdk.Account> {
async getAccount(managerId: string, index: number): Promise<Account> {
const manager = profileManagers[managerId]
const account = await manager.getAccount(index)
return bindMethodsAcrossContextBridge(IotaSdk.Account, account)
return bindMethodsAcrossContextBridge(Account, account)
},
async getAccounts(managerId: string): Promise<IotaSdk.Account[]> {
async getAccounts(managerId: string): Promise<Account[]> {
const manager = profileManagers[managerId]
const accounts = await manager.getAccounts()
return accounts.map((account) => bindMethodsAcrossContextBridge(IotaSdk.Account, account))
return accounts.map((account) => bindMethodsAcrossContextBridge(Account, account))
},
async getClient(managerId: string): Promise<IotaSdk.Client> {
async getClient(managerId: string): Promise<Client> {
const manager = profileManagers[managerId]
const client = await manager.getClient()
return bindMethodsAcrossContextBridge(IotaSdk.Client, client)
return bindMethodsAcrossContextBridge(Client, client)
},
async recoverAccounts(managerId: string, payload: PayloadType): Promise<IotaSdk.Account[]> {
async recoverAccounts(managerId: string, payload: PayloadType): Promise<Account[]> {
const manager = profileManagers[managerId]
const accounts = await manager.recoverAccounts(
payload.accountStartIndex,
payload.accountGapLimit,
payload.addressGapLimit,
payload.syncOptions
)
return accounts.map((account) => bindMethodsAcrossContextBridge(IotaSdk.Account, account))
return accounts.map((account) => bindMethodsAcrossContextBridge(Account, account))
},
async getSecretManager(managerId: string): Promise<IotaSdk.SecretManager> {
async getSecretManager(managerId: string): Promise<SecretManager> {
const manager = profileManagers[managerId]
const secretManager = await manager.getSecretManager()
return bindMethodsAcrossContextBridge(IotaSdk.SecretManager, secretManager)
return bindMethodsAcrossContextBridge(SecretManager, secretManager)
},
migrateStrongholdSnapshotV2ToV3(
currentPath: string,
newPath: string,
currentPassword: string,
newPassword: string
): unknown {
return IotaSdk.migrateStrongholdSnapshotV2ToV3(
return migrateStrongholdSnapshotV2ToV3(
currentPath,
newPath,
STRONGHOLD_V2_SALT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { windows } from '../constants/windows.constant'
export function registerPowerMonitorListeners(): void {
powerMonitor.on('suspend', () => {
// MacOS, Windows and Linux
windows?.main?.webContents?.send('lock-screen')
windows.main?.webContents?.send('lock-screen')
})
powerMonitor.on('lock-screen', () => {
// MacOS and Windows
windows?.main?.webContents?.send('lock-screen')
windows.main?.webContents?.send('lock-screen')
})
}
44 changes: 25 additions & 19 deletions packages/desktop/lib/electron/managers/transak.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { TRANSAK_WIDGET_URL } from '@auxiliary/transak/constants'
import { buildUrl } from '@core/utils/url'
import { MarketCurrency } from '@core/market/enums/market-currency.enum'
import fs from 'fs'
import { IError } from '@core/error'
import { QueryParameters } from '@core/utils'

export default class TransakManager implements ITransakManager {
private rect: Electron.Rectangle
private rect: Electron.Rectangle | undefined

private getPreloadPath(): string {
const preloadPath = app.isPackaged
Expand Down Expand Up @@ -54,13 +56,13 @@ export default class TransakManager implements ITransakManager {
windows.transak?.show()
}

public openWindow(data: ITransakWindowData): BrowserWindow {
public openWindow(data: ITransakWindowData): BrowserWindow | undefined {
let preloadPath: string
try {
preloadPath = this.getPreloadPath()
} catch (err) {
windows.main.webContents.send('transak-not-loaded')
console.error(err.message)
windows.main?.webContents?.send?.('transak-not-loaded')
console.error((err as IError).message)
return
}

Expand All @@ -71,7 +73,7 @@ export default class TransakManager implements ITransakManager {
this.rect = { x: 0, y: 0, width: 480, height: 613 }

windows.transak = new BrowserWindow({
parent: windows.main,
...(windows.main && { parent: windows.main }),
width: 480,
height: 613,
useContentSize: true,
Expand Down Expand Up @@ -104,7 +106,7 @@ export default class TransakManager implements ITransakManager {
windows.transak.setWindowButtonVisibility(false)
}

windows.main.on('move', () => this.positionWindow())
windows.main?.on?.('move', () => this.positionWindow())

windows.transak.once('closed', () => {
windows.transak = null
Expand All @@ -121,10 +123,10 @@ export default class TransakManager implements ITransakManager {
let initialUrl: string
try {
initialUrl = this.getUrl(data)
void windows.transak.loadURL(initialUrl)
} catch (err) {
console.error(err)
}
void windows.transak.loadURL(initialUrl)

windows.transak.webContents.setWindowOpenHandler(({ url }) => {
void shell.openExternal(url)
Expand All @@ -135,20 +137,20 @@ export default class TransakManager implements ITransakManager {

windows.transak.webContents.addListener('did-navigate', (_, url) => {
const _url = new URL(url)
windows.main.webContents.send('transak-url', _url.origin)
windows.main?.webContents?.send?.('transak-url', _url.origin)
})

windows.transak.webContents.addListener('did-navigate-in-page', (_, url) => {
const urlToBeMatched = TRANSAK_WIDGET_URL + '/googlepay'
if (url.startsWith(urlToBeMatched)) {
windows.main.webContents.send('try-open-url-in-browser', url)
void windows.transak.loadURL(initialUrl)
windows.main?.webContents?.send?.('try-open-url-in-browser', url)
void windows.transak?.loadURL?.(initialUrl)
}
})

windows.transak.webContents.addListener('will-navigate', (event) => {
event.preventDefault()
windows.main.webContents.send('try-open-url-in-browser', event.url)
windows.main?.webContents.send('try-open-url-in-browser', event.url)
})

return windows.transak
Expand All @@ -162,20 +164,20 @@ export default class TransakManager implements ITransakManager {
public positionWindow(): void {
try {
if (windows.transak) {
const [mainWindowX, mainWindowY] = windows.main.getPosition()
const [, mainWindowHeight] = windows.main.getSize()
const [, bodyHeight] = windows.main.getContentSize()
const [mainWindowX, mainWindowY] = windows.main?.getPosition() ?? []
const [, mainWindowHeight] = windows.main?.getSize() ?? []
const [, bodyHeight] = windows.main?.getContentSize() ?? []

const menuHeight = mainWindowHeight - bodyHeight

const newX = Math.floor(mainWindowX + this.rect.x)
const newY = Math.floor(mainWindowY + menuHeight + this.rect.y)
const newX = Math.floor(mainWindowX + (this.rect?.x ?? 0))
const newY = Math.floor(mainWindowY + menuHeight + (this.rect?.y ?? 0))

windows.transak.setBounds({
x: newX,
y: newY,
height: this.rect.height,
width: this.rect.width,
height: this.rect?.height,
width: this.rect?.width,
})

if (process.platform === 'linux') {
Expand Down Expand Up @@ -205,6 +207,10 @@ export default class TransakManager implements ITransakManager {
const { address, currency, service, amount } = data
const apiKey = process.env.TRANSAK_API_KEY

if (typeof apiKey !== 'string') {
throw new Error('Undefined Transak API key')
}

if (!Object.values(MarketCurrency).includes(currency as MarketCurrency)) {
throw new Error('Invalid Transak currency')
}
Expand All @@ -220,7 +226,7 @@ export default class TransakManager implements ITransakManager {
: 'LIGHT'
: nativeTheme.themeSource.toUpperCase()

const queryParams = {
const queryParams: QueryParameters = {
apiKey,
defaultFiatCurrency: currency,
defaultFiatAmount: amount,
Expand Down
6 changes: 3 additions & 3 deletions packages/desktop/lib/electron/menus/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function toggleMaximize(): boolean {
}

export function initMenu(): void {
let mainMenu = null
let mainMenu: Electron.Menu

app.once('ready', () => {
ipcMain.handle('menu-update', (e, args) => {
Expand All @@ -50,7 +50,7 @@ export function initMenu(): void {
})

ipcMain.handle('menu-popup', () => {
mainMenu.popup(getOrInitWindow('main'))
mainMenu.popup({ window: getOrInitWindow('main') })
})
ipcMain.handle('menu-data', () => state)
handleWindowControls()
Expand Down Expand Up @@ -142,7 +142,7 @@ function getFirstSubmenuItems(): Electron.MenuItemConstructorOptions[] {
function getDarwinSubmenuItems(): Electron.MenuItemConstructorOptions[] {
return [
roleMenuItem(`${state.strings.hide} ${app.name}`, 'hide'),
roleMenuItem(state.strings.hideOthers, 'hideothers' as Electron.MenuItemConstructorOptions['role']),
roleMenuItem(state.strings.hideOthers, 'hideOthers'),
roleMenuItem(state.strings.showAll, 'unhide'),
]
}
7 changes: 5 additions & 2 deletions packages/desktop/lib/electron/preloads/transak.preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
window.addEventListener('DOMContentLoaded', () => {
window.document.getElementById('appRoot').style.borderRadius = '16px'
window.document.getElementById('appRoot').style.overflow = 'hidden'
const appRoot = window.document.getElementById('appRoot')
if (appRoot) {
appRoot.style.borderRadius = '16px'
appRoot.style.overflow = 'hidden'
}
})
2 changes: 1 addition & 1 deletion packages/desktop/lib/electron/processes/ledger.process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function messageHandler(message: ILedgerProcessMessage): Promise<void> {
try {
await openTransport()

let data: LedgerApiRequestResponse
let data: LedgerApiRequestResponse | undefined
const { method, payload } = message
switch (method) {
case LedgerApiMethod.GenerateEvmAddress: {
Expand Down
Loading

0 comments on commit eecd944

Please sign in to comment.