feat: LNC wallet

This commit is contained in:
2023-02-24 10:25:14 +00:00
parent 1b363ec15f
commit cff605b188
8 changed files with 125 additions and 46 deletions

View File

@ -0,0 +1,84 @@
import { InvoiceRequest, LNWallet, Login, WalletError, WalletInfo, WalletInvoice, WalletInvoiceState } from "Wallet";
import LNC from "@lightninglabs/lnc-web";
export class LNCWallet implements LNWallet {
#lnc: LNC;
private constructor(pairingPhrase?: string, password?: string) {
this.#lnc = new LNC({
pairingPhrase,
password,
});
}
static async Initialize(pairingPhrase: string, password: string) {
const lnc = new LNCWallet(pairingPhrase, password);
await lnc.login();
return lnc;
}
static async Connect(password: string) {
const lnc = new LNCWallet(undefined, password);
await lnc.login();
return lnc;
}
createAccount(): Promise<WalletError | Login> {
throw new Error("Not implemented");
}
async getInfo(): Promise<WalletInfo | WalletError> {
const nodeInfo = await this.#lnc.lnd.lightning.getInfo();
return {
nodePubKey: nodeInfo.identityPubkey,
alias: nodeInfo.alias,
} as WalletInfo;
}
close(): Promise<boolean | WalletError> {
if (this.#lnc.isConnected) {
this.#lnc.disconnect();
}
return Promise.resolve(true);
}
async login(): Promise<boolean | WalletError> {
await this.#lnc.connect();
while (!this.#lnc.isConnected) {
await new Promise(resolve => {
setTimeout(resolve, 100);
});
}
return true;
}
async getBalance(): Promise<number | WalletError> {
const rsp = await this.#lnc.lnd.lightning.channelBalance();
console.debug(rsp);
return parseInt(rsp.localBalance?.sat ?? "0");
}
createInvoice(req: InvoiceRequest): Promise<WalletInvoice | WalletError> {
throw new Error("Not implemented");
}
payInvoice(pr: string): Promise<WalletInvoice | WalletError> {
throw new Error("Not implemented");
}
async getInvoices(): Promise<WalletInvoice[] | WalletError> {
const invoices = await this.#lnc.lnd.lightning.listPayments({
includeIncomplete: true,
maxPayments: "10",
reversed: true,
});
return invoices.payments.map(a => {
return {
amount: parseInt(a.valueSat),
state: a.status === "SUCCEEDED" ? WalletInvoiceState.Paid : WalletInvoiceState.Pending,
timestamp: parseInt(a.creationTimeNs) / 1e9,
} as WalletInvoice;
});
}
}

View File

@ -1,5 +1,4 @@
import { EventPublisher } from "Feed/EventPublisher";
import EventKind from "Nostr/EventKind";
import {
InvoiceRequest,
LNWallet,
@ -48,6 +47,10 @@ export default class LNDHubWallet implements LNWallet {
}
}
close(): Promise<boolean | WalletError> {
throw new Error("Not implemented");
}
async createAccount() {
return Promise.resolve(UnknownWalletError);
}
@ -136,7 +139,7 @@ export default class LNDHubWallet implements LNWallet {
private async getJson<T>(method: "GET" | "POST", path: string, body?: any): Promise<T | WalletError> {
let auth = `Bearer ${this.auth?.access_token}`;
if (this.type === "snort") {
const ev = await this.publisher?.generic(`${new URL(this.url).pathname}${path}`, EventKind.Ephemeral);
const ev = await this.publisher?.generic(`${new URL(this.url).pathname}${path}`, 30_000);
auth = JSON.stringify(ev?.ToObject());
}
const rsp = await fetch(`${this.url}${path}`, {

View File

@ -1,7 +1,3 @@
import useEventPublisher, { EventPublisher } from "Feed/EventPublisher";
import { useEffect, useState } from "react";
import LNDHubWallet from "./LNDHub";
export enum WalletErrorCode {
BadAuth = 1,
NotEnoughBalance = 2,
@ -70,32 +66,13 @@ export interface LNWallet {
createAccount: () => Promise<Login | WalletError>;
getInfo: () => Promise<WalletInfo | WalletError>;
login: () => Promise<boolean | WalletError>;
close: () => Promise<boolean | WalletError>;
getBalance: () => Promise<Sats | WalletError>;
createInvoice: (req: InvoiceRequest) => Promise<WalletInvoice | WalletError>;
payInvoice: (pr: string) => Promise<WalletInvoice | WalletError>;
getInvoices: () => Promise<WalletInvoice[] | WalletError>;
}
export async function openWallet(config: string, publisher?: EventPublisher) {
const wallet = new LNDHubWallet(config, publisher);
await wallet.login();
return wallet;
}
export function useWallet() {
const [wallet, setWallet] = useState<LNWallet>();
const publisher = useEventPublisher();
useEffect(() => {
if (publisher) {
const cfg = window.localStorage.getItem("wallet-lndhub");
if (cfg) {
openWallet(cfg, publisher)
.then(a => setWallet(a))
.catch(console.error);
}
}
}, [publisher]);
return wallet;
export function useWallet(): LNWallet | undefined {
return undefined;
}