bug: invoice amount from millisats

This commit is contained in:
Kieran 2023-03-06 10:18:33 +00:00
parent 840e2ff400
commit 33bbadeda8
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 18 additions and 9 deletions

View File

@ -66,7 +66,7 @@ export default function Invoice(props: InvoiceProps) {
<p className="invoice-amount"> <p className="invoice-amount">
{amount > 0 && ( {amount > 0 && (
<> <>
{amount.toLocaleString()} <span className="sats">sat{amount === 1 ? "" : "s"}</span> {(amount / 1_000).toLocaleString()} <span className="sats">sat{amount === 1_000 ? "" : "s"}</span>
</> </>
)} )}
</p> </p>

View File

@ -240,8 +240,17 @@ export const delay = (t: number) => {
}); });
}; };
export type InvoiceDetails = ReturnType<typeof decodeInvoice>; export interface InvoiceDetails {
export function decodeInvoice(pr: string) { amount?: number;
expire?: number;
timestamp?: number;
description?: string;
descriptionHash?: string;
paymentHash?: string;
expired: boolean;
}
export function decodeInvoice(pr: string): InvoiceDetails | undefined {
try { try {
const parsed = invoiceDecode(pr); const parsed = invoiceDecode(pr);
@ -249,17 +258,17 @@ export function decodeInvoice(pr: string) {
const amount = amountSection ? Number(amountSection.value as number | string) : undefined; const amount = amountSection ? Number(amountSection.value as number | string) : undefined;
const timestampSection = parsed.sections.find(a => a.name === "timestamp"); 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 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 descriptionSection = parsed.sections.find(a => a.name === "description")?.value;
const descriptionHashSection = parsed.sections.find(a => a.name === "description_hash")?.value; const descriptionHashSection = parsed.sections.find(a => a.name === "description_hash")?.value;
const paymentHashSection = parsed.sections.find(a => a.name === "payment_hash")?.value; const paymentHashSection = parsed.sections.find(a => a.name === "payment_hash")?.value;
const ret = { const ret = {
amount: amount, amount: amount,
expire: !isNaN(timestamp) && !isNaN(expire) ? timestamp + expire : undefined, expire: timestamp && expire ? timestamp + expire : undefined,
timestamp: !isNaN(timestamp) ? timestamp : undefined, timestamp: timestamp,
description: descriptionSection as string | undefined, description: descriptionSection as string | undefined,
descriptionHash: descriptionHashSection ? bytesToHex(descriptionHashSection as Uint8Array) : undefined, descriptionHash: descriptionHashSection ? bytesToHex(descriptionHashSection as Uint8Array) : undefined,
paymentHash: paymentHashSection ? bytesToHex(paymentHashSection as Uint8Array) : undefined, paymentHash: paymentHashSection ? bytesToHex(paymentHashSection as Uint8Array) : undefined,

View File

@ -85,8 +85,8 @@ export function prToWalletInvoice(pr: string) {
return { return {
amount: parsedInvoice.amount ?? 0, amount: parsedInvoice.amount ?? 0,
memo: parsedInvoice.description, memo: parsedInvoice.description,
paymentHash: parsedInvoice.paymentHash, paymentHash: parsedInvoice.paymentHash ?? "",
timestamp: parsedInvoice.timestamp, timestamp: parsedInvoice.timestamp ?? 0,
state: parsedInvoice.expired ? WalletInvoiceState.Expired : WalletInvoiceState.Pending, state: parsedInvoice.expired ? WalletInvoiceState.Expired : WalletInvoiceState.Pending,
pr, pr,
} as WalletInvoice; } as WalletInvoice;