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");
}
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 = {

View File

@ -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";

View File

@ -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<WalletConfig>;
config?: WalletConfig;
wallet?: LNWallet;
configs: Array<WalletConfig>;
config?: WalletConfig;
wallet?: LNWallet;
}
export class WalletStore extends ExternalStore<WalletStoreSnapshot> {
#configs: Array<WalletConfig>;
#instance: Map<string, LNWallet>;
#configs: Array<WalletConfig>;
#instance: Map<string, LNWallet>;
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<LNWallet> | 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<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();
window.document.addEventListener("close", () => {
Wallets.free();
Wallets.free();
});
export function useWallet() {
const wallet = useSyncExternalStore<WalletStoreSnapshot>(
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<WalletStoreSnapshot>(
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);
}
}

View File

@ -31,14 +31,14 @@ interface WalletConnectResponse<T> {
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;
};
}

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 interface WalletEvents {
change: (data?: string) => void
change: (data?: string) => void;
}
export type LNWallet = EventEmitter<WalletEvents> & {
@ -122,7 +122,7 @@ export type LNWallet = EventEmitter<WalletEvents> & {
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 }
export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet };