Skip to content

Commit

Permalink
fix: linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiShandy committed Oct 2, 2023
1 parent ef6dc28 commit 22ba30b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 40 deletions.
10 changes: 7 additions & 3 deletions src/l402/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>,
options: Record<string, any>,
fetchArgs: RequestInit,
options: {
headerKey?: string;
webln?: WebLNProvider;
store?: Storage;
},
) => {
if (!options) {
options = {};
Expand All @@ -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 = {};
}
Expand Down
9 changes: 1 addition & 8 deletions src/lightning-address.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
13 changes: 7 additions & 6 deletions src/lightning-address.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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://lnaddressproxy.getalby.com";

Expand Down Expand Up @@ -81,11 +82,11 @@ export default class LightningAddress {
const keysendResult = await fetch(this.keysendUrl());
const nostrResult = await fetch(this.nostrUrl());

let lnurlData: Record<string, string> | undefined;
let lnurlData: LnUrlRawData | undefined;
if (lnurlResult.ok) {
lnurlData = await lnurlResult.json();
}
let keysendData: Record<string, string> | undefined;
let keysendData: KeySendRawData | undefined;
if (keysendResult.ok) {
keysendData = await keysendResult.json();
}
Expand Down Expand Up @@ -243,8 +244,8 @@ export default class LightningAddress {
}

private parseResponse(
lnurlpData: Record<string, string> | undefined,
keysendData: Record<string, string> | undefined,
lnurlpData: LnUrlRawData | undefined,
keysendData: KeySendRawData | undefined,
nostrData: NostrResponse | undefined,
) {
if (lnurlpData) {
Expand Down
9 changes: 4 additions & 5 deletions src/podcasting2/boostagrams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
13 changes: 12 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +27,7 @@ export type LnUrlPayResponse = {
description: string;
image: string;
commentAllowed?: number;
rawData: { [key: string]: string | number };
rawData: LnUrlRawData;
allowsNostr: boolean;
payerData?: LUD18ServicePayerData;
};
Expand Down
11 changes: 8 additions & 3 deletions src/utils/keysend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import type { KeysendResponse } from "../types";

const TAG_KEYSEND = "keysend";

export const parseKeysendResponse = (
data: Record<string, any>,
): 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");

Expand Down
16 changes: 9 additions & 7 deletions src/utils/lnurl.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string, any>,
): LnUrlPayResponse => {
export const parseLnUrlPayResponse = (data: LnUrlRawData): LnUrlPayResponse => {
if (data.tag !== TAG_PAY_REQUEST)
throw new Error("Invalid pay service params");

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
export class MemoryStorage {
storage;

constructor(initial?: any) {
constructor(initial?: Record<string, unknown>) {
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;
2 changes: 1 addition & 1 deletion src/window.js
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit 22ba30b

Please sign in to comment.