bug: lndhub payment state

refactor: use webln package
This commit is contained in:
2023-05-18 11:17:32 +01:00
parent 7ab8eff33a
commit c52eb38833
11 changed files with 117 additions and 120 deletions

View File

@ -10,6 +10,10 @@ export class CashuWallet implements LNWallet {
this.#mint = mint;
}
canAutoLogin(): boolean {
return true;
}
isReady(): boolean {
return this.#wallet !== undefined;
}

View File

@ -30,6 +30,10 @@ export class LNCWallet implements LNWallet {
});
}
canAutoLogin(): boolean {
return false;
}
isReady(): boolean {
return this.#lnc.isReady;
}

View File

@ -43,6 +43,10 @@ export default class LNDHubWallet implements LNWallet {
return this.auth !== undefined;
}
canAutoLogin(): boolean {
return true;
}
close(): Promise<boolean> {
return Promise.resolve(true);
}
@ -91,7 +95,12 @@ export default class LNDHubWallet implements LNWallet {
return {
pr: pr,
paymentHash: pRsp.payment_hash,
state: pRsp.payment_error === undefined ? WalletInvoiceState.Paid : WalletInvoiceState.Pending,
preimage: pRsp.payment_preimage,
state: pRsp.payment_error
? WalletInvoiceState.Failed
: pRsp.payment_preimage
? WalletInvoiceState.Paid
: WalletInvoiceState.Pending,
} as WalletInvoice;
}

View File

@ -50,10 +50,14 @@ export class NostrConnectWallet implements LNWallet {
} as WalletConnectConfig;
}
isReady(): boolean {
canAutoLogin(): boolean {
return true;
}
isReady(): boolean {
return this.#conn !== undefined;
}
async getInfo() {
await this.login();
return await new Promise<WalletInfo>((resolve, reject) => {

View File

@ -1,3 +1,4 @@
import { requestProvider, WebLNProvider } from "webln";
import {
InvoiceRequest,
LNWallet,
@ -12,77 +13,19 @@ import {
WalletKind,
WalletStore,
} from "Wallet";
import { delay } from "Util";
import { unwrap } from "Util";
import { barrierQueue, processWorkQueue, WorkQueueItem } from "WorkQueue";
let isWebLnBusy = false;
export const barrierWebLn = async <T>(then: () => Promise<T>): Promise<T> => {
while (isWebLnBusy) {
await delay(10);
}
isWebLnBusy = true;
try {
return await then();
} finally {
isWebLnBusy = false;
}
};
interface SendPaymentResponse {
paymentHash?: string;
preimage: string;
route?: {
total_amt: number;
total_fees: number;
};
}
interface RequestInvoiceArgs {
amount?: string | number;
defaultAmount?: string | number;
minimumAmount?: string | number;
maximumAmount?: string | number;
defaultMemo?: string;
}
interface RequestInvoiceResponse {
paymentRequest: string;
}
interface GetInfoResponse {
node: {
alias: string;
pubkey: string;
color?: string;
};
}
interface SignMessageResponse {
message: string;
signature: string;
}
interface WebLN {
enabled: boolean;
getInfo(): Promise<GetInfoResponse>;
enable(): Promise<void>;
makeInvoice(args: RequestInvoiceArgs): Promise<RequestInvoiceResponse>;
signMessage(message: string): Promise<SignMessageResponse>;
verifyMessage(signature: string, message: string): Promise<void>;
sendPayment: (pr: string) => Promise<SendPaymentResponse>;
}
declare global {
interface Window {
webln?: WebLN;
}
}
const WebLNQueue: Array<WorkQueueItem> = [];
processWorkQueue(WebLNQueue);
/**
* Adds a wallet config for WebLN if detected
*/
export function setupWebLNWalletConfig(store: WalletStore) {
export async function setupWebLNWalletConfig(store: WalletStore) {
const wallets = store.list();
if (window.webln && !wallets.some(a => a.kind === WalletKind.WebLN)) {
const provider = await requestProvider();
if (provider && !wallets.some(a => a.kind === WalletKind.WebLN)) {
const newConfig = {
id: "webln",
kind: WalletKind.WebLN,
@ -96,17 +39,20 @@ export function setupWebLNWalletConfig(store: WalletStore) {
}
export class WebLNWallet implements LNWallet {
#provider?: WebLNProvider;
isReady(): boolean {
if (window.webln) {
return true;
}
return false;
return this.#provider !== undefined;
}
canAutoLogin(): boolean {
return true;
}
async getInfo(): Promise<WalletInfo> {
await this.login();
if (this.isReady()) {
const rsp = await barrierWebLn(async () => await window.webln?.getInfo());
if (this.isReady() && this.#provider) {
const rsp = await barrierQueue(WebLNQueue, async () => await unwrap(this.#provider).getInfo());
if (rsp) {
return {
nodePubKey: rsp.node.pubkey,
@ -120,8 +66,8 @@ export class WebLNWallet implements LNWallet {
}
async login(): Promise<boolean> {
if (window.webln && !window.webln.enabled) {
await window.webln.enable();
if (this.#provider === undefined) {
this.#provider = await requestProvider();
}
return true;
}
@ -137,9 +83,10 @@ export class WebLNWallet implements LNWallet {
async createInvoice(req: InvoiceRequest): Promise<WalletInvoice> {
await this.login();
if (this.isReady()) {
const rsp = await barrierWebLn(
const rsp = await barrierQueue(
WebLNQueue,
async () =>
await window.webln?.makeInvoice({
await unwrap(this.#provider).makeInvoice({
amount: req.amount,
defaultMemo: req.memo,
})
@ -162,7 +109,7 @@ export class WebLNWallet implements LNWallet {
if (!invoice) {
throw new WalletError(WalletErrorCode.InvalidInvoice, "Could not parse invoice");
}
const rsp = await barrierWebLn(async () => await window.webln?.sendPayment(pr));
const rsp = await barrierQueue(WebLNQueue, async () => await unwrap(this.#provider).sendPayment(pr));
if (rsp) {
invoice.state = WalletInvoiceState.Paid;
invoice.preimage = rsp.preimage;

View File

@ -100,6 +100,7 @@ export type MilliSats = number;
export interface LNWallet {
isReady(): boolean;
canAutoLogin(): boolean;
getInfo: () => Promise<WalletInfo>;
login: (password?: string) => Promise<boolean>;
close: () => Promise<boolean>;
@ -140,8 +141,8 @@ export class WalletStore {
configs: [],
});
this.load(false);
setupWebLNWalletConfig(this);
this.snapshotState();
setupWebLNWalletConfig(this);
}
hook(fn: WalletStateHook) {