Skip to content

Commit

Permalink
refactor: remove url library (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Burtey <[email protected]>
  • Loading branch information
nicolasburtey and Nicolas Burtey authored Dec 4, 2023
1 parent 4a1ebe0 commit bd983c7
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 157 deletions.
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"tsc:check": "tsc --pretty --noEmit --skipLibCheck"
},
"peerDependencies": {
"@bitcoinerlab/secp256k1": "^1.0.5",
"bitcoinjs-lib": "^6.1.5",
"bolt11": "~1.3.4",
"lnurl-pay": "^1.0.1",
"url": ">=0.11.0",
"@bitcoinerlab/secp256k1": "^1.0.5"
"lnurl-pay": "^3.0.1"
},
"devDependencies": {
"@bitcoinerlab/secp256k1": "^1.0.5",
Expand All @@ -35,11 +34,10 @@
"eslint": "^8.41.0",
"eslint-plugin-import": "^2.27.5",
"jest": "^29.5.0",
"lnurl-pay": "^2.2.2",
"lnurl-pay": "^3.0.1",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4",
"url": "^0.11.0"
"typescript": "^5.0.4"
},
"dependencies": {},
"resolutions": {
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./lnurl/index"
export * from "./parsing/index"
52 changes: 0 additions & 52 deletions src/lnurl/index.ts

This file was deleted.

43 changes: 24 additions & 19 deletions src/parsing/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-lines */
import bolt11 from "bolt11"
import url from "url"
import * as bitcoinjs from "bitcoinjs-lib"
import { utils } from "lnurl-pay"
import * as ecc from "@bitcoinerlab/secp256k1"
Expand Down Expand Up @@ -86,6 +85,7 @@ export const PaymentType = {
export type PaymentType = (typeof PaymentType)[keyof typeof PaymentType]

export type UnknownPaymentDestination = {
valid: false
paymentType: typeof PaymentType.Unknown
}

Expand Down Expand Up @@ -172,8 +172,8 @@ export type IntraledgerPaymentDestination =
handle: string
}
| {
paymentType: typeof PaymentType.Intraledger
valid: false
paymentType: typeof PaymentType.Intraledger
invalidReason: InvalidIntraledgerReason
handle: string
}
Expand Down Expand Up @@ -227,12 +227,12 @@ type ParsePaymentDestinationArgs = {
lnAddressDomains: string[]
}

const inputDataToObject = (data: string): any => {
return url.parse(data, true)
}

const getLNParam = (data: string): string | undefined => {
return inputDataToObject(data)?.query?.lightning
const getLNParam = (data: string): string | null => {
try {
return new URL(data).searchParams?.get("lightning")
} catch (err) {
return null
}
}

const getProtocolAndData = (
Expand Down Expand Up @@ -272,7 +272,10 @@ const getPaymentType = ({
return PaymentType.Lightning
}

if (destinationWithoutProtocol && getLNParam(destinationWithoutProtocol)) {
if (
destinationWithoutProtocol &&
getLNParam(`lightning:${destinationWithoutProtocol}`)
) {
return PaymentType.Unified
}

Expand Down Expand Up @@ -316,7 +319,7 @@ const getIntraLedgerPayResponse = ({
: destinationWithoutProtocol

if (protocol.match(/^(http|\/\/)/iu)) {
const domain = url.parse(destination).hostname
const domain = new URL(destination).hostname
if (!lnAddressDomains.find((lnAddressDomain) => lnAddressDomain === domain)) {
return {
valid: false,
Expand All @@ -336,6 +339,7 @@ const getIntraLedgerPayResponse = ({
}

return {
valid: false,
paymentType: PaymentType.Unknown,
}
}
Expand Down Expand Up @@ -467,22 +471,23 @@ const getOnChainPayResponse = ({
}): OnchainPaymentDestination => {
const paymentType = PaymentType.Onchain
try {
const decodedData = inputDataToObject(destinationWithoutProtocol)
const decodedData = new URL(`bitcoin:${destinationWithoutProtocol}`)

// some apps encode addresses in UPPERCASE
const path = decodedData?.pathname as string
const path = decodedData?.pathname
if (!path) {
throw new Error("No address detected in decoded destination")
}

const label: string | undefined = decodedData?.query?.label
const message: string | undefined = decodedData?.query?.message
const label: string | null = decodedData?.searchParams.get("label")
const message: string | null = decodedData?.searchParams.get("message")
const memo = label || message || undefined
let amount: number | undefined = undefined
try {
amount = decodedData?.query?.amount
? parseAmount(decodedData.query.amount as string)
: undefined
const parsedAmount = decodedData?.searchParams.get("amount")
if (parsedAmount) {
amount = parseAmount(parsedAmount)
}
} catch (err) {
console.debug("[Parse error: amount]:", err)
return {
Expand Down Expand Up @@ -573,8 +578,8 @@ export const parsePaymentDestination = ({
network,
})
case PaymentType.Unknown:
return { paymentType: PaymentType.Unknown }
return { paymentType: PaymentType.Unknown, valid: false }
}

return { paymentType: PaymentType.Unknown }
return { paymentType: PaymentType.Unknown, valid: false }
}
Loading

0 comments on commit bd983c7

Please sign in to comment.