Skip to content

Commit

Permalink
fix: do not dynamically import crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Dec 6, 2023
1 parent 297dff7 commit dafc1fc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,25 @@ This library uses a [proxy](https://github.com/getAlby/lightning-address-details

You can disable the proxy by explicitly setting the proxy to false when initializing a lightning address:

```js
const lightningAddress = new LightningAddress("[email protected]", {
proxy: false,
});
```
const lightningAddress = new LightningAddress("[email protected]", {proxy: false});

## crypto dependency

If you get an `crypto is not defined` in NodeJS error you have to import it first:

```js
import * as crypto from 'crypto'; // or 'node:crypto'
globalThis.crypto = crypto as any;
//or: global.crypto = require('crypto');
```

## fetch() dependency

This library relies on a global fetch object which will work in browsers and node v18.x or newer. In old version yoi can manually install a global fetch option or polyfill if needed.
This library relies on a global fetch object which will work in browsers and node v18.x or newer. In old version you can manually install a global fetch option or polyfill if needed.

For example:

Expand Down
2 changes: 2 additions & 0 deletions examples/request-invoice.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as crypto from "crypto"; // or 'node:crypto'
global.crypto = crypto;
import { LightningAddress } from "@getalby/lightning-tools";

const ln = new LightningAddress("[email protected]");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"format:fix": "prettier --loglevel silent --write '**/*.(md|json)' 'src/**/*.(js|ts)' 'examples/**/*.js'",
"test": "jest",
"clean": "rm -rf dist",
"build": "microbundle --no-sourcemap --external crypto",
"build": "microbundle --no-sourcemap",
"dev": "microbundle watch",
"prepare": "husky install"
},
Expand Down
5 changes: 0 additions & 5 deletions src/utils/sha256.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
export async function sha256(message: string | Uint8Array) {
let crypto = globalThis.crypto as Pick<Crypto, "subtle">;
if (!crypto) {
crypto = await import("crypto");
}

// encode as UTF-8
const msgBuffer =
typeof message === "string" ? new TextEncoder().encode(message) : message;
Expand Down

0 comments on commit dafc1fc

Please sign in to comment.