-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add README for nwc client #230
Draft
im-adithya
wants to merge
3
commits into
master
Choose a base branch
from
task-nwc-readme
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,25 @@ or for use without any build tools: | |
|
||
```html | ||
<script type="module"> | ||
import { webln } from "https://esm.sh/@getalby/sdk@3.5.1"; // jsdelivr.net, skypack.dev also work | ||
import { nwc } from "https://esm.sh/@getalby/sdk@3.6.1"; // jsdelivr.net, skypack.dev also work | ||
|
||
(async () => { | ||
const client = new nwc.NWCClient({ | ||
nostrWalletConnectUrl: YOUR_NWC_URL, | ||
}); | ||
const balanceResponse = await client.getBalance(); | ||
console.log("Wallet balance", balanceResponse.balance); | ||
client.close(); | ||
})(); | ||
</script> | ||
``` | ||
|
||
Alternatively, use `NostrWebLNProvider` | ||
|
||
```html | ||
<script type="module"> | ||
import { webln } from "https://esm.sh/@getalby/sdk@3.6.1"; // jsdelivr.net, skypack.dev also work | ||
|
||
// use webln normally... | ||
(async () => { | ||
const nwc = new webln.NostrWebLNProvider({ | ||
nostrWalletConnectUrl: YOUR_NWC_URL, | ||
|
@@ -45,15 +61,175 @@ or for use without any build tools: | |
|
||
## Nostr Wallet Connect Documentation | ||
|
||
Nostr Wallet Connect is an open protocol enabling applications to interact with bitcoin lightning wallets. It allows users to connect their existing wallets to your application allowing developers to easily integrate bitcoin lightning functionality. | ||
[Nostr Wallet Connect](https://nwc.dev) is an open protocol enabling applications to interact with bitcoin lightning wallets. It allows users to connect their existing wallets to your application allowing developers to easily integrate bitcoin lightning functionality. | ||
|
||
The Alby JS SDK allows you to easily integrate Nostr Wallet Connect into any JavaScript based application. | ||
|
||
### NWCClient | ||
|
||
### Options | ||
|
||
- `providerName`: name of the provider to load the default options. currently `alby` (default) | ||
- `nostrWalletConnectUrl`: full Nostr Wallet Connect URL as defined by the [spec](https://github.com/getAlby/nips/blob/master/47.md) | ||
- `relayUrl`: URL of the Nostr relay to be used (e.g. wss://nostr-relay.getalby.com) | ||
- `walletPubkey`: pubkey of the Nostr Wallet Connect app | ||
- `secret`: secret key to sign the request event (if not available window.nostr will be used) | ||
- `authorizationUrl`: URL to the NWC interface for the user to and the app connection | ||
|
||
### Quick start example | ||
|
||
```js | ||
import { nwc } from "@getalby/sdk"; | ||
const nwc = new nwc.NWCClient({ | ||
nostrWalletConnectUrl: loadNWCUrl(), | ||
}); // loadNWCUrl is some function to get the NWC URL from some (encrypted) storage | ||
|
||
// now you can send payments by passing in the invoice in an object | ||
const response = await nwc.payInvoice({ invoice }); | ||
``` | ||
|
||
That's it! Unlike WebLN, you don't even have to enable to connect to the relay. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think people reading this understand this? and this is not the reason to use or not use WebLN. |
||
|
||
### NWCClient Functions | ||
|
||
The `NWCClient` exposes all the methods listed in [NIP-47](https://github.com/nostr-protocol/nips/blob/master/47.md). You can check the usage of each method as listed in the [examples](https://github.com/getAlby/js-sdk/tree/master/examples/nwc/client). | ||
|
||
Please note that although `NWCClient` supports all methods, the NWC connection URI might not have support for some. You can check what all methods are supported by the Wallet Service by using the `getWalletServiceInfo` method like this: | ||
|
||
```js | ||
import { nwc } from "@getalby/sdk"; | ||
const nwc = new nwc.NWCClient({ | ||
nostrWalletConnectUrl: loadNWCUrl(), | ||
}); | ||
|
||
const response = await nwc.getWalletServiceInfo(); | ||
|
||
console.info(response); // { capabilities: ['pay_invoice', 'pay_keysend', 'get_balance', 'get_info', 'make_invoice', 'lookup_invoice', 'list_transactions', 'sign_message', 'notifications'], notifications: ['payment_sent', 'payment_received'] } | ||
``` | ||
|
||
#### `static withNewSecret()` | ||
|
||
Initializes a new `NWCClient` instance but generates a new random secret. The pubkey of that secret then needs to be authorized by the user (this can be initiated by redirecting the user to the `getAuthorizationUrl()` URL or calling `initNWC()` to open an authorization popup). | ||
|
||
##### Example | ||
|
||
```js | ||
const client = nwc.NWCClient.withNewSecret({ | ||
authorizationUrl: "https://nwc.getalby.com/apps/new", | ||
}); | ||
await client.initNWC(); | ||
``` | ||
|
||
#### `getNostrWalletConnectUrl()` | ||
|
||
Returns the `nostr+walletconnect://` URL which includes all the connection information (`walletPubkey`, `relayUrl`, `secret`) | ||
This can be used to get and persist the string for later use. | ||
|
||
#### `initNWC({ name: string })` | ||
|
||
Opens a new window prompt with the `getAuthorizationUrl()` (the user's NWC UI) to ask the user to authorize the app connection. | ||
The promise resolves when the connection is authorized and the popup sends a `nwc:success` message or rejects when the prompt is closed. | ||
Pass a `name` to the NWC provider describing the application. | ||
|
||
```js | ||
const client = nwc.NWCClient.withNewSecret(); | ||
try { | ||
await client.initNWC({ name: "ACME app" }); | ||
} catch (e) { | ||
console.warn("Prompt closed"); | ||
} | ||
let response; | ||
try { | ||
response = await client.payInvoice({ invoice }); | ||
// if success then the response.preimage will be only | ||
console.info(`payment successful, the preimage is ${response.preimage}`); | ||
} catch (e) { | ||
console.error(e.error || e); | ||
} | ||
``` | ||
|
||
#### `payInvoice({ invoice: string })` | ||
|
||
Takes a bolt11 invoice and calls the NWC `pay_invoice` function. | ||
It returns a promise object that is resolved with an object with the preimage or is rejected with an error | ||
|
||
##### Example | ||
|
||
```js | ||
const client = new nwc.NWCClient({ nostrWalletConnectUrl: loadNWCUrl() }); | ||
const response = await client.payInvoice({ invoice }); | ||
console.log(response); | ||
``` | ||
|
||
See other [NIP-47 method examples](https://github.com/getAlby/js-sdk/tree/master/examples/nwc/client). | ||
|
||
#### For Node.js | ||
|
||
To use this on Node.js you first must install `websocket-polyfill` and import it: | ||
|
||
```js | ||
import "websocket-polyfill"; | ||
// or: require('websocket-polyfill'); | ||
``` | ||
|
||
if you get an `crypto is not defined` error, either upgrade to node.js 20 or above, or import it manually: | ||
|
||
```js | ||
import * as crypto from 'crypto'; // or 'node:crypto' | ||
globalThis.crypto = crypto as any; | ||
//or: global.crypto = require('crypto'); | ||
``` | ||
|
||
### Examples | ||
|
||
#### Use a custom, user provided Nostr Wallet Connect URL | ||
|
||
```js | ||
import { nwc } from '@getalby/sdk'; | ||
|
||
const client = new nwc.NWCClient({ nostrWalletConnectUrl: 'nostr+walletconnect://69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9?relay=wss://nostr.bitcoiner.social&secret=c60320b3ecb6c15557510d1518ef41194e9f9337c82621ddef3f979f668bfebd'); | ||
const response = await client.payInvoice({ invoice }); | ||
console.log(response.preimage); | ||
|
||
client.close(); // close the websocket connection | ||
``` | ||
|
||
#### Generate a new NWC connect url using a locally-generated secret | ||
|
||
```js | ||
// same options can be provided to .withNewSecret() as creating a new NWCClient() | ||
const client = nwc.NWCClient.withNewSecret(); | ||
|
||
// use the `initNWC` helper which opens a popup to initiate the connection flow. | ||
// the promise resolves once the NWC app returned. | ||
await client.initNWC({ | ||
name: `My app name`, | ||
}); | ||
|
||
// ... enable and send a payment | ||
|
||
// if you want to get the connect url with the secret: | ||
// const nostrWalletConnectUrl = client.getNostrWalletConnectUrl(true) | ||
``` | ||
|
||
##### NWC Authorization URL Options | ||
|
||
initNWC accepts the following parameters while creating an app | ||
|
||
- `name`: name of the app | ||
- `requestMethods`: NIP-47 request methods required by the app connection | ||
- `returnTo`: string; | ||
- `expiresAt`: app expiration in epoch seconds; | ||
- `maxAmount`: max budget if the app requires `pay_invoice`; | ||
- `budgetRenewal`: renewal of the budget amount if the app requires `pay_invoice`; | ||
|
||
### NostrWebLNProvider (aliased as NWC) | ||
|
||
The `NostrWebLNProvider` exposes the [WebLN](webln.guide/) interface to execute lightning wallet functionality through Nostr Wallet Connect, such as sending payments, making invoices and getting the node balance. | ||
|
||
(Note: in the future more WebLN functions will be added to Nostr Wallet Connect) | ||
|
||
### NostrWebLNProvider (aliased as NWC) Options | ||
### Options | ||
|
||
- `providerName`: name of the provider to load the default options. currently `alby` (default) | ||
- `nostrWalletConnectUrl`: full Nostr Wallet Connect URL as defined by the [spec](https://github.com/getAlby/nips/blob/master/47.md) | ||
|
@@ -98,7 +274,7 @@ The goal of the Nostr Wallet Connect provider is to be API compatible with [webl | |
|
||
#### `static withNewSecret()` | ||
|
||
Initialized a new `NostrWebLNProvider` instance but generates a new random secret. The pubkey of that secret then needs to be authorized by the user (this can be initiated by redirecting the user to the `getAuthorizationUrl()` URL or calling `initNWC()` to open an authorization popup. | ||
Initialized a new `NostrWebLNProvider` instance but generates a new random secret. The pubkey of that secret then needs to be authorized by the user (this can be initiated by redirecting the user to the `getAuthorizationUrl()` URL or calling `initNWC()` to open an authorization popup). | ||
|
||
##### Example | ||
|
||
|
@@ -107,7 +283,7 @@ const nwc = NostrWebLNProvider.withNewSecret(); | |
await nwc.initNWC(); | ||
``` | ||
|
||
#### sendPayment(invice: string) | ||
#### sendPayment(invoice: string) | ||
|
||
Takes a bolt11 invoice and calls the NWC `pay_invoice` function. | ||
It returns a promise object that is resolved with an object with the preimage or is rejected with an error | ||
|
@@ -135,8 +311,8 @@ Pass a `name` to the NWC provider describing the application. | |
```js | ||
const nwc = NostrWebLNProvider.withNewSecret(); | ||
try { | ||
await nwc.initNWC({name: 'ACME app' ); | ||
} catch(e) { | ||
await nwc.initNWC({ name: "ACME app" }); | ||
} catch (e) { | ||
console.warn("Prompt closed"); | ||
} | ||
await nwc.enable(); | ||
|
@@ -145,8 +321,7 @@ try { | |
response = await nwc.sendPayment(invoice); | ||
// if success then the response.preimage will be only | ||
console.info(`payment successful, the preimage is ${response.preimage}`); | ||
} | ||
catch (e) { | ||
} catch (e) { | ||
console.error(e.error || e); | ||
} | ||
``` | ||
|
@@ -223,7 +398,7 @@ await webln.initNWC("alby", { | |
// ... enable and send a payment | ||
|
||
// if you want to get the connect url with the secret: | ||
// const nostrWalletConnectUrl nwc.getNostrWalletConnectUrl(true) | ||
// const nostrWalletConnectUrl = nwc.getNostrWalletConnectUrl(true) | ||
``` | ||
|
||
## OAuth API Documentation | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you fix the headers, they should not all be
###