From 20c3f7f1a1392a74c6f0152bef01fc810111cb36 Mon Sep 17 00:00:00 2001 From: Nicolas Burtey Date: Mon, 16 Jan 2023 12:31:21 +0000 Subject: [PATCH] chore: check for invalid checksum --- src/parsing-v2/index.spec.ts | 17 ++++++++++++++++- src/parsing-v2/index.ts | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/parsing-v2/index.spec.ts b/src/parsing-v2/index.spec.ts index 3b4bf68..249b8d2 100644 --- a/src/parsing-v2/index.spec.ts +++ b/src/parsing-v2/index.spec.ts @@ -73,7 +73,7 @@ const checkOnChainFail = (address: string, network: Network) => { } describe("parsePaymentDestination validations", () => { - it("classifies empty input as unknown", () => { + it("classifies empty input as nullInput", () => { const result = parsePaymentDestination({ destination: "", network: "mainnet", @@ -82,6 +82,21 @@ describe("parsePaymentDestination validations", () => { expect(result.paymentType).toBe(PaymentType.NullInput) }) + it("test for invalidChecksum", () => { + const result = parsePaymentDestination({ + destination: + "lntb1p3u2vt8pp5xhw2v5vl0ae3e4xqyjr8jkmv4ngzyk27dgpz9ftvmq2v5k2lwj3qdqqcqzpuxqyz5vqsp5adrt4eszrdke3efv2rv7m972ct2p3ersmcq7jntkdqm5zq6ffhyq9qyyssq5hw60yjskzu8067zveew58qxraahs73zp4l9x0yh7t8hl3ah0pe8u2wuzp7uk0tkq5luyx49rdwtj8y9tjap27un45rgakfz5x4cvqcprsl5jgs", + lnAddressDomains: ["ln.bitcoinbeach.com", "pay.bbw.sv"], + network: "signet", + }) + + expect(result).toEqual({ + valid: false, + paymentType: "lightning", + invalidReason: InvalidLightningDestinationReason.InvalidChecksum, + }) + }) + it("validates an lnurl destination", () => { const result = parsePaymentDestination({ destination: lnUrlInvoice, diff --git a/src/parsing-v2/index.ts b/src/parsing-v2/index.ts index f45ccc2..1f4275a 100644 --- a/src/parsing-v2/index.ts +++ b/src/parsing-v2/index.ts @@ -109,6 +109,7 @@ export enum InvalidLightningDestinationReason { InvoiceExpired = "InvoiceExpired", WrongNetwork = "WrongNetwork", Unknown = "Unknown", + InvalidChecksum = "InvalidChecksum", } export type LightningPaymentDestination = @@ -368,6 +369,15 @@ const getLightningPayResponse = ({ try { payReq = bolt11.decode(lnProtocol, parseBolt11Network(network)) } catch (err) { + const message = err.message + + if (message.indexOf("Invalid checksum for") !== -1) { + return { + valid: false, + paymentType, + invalidReason: InvalidLightningDestinationReason.InvalidChecksum, + } + } return { valid: false, paymentType, @@ -489,6 +499,8 @@ export const parsePaymentDestination = ({ rawDestination: destination, }) + console.log("paymentType", paymentType) + switch (paymentType) { case PaymentType.Lnurl: return getLNURLPayResponse({