Skip to content

Commit

Permalink
Implement lookup_invoice for NWC
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Dec 17, 2024
1 parent 453c176 commit 1e01bdb
Showing 1 changed file with 71 additions and 13 deletions.
84 changes: 71 additions & 13 deletions src/stores/nwc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useWalletStore } from "./wallet";
import { useProofsStore } from "./proofs";
import { notify, notifyError, notifyWarning } from "../js/notify";
import { useSettingsStore } from "./settings";
import { decode as decodeBolt11 } from 'light-bolt11-decoder'

type NWCConnection = {
walletPublicKey: string;
Expand All @@ -30,6 +31,19 @@ type NWCCommand = {
params: any;
};

type NWCTransaction = {
type: string,
invoice: string,
description: string | null,
preimage: string | null,
payment_hash: string | null,
amount: number,
fees_paid: number | null,
created_at: number,
settled_at: number | null
expires_at: number | null
}

type NWCResult = {
result_type: string;
result: any;
Expand Down Expand Up @@ -59,6 +73,7 @@ export const useNWCStore = defineStore("nwc", {
"get_balance",
"get_info",
"list_transactions",
"lookup_invoice"
],
relays: useLocalStorage<string[]>(
"cashu.nwc.relays",
Expand Down Expand Up @@ -195,18 +210,6 @@ export const useNWCStore = defineStore("nwc", {
},
handleListTransactions: async function (nwcCommand: NWCCommand) {
console.log("### list_transactions", nwcCommand.method);
type nwcTransaction = {
type: string;
invoice: string;
description: string | null;
preimage: string | null;
payment_hash: string | null;
amount: number;
fees_paid: number | null;
created_at: number;
settled_at: number | null;
expires_at: number | null;
};
const walletStore = useWalletStore();
const from = nwcCommand.params.from || 0;
const until = nwcCommand.params.until || Math.floor(Date.now() / 1000);
Expand Down Expand Up @@ -271,6 +274,59 @@ export const useNWCStore = defineStore("nwc", {
},
};
},
handleLookupInvoice: async function (nwcCommand: NWCCommand) {
let hash = nwcCommand.params.payment_hash
if (!hash) {
const bolt11 = nwcCommand.params.invoice
const decoded = bolt11 ? decodeBolt11(bolt11) : null
// @ts-ignore
hash = decoded?.sections.find(s => s.name === "payment_hash")?.value
}
if (!hash) {
return {
result_type: nwcCommand.method,
error: { code: "OTHER", message: "invoice or payment_hash required"}
}
}

console.log("### lookup_invoice")
const walletStore = useWalletStore()
const invoiceHistory = walletStore.invoiceHistory

for (const inv of invoiceHistory) {
const decoded = decodeBolt11( nwcCommand.params.invoice)
// @ts-ignore
const invHash = decoded.sections.find(s => s.name === "payment_hash")?.value
if (invHash === hash) {
return {
result_type: nwcCommand.method,
result: this.mapToNwcTransaction(inv)
}
}
}

return {
result_type: nwcCommand.method,
error: {
code: "NOT_FOUND", message: "invoice not found"
}
}
},
mapToNwcTransaction(invoice: InvoiceHistory) {
let type = invoice.amount > 0 ? "incoming" : "outgoing"
let amount = Math.abs(invoice.amount) * 1000
let created_at = Math.floor(new Date(invoice.date).getTime() / 1000)
let settled_at = invoice.status == "paid" ? Math.floor(new Date(invoice.date).getTime() / 1000) : null
return {
type: type,
invoice: invoice.bolt11,
description: invoice.memo,
amount: amount,
fees_paid: 0,
created_at: created_at,
settled_at: settled_at,
} as NWCTransaction
},
// ––––---------- NWC Connection ––––----------
replyNWC: async function (
result: NWCResult | NWCError,
Expand Down Expand Up @@ -330,7 +386,9 @@ export const useNWCStore = defineStore("nwc", {
} else if (nwcCommand.method === "make_invoice") {
result = await this.handleMakeInvoice(nwcCommand)
} else if (nwcCommand.method == "list_transactions") {
result = await this.handleListTransactions(nwcCommand);
result = await this.handleListTransactions(nwcCommand)
} else if (nwcCommand.method === "lookup_invoice") {
result = await this.handleLookupInvoice(nwcCommand)
} else {
console.log("### method not supported", nwcCommand.method);
result = {
Expand Down

0 comments on commit 1e01bdb

Please sign in to comment.