Skip to content

Commit

Permalink
Merge pull request #59 from pavanjoshi914/add-linting
Browse files Browse the repository at this point in the history
feat: format files
  • Loading branch information
rolznz committed Aug 20, 2023
2 parents f5a176e + ca33df7 commit 5c44074
Show file tree
Hide file tree
Showing 21 changed files with 629 additions and 422 deletions.
75 changes: 51 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ An npm package that provides useful and common tools and helpers to build lightn
```
npm install @getalby/lightning-tools
```

or

```
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>
Expand All @@ -41,7 +45,6 @@ await ln.fetch();
console.log(ln.lnurlpData); // returns a [LNURLPayResponse](https://github.com/getAlby/js-lightning-tools/blob/master/src/types.ts#L1-L15)
// get the keysend data:
console.log(ln.keysendData);

```

#### Get an invoice:
Expand All @@ -54,7 +57,7 @@ const ln = new LightningAddress("hello@getalby.com");
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});
const invoice = await ln.requestInvoice({ satoshi: 1000 });

console.log(invoice.paymentRequest); // print the payment request
console.log(invoice.paymentHash); // print the payment hash
Expand All @@ -67,7 +70,7 @@ import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();

const invoice = await ln.requestInvoice({satoshi: 1000});
const invoice = await ln.requestInvoice({ satoshi: 1000 });

// if the LNURL providers supports LNURL-verify:
const paid = await invoice.verifyPayment(); // returns true of false
Expand All @@ -80,20 +83,19 @@ await window.webln.enable();
const response = await window.webln.sendPayment(invoice.paymentRequest);
const paid = invoice.validatePreimage(response.preimage); // returns true or false
if (paid) {
console.log('paid');
console.log("paid");
}

// or use the convenenice method:
await invoice.isPaid();

```

It is also possible to manually initialize the `Invoice`

```js
const { Invoice } = require("alby-tools");

const invoice = new Invoice({paymentRequest: pr, preimage: preimage});
const invoice = new Invoice({ paymentRequest: pr, preimage: preimage });
await invoice.isPaid();
```

Expand All @@ -118,7 +120,7 @@ const boost = {
ts: 2121,
name: "Satoshi",
sender_name: "Alby",
}
};
await ln.boost(boost);
```

Expand All @@ -137,7 +139,7 @@ const response = await ln.zap({
satoshi: 1000,
comment: "Awesome post",
relays: ["wss://relay.damus.io"],
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
});
console.log(response.preimage); // print the preimage
```
Expand All @@ -160,12 +162,12 @@ This library includes a `fetchWithL402` function to consume L402 protected resou

#### fetchWithL402(url: string, fetchArgs, options)

+ url: the L402 protected URL
+ fetchArgs: arguments are passed to the underlaying `fetch()` function used to do the HTTP request
+ options:
+ webln: the webln object used to call `sendPayment()` defaults to globalThis.webln
+ 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.
+ headerKey: defaults to L402 but if you need to consume an old LSAT API set this to LSAT
- url: the L402 protected URL
- fetchArgs: arguments are passed to the underlaying `fetch()` function used to do the HTTP request
- options:
- webln: the webln object used to call `sendPayment()` defaults to globalThis.webln
- 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.
- headerKey: defaults to L402 but if you need to consume an old LSAT API set this to LSAT

##### Examples

Expand All @@ -174,37 +176,59 @@ import { fetchWithL402 } from "@getalby/lightning-tools";

// this will fetch the resouce and pay the invoice with window.webln.
// the tokens/preimage data will be stored in the browser's localStorage and used for any following request
await fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { store: window.localStorage }).then(res => res.json()).then(console.log)
await fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ store: window.localStorage },
)
.then((res) => res.json())
.then(console.log);
```

```js
import { fetchWithL402 } from "@getalby/lightning-tools";
import { webln } from "alby-js-sdk";

// use a NWC WebLN provide to do the payments
const nwc = new webln.NostrWebLNProvider({ nostrWalletConnectUrl: loadNWCUrl() });
const nwc = new webln.NostrWebLNProvider({
nostrWalletConnectUrl: loadNWCUrl(),
});

// this will fetch the resouce and pay the invoice with a NWC webln object
await fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { webln: nwc }).then(res => res.json()).then(console.log)
await fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ webln: nwc },
)
.then((res) => res.json())
.then(console.log);
```

```js
import { l402 } from "@getalby/lightning-tools";

// do not store the tokens
await l402.fetchWithL402('https://lsat-weather-api.getalby.repl.co/kigali', {}, { store: new l402.storage.NoStorage() })
await l402.fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ store: new l402.storage.NoStorage() },
);
```

### 💵 Fiat conversions

Helpers to convert sats values to fiat and fiat values to sats.

##### getFiatValue(satoshi: number, currency: string): number

Returns the fiat value for a specified currrency of a satoshi amount

##### getSatoshiValue(amount: number, currency: string): number

Returns the satoshi value for a specified amount (in the smallest denomination) and currency

##### getFormattedFiatValue(satoshi: number, currency: string, locale: string): string

Like `getFiatValue` but returns a formatted string for a given locale using JavaScript's `toLocaleString`

#### Examples
Expand All @@ -216,6 +240,7 @@ await getFormattedFiatValue(stoshi: 2100, currency: 'usd', locale: 'en')
```

### 🤖 Lightning Address Proxy

This library uses a [proxy](https://github.com/getAlby/lightning-address-details-proxy) to simplify requests to lightning providers.

- Many ln addresses don't support CORS, which means fetching the data directly in a browser environment will not always work.
Expand All @@ -228,15 +253,17 @@ const lightningAddress = new LightningAddress("hello@getalby.com", {proxy: false
```

## 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.

For example:

```js
import fetch from "cross-fetch"; // or "@inrupt/universal-fetch"
globalThis.fetch = fetch;

// or as a polyfill:
import 'cross-fetch/polyfill';
import "cross-fetch/polyfill";
```

## 🛠 Development
Expand All @@ -250,11 +277,11 @@ yarn run build

We are happy to help, please contact us or create an issue.

* [Twitter: @getAlby](https://twitter.com/getAlby)
* [Telegram group](https://t.me/getAlby)
* support at getalby.com
* [bitcoin.design](https://bitcoin.design/) Discord community (find us on the #alby channel)
* 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.
- [Twitter: @getAlby](https://twitter.com/getAlby)
- [Telegram group](https://t.me/getAlby)
- support at getalby.com
- [bitcoin.design](https://bitcoin.design/) Discord community (find us on the #alby channel)
- 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.

## License

Expand Down
19 changes: 11 additions & 8 deletions examples/l402.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import { fetchWithL402 } from "@getalby/lightning-tools";
import { webln } from "alby-js-sdk";
import 'websocket-polyfill';
import "websocket-polyfill";
import * as crypto from "crypto";
globalThis.crypto = crypto;

Expand All @@ -13,12 +12,16 @@ if (!nostrWalletConnectUrl) {
throw new Error("Please set a NWC_URL env variable");
}

const nostrWeblnProvider = new webln.NostrWebLNProvider({ nostrWalletConnectUrl })
nostrWeblnProvider.on('sendPayment', (response) => {
const nostrWeblnProvider = new webln.NostrWebLNProvider({
nostrWalletConnectUrl,
});
nostrWeblnProvider.on("sendPayment", (response) => {
console.log(`payment response:`, response);
});

fetchWithL402(url, {}, {webln: nostrWeblnProvider}).then(response => response.json()).then((data) => {
console.log(data);
nostrWeblnProvider.close();
})
fetchWithL402(url, {}, { webln: nostrWeblnProvider })
.then((response) => response.json())
.then((data) => {
console.log(data);
nostrWeblnProvider.close();
});
25 changes: 13 additions & 12 deletions examples/zaps-nwc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LightningAddress } from "@getalby/lightning-tools";
import { webln } from "alby-js-sdk";
import 'websocket-polyfill';
import "websocket-polyfill";
import * as crypto from "crypto";
import { finishEvent, getPublicKey } from "nostr-tools";
globalThis.crypto = crypto;
Expand All @@ -17,31 +17,32 @@ if (!nostrPrivateKey || !nostrWalletConnectUrl) {
}

(async () => {

const nostrWeblnProvider = new webln.NostrWebLNProvider({ nostrWalletConnectUrl })
const nostrWeblnProvider = new webln.NostrWebLNProvider({
nostrWalletConnectUrl,
});
// or use nostrWeblnProvider.initNWC(); to get a new NWC url
const nostrProvider = {
getPublicKey: () => Promise.resolve(getPublicKey(nostrPrivateKey)),
signEvent: (event) => Promise.resolve(finishEvent(event, nostrPrivateKey)),
}
};

const ln = new LightningAddress("hello@getalby.com", {
webln: nostrWeblnProvider,
});
await ln.fetch();

if (!ln.nostrPubkey) {
throw new Error('No nostr pubkey available'); // seems the lightning address is no NIP05 address
throw new Error("No nostr pubkey available"); // seems the lightning address is no NIP05 address
}

const zapArgs = {
satoshi: 1000,
comment: "Awesome post",
relays: ["wss://relay.damus.io"],
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"
}
const response = await ln.zap(zapArgs, {nostr: nostrProvider}); // generates a zap invoice
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
};

const response = await ln.zap(zapArgs, { nostr: nostrProvider }); // generates a zap invoice
console.log("Preimage", response.preimage); // print the preimage
nostrWeblnProvider.close();
})();
})();
41 changes: 20 additions & 21 deletions examples/zaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ import { LightningAddress } from "@getalby/lightning-tools";

(async () => {
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();
await ln.fetch();

if (!ln.nostrPubkey) {
alert('No nostr pubkey available'); // seems the lightning address is no NIP05 address
}
if (!ln.nostrPubkey) {
alert("No nostr pubkey available"); // seems the lightning address is no NIP05 address
}

const zapArgs = {
satoshi: 1000,
comment: "Awesome post",
relays: ["wss://relay.damus.io"],
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" // optional, omit to zap profile directly
}
const zapArgs = {
satoshi: 1000,
comment: "Awesome post",
relays: ["wss://relay.damus.io"],
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245", // optional, omit to zap profile directly
};

if (window.webln) {
// zap in one go with WebLN (https://www.webln.guide) (easiest for web apps)
const response = await ln.zap(zapArgs); // signs zap request event, generates invoice and pays it
console.log(response.preimage); // print the preimage
}
else {
// or manually (create an invoice and give it to the user to pay)
const invoice = await ln.zapInvoice(zapArgs); // generates a zap invoice
console.log(invoice.paymentRequest); // print the payment request
await invoice.isPaid(); // check the payment status as descibed above
}
if (window.webln) {
// zap in one go with WebLN (https://www.webln.guide) (easiest for web apps)
const response = await ln.zap(zapArgs); // signs zap request event, generates invoice and pays it
console.log(response.preimage); // print the preimage
} else {
// or manually (create an invoice and give it to the user to pay)
const invoice = await ln.zapInvoice(zapArgs); // generates a zap invoice
console.log(invoice.paymentRequest); // print the payment request
await invoice.isPaid(); // check the payment status as descibed above
}
})();
4 changes: 2 additions & 2 deletions prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"trailingComma": "es5"
}
"trailingComma": "es5"
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * as boostagrams from "./podcasting2/boostagrams";
export * as fiat from "./utils/fiat";
export * as nostr from "./utils/nostr";
export { fetchWithL402, sendBoostagram, LightningAddress, Invoice };
export * from './types';
export * from "./types";
Loading

0 comments on commit 5c44074

Please sign in to comment.