chore: Update translations
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
kieran 2024-04-11 12:30:12 +00:00
parent 8137317bfe
commit 27a111466a
6 changed files with 162 additions and 165 deletions

View File

@ -20,14 +20,12 @@ const ConnectCashu = () => {
throw new Error("Mint URL is required"); throw new Error("Mint URL is required");
} }
const connection = new CashuWallet( const connection = new CashuWallet({
{ url: config,
url: config, keys: {},
keys: {}, proofs: [],
proofs: [], keysets: [],
keysets: [], });
},
);
await connection.login(); await connection.login();
const info = await connection.getInfo(); const info = await connection.getInfo();
const newWallet = { const newWallet = {

View File

@ -1,5 +1,5 @@
/* eslint-disable max-lines */ /* eslint-disable max-lines */
import { LNWallet,Sats, WalletInvoice } from "@snort/wallet"; import { LNWallet, Sats, WalletInvoice } from "@snort/wallet";
import classNames from "classnames"; import classNames from "classnames";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { FormattedMessage, FormattedNumber, useIntl } from "react-intl"; import { FormattedMessage, FormattedNumber, useIntl } from "react-intl";

View File

@ -3,177 +3,176 @@ import { LNWallet, loadWallet, WalletInfo, WalletKind } from "@snort/wallet";
import { useEffect, useSyncExternalStore } from "react"; import { useEffect, useSyncExternalStore } from "react";
export interface WalletConfig { export interface WalletConfig {
id: string; id: string;
kind: WalletKind; kind: WalletKind;
active: boolean; active: boolean;
info: WalletInfo; info: WalletInfo;
/** /**
* Opaque string for wallet config * Opaque string for wallet config
*/ */
data?: string; data?: string;
} }
export interface WalletStoreSnapshot { export interface WalletStoreSnapshot {
configs: Array<WalletConfig>; configs: Array<WalletConfig>;
config?: WalletConfig; config?: WalletConfig;
wallet?: LNWallet; wallet?: LNWallet;
} }
export class WalletStore extends ExternalStore<WalletStoreSnapshot> { export class WalletStore extends ExternalStore<WalletStoreSnapshot> {
#configs: Array<WalletConfig>; #configs: Array<WalletConfig>;
#instance: Map<string, LNWallet>; #instance: Map<string, LNWallet>;
constructor() { constructor() {
super(); super();
this.#configs = []; this.#configs = [];
this.#instance = new Map(); this.#instance = new Map();
this.load(false); this.load(false);
this.notifyChange(); 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");
} }
if (this.#instance.has(activeConfig.id)) {
list() { return unwrap(this.#instance.get(activeConfig.id));
return Object.freeze([...this.#configs]); } else {
} const w = this.#activateWallet(activeConfig);
if (w) {
get() { if ("then" in w) {
const activeConfig = this.#configs.find(a => a.active); w.then(async wx => {
if (!activeConfig) { this.#instance.set(activeConfig.id, wx);
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) {
this.notifyChange(); this.notifyChange();
} });
} return undefined;
} else {
free() { this.#instance.set(activeConfig.id, w);
this.#instance.forEach(w => w.close()); this.notifyChange();
}
takeSnapshot(): WalletStoreSnapshot {
return {
configs: [...this.#configs],
config: this.#configs.find(a => a.active),
wallet: this.get(),
} as WalletStoreSnapshot;
}
#activateWallet(cfg: WalletConfig): LNWallet | Promise<LNWallet> | undefined {
const w = loadWallet(cfg.kind, cfg.data);
if (w) {
w.on("change", d => this.#onWalletChange(cfg, d));
} }
return w; return w;
} else {
throw new Error("Unable to activate wallet config");
}
} }
}
#onWalletChange(cfg: WalletConfig, data?: string) { add(cfg: WalletConfig) {
if (data) { this.#configs.push(cfg);
const activeConfig = this.#configs.find(a => a.id === cfg.id); this.save();
if (activeConfig) { }
activeConfig.data = data;
} remove(id: string) {
this.save(); const idx = this.#configs.findIndex(a => a.id === id);
} else { if (idx === -1) {
this.notifyChange(); 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<LNWallet> | 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(); export const Wallets = new WalletStore();
window.document.addEventListener("close", () => { window.document.addEventListener("close", () => {
Wallets.free(); Wallets.free();
}); });
export function useWallet() { export function useWallet() {
const wallet = useSyncExternalStore<WalletStoreSnapshot>( const wallet = useSyncExternalStore<WalletStoreSnapshot>(
h => Wallets.hook(h), h => Wallets.hook(h),
() => Wallets.snapshot(), () => Wallets.snapshot(),
); );
useEffect(() => { useEffect(() => {
if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) { if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) {
wallet.wallet.login().catch(console.error); wallet.wallet.login().catch(console.error);
} }
}, [wallet]); }, [wallet]);
return wallet; return wallet;
} }
/** /**
* Adds a wallet config for WebLN if detected * Adds a wallet config for WebLN if detected
*/ */
export function setupWebLNWalletConfig(store: WalletStore) { export function setupWebLNWalletConfig(store: WalletStore) {
const wallets = store.list(); const wallets = store.list();
const existing = wallets.find(a => a.kind === WalletKind.WebLN); const existing = wallets.find(a => a.kind === WalletKind.WebLN);
if (window.webln && !existing) { if (window.webln && !existing) {
const newConfig = { const newConfig = {
id: "webln", id: "webln",
kind: WalletKind.WebLN, kind: WalletKind.WebLN,
active: wallets.length === 0, active: wallets.length === 0,
info: { info: {
alias: "WebLN", alias: "WebLN",
}, },
} as WalletConfig; } as WalletConfig;
store.add(newConfig); store.add(newConfig);
} else if (existing) { } else if (existing) {
store.remove(existing.id); store.remove(existing.id);
} }
} }

View File

@ -31,14 +31,14 @@ interface WalletConnectResponse<T> {
result?: T; result?: T;
error?: { error?: {
code: code:
| "RATE_LIMITED" | "RATE_LIMITED"
| "NOT_IMPLEMENTED" | "NOT_IMPLEMENTED"
| "INSUFFICIENT_BALANCE" | "INSUFFICIENT_BALANCE"
| "QUOTA_EXCEEDED" | "QUOTA_EXCEEDED"
| "RESTRICTED" | "RESTRICTED"
| "UNAUTHORIZED" | "UNAUTHORIZED"
| "INTERNAL" | "INTERNAL"
| "OTHER"; | "OTHER";
message: string; message: string;
}; };
} }

View File

@ -1 +1 @@
/// <reference types="@webbtc/webln-types" /> /// <reference types="@webbtc/webln-types" />

View File

@ -104,7 +104,7 @@ export type Sats = number;
export type MilliSats = number; export type MilliSats = number;
export interface WalletEvents { export interface WalletEvents {
change: (data?: string) => void change: (data?: string) => void;
} }
export type LNWallet = EventEmitter<WalletEvents> & { export type LNWallet = EventEmitter<WalletEvents> & {
@ -122,7 +122,7 @@ export type LNWallet = EventEmitter<WalletEvents> & {
canGetBalance: () => boolean; canGetBalance: () => boolean;
canCreateInvoice: () => boolean; canCreateInvoice: () => boolean;
canPayInvoice: () => boolean; canPayInvoice: () => boolean;
} };
/** /**
* Load wallet by kind * Load wallet by kind
@ -152,4 +152,4 @@ export function loadWallet(kind: WalletKind, data: string | undefined) {
} }
} }
export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet } export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet };