Skip to content

Commit

Permalink
feat: initial values for wallets and providers
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Oct 15, 2024
1 parent 2de747c commit 8ba5150
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
17 changes: 17 additions & 0 deletions background/server/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SetSettingStateParams } from "../types/api"

export const StorageKeys = [
"InitWalletDefault",
"WalletCurrent",
"WalletList",
"ProviderCurrent",
Expand All @@ -10,6 +11,22 @@ export const StorageKeys = [
export type StorageKey = (typeof StorageKeys)[number]

export namespace Storage {
export const readBoolean = async (
key: StorageKey,
): Promise<boolean | undefined> => {
const value = await chrome.storage.local.get(key)
if (value && (value[key] as boolean)) {
return value[key] as boolean
}
}

export const writeBoolean = async (
key: StorageKey,
value: boolean,
): Promise<void> => {
await chrome.storage.local.set({ [key]: value })
}

export const readString = async (
key: StorageKey,
): Promise<string | undefined> => {
Expand Down
52 changes: 46 additions & 6 deletions background/server/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,52 @@ class IServerWallet implements Wallet, ServerHandlers {
this.providerCurrent = undefined
}

constructor() {
this.readWalletList()
this.readWalletCurrent()
this.readProviderList()
this.readProviderCurrent()
private async setInitialValues() {
const defaultWallets: Map<string, string> = new Map([
[
"Test1",
"source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast",
],
])
const defaultProviders: Map<string, string> = new Map([
["Portal Loop", "https://rpc.gno.land:443"],
["Portal Loop (WebSocket)", "wss://rpc.gno.land:443/websocket"],
["Test4", "https://rpc.test4.gno.land:443"],
["Test4 (WebSocket)", "wss://rpc.test4.gno.land:443/websocket"],
["Test3", "https://rpc.test3.gno.land:443"],
["Test3 (WebSocket)", "wss://rpc.test3.gno.land:443/websocket"],
["Staging", "https://rpc.staging.gno.land:443"],
["Staging (WebSocket)", "wss://rpc.staging.gno.land:443/websocket"],
])

await Storage.writeMapString("WalletList", defaultWallets)
await Storage.writeMapString("ProviderList", defaultProviders)
await Storage.writeString(
"WalletCurrent",
defaultWallets.keys().next().value,
)
await Storage.writeString(
"ProviderCurrent",
defaultProviders.keys().next().value,
)
}

async init() {
if ((await Storage.readBoolean("InitWalletDefault")) != true) {
await this.setInitialValues()
await Storage.writeBoolean("InitWalletDefault", true)
}

await this.readWalletList()
await this.readWalletCurrent()
await this.readProviderList()
await this.readProviderCurrent()

this.connectToProvider()
}
}

export const ServerWallet = new IServerWallet()
const asyncContructor = new IServerWallet()
await asyncContructor.init()

export const ServerWallet = asyncContructor

0 comments on commit 8ba5150

Please sign in to comment.