From 33bbadeda8444089ce6e340a245a5f91a1f3a2d6 Mon Sep 17 00:00:00 2001 From: Kieran Date: Mon, 6 Mar 2023 10:18:33 +0000 Subject: [PATCH] bug: invoice amount from millisats --- packages/app/src/Element/Invoice.tsx | 2 +- packages/app/src/Util.ts | 21 +++++++++++++++------ packages/app/src/Wallet/index.ts | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/app/src/Element/Invoice.tsx b/packages/app/src/Element/Invoice.tsx index f961375b..2d6d1251 100644 --- a/packages/app/src/Element/Invoice.tsx +++ b/packages/app/src/Element/Invoice.tsx @@ -66,7 +66,7 @@ export default function Invoice(props: InvoiceProps) {

{amount > 0 && ( <> - {amount.toLocaleString()} sat{amount === 1 ? "" : "s"} + {(amount / 1_000).toLocaleString()} sat{amount === 1_000 ? "" : "s"} )}

diff --git a/packages/app/src/Util.ts b/packages/app/src/Util.ts index 137d95b0..fb206015 100644 --- a/packages/app/src/Util.ts +++ b/packages/app/src/Util.ts @@ -240,8 +240,17 @@ export const delay = (t: number) => { }); }; -export type InvoiceDetails = ReturnType; -export function decodeInvoice(pr: string) { +export interface InvoiceDetails { + amount?: number; + expire?: number; + timestamp?: number; + description?: string; + descriptionHash?: string; + paymentHash?: string; + expired: boolean; +} + +export function decodeInvoice(pr: string): InvoiceDetails | undefined { try { const parsed = invoiceDecode(pr); @@ -249,17 +258,17 @@ export function decodeInvoice(pr: string) { const amount = amountSection ? Number(amountSection.value as number | string) : undefined; const timestampSection = parsed.sections.find(a => a.name === "timestamp"); - const timestamp = timestampSection ? (timestampSection.value as number) : NaN; + const timestamp = timestampSection ? Number(timestampSection.value as number | string) : undefined; const expirySection = parsed.sections.find(a => a.name === "expiry"); - const expire = expirySection ? (expirySection.value as number) : NaN; + const expire = expirySection ? Number(expirySection.value as number | string) : undefined; const descriptionSection = parsed.sections.find(a => a.name === "description")?.value; const descriptionHashSection = parsed.sections.find(a => a.name === "description_hash")?.value; const paymentHashSection = parsed.sections.find(a => a.name === "payment_hash")?.value; const ret = { amount: amount, - expire: !isNaN(timestamp) && !isNaN(expire) ? timestamp + expire : undefined, - timestamp: !isNaN(timestamp) ? timestamp : undefined, + expire: timestamp && expire ? timestamp + expire : undefined, + timestamp: timestamp, description: descriptionSection as string | undefined, descriptionHash: descriptionHashSection ? bytesToHex(descriptionHashSection as Uint8Array) : undefined, paymentHash: paymentHashSection ? bytesToHex(paymentHashSection as Uint8Array) : undefined, diff --git a/packages/app/src/Wallet/index.ts b/packages/app/src/Wallet/index.ts index a53377e0..c0632777 100644 --- a/packages/app/src/Wallet/index.ts +++ b/packages/app/src/Wallet/index.ts @@ -85,8 +85,8 @@ export function prToWalletInvoice(pr: string) { return { amount: parsedInvoice.amount ?? 0, memo: parsedInvoice.description, - paymentHash: parsedInvoice.paymentHash, - timestamp: parsedInvoice.timestamp, + paymentHash: parsedInvoice.paymentHash ?? "", + timestamp: parsedInvoice.timestamp ?? 0, state: parsedInvoice.expired ? WalletInvoiceState.Expired : WalletInvoiceState.Pending, pr, } as WalletInvoice;