diff --git a/src/l402/index.ts b/src/l402/index.ts index cfc996c..f60c375 100644 --- a/src/l402/index.ts +++ b/src/l402/index.ts @@ -9,8 +9,12 @@ const HEADER_KEY = "L402"; // we have to update this to L402 at some point export const fetchWithL402 = async ( url: string, - fetchArgs: Record, - options: Record, + fetchArgs: RequestInit, + options: { + headerKey?: string; + webln?: WebLNProvider; + store?: Storage; + }, ) => { if (!options) { options = {}; @@ -20,7 +24,7 @@ export const fetchWithL402 = async ( if (!webln) { throw new Error("WebLN is missing"); } - let store = options.store || memoryStorage; + const store = options.store || memoryStorage; if (!fetchArgs) { fetchArgs = {}; } diff --git a/src/lightning-address.test.ts b/src/lightning-address.test.ts index 052181c..52f75ba 100644 --- a/src/lightning-address.test.ts +++ b/src/lightning-address.test.ts @@ -1,14 +1,7 @@ import { WebLNProvider } from "@webbtc/webln-types"; import LightningAddress, { DEFAULT_PROXY } from "./lightning-address"; import { Event, NostrProvider } from "./types"; -import { - UnsignedEvent, - finishEvent, - generatePrivateKey, - getEventHash, - getPublicKey, - signEvent, -} from "nostr-tools"; +import { finishEvent, generatePrivateKey, getPublicKey } from "nostr-tools"; const dummyWebLN: WebLNProvider = { enable: () => Promise.resolve(), diff --git a/src/lightning-address.ts b/src/lightning-address.ts index 478f7e5..127bc38 100644 --- a/src/lightning-address.ts +++ b/src/lightning-address.ts @@ -1,9 +1,10 @@ -import { parseKeysendResponse } from "./utils/keysend"; +import { Data as KeySendRawData, parseKeysendResponse } from "./utils/keysend"; import { isUrl, isValidAmount, parseLnUrlPayResponse } from "./utils/lnurl"; import Invoice from "./invoice"; import { InvoiceArgs, LnUrlPayResponse, + LnUrlRawData, NostrResponse, RequestInvoiceArgs, ZapArgs, @@ -16,7 +17,7 @@ import { WebLNProvider, SendPaymentResponse } from "@webbtc/webln-types"; import { KeysendResponse } from "./types"; const LN_ADDRESS_REGEX = - /^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + /^((?:[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; export const DEFAULT_PROXY = "https://api.getalby.com/lnurl"; @@ -85,11 +86,11 @@ export default class LightningAddress { const keysendResult = await fetch(this.keysendUrl()); const nostrResult = await fetch(this.nostrUrl()); - let lnurlData: Record | undefined; + let lnurlData: LnUrlRawData | undefined; if (lnurlResult.ok) { lnurlData = await lnurlResult.json(); } - let keysendData: Record | undefined; + let keysendData: KeySendRawData | undefined; if (keysendResult.ok) { keysendData = await keysendResult.json(); } @@ -249,8 +250,8 @@ export default class LightningAddress { } private parseResponse( - lnurlpData: Record | undefined, - keysendData: Record | undefined, + lnurlpData: LnUrlRawData | undefined, + keysendData: KeySendRawData | undefined, nostrData: NostrResponse | undefined, ) { if (lnurlpData) { diff --git a/src/podcasting2/boostagrams.ts b/src/podcasting2/boostagrams.ts index 8c34b22..2a71692 100644 --- a/src/podcasting2/boostagrams.ts +++ b/src/podcasting2/boostagrams.ts @@ -33,16 +33,15 @@ export type Boost = { }; export const boost = async (args: BoostArguments, options?: BoostOptions) => { - let { boost, amount } = args; + const { boost } = args; if (!options) { options = {}; } const webln: WebLNProvider = options.webln || globalThis.webln; - if (!amount) { - amount = Math.floor(boost.value_msat / 1000); - } - let weblnParams: WeblnBoostParams = { + const amount = args.amount || Math.floor(boost.value_msat / 1000); + + const weblnParams: WeblnBoostParams = { destination: args.destination, amount: amount, customRecords: { diff --git a/src/types.ts b/src/types.ts index 08ba8c7..e29065c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,17 @@ export type KeysendResponse = { destination: string; }; +export type LnUrlRawData = { + tag: string; + callback: string; + minSendable: number; + maxSendable: number; + metadata: string; + payerData?: LUD18ServicePayerData; + commentAllowed?: number; + allowsNostr?: boolean; +}; + export type LnUrlPayResponse = { callback: string; fixed: boolean; @@ -16,7 +27,7 @@ export type LnUrlPayResponse = { description: string; image: string; commentAllowed?: number; - rawData: { [key: string]: string | number }; + rawData: LnUrlRawData; allowsNostr: boolean; payerData?: LUD18ServicePayerData; }; diff --git a/src/utils/keysend.ts b/src/utils/keysend.ts index 703f79a..627d403 100644 --- a/src/utils/keysend.ts +++ b/src/utils/keysend.ts @@ -2,9 +2,14 @@ import type { KeysendResponse } from "../types"; const TAG_KEYSEND = "keysend"; -export const parseKeysendResponse = ( - data: Record, -): KeysendResponse => { +export type Data = { + tag: string; + status: string; + customData: { customKey: string; customValue: string }[]; + pubkey: string; +}; + +export const parseKeysendResponse = (data: Data): KeysendResponse => { if (data.tag !== TAG_KEYSEND) throw new Error("Invalid keysend params"); if (data.status !== "OK") throw new Error("Keysend status not OK"); diff --git a/src/utils/lnurl.ts b/src/utils/lnurl.ts index 8891f1d..47e5b91 100644 --- a/src/utils/lnurl.ts +++ b/src/utils/lnurl.ts @@ -1,10 +1,14 @@ import Hex from "crypto-js/enc-hex.js"; import sha256 from "crypto-js/sha256.js"; -import type { LUD18ServicePayerData, LnUrlPayResponse } from "../types"; +import type { + LUD18ServicePayerData, + LnUrlPayResponse, + LnUrlRawData, +} from "../types"; const URL_REGEX = - /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/; + /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)/; export const isUrl = (url: string | null): url is string => { if (!url) return false; @@ -26,9 +30,7 @@ export const isValidAmount = ({ const TAG_PAY_REQUEST = "payRequest"; // From: https://github.com/dolcalmi/lnurl-pay/blob/main/src/request-pay-service-params.ts -export const parseLnUrlPayResponse = ( - data: Record, -): LnUrlPayResponse => { +export const parseLnUrlPayResponse = (data: LnUrlRawData): LnUrlPayResponse => { if (data.tag !== TAG_PAY_REQUEST) throw new Error("Invalid pay service params"); @@ -67,9 +69,9 @@ export const parseLnUrlPayResponse = ( break; } } - let payerData = data.payerData as LUD18ServicePayerData | undefined; + const payerData = data.payerData as LUD18ServicePayerData | undefined; - let domain; + let domain: string | undefined; try { domain = new URL(callback).hostname; } catch { diff --git a/src/utils/storage.ts b/src/utils/storage.ts index b21672f..3e2aabb 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -1,27 +1,27 @@ export class MemoryStorage { storage; - constructor(initial?: any) { + constructor(initial?: Record) { this.storage = initial || {}; } - getItem(key) { + getItem(key: string) { return this.storage[key]; } - setItem(key, value) { + setItem(key: string, value: unknown) { this.storage[key] = value; } } export class NoStorage { - constructor(initial?: any) {} + constructor(initial?: unknown) {} - getItem(key) { + getItem(key: string) { return null; } - setItem(key, value) {} + setItem(key: string, value: unknown) {} } export default MemoryStorage; diff --git a/src/window.js b/src/window.js index 02d7384..915186d 100644 --- a/src/window.js +++ b/src/window.js @@ -1,3 +1,3 @@ // assign alby-tools exports to global window object (for index.browser.js) -// @ts-ignore +// @ts-ignore this file is created at build time window["lightningTools"] = require("./index.cjs");