-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from getAlby/feat/remove-browserify
feat: remove browserify, crypto-js and source maps
- Loading branch information
Showing
15 changed files
with
106 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,19 @@ yarn add @getalby/lightning-tools | |
|
||
or for use without any build tools: | ||
|
||
``` | ||
// lightning-tools now available at window.lightningTools | ||
<script src="https://cdn.jsdelivr.net/npm/@getalby/lightning-tools@latest/dist/index.browser.js"></script> | ||
```html | ||
<script type="module"> | ||
import { LightningAddress } from "https://esm.sh/@getalby/[email protected]"; // jsdelivr.net, skypack.dev also work | ||
// use LightningAddress normally... | ||
(async () => { | ||
const ln = new LightningAddress("[email protected]"); | ||
// fetch the LNURL data | ||
await ln.fetch(); | ||
// get the LNURL-pay data: | ||
console.log(ln.lnurlpData); | ||
})(); | ||
</script> | ||
``` | ||
|
||
**This library relies on a global `fetch()` function which will work in [browsers](https://caniuse.com/?search=fetch) and node v18 or newer.** (In older versions you have to use a polyfill.) | ||
|
@@ -179,7 +189,7 @@ import { fetchWithL402 } from "@getalby/lightning-tools"; | |
await fetchWithL402( | ||
"https://lsat-weather-api.getalby.repl.co/kigali", | ||
{}, | ||
{ store: window.localStorage }, | ||
{ store: window.localStorage } | ||
) | ||
.then((res) => res.json()) | ||
.then(console.log); | ||
|
@@ -198,7 +208,7 @@ const nwc = new webln.NostrWebLNProvider({ | |
await fetchWithL402( | ||
"https://lsat-weather-api.getalby.repl.co/kigali", | ||
{}, | ||
{ webln: nwc }, | ||
{ webln: nwc } | ||
) | ||
.then((res) => res.json()) | ||
.then(console.log); | ||
|
@@ -211,7 +221,7 @@ import { l402 } from "@getalby/lightning-tools"; | |
await l402.fetchWithL402( | ||
"https://lsat-weather-api.getalby.repl.co/kigali", | ||
{}, | ||
{ store: new l402.storage.NoStorage() }, | ||
{ store: new l402.storage.NoStorage() } | ||
); | ||
``` | ||
|
||
|
@@ -262,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: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as crypto from "crypto"; // or 'node:crypto' | ||
global.crypto = crypto; | ||
import { LightningAddress } from "@getalby/lightning-tools"; | ||
|
||
const ln = new LightningAddress("[email protected]"); | ||
|
||
await ln.fetch(); | ||
// request an invoice for 1000 satoshis | ||
// this returns a new `Invoice` class that can also be used to validate the payment | ||
const invoice = await ln.requestInvoice({ satoshi: 1000 }); | ||
|
||
console.log(invoice.paymentRequest); // print the payment request | ||
console.log(invoice.paymentHash); // print the payment hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import jestFetchMock from 'jest-fetch-mock'; | ||
import jestFetchMock from "jest-fetch-mock"; | ||
|
||
jestFetchMock.enableMocks(); | ||
jestFetchMock.enableMocks(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// from https://stackoverflow.com/a/50868276 | ||
export const fromHexString = (hexString: string) => | ||
Uint8Array.from( | ||
hexString.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest | ||
export async function sha256(message: string | Uint8Array) { | ||
// encode as UTF-8 | ||
const msgBuffer = | ||
typeof message === "string" ? new TextEncoder().encode(message) : message; | ||
|
||
// hash the message | ||
const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer); | ||
|
||
// convert ArrayBuffer to Array | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
|
||
// convert bytes to hex string | ||
const hashHex = hashArray | ||
.map((b) => b.toString(16).padStart(2, "0")) | ||
.join(""); | ||
return hashHex; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters