-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add support for X402 #320
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
Open
bumi
wants to merge
23
commits into
master
Choose a base branch
from
x402
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.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c0a65cd
feat: add support for X402
bumi 42be548
chore: cleanup
bumi 3943ba9
chore: default to no storage
bumi 4814875
feat: add fetch402 function
bumi ed3bbc3
Update README.md
bumi 248dcc7
chore: cleanup and require NWC compatible wallet interface
bumi b3ee2af
test: x402/l402 generated tests
bumi ee426e6
docs: update readme
bumi 401d27f
docs: readme
bumi fe45a87
feat: use proper Header object to better handle http headers
bumi bf14464
fix: ai fixing the tests on CI
bumi e31ce7f
feat: expose millisats amount in Invoice
bumi 1dd777e
feat: new X402 lightning spec
bumi 35e9c99
test: adjust tests
bumi ff358fa
fix: updated paymentMethod, remove preimage
reneaaron ca4bd17
fix: split x402 / l402 + shared utils
reneaaron 27b1b5b
fix: unify handle 402
reneaaron 17e5648
fix: unify examples
reneaaron a26f3c0
fix: formatting
reneaaron 9ec59a7
fix: make examples runnable
reneaaron 92f1923
fix: links
reneaaron 6b02c67
fix: updated readme
reneaaron 3fdf7f5
fix: remove test endpoints
reneaaron 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 hidden or 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 hidden or 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,21 @@ | ||
| import { fetch402 } from "@getalby/lightning-tools/402"; | ||
| import { NWCClient } from "@getalby/sdk"; | ||
|
|
||
| // fetch402 works with both L402 and X402 endpoints — | ||
| // it detects the protocol from the server's response headers automatically. | ||
| const url = process.env.URL || "https://x402.albylabs.com/demo/quote"; | ||
|
|
||
| const nostrWalletConnectUrl = process.env.NWC_URL; | ||
|
|
||
| if (!nostrWalletConnectUrl) { | ||
| throw new Error("Please set a NWC_URL env variable"); | ||
| } | ||
|
|
||
| const nwc = new NWCClient({ nostrWalletConnectUrl }); | ||
|
|
||
| fetch402(url, {}, { wallet: nwc }) | ||
| .then((response) => response.json()) | ||
| .then((data) => { | ||
| console.info(data); | ||
| }) | ||
| .finally(() => nwc.close()); |
This file contains hidden or 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,52 @@ | ||
| # Examples | ||
|
|
||
| Runnable examples for `@getalby/lightning-tools`. | ||
|
|
||
| ## Setup | ||
|
|
||
| ```bash | ||
| cd examples | ||
| yarn | ||
| ``` | ||
|
|
||
| > `yarn` links the local package via `file:..` — if you change library source, rebuild first: | ||
| > | ||
| > ```bash | ||
| > cd .. && yarn build && cd examples | ||
| > ``` | ||
|
|
||
| ## Running examples | ||
|
|
||
| ### x402 / L402 — paid API fetch | ||
|
|
||
| These examples fetch a URL protected by an HTTP 402 payment wall. You need an [NWC](https://www.nwc.dev) connection string. | ||
|
|
||
| ```bash | ||
| NWC_URL="nostr+walletconnect://..." yarn 402 # auto-detects L402 or x402 | ||
| NWC_URL="nostr+walletconnect://..." yarn x402 # x402 only | ||
| NWC_URL="nostr+walletconnect://..." yarn l402 # L402 only | ||
| ``` | ||
|
|
||
| Override the default URL: | ||
|
|
||
| ```bash | ||
| URL="https://your-402-endpoint.example.com" NWC_URL="nostr+walletconnect://..." yarn 402 | ||
| ``` | ||
|
|
||
| ### Lightning Address — request invoice | ||
|
|
||
| Fetches LNURL-pay data for a lightning address and requests an invoice. No credentials needed. | ||
|
|
||
| ```bash | ||
| yarn request-invoice | ||
| ``` | ||
|
|
||
| ### Zaps via NWC | ||
|
|
||
| Sends a zap to a lightning address using NWC for payment and a Nostr key for signing. | ||
|
|
||
| ```bash | ||
| NOSTR_PRIVATE_KEY="your-hex-private-key" NWC_URL="nostr+walletconnect://..." yarn zaps-nwc | ||
| ``` | ||
|
|
||
| > **`zaps.js`** is a browser-only example (uses `window.webln`) and is not runnable as a Node script. |
This file contains hidden or 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,25 +1,19 @@ | ||
| import { fetchWithL402 } from "@getalby/lightning-tools/l402"; | ||
| import { NostrWebLNProvider } from "@getalby/sdk"; | ||
| import "websocket-polyfill"; | ||
| import { NWCClient } from "@getalby/sdk"; | ||
|
|
||
| const url = "https://lsat-weather-api.getalby.repl.co/kigali"; | ||
| const url = process.env.URL || "https://l402.example.com/protected-resource"; | ||
|
|
||
| const nostrWalletConnectUrl = process.env.NWC_URL; | ||
|
|
||
| if (!nostrWalletConnectUrl) { | ||
| throw new Error("Please set a NWC_URL env variable"); | ||
| } | ||
|
|
||
| const nwc = new NostrWebLNProvider({ | ||
| nostrWalletConnectUrl, | ||
| }); | ||
| nwc.on("sendPayment", (response) => { | ||
| console.info(`payment response:`, response); | ||
| }); | ||
| const nwc = new NWCClient({ nostrWalletConnectUrl }); | ||
|
|
||
| fetchWithL402(url, {}, { wallet: nwc }) | ||
| .then((response) => response.json()) | ||
| .then((data) => { | ||
| console.info(data); | ||
| nwc.close(); | ||
| }); | ||
| }) | ||
| .finally(() => nwc.close()); |
This file contains hidden or 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 @@ | ||
| { | ||
| "name": "@getalby/lightning-tools-examples", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "402": "node 402.js", | ||
| "l402": "node l402.js", | ||
| "x402": "node x402.js", | ||
| "request-invoice": "node request-invoice.js", | ||
| "zaps-nwc": "node zaps-nwc.js" | ||
| }, | ||
| "dependencies": { | ||
| "@getalby/lightning-tools": "file:..", | ||
| "@getalby/sdk": "^7.0.0", | ||
| "@noble/hashes": "^2.0.1", | ||
| "nostr-tools": "^2.16.1" | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| import { fetchWithX402 } from "@getalby/lightning-tools/x402"; | ||
| import { NWCClient } from "@getalby/sdk"; | ||
|
|
||
| const url = process.env.URL || "https://x402.albylabs.com/demo/quote"; | ||
|
|
||
| const nostrWalletConnectUrl = process.env.NWC_URL; | ||
|
|
||
| if (!nostrWalletConnectUrl) { | ||
| throw new Error("Please set a NWC_URL env variable"); | ||
| } | ||
|
|
||
| const nwc = new NWCClient({ nostrWalletConnectUrl }); | ||
|
|
||
| fetchWithX402(url, {}, { wallet: nwc }) | ||
| .then((response) => response.json()) | ||
| .then((data) => { | ||
| console.info(data); | ||
| }) | ||
| .finally(() => nwc.close()); |
This file contains hidden or 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,76 @@ | ||
| # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
| # yarn lockfile v1 | ||
|
|
||
|
|
||
| "@getalby/lightning-tools@^6.0.0": | ||
| version "6.1.0" | ||
| resolved "https://registry.yarnpkg.com/@getalby/lightning-tools/-/lightning-tools-6.1.0.tgz#558b90a83b961cb6aa760e62de69f5b5ceeb2fe9" | ||
| integrity sha512-rGurar9X4Gm+9xwoNYS8s9YLK7ZYqvbqv4KbHLYV0LEeB0HxZHRgmxblGqg+fYfp6iiYHx+edIgUpt9rS3VwFw== | ||
|
|
||
| "@getalby/lightning-tools@file:..": | ||
| version "7.0.2" | ||
|
|
||
| "@getalby/sdk@^7.0.0": | ||
| version "7.0.0" | ||
| resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-7.0.0.tgz#6ab17f27bd9e762d383b70cfabd0cdeeded6bd53" | ||
| integrity sha512-0c8gyvFbRDHZIgHmOD/dfyPukxZLeidx/hx7SXlMIS/hsx4mXpKpo9Gx1zW90buElnd3k9TVB/S/bnFSEZPE7w== | ||
| dependencies: | ||
| "@getalby/lightning-tools" "^6.0.0" | ||
| nostr-tools "^2.17.0" | ||
|
|
||
| "@noble/ciphers@2.1.1": | ||
| version "2.1.1" | ||
| resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-2.1.1.tgz#c8c74fcda8c3d1f88797d0ecda24f9fc8b92b052" | ||
| integrity sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw== | ||
|
|
||
| "@noble/curves@2.0.1": | ||
| version "2.0.1" | ||
| resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-2.0.1.tgz#64ba8bd5e8564a02942655602515646df1cdb3ad" | ||
| integrity sha512-vs1Az2OOTBiP4q0pwjW5aF0xp9n4MxVrmkFBxc6EKZc6ddYx5gaZiAsZoq0uRRXWbi3AT/sBqn05eRPtn1JCPw== | ||
| dependencies: | ||
| "@noble/hashes" "2.0.1" | ||
|
|
||
| "@noble/hashes@2.0.1", "@noble/hashes@^2.0.1": | ||
| version "2.0.1" | ||
| resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-2.0.1.tgz#fc1a928061d1232b0a52bb754393c37a5216c89e" | ||
| integrity sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw== | ||
|
|
||
| "@scure/base@2.0.0": | ||
| version "2.0.0" | ||
| resolved "https://registry.yarnpkg.com/@scure/base/-/base-2.0.0.tgz#ba6371fddf92c2727e88ad6ab485db6e624f9a98" | ||
| integrity sha512-3E1kpuZginKkek01ovG8krQ0Z44E3DHPjc5S2rjJw9lZn3KSQOs8S7wqikF/AH7iRanHypj85uGyxk0XAyC37w== | ||
|
|
||
| "@scure/bip32@2.0.1": | ||
| version "2.0.1" | ||
| resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-2.0.1.tgz#4ceea207cee8626d3fe8f0b6ab68b6af8f81c482" | ||
| integrity sha512-4Md1NI5BzoVP+bhyJaY3K6yMesEFzNS1sE/cP+9nuvE7p/b0kx9XbpDHHFl8dHtufcbdHRUUQdRqLIPHN/s7yA== | ||
| dependencies: | ||
| "@noble/curves" "2.0.1" | ||
| "@noble/hashes" "2.0.1" | ||
| "@scure/base" "2.0.0" | ||
|
|
||
| "@scure/bip39@2.0.1": | ||
| version "2.0.1" | ||
| resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-2.0.1.tgz#47a6dc15e04faf200041239d46ae3bb7c3c96add" | ||
| integrity sha512-PsxdFj/d2AcJcZDX1FXN3dDgitDDTmwf78rKZq1a6c1P1Nan1X/Sxc7667zU3U+AN60g7SxxP0YCVw2H/hBycg== | ||
| dependencies: | ||
| "@noble/hashes" "2.0.1" | ||
| "@scure/base" "2.0.0" | ||
|
|
||
| nostr-tools@^2.16.1, nostr-tools@^2.17.0: | ||
| version "2.23.3" | ||
| resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-2.23.3.tgz#1a7501988b72499cf27c8f3951f00d11d9ac6025" | ||
| integrity sha512-AALyt9k8xPdF4UV2mlLJ2mgCn4kpTB0DZ8t2r6wjdUh6anfx2cTVBsHUlo9U0EY/cKC5wcNyiMAmRJV5OVEalA== | ||
| dependencies: | ||
| "@noble/ciphers" "2.1.1" | ||
| "@noble/curves" "2.0.1" | ||
| "@noble/hashes" "2.0.1" | ||
| "@scure/base" "2.0.0" | ||
| "@scure/bip32" "2.0.1" | ||
| "@scure/bip39" "2.0.1" | ||
| nostr-wasm "0.1.0" | ||
|
|
||
| nostr-wasm@0.1.0: | ||
| version "0.1.0" | ||
| resolved "https://registry.yarnpkg.com/nostr-wasm/-/nostr-wasm-0.1.0.tgz#17af486745feb2b7dd29503fdd81613a24058d94" | ||
| integrity sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA== |
This file contains hidden or 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
Oops, something went wrong.
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.
Why is that? 🤔
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.
why is what?