@getalby/lightning-tools 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +261 -0
- package/dist/index.browser.js +1833 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.modern.js +2 -0
- package/dist/index.modern.js.map +1 -0
- package/dist/index.module.js +2 -0
- package/dist/index.module.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/invoice.d.ts +12 -0
- package/dist/invoice.d.ts.map +1 -0
- package/dist/l402/index.d.ts +4 -0
- package/dist/l402/index.d.ts.map +1 -0
- package/dist/l402/parse.d.ts +4 -0
- package/dist/l402/parse.d.ts.map +1 -0
- package/dist/lightning-address.d.ts +39 -0
- package/dist/lightning-address.d.ts.map +1 -0
- package/dist/lightning-address.test.d.ts +2 -0
- package/dist/lightning-address.test.d.ts.map +1 -0
- package/dist/podcasting2/boostagrams.d.ts +26 -0
- package/dist/podcasting2/boostagrams.d.ts.map +1 -0
- package/dist/types.d.ts +92 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/fiat.d.ts +15 -0
- package/dist/utils/fiat.d.ts.map +1 -0
- package/dist/utils/invoice.d.ts +2 -0
- package/dist/utils/invoice.d.ts.map +1 -0
- package/dist/utils/keysend.d.ts +3 -0
- package/dist/utils/keysend.d.ts.map +1 -0
- package/dist/utils/lnurl.d.ts +9 -0
- package/dist/utils/lnurl.d.ts.map +1 -0
- package/dist/utils/lnurl.test.d.ts +2 -0
- package/dist/utils/lnurl.test.d.ts.map +1 -0
- package/dist/utils/nostr.d.ts +7 -0
- package/dist/utils/nostr.d.ts.map +1 -0
- package/dist/utils/storage.d.ts +13 -0
- package/dist/utils/storage.d.ts.map +1 -0
- package/dist/window.d.ts +2 -0
- package/dist/window.d.ts.map +1 -0
- package/dist/window.js +3 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Alby contributors <hello@getalby.com> (https://getalby.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="100%" src="docs/Header.png">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Lightning Web SDK
|
|
6
|
+
|
|
7
|
+
An npm package that provides useful and common tools and helpers to build lightning web applications.
|
|
8
|
+
|
|
9
|
+
## 🚀 Quick Start
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @getalby/lightning-tools
|
|
13
|
+
```
|
|
14
|
+
or
|
|
15
|
+
```
|
|
16
|
+
yarn add @getalby/lightning-tools
|
|
17
|
+
```
|
|
18
|
+
or for use without any build tools:
|
|
19
|
+
```
|
|
20
|
+
// lightning-tools now available at window.lightningTools
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/@getalby/lightning-tools@latest/dist/index.browser.js"></script>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**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.)
|
|
25
|
+
|
|
26
|
+
## 🤙 Usage
|
|
27
|
+
|
|
28
|
+
### Lightning Address
|
|
29
|
+
|
|
30
|
+
The `LightningAddress` class provides helpers to work with lightning addresses
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import { LightningAddress } from "@getalby/lightning-tools";
|
|
34
|
+
|
|
35
|
+
const ln = new LightningAddress("hello@getalby.com");
|
|
36
|
+
|
|
37
|
+
// fetch the LNURL data
|
|
38
|
+
await ln.fetch();
|
|
39
|
+
|
|
40
|
+
// get the LNURL-pay data:
|
|
41
|
+
console.log(ln.lnurlpData); // returns a [LNURLPayResponse](https://github.com/getAlby/js-lightning-tools/blob/master/src/types.ts#L1-L15)
|
|
42
|
+
// get the keysend data:
|
|
43
|
+
console.log(ln.keysendData);
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Get an invoice:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import { LightningAddress } from "@getalby/lightning-tools";
|
|
51
|
+
|
|
52
|
+
const ln = new LightningAddress("hello@getalby.com");
|
|
53
|
+
|
|
54
|
+
await ln.fetch();
|
|
55
|
+
// request an invoice for 1000 satoshis
|
|
56
|
+
// this returns a new `Invoice` class that can also be used to validate the payment
|
|
57
|
+
const invoice = await ln.requestInvoice({satoshi: 1000});
|
|
58
|
+
|
|
59
|
+
console.log(invoice.paymentRequest); // print the payment request
|
|
60
|
+
console.log(invoice.paymentHash); // print the payment hash
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Verify a payment
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
import { LightningAddress } from "@getalby/lightning-tools";
|
|
67
|
+
const ln = new LightningAddress("hello@getalby.com");
|
|
68
|
+
await ln.fetch();
|
|
69
|
+
|
|
70
|
+
const invoice = await ln.requestInvoice({satoshi: 1000});
|
|
71
|
+
|
|
72
|
+
// if the LNURL providers supports LNURL-verify:
|
|
73
|
+
const paid = await invoice.verifyPayment(); // returns true of false
|
|
74
|
+
if (paid) {
|
|
75
|
+
console.log(invoice.preimage);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// if you have the preimage for example in a WebLN context
|
|
79
|
+
await window.webln.enable();
|
|
80
|
+
const response = await window.webln.sendPayment(invoice.paymentRequest);
|
|
81
|
+
const paid = invoice.validatePreimage(response.preimage); // returns true or false
|
|
82
|
+
if (paid) {
|
|
83
|
+
console.log('paid');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// or use the convenenice method:
|
|
87
|
+
await invoice.isPaid();
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
It is also possible to manually initialize the `Invoice`
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const { Invoice } = require("alby-tools");
|
|
95
|
+
|
|
96
|
+
const invoice = new Invoice({paymentRequest: pr, preimage: preimage});
|
|
97
|
+
await invoice.isPaid();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Boost a LN address:
|
|
101
|
+
|
|
102
|
+
You can also attach additional metadata information like app name, version, name of the podcast which is boosted etc. to the keysend payment.
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
import { LightningAddress } from "@getalby/lightning-tools";
|
|
106
|
+
const ln = new LightningAddress("hello@getalby.com");
|
|
107
|
+
await ln.fetch();
|
|
108
|
+
|
|
109
|
+
const boost = {
|
|
110
|
+
action: "boost",
|
|
111
|
+
value_msat: 21000,
|
|
112
|
+
value_msat_total: 21000,
|
|
113
|
+
app_name: "Podcastr",
|
|
114
|
+
app_version: "v2.1",
|
|
115
|
+
feedId: "21",
|
|
116
|
+
podcast: "random podcast",
|
|
117
|
+
episode: "1",
|
|
118
|
+
ts: 2121,
|
|
119
|
+
name: "Satoshi",
|
|
120
|
+
sender_name: "Alby",
|
|
121
|
+
}
|
|
122
|
+
await ln.boost(boost);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Zapping a LN address on Nostr:
|
|
126
|
+
|
|
127
|
+
Nostr is a simple, open protocol that enables truly censorship-resistant and global value-for-value publishing on the web. Nostr integrates deeply with Lightning. [more info](https://nostr.how/)
|
|
128
|
+
|
|
129
|
+
This librarys provides helpers to create [zaps](https://github.com/nostr-protocol/nips/blob/master/57.md).
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
import { LightningAddress } from "@getalby/lightning-tools";
|
|
133
|
+
const ln = new LightningAddress("hello@getalby.com");
|
|
134
|
+
await ln.fetch();
|
|
135
|
+
|
|
136
|
+
const response = await ln.zap({
|
|
137
|
+
satoshi: 1000,
|
|
138
|
+
comment: "Awesome post",
|
|
139
|
+
relays: ["wss://relay.damus.io"],
|
|
140
|
+
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"
|
|
141
|
+
});
|
|
142
|
+
console.log(response.preimage); // print the preimage
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
For a full example see [examples/zaps](examples/zaps.js)
|
|
146
|
+
|
|
147
|
+
#### Zapping a LN address on Nostr using Nostr Wallet Connect:
|
|
148
|
+
|
|
149
|
+
Native zaps without a browser extension are possible by using a Nostr Wallet Connect WebLN provider.
|
|
150
|
+
|
|
151
|
+
See [examples/zaps-nwc](examples/zaps-nwc.js)
|
|
152
|
+
|
|
153
|
+
### L402
|
|
154
|
+
|
|
155
|
+
L402 is a protocol standard based on the HTTP 402 Payment Required error code
|
|
156
|
+
designed to support the use case of charging for services and
|
|
157
|
+
authenticating users in distributed networks.
|
|
158
|
+
|
|
159
|
+
This library includes a `fetchWithL402` function to consume L402 protected resources.
|
|
160
|
+
|
|
161
|
+
#### fetchWithL402(url: string, fetchArgs, options)
|
|
162
|
+
|
|
163
|
+
+ url: the L402 protected URL
|
|
164
|
+
+ fetchArgs: arguments are passed to the underlaying `fetch()` function used to do the HTTP request
|
|
165
|
+
+ options:
|
|
166
|
+
+ webln: the webln object used to call `sendPayment()` defaults to globalThis.webln
|
|
167
|
+
+ store: a key/value store object to persiste the l402 for each URL. The store must implement a `getItem()`/`setItem()` function as the browser's localStorage. By default a memory storage is used.
|
|
168
|
+
+ headerKey: defaults to L402 but if you need to consume an old LSAT API set this to LSAT
|
|
169
|
+
|
|
170
|
+
##### Examples
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
import { fetchWithL402 } from "@getalby/lightning-tools";
|
|
174
|
+
|
|
175
|
+
// this will fetch the resouce and pay the invoice with window.webln.
|
|
176
|
+
// the tokens/preimage data will be stored in the browser's localStorage and used for any following request
|
|
177
|
+
await fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { store: window.localStorage }).then(res => res.json()).then(console.log)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
import { fetchWithL402 } from "@getalby/lightning-tools";
|
|
182
|
+
import { webln } from "alby-js-sdk";
|
|
183
|
+
|
|
184
|
+
// use a NWC WebLN provide to do the payments
|
|
185
|
+
const nwc = new webln.NostrWebLNProvider({ nostrWalletConnectUrl: loadNWCUrl() });
|
|
186
|
+
|
|
187
|
+
// this will fetch the resouce and pay the invoice with a NWC webln object
|
|
188
|
+
await fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { webln: nwc }).then(res => res.json()).then(console.log)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
import { l402 } from "@getalby/lightning-tools";
|
|
193
|
+
|
|
194
|
+
// do not store the tokens
|
|
195
|
+
await l402.fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { store: new l402.storage.NoStorage() })
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 💵 Fiat conversions
|
|
199
|
+
Helpers to convert sats values to fiat and fiat values to sats.
|
|
200
|
+
|
|
201
|
+
##### getFiatValue(satoshi: number, currency: string): number
|
|
202
|
+
Returns the fiat value for a specified currrency of a satoshi amount
|
|
203
|
+
|
|
204
|
+
##### getSatoshiValue(amount: number, currency: string): number
|
|
205
|
+
Returns the satoshi value for a specified amount (in the smallest denomination) and currency
|
|
206
|
+
|
|
207
|
+
##### getFormattedFiatValue(satoshi: number, currency: string, locale: string): string
|
|
208
|
+
Like `getFiatValue` but returns a formatted string for a given locale using JavaScript's `toLocaleString`
|
|
209
|
+
|
|
210
|
+
#### Examples
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
await getFiatValue(satoshi: 2100, currency: 'eur');
|
|
214
|
+
await getSatoshiValue(amount: 100, currency: 'eur'); // for 1 EUR
|
|
215
|
+
await getFormattedFiatValue(stoshi: 2100, currency: 'usd', locale: 'en')
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 🤖 Lightning Address Proxy
|
|
219
|
+
This library uses a [proxy](https://github.com/getAlby/lightning-address-details-proxy) to simplify requests to lightning providers.
|
|
220
|
+
|
|
221
|
+
- Many ln addresses don't support CORS, which means fetching the data directly in a browser environment will not always work.
|
|
222
|
+
- Two requests are required to retrieve lnurlp and keysend data for a lightning address. The proxy will do these for you with a single request.
|
|
223
|
+
|
|
224
|
+
You can disable the proxy by explicitly setting the proxy to false when initializing a lightning address:
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
const lightningAddress = new LightningAddress("hello@getalby.com", {proxy: false});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## fetch() dependency
|
|
231
|
+
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.
|
|
232
|
+
|
|
233
|
+
For example:
|
|
234
|
+
```js
|
|
235
|
+
import fetch from "cross-fetch"; // or "@inrupt/universal-fetch"
|
|
236
|
+
globalThis.fetch = fetch;
|
|
237
|
+
|
|
238
|
+
// or as a polyfill:
|
|
239
|
+
import 'cross-fetch/polyfill';
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## 🛠Development
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
yarn install
|
|
246
|
+
yarn run build
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Need help?
|
|
250
|
+
|
|
251
|
+
We are happy to help, please contact us or create an issue.
|
|
252
|
+
|
|
253
|
+
* [Twitter: @getAlby](https://twitter.com/getAlby)
|
|
254
|
+
* [Telegram group](https://t.me/getAlby)
|
|
255
|
+
* support at getalby.com
|
|
256
|
+
* [bitcoin.design](https://bitcoin.design/) Discord community (find us on the #alby channel)
|
|
257
|
+
* Read the [Alby developer guide](https://guides.getalby.com/overall-guide/alby-for-developers/getting-started) to better understand how Alby packages and APIs can be used to power your app.
|
|
258
|
+
|
|
259
|
+
## License
|
|
260
|
+
|
|
261
|
+
MIT
|