Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: check for invalid checksum #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/parsing-v2/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/parsing-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export enum InvalidLightningDestinationReason {
InvoiceExpired = "InvoiceExpired",
WrongNetwork = "WrongNetwork",
Unknown = "Unknown",
InvalidChecksum = "InvalidChecksum",
}

export type LightningPaymentDestination =
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -489,6 +499,8 @@ export const parsePaymentDestination = ({
rawDestination: destination,
})

console.log("paymentType", paymentType)

switch (paymentType) {
case PaymentType.Lnurl:
return getLNURLPayResponse({
Expand Down