Skip to content

Commit

Permalink
Merge branch 'develop' into 1976-persist-verification-state-for-dapp
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuditi authored Apr 27, 2024
2 parents cf0026b + a9092a1 commit 77bbd04
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 93 deletions.
52 changes: 26 additions & 26 deletions firefly.diff

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions packages/desktop/lib/electron/managers/json-file.manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IError } from '@core/error/interfaces'
import { app } from 'electron'
import fs from 'fs'
import path from 'path'

export class JsonFileManager {
public static saveJsonToFile(filename: string, data: object): void {
try {
fs.writeFileSync(JsonFileManager.getFilePath(filename), JSON.stringify(data))
} catch (err) {
console.error(err)
}
}

public static loadJsonFromFile(filename: string): object | undefined {
try {
return JSON.parse(fs.readFileSync(JsonFileManager.getFilePath(filename)).toString())
} catch (err) {
if (!(err as IError).message?.includes('ENOENT')) {
console.error(err)
}
}
}

private static getFilePath(filename: string): string {
const userDataPath = app.getPath('userData')
return path.join(userDataPath, filename)
}
}
35 changes: 6 additions & 29 deletions packages/desktop/lib/electron/processes/main.process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { getMachineId } from '../utils/os.utils'
import { registerPowerMonitorListeners } from '../listeners'
import ThirdPartyAppManager from '../managers/third-party-profiles.manager'
import { ITransakWindowData } from '@core/app/interfaces'
import { IError } from '@core/error'
import { JsonFileManager } from '../managers/json-file.manager'

export let appIsReady = false

Expand Down Expand Up @@ -652,7 +652,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt
}

function setBounds(): void {
const settings = <ISettings>loadJsonConfig(settingsFilename)
const settings = <ISettings>JsonFileManager.loadJsonFromFile(settingsFilename)

if (settings && settings.windowState && settings.windowState[windowName]) {
windowState = settings.windowState[windowName]
Expand All @@ -666,13 +666,13 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt
windowState = window.getBounds() as IAppState
}

let settings = loadJsonConfig(settingsFilename) as ISettings
let settings = JsonFileManager.loadJsonFromFile(settingsFilename) as ISettings

settings = settings || <ISettings>{}
settings.windowState = settings.windowState || <IAppState>{}
settings.windowState[windowName] = windowState

saveJsonConfig(settingsFilename, settings)
JsonFileManager.saveJsonToFile(settingsFilename, settings)
}

function track(win: BrowserWindow): void {
Expand Down Expand Up @@ -712,37 +712,14 @@ interface IAppState {

function updateSettings(data: object): void {
const filename = 'settings.json'
const config = loadJsonConfig(filename)
const config = JsonFileManager.loadJsonFromFile(filename)

/**
* CAUTION: We must be careful saving properties to this file, as
* once we decide to save it there then it will be there forever
* even if the name changes later.
*/
saveJsonConfig(filename, { ...config, ...data })
}

function saveJsonConfig(filename: string, data: object): void {
try {
fs.writeFileSync(getJsonConfig(filename), JSON.stringify(data))
} catch (err) {
console.error(err)
}
}

function loadJsonConfig(filename: string): object | undefined {
try {
return JSON.parse(fs.readFileSync(getJsonConfig(filename)).toString())
} catch (err) {
if (!(err as IError).message?.includes('ENOENT')) {
console.error(err)
}
}
}

function getJsonConfig(filename: string): string {
const userDataPath = app.getPath('userData')
return path.join(userDataPath, filename)
JsonFileManager.saveJsonToFile(filename, { ...config, ...data })
}

export function updateAppVersionDetails(details: object): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DEFAULT_PERSISTED_PROFILE_OBJECT } from '@core/profile/constants'
import { IOnboardingProfile } from '../interfaces'
import { DEFAULT_L1_EVM_NETWORK_CONFIGURATION, SupportedNetworkId } from '@core/network/constants'
import features from '@features/features'
import { IPureEvmNetworkConfiguration, IStardustNetwork } from '@core/network/interfaces'
import { IPureEvmNetworkConfiguration } from '@core/network/interfaces'

export function convertOnboardingProfileToPersistedProfile(
onboardingProfile?: Partial<IOnboardingProfile>
Expand All @@ -26,7 +26,7 @@ export function convertOnboardingProfileToPersistedProfile(

return {
...structuredClone(DEFAULT_PERSISTED_PROFILE_OBJECT),
...{ network: onboardingProfile?.network as IStardustNetwork },
...{ network },
...(evmNetworks && { evmNetworks }),
...(onboardingProfile?.name && { name: onboardingProfile.name }),
...(onboardingProfile?.id && { id: onboardingProfile.id }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import { get } from 'svelte/store'
import { IBaseToken } from '@core/token/interfaces'
import { activeProfile, updateActiveProfile } from '@core/profile/stores'
import { NetworkNamespace } from '../enums'
import {
Expand All @@ -10,10 +9,9 @@ import {
IStardustNetwork,
IStardustNetworkMetadata,
} from '../interfaces'
import { addNetwork, networkStatus } from '../stores'
import { NetworkId, StardustNetworkId } from '../types'

import { IBaseToken } from '@core/token'
import { IscChain } from '../classes'
import { addChain, networkStatus, removeChain } from '../stores'
import { EvmNetworkId, StardustNetworkId } from '../types'

export class StardustNetwork implements IStardustNetwork {
public readonly id: StardustNetworkId
Expand All @@ -24,7 +22,7 @@ export class StardustNetwork implements IStardustNetwork {
public readonly bech32Hrp: string
public readonly protocol: IProtocol
public readonly baseToken: IBaseToken
public readonly chainConfigurations: IIscChainConfiguration[]
public iscChains: IscChain[]

constructor(persistedNetwork: IStardustNetworkMetadata) {
this.id = persistedNetwork.id
Expand All @@ -35,7 +33,12 @@ export class StardustNetwork implements IStardustNetwork {
this.networkName = persistedNetwork.protocol.networkName
this.protocol = persistedNetwork.protocol
this.baseToken = persistedNetwork.baseToken
this.chainConfigurations = persistedNetwork.chainConfigurations

this.iscChains = persistedNetwork.chainConfigurations
.map((chainConfiguration) => {
return new IscChain(chainConfiguration)
})
.filter(Boolean)
}

getStatus(): INetworkStatus {
Expand All @@ -50,28 +53,27 @@ export class StardustNetwork implements IStardustNetwork {
network.chainConfigurations.push(chainConfiguration)
updateActiveProfile({ network })

this.chainConfigurations.push(chainConfiguration)
addNetwork(chainConfiguration)
const iscChain = new IscChain(chainConfiguration)
this.iscChains.push(iscChain)
addChain(iscChain)
}
}

private isChainAlreadyAdded(chainConfiguration: IIscChainConfiguration): boolean {
return this.chainConfigurations.some((evmNetwork) => {
const hasSameName = evmNetwork.name === chainConfiguration.name
const hasSameId = evmNetwork.id === chainConfiguration.id
return this.iscChains.some(({ name, id }) => {
const hasSameName = name === chainConfiguration.name
const hasSameId = id === chainConfiguration.id
return hasSameName || hasSameId
})
}

editChain(networkId: NetworkId, payload: Partial<IIscChainConfiguration>): Promise<void> {
return Promise.resolve()
}

removeChain(networkId: NetworkId): void {
removeChain(networkId: EvmNetworkId): void {
const network = get(activeProfile).network
const newChains = network.chainConfigurations.filter(
(chainConfiguration) => chainConfiguration.id !== networkId
)
this.iscChains = this.iscChains.filter((chain) => chain.id === networkId)
removeChain(networkId)
updateActiveProfile({ network: { ...network, chainConfigurations: newChains } })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { NetworkNamespace } from '../enums'
import { NetworkId } from '../types'
import { INetworkStatus } from './network-status.interface'
import { IIscChainConfiguration } from './evm-network-configuration.interface'
import { IStardustNetworkMetadata } from '@core/network'
import { IscChain, IStardustNetworkMetadata } from '@core/network'

export interface IStardustNetwork extends IStardustNetworkMetadata {
export interface IStardustNetwork extends Omit<IStardustNetworkMetadata, 'chainConfigurations'> {
namespace: NetworkNamespace.Stardust
bech32Hrp: string

iscChains: IscChain[]

getStatus(): INetworkStatus

addChain(chainConfiguration: IIscChainConfiguration): void
editChain(networkId: NetworkId, payload: Partial<IIscChainConfiguration>): Promise<void>
removeChain(networkId: NetworkId): void
}
29 changes: 15 additions & 14 deletions packages/shared/src/lib/core/network/stores/networks.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import { activeProfile } from '@core/profile/stores'
import features from '@features/features'

import { IscChain, EvmNetwork, StardustNetwork } from '../classes'
import { IEvmNetwork, IIscChainConfiguration, IStardustNetwork } from '../interfaces'
import { Network, NetworkId } from '../types'
import { IEvmNetwork, IIscChain, IStardustNetwork } from '../interfaces'
import { EvmNetworkId, Network, NetworkId } from '../types'
import { EvmNetworkType, NetworkNamespace } from '../enums'

export const networks: Writable<Network[]> = writable([])

export function initializeNetworks(): void {
const profile = get(activeProfile)
let _networks: Network[] = []
const _networks: Network[] = []
if (profile?.network) {
const stardustNetwork = new StardustNetwork(profile.network)
const chains = profile.network.chainConfigurations
.map((chainConfiguration) => {
return new IscChain(chainConfiguration)
})
.filter(Boolean) as IEvmNetwork[]
_networks = [stardustNetwork, ...chains]
_networks.push(stardustNetwork, ...stardustNetwork.iscChains)
}

if (features.network.evmNetworks.enabled) {
Expand All @@ -32,20 +27,26 @@ export function initializeNetworks(): void {
networks.set(_networks)
}

export function addNetwork(chainConfiguration: IIscChainConfiguration): void {
const network = getNetwork(chainConfiguration.id)
export function getNetwork(networkId: NetworkId): Network | undefined {
return get(networks)?.find((network) => network.id === networkId)
}

export function addChain(chain: IIscChain): void {
const network = getNetwork(chain.id)
if (network) {
return
}

networks.update((networks) => {
networks.push(new IscChain(chainConfiguration))
networks.push(chain)
return networks
})
}

export function getNetwork(networkId: NetworkId): Network | undefined {
return get(networks)?.find((network) => network.id === networkId)
export function removeChain(chainId: EvmNetworkId): void {
networks.update((networks) => {
return networks.filter(({ id }) => id !== chainId)
})
}

export function getL1Network(): IStardustNetwork {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export function buildPersistedNetworkFromNodeInfoResponse(
coinType: _coinType,
protocol: nodeInfoResponse?.nodeInfo?.protocol,
baseToken: { standard: TokenStandard.BaseToken, ...nodeInfoResponse?.nodeInfo?.baseToken },
chainConfigurations: chainConfigurations,
chainConfigurations,
}
}

0 comments on commit 77bbd04

Please sign in to comment.