From 27a111466af3b43efd17093c92bd109ab297f8c2 Mon Sep 17 00:00:00 2001 From: kieran Date: Thu, 11 Apr 2024 12:30:12 +0000 Subject: [PATCH] chore: Update translations --- .../app/src/Pages/settings/wallet/Cashu.tsx | 14 +- packages/app/src/Pages/wallet/index.tsx | 2 +- packages/app/src/Wallet/index.ts | 287 +++++++++--------- packages/wallet/src/NostrWalletConnect.ts | 16 +- packages/wallet/src/custom.d.ts | 2 +- packages/wallet/src/index.ts | 6 +- 6 files changed, 162 insertions(+), 165 deletions(-) diff --git a/packages/app/src/Pages/settings/wallet/Cashu.tsx b/packages/app/src/Pages/settings/wallet/Cashu.tsx index 3a0153c8..820d786a 100644 --- a/packages/app/src/Pages/settings/wallet/Cashu.tsx +++ b/packages/app/src/Pages/settings/wallet/Cashu.tsx @@ -20,14 +20,12 @@ const ConnectCashu = () => { throw new Error("Mint URL is required"); } - const connection = new CashuWallet( - { - url: config, - keys: {}, - proofs: [], - keysets: [], - }, - ); + const connection = new CashuWallet({ + url: config, + keys: {}, + proofs: [], + keysets: [], + }); await connection.login(); const info = await connection.getInfo(); const newWallet = { diff --git a/packages/app/src/Pages/wallet/index.tsx b/packages/app/src/Pages/wallet/index.tsx index bf525270..fb45d33f 100644 --- a/packages/app/src/Pages/wallet/index.tsx +++ b/packages/app/src/Pages/wallet/index.tsx @@ -1,5 +1,5 @@ /* eslint-disable max-lines */ -import { LNWallet,Sats, WalletInvoice } from "@snort/wallet"; +import { LNWallet, Sats, WalletInvoice } from "@snort/wallet"; import classNames from "classnames"; import { useEffect, useState } from "react"; import { FormattedMessage, FormattedNumber, useIntl } from "react-intl"; diff --git a/packages/app/src/Wallet/index.ts b/packages/app/src/Wallet/index.ts index b5578de1..faf6e178 100644 --- a/packages/app/src/Wallet/index.ts +++ b/packages/app/src/Wallet/index.ts @@ -3,177 +3,176 @@ import { LNWallet, loadWallet, WalletInfo, WalletKind } from "@snort/wallet"; import { useEffect, useSyncExternalStore } from "react"; export interface WalletConfig { - id: string; - kind: WalletKind; - active: boolean; - info: WalletInfo; + id: string; + kind: WalletKind; + active: boolean; + info: WalletInfo; - /** - * Opaque string for wallet config - */ - data?: string; + /** + * Opaque string for wallet config + */ + data?: string; } export interface WalletStoreSnapshot { - configs: Array; - config?: WalletConfig; - wallet?: LNWallet; + configs: Array; + config?: WalletConfig; + wallet?: LNWallet; } export class WalletStore extends ExternalStore { - #configs: Array; - #instance: Map; + #configs: Array; + #instance: Map; - constructor() { - super(); - this.#configs = []; - this.#instance = new Map(); - this.load(false); - this.notifyChange(); + constructor() { + super(); + this.#configs = []; + this.#instance = new Map(); + this.load(false); + this.notifyChange(); + } + + list() { + return Object.freeze([...this.#configs]); + } + + get() { + const activeConfig = this.#configs.find(a => a.active); + if (!activeConfig) { + if (this.#configs.length === 0) { + return undefined; + } + throw new Error("No active wallet config"); } - - list() { - return Object.freeze([...this.#configs]); - } - - get() { - const activeConfig = this.#configs.find(a => a.active); - if (!activeConfig) { - if (this.#configs.length === 0) { - return undefined; - } - throw new Error("No active wallet config"); - } - if (this.#instance.has(activeConfig.id)) { - return unwrap(this.#instance.get(activeConfig.id)); - } else { - const w = this.#activateWallet(activeConfig); - if (w) { - if ("then" in w) { - w.then(async wx => { - this.#instance.set(activeConfig.id, wx); - this.notifyChange(); - }); - return undefined; - } else { - this.#instance.set(activeConfig.id, w); - this.notifyChange(); - } - return w; - } else { - throw new Error("Unable to activate wallet config"); - } - } - } - - add(cfg: WalletConfig) { - this.#configs.push(cfg); - this.save(); - } - - remove(id: string) { - const idx = this.#configs.findIndex(a => a.id === id); - if (idx === -1) { - throw new Error("Wallet not found"); - } - const [removed] = this.#configs.splice(idx, 1); - if (removed.active && this.#configs.length > 0) { - this.#configs[0].active = true; - } - this.save(); - } - - switch(id: string) { - this.#configs.forEach(a => (a.active = a.id === id)); - this.save(); - } - - save() { - const json = JSON.stringify(this.#configs); - window.localStorage.setItem("wallet-config", json); - this.notifyChange(); - } - - load(snapshot = true) { - const cfg = window.localStorage.getItem("wallet-config"); - if (cfg) { - this.#configs = JSON.parse(cfg); - } - if (snapshot) { + if (this.#instance.has(activeConfig.id)) { + return unwrap(this.#instance.get(activeConfig.id)); + } else { + const w = this.#activateWallet(activeConfig); + if (w) { + if ("then" in w) { + w.then(async wx => { + this.#instance.set(activeConfig.id, wx); this.notifyChange(); - } - } - - free() { - this.#instance.forEach(w => w.close()); - } - - takeSnapshot(): WalletStoreSnapshot { - return { - configs: [...this.#configs], - config: this.#configs.find(a => a.active), - wallet: this.get(), - } as WalletStoreSnapshot; - } - - #activateWallet(cfg: WalletConfig): LNWallet | Promise | undefined { - const w = loadWallet(cfg.kind, cfg.data); - if (w) { - w.on("change", d => this.#onWalletChange(cfg, d)); + }); + return undefined; + } else { + this.#instance.set(activeConfig.id, w); + this.notifyChange(); } return w; + } else { + throw new Error("Unable to activate wallet config"); + } } + } - #onWalletChange(cfg: WalletConfig, data?: string) { - if (data) { - const activeConfig = this.#configs.find(a => a.id === cfg.id); - if (activeConfig) { - activeConfig.data = data; - } - this.save(); - } else { - this.notifyChange(); - } + add(cfg: WalletConfig) { + this.#configs.push(cfg); + this.save(); + } + + remove(id: string) { + const idx = this.#configs.findIndex(a => a.id === id); + if (idx === -1) { + throw new Error("Wallet not found"); } + const [removed] = this.#configs.splice(idx, 1); + if (removed.active && this.#configs.length > 0) { + this.#configs[0].active = true; + } + this.save(); + } + + switch(id: string) { + this.#configs.forEach(a => (a.active = a.id === id)); + this.save(); + } + + save() { + const json = JSON.stringify(this.#configs); + window.localStorage.setItem("wallet-config", json); + this.notifyChange(); + } + + load(snapshot = true) { + const cfg = window.localStorage.getItem("wallet-config"); + if (cfg) { + this.#configs = JSON.parse(cfg); + } + if (snapshot) { + this.notifyChange(); + } + } + + free() { + this.#instance.forEach(w => w.close()); + } + + takeSnapshot(): WalletStoreSnapshot { + return { + configs: [...this.#configs], + config: this.#configs.find(a => a.active), + wallet: this.get(), + } as WalletStoreSnapshot; + } + + #activateWallet(cfg: WalletConfig): LNWallet | Promise | undefined { + const w = loadWallet(cfg.kind, cfg.data); + if (w) { + w.on("change", d => this.#onWalletChange(cfg, d)); + } + return w; + } + + #onWalletChange(cfg: WalletConfig, data?: string) { + if (data) { + const activeConfig = this.#configs.find(a => a.id === cfg.id); + if (activeConfig) { + activeConfig.data = data; + } + this.save(); + } else { + this.notifyChange(); + } + } } export const Wallets = new WalletStore(); window.document.addEventListener("close", () => { - Wallets.free(); + Wallets.free(); }); export function useWallet() { - const wallet = useSyncExternalStore( - h => Wallets.hook(h), - () => Wallets.snapshot(), - ); - useEffect(() => { - if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) { - wallet.wallet.login().catch(console.error); - } - }, [wallet]); - return wallet; + const wallet = useSyncExternalStore( + h => Wallets.hook(h), + () => Wallets.snapshot(), + ); + useEffect(() => { + if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) { + wallet.wallet.login().catch(console.error); + } + }, [wallet]); + return wallet; } - /** * Adds a wallet config for WebLN if detected */ export function setupWebLNWalletConfig(store: WalletStore) { - const wallets = store.list(); + const wallets = store.list(); - const existing = wallets.find(a => a.kind === WalletKind.WebLN); - if (window.webln && !existing) { - const newConfig = { - id: "webln", - kind: WalletKind.WebLN, - active: wallets.length === 0, - info: { - alias: "WebLN", - }, - } as WalletConfig; - store.add(newConfig); - } else if (existing) { - store.remove(existing.id); - } + const existing = wallets.find(a => a.kind === WalletKind.WebLN); + if (window.webln && !existing) { + const newConfig = { + id: "webln", + kind: WalletKind.WebLN, + active: wallets.length === 0, + info: { + alias: "WebLN", + }, + } as WalletConfig; + store.add(newConfig); + } else if (existing) { + store.remove(existing.id); + } } diff --git a/packages/wallet/src/NostrWalletConnect.ts b/packages/wallet/src/NostrWalletConnect.ts index 25997b0c..fdddf8ff 100644 --- a/packages/wallet/src/NostrWalletConnect.ts +++ b/packages/wallet/src/NostrWalletConnect.ts @@ -31,14 +31,14 @@ interface WalletConnectResponse { result?: T; error?: { code: - | "RATE_LIMITED" - | "NOT_IMPLEMENTED" - | "INSUFFICIENT_BALANCE" - | "QUOTA_EXCEEDED" - | "RESTRICTED" - | "UNAUTHORIZED" - | "INTERNAL" - | "OTHER"; + | "RATE_LIMITED" + | "NOT_IMPLEMENTED" + | "INSUFFICIENT_BALANCE" + | "QUOTA_EXCEEDED" + | "RESTRICTED" + | "UNAUTHORIZED" + | "INTERNAL" + | "OTHER"; message: string; }; } diff --git a/packages/wallet/src/custom.d.ts b/packages/wallet/src/custom.d.ts index fb54ca1a..6c1c34bf 100644 --- a/packages/wallet/src/custom.d.ts +++ b/packages/wallet/src/custom.d.ts @@ -1 +1 @@ -/// \ No newline at end of file +/// diff --git a/packages/wallet/src/index.ts b/packages/wallet/src/index.ts index fa9d5b90..73f17611 100644 --- a/packages/wallet/src/index.ts +++ b/packages/wallet/src/index.ts @@ -104,7 +104,7 @@ export type Sats = number; export type MilliSats = number; export interface WalletEvents { - change: (data?: string) => void + change: (data?: string) => void; } export type LNWallet = EventEmitter & { @@ -122,7 +122,7 @@ export type LNWallet = EventEmitter & { canGetBalance: () => boolean; canCreateInvoice: () => boolean; canPayInvoice: () => boolean; -} +}; /** * Load wallet by kind @@ -152,4 +152,4 @@ export function loadWallet(kind: WalletKind, data: string | undefined) { } } -export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet } \ No newline at end of file +export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet };