Skip to content

Commit db8e23f

Browse files
committed
chore: remove crypto dependency
1 parent e22a0a9 commit db8e23f

File tree

8 files changed

+21
-43
lines changed

8 files changed

+21
-43
lines changed

examples/l402.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { fetchWithL402 } from "@getalby/lightning-tools";
22
import { webln } from "alby-js-sdk";
33
import "websocket-polyfill";
4-
import * as crypto from "crypto";
5-
globalThis.crypto = crypto;
64

75
const url = "https://lsat-weather-api.getalby.repl.co/kigali";
86

@@ -16,12 +14,12 @@ const nostrWeblnProvider = new webln.NostrWebLNProvider({
1614
nostrWalletConnectUrl,
1715
});
1816
nostrWeblnProvider.on("sendPayment", (response) => {
19-
console.log(`payment response:`, response);
17+
console.info(`payment response:`, response);
2018
});
2119

2220
fetchWithL402(url, {}, { webln: nostrWeblnProvider })
2321
.then((response) => response.json())
2422
.then((data) => {
25-
console.log(data);
23+
console.info(data);
2624
nostrWeblnProvider.close();
2725
});

examples/request-invoice.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as crypto from "crypto"; // or 'node:crypto'
2-
global.crypto = crypto;
31
import { LightningAddress } from "@getalby/lightning-tools";
42

53
const ln = new LightningAddress("[email protected]");
@@ -9,5 +7,5 @@ await ln.fetch();
97
// this returns a new `Invoice` class that can also be used to validate the payment
108
const invoice = await ln.requestInvoice({ satoshi: 1000 });
119

12-
console.log(invoice.paymentRequest); // print the payment request
13-
console.log(invoice.paymentHash); // print the payment hash
10+
console.info(invoice.paymentRequest); // print the payment request
11+
console.info(invoice.paymentHash); // print the payment hash

examples/zaps-nwc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { LightningAddress } from "@getalby/lightning-tools";
22
import { webln } from "alby-js-sdk";
33
import "websocket-polyfill";
4-
import * as crypto from "crypto";
54
import { finalizeEvent, getPublicKey } from "nostr-tools";
65
import { hexToBytes } from "@noble/hashes/utils";
7-
/*global globalThis*/
8-
globalThis.crypto = crypto;
96

107
// your private key is required to sign zap request events
118
const nostrPrivateKey = process.env.NOSTR_PRIVATE_KEY;

examples/zaps.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import { LightningAddress } from "@getalby/lightning-tools";
1818
if (window.webln) {
1919
// zap in one go with WebLN (https://www.webln.guide) (easiest for web apps)
2020
const response = await ln.zap(zapArgs); // signs zap request event, generates invoice and pays it
21-
console.log(response.preimage); // print the preimage
21+
console.info(response.preimage); // print the preimage
2222
} else {
2323
// or manually (create an invoice and give it to the user to pay)
2424
const invoice = await ln.zapInvoice(zapArgs); // generates a zap invoice
25-
console.log(invoice.paymentRequest); // print the payment request
25+
console.info(invoice.paymentRequest); // print the payment request
2626
await invoice.isPaid(); // check the payment status as described above
2727
}
2828
})();

src/invoice.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { decodeInvoice } from "./utils/invoice";
22
import { InvoiceArgs } from "./types";
3-
import { sha256 } from "./utils/sha256";
3+
import { sha256 } from "@noble/hashes/sha256";
4+
import { bytesToHex } from "@noble/hashes/utils";
45
import { fromHexString } from "./utils/hex";
56

67
export default class Invoice {
@@ -46,11 +47,11 @@ export default class Invoice {
4647
}
4748
}
4849

49-
async validatePreimage(preimage: string): Promise<boolean> {
50+
validatePreimage(preimage: string): boolean {
5051
if (!preimage || !this.paymentHash) return false;
5152

5253
try {
53-
const preimageHash = await sha256(fromHexString(preimage));
54+
const preimageHash = bytesToHex(sha256(fromHexString(preimage)));
5455
return this.paymentHash === preimageHash;
5556
} catch {
5657
return false;
@@ -68,8 +69,8 @@ export default class Invoice {
6869

6970
return json.settled;
7071
} catch (error) {
71-
console.error("failed to check LNURL-verify", error)
72-
return false
72+
console.error("failed to check LNURL-verify", error);
73+
return false;
7374
}
7475
}
7576

src/utils/lnurl.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type {
33
LnUrlPayResponse,
44
LnUrlRawData,
55
} from "../types";
6-
import { sha256 } from "./sha256";
6+
import { sha256 } from "@noble/hashes/sha256";
7+
import { bytesToHex } from "@noble/hashes/utils";
78

89
const URL_REGEX =
910
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)/;
@@ -45,10 +46,10 @@ export const parseLnUrlPayResponse = async (
4546
let metadataHash: string;
4647
try {
4748
metadata = JSON.parse(data.metadata + "");
48-
metadataHash = await sha256(data.metadata + "");
49+
metadataHash = bytesToHex(sha256(data.metadata + ""));
4950
} catch {
5051
metadata = [];
51-
metadataHash = await sha256("[]");
52+
metadataHash = bytesToHex(sha256("[]"));
5253
}
5354

5455
let email = "";

src/utils/nostr.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Event, NostrResponse, ZapArgs, ZapOptions } from "../types";
2-
import { sha256 } from "./sha256";
2+
import { sha256 } from "@noble/hashes/sha256";
3+
import { bytesToHex } from "@noble/hashes/utils";
34

45
export async function generateZapEvent(
56
{ satoshi, comment, p, e, relays }: ZapArgs,
@@ -31,7 +32,7 @@ export async function generateZapEvent(
3132
content: comment ?? "",
3233
};
3334

34-
nostrEvent.id = await getEventHash(nostrEvent);
35+
nostrEvent.id = getEventHash(nostrEvent);
3536
return await nostr.signEvent(nostrEvent);
3637
}
3738

@@ -68,8 +69,8 @@ export function serializeEvent(evt: Event): string {
6869
]);
6970
}
7071

71-
export function getEventHash(event: Event): Promise<string> {
72-
return sha256(serializeEvent(event));
72+
export function getEventHash(event: Event): string {
73+
return bytesToHex(sha256(serializeEvent(event)));
7374
}
7475

7576
export function parseNostrResponse(

src/utils/sha256.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)