@getalby/cli
Advanced tools
| import { fetch402 } from "../tools/lightning/fetch.js"; | ||
| import { getClient, handleError, output } from "../utils.js"; | ||
| export function registerFetch402Command(program) { | ||
| program | ||
| .command("fetch") | ||
| .description("Fetch a payment-protected resource (auto-detects L402, X402, MPP)") | ||
| .requiredOption("-u, --url <url>", "URL to fetch") | ||
| .option("-m, --method <method>", "HTTP method (GET, POST, etc.)") | ||
| .option("-b, --body <json>", "Request body (JSON string)") | ||
| .option("-H, --headers <json>", "Additional headers (JSON string)") | ||
| .option("--max-amount <sats>", "Maximum amount in sats to pay per request. Aborts if the endpoint requests more. (default: 5000, 0 = no limit)", parseInt) | ||
| .action(async (options) => { | ||
| await handleError(async () => { | ||
| const client = await getClient(program); | ||
| const result = await fetch402(client, { | ||
| url: options.url, | ||
| method: options.method, | ||
| body: options.body, | ||
| headers: options.headers ? JSON.parse(options.headers) : undefined, | ||
| maxAmountSats: options.maxAmount, | ||
| }); | ||
| output(result); | ||
| }); | ||
| }); | ||
| } |
| import { fetch402 as fetch402Lib } from "@getalby/lightning-tools/402"; | ||
| const DEFAULT_MAX_AMOUNT_SATS = 5000; | ||
| export async function fetch402(client, params) { | ||
| const method = params.method?.toUpperCase(); | ||
| const requestOptions = { | ||
| method, | ||
| }; | ||
| if (method && method !== "GET" && method !== "HEAD") { | ||
| requestOptions.body = params.body; | ||
| requestOptions.headers = { | ||
| "Content-Type": "application/json", | ||
| ...params.headers, | ||
| }; | ||
| } | ||
| else if (params.headers) { | ||
| requestOptions.headers = params.headers; | ||
| } | ||
| const maxAmountSats = params.maxAmountSats ?? DEFAULT_MAX_AMOUNT_SATS; | ||
| const result = await fetch402Lib(params.url, requestOptions, { | ||
| wallet: client, | ||
| maxAmount: maxAmountSats || undefined, | ||
| }); | ||
| const responseContent = await result.text(); | ||
| if (!result.ok) { | ||
| throw new Error(`fetch returned non-OK status: ${result.status} ${responseContent}`); | ||
| } | ||
| return { | ||
| content: responseContent, | ||
| }; | ||
| } |
+5
-3
@@ -22,3 +22,3 @@ #!/usr/bin/env node | ||
| import { registerRequestInvoiceFromLightningAddressCommand } from "./commands/request-invoice-from-lightning-address.js"; | ||
| import { registerFetchL402Command } from "./commands/fetch-l402.js"; | ||
| import { registerFetch402Command } from "./commands/fetch.js"; | ||
| import { registerConnectCommand } from "./commands/connect.js"; | ||
@@ -34,3 +34,3 @@ import { registerAuthCommand } from "./commands/auth.js"; | ||
| " $ npx @getalby/cli pay-invoice --invoice lnbc...") | ||
| .version("0.3.0") | ||
| .version("0.4.0") | ||
| .option("-c, --connection-secret <string>", "NWC connection secret (nostr+walletconnect://...) or path to file containing it (preferred)") | ||
@@ -77,3 +77,5 @@ .option("-w, --wallet-name <name>", "Use a named wallet's connection secret (~/.alby-cli/connection-secret-<name>.key)") | ||
| registerRequestInvoiceFromLightningAddressCommand(program); | ||
| registerFetchL402Command(program); | ||
| // Register fetch command for payment-protected resources | ||
| program.commandsGroup("HTTP 402 Payments (require --connection-secret):"); | ||
| registerFetch402Command(program); | ||
| // Register setup commands | ||
@@ -80,0 +82,0 @@ program.commandsGroup("Setup:"); |
+2
-2
@@ -5,3 +5,3 @@ { | ||
| "repository": "https://github.com/getAlby/cli.git", | ||
| "version": "0.3.0", | ||
| "version": "0.4.0", | ||
| "type": "module", | ||
@@ -40,3 +40,3 @@ "main": "build/index.js", | ||
| "dependencies": { | ||
| "@getalby/lightning-tools": "^7.0.2", | ||
| "@getalby/lightning-tools": "^8.0.0", | ||
| "@getalby/sdk": "^7.0.0", | ||
@@ -43,0 +43,0 @@ "@noble/hashes": "^2.0.1", |
+25
-61
@@ -60,7 +60,4 @@ # Alby NWC CLI | ||
| # Pass a file path to a connection secret | ||
| # Or pass a connection secret directly | ||
| npx @getalby/cli -c /path/to/secret.txt <command> [options] | ||
| # Or pass connection string directly | ||
| npx @getalby/cli -c "nostr+walletconnect://..." <command> [options] | ||
| ``` | ||
@@ -96,40 +93,46 @@ | ||
| These commands require a wallet connection (`-c`, `--wallet-name`, or `NWC_URL`): | ||
| These commands require a wallet connection - either default connection, or specify a custom connection with `-w`, '-c', or `NWC_URL` environment variable: | ||
| ```bash | ||
| # Get wallet balance | ||
| npx @getalby/cli -c "nostr+walletconnect://..." get-balance | ||
| npx @getalby/cli get-balance | ||
| # Get wallet info | ||
| npx @getalby/cli -c "nostr+walletconnect://..." get-info | ||
| npx @getalby/cli get-info | ||
| # Get wallet service capabilities | ||
| npx @getalby/cli -c "nostr+walletconnect://..." get-wallet-service-info | ||
| npx @getalby/cli get-wallet-service-info | ||
| # Create an invoice | ||
| npx @getalby/cli -c "nostr+walletconnect://..." make-invoice --amount 1000 --description "Payment" | ||
| npx @getalby/cli make-invoice --amount 1000 --description "Payment" | ||
| # Pay an invoice | ||
| npx @getalby/cli -c "nostr+walletconnect://..." pay-invoice --invoice "lnbc..." | ||
| npx @getalby/cli pay-invoice --invoice "lnbc..." | ||
| # Send a keysend payment | ||
| npx @getalby/cli -c "nostr+walletconnect://..." pay-keysend --pubkey "02abc..." --amount 100 | ||
| npx @getalby/cli pay-keysend --pubkey "02abc..." --amount 100 | ||
| # Look up an invoice by payment hash | ||
| npx @getalby/cli -c "nostr+walletconnect://..." lookup-invoice --payment-hash "abc123..." | ||
| npx @getalby/cli lookup-invoice --payment-hash "abc123..." | ||
| # List transactions | ||
| npx @getalby/cli -c "nostr+walletconnect://..." list-transactions --limit 10 | ||
| npx @getalby/cli list-transactions --limit 10 | ||
| # Get wallet budget | ||
| npx @getalby/cli -c "nostr+walletconnect://..." get-budget | ||
| npx @getalby/cli get-budget | ||
| # Sign a message | ||
| npx @getalby/cli -c "nostr+walletconnect://..." sign-message --message "Hello, World!" | ||
| npx @getalby/cli sign-message --message "Hello, World!" | ||
| # Fetch L402-protected resource | ||
| npx @getalby/cli -c "nostr+walletconnect://..." fetch-l402 --url "https://example.com/api" | ||
| # Fetch a payment-protected resource (auto-detects L402, X402, MPP) | ||
| npx @getalby/cli fetch --url "https://example.com/api" | ||
| # Fetch with custom method, headers, and body | ||
| npx @getalby/cli fetch --url "https://example.com/api" --method POST --body '{"query":"hello"}' --headers '{"Accept":"application/json"}' | ||
| # Fetch with a custom max amount (default: 5000 sats, 0 = no limit) | ||
| npx @getalby/cli fetch --url "https://example.com/api" --max-amount 1000 | ||
| # Wait for a payment notification | ||
| npx @getalby/cli -c "nostr+walletconnect://..." wait-for-payment --payment-hash "abc123..." | ||
| npx @getalby/cli wait-for-payment --payment-hash "abc123..." | ||
| ``` | ||
@@ -143,9 +146,9 @@ | ||
| # Create a HOLD invoice (you provide the payment hash) | ||
| npx @getalby/cli -c "nostr+walletconnect://..." make-hold-invoice --amount 1000 --payment-hash "abc123..." | ||
| npx @getalby/cli make-hold-invoice --amount 1000 --payment-hash "abc123..." | ||
| # Settle a HOLD invoice (claim the payment) | ||
| npx @getalby/cli -c "nostr+walletconnect://..." settle-hold-invoice --preimage "def456..." | ||
| npx @getalby/cli settle-hold-invoice --preimage "def456..." | ||
| # Cancel a HOLD invoice (reject the payment) | ||
| npx @getalby/cli -c "nostr+walletconnect://..." cancel-hold-invoice --payment-hash "abc123..." | ||
| npx @getalby/cli cancel-hold-invoice --payment-hash "abc123..." | ||
| ``` | ||
@@ -176,45 +179,6 @@ | ||
| ### Wallet Commands | ||
| Run `npx @getalby/cli help` for a full list of commands and possible arguments. | ||
| These require a wallet connection (`-c`, `--wallet-name`, or `NWC_URL`): | ||
| | Command | Description | Required Options | | ||
| | ------------------------- | ------------------------------ | ------------------------------- | | ||
| | `get-balance` | Get wallet balance | - | | ||
| | `get-info` | Get wallet info | - | | ||
| | `get-wallet-service-info` | Get wallet capabilities | - | | ||
| | `get-budget` | Get wallet budget | - | | ||
| | `make-invoice` | Create a lightning invoice | `--amount` | | ||
| | `pay-invoice` | Pay a lightning invoice | `--invoice` | | ||
| | `pay-keysend` | Send a keysend payment | `--pubkey`, `--amount` | | ||
| | `lookup-invoice` | Look up an invoice | `--payment-hash` or `--invoice` | | ||
| | `list-transactions` | List transactions | - | | ||
| | `sign-message` | Sign a message with wallet key | `--message` | | ||
| | `wait-for-payment` | Wait for payment notification | `--payment-hash` | | ||
| | `fetch-l402` | Fetch L402-protected resource | `--url` | | ||
| ### HOLD Invoice Commands | ||
| These require a wallet connection (`-c`, `--wallet-name`, or `NWC_URL`): | ||
| | Command | Description | Required Options | | ||
| | --------------------- | --------------------- | ---------------------------- | | ||
| | `make-hold-invoice` | Create a HOLD invoice | `--amount`, `--payment-hash` | | ||
| | `settle-hold-invoice` | Settle a HOLD invoice | `--preimage` | | ||
| | `cancel-hold-invoice` | Cancel a HOLD invoice | `--payment-hash` | | ||
| ### Lightning Tools | ||
| These don't require a wallet connection: | ||
| | Command | Description | Required Options | | ||
| | ---------------------------------------- | -------------------------------------- | ------------------------- | | ||
| | `fiat-to-sats` | Convert fiat to sats | `--currency`, `--amount` | | ||
| | `sats-to-fiat` | Convert sats to fiat | `--amount`, `--currency` | | ||
| | `parse-invoice` | Parse a BOLT-11 invoice | `--invoice` | | ||
| | `verify-preimage` | Verify preimage against invoice | `--invoice`, `--preimage` | | ||
| | `request-invoice-from-lightning-address` | Request invoice from lightning address | `--address`, `--amount` | | ||
| ## Output | ||
| All commands output JSON to stdout. Errors are output to stderr as JSON with an `error` field. |
| import { fetchL402 } from "../tools/lightning/fetch_l402.js"; | ||
| import { getClient, handleError, output } from "../utils.js"; | ||
| export function registerFetchL402Command(program) { | ||
| program | ||
| .command("fetch-l402") | ||
| .description("Fetch L402-protected resource") | ||
| .requiredOption("-u, --url <url>", "URL to fetch") | ||
| .option("-m, --method <method>", "HTTP method (GET, POST, etc.)") | ||
| .option("-b, --body <json>", "Request body (JSON string)") | ||
| .option("-H, --headers <json>", "Additional headers (JSON string)") | ||
| .action(async (options) => { | ||
| await handleError(async () => { | ||
| const client = await getClient(program); | ||
| const result = await fetchL402(client, { | ||
| url: options.url, | ||
| method: options.method, | ||
| body: options.body, | ||
| headers: options.headers ? JSON.parse(options.headers) : undefined, | ||
| }); | ||
| output(result); | ||
| }); | ||
| }); | ||
| } |
| import { fetchWithL402 } from "@getalby/lightning-tools"; | ||
| export async function fetchL402(client, params) { | ||
| const requestOptions = { | ||
| method: params.method, | ||
| }; | ||
| if (params.method && params.method !== "GET" && params.method !== "HEAD") { | ||
| requestOptions.body = params.body; | ||
| requestOptions.headers = { | ||
| "Content-Type": "application/json", | ||
| ...params.headers, | ||
| }; | ||
| } | ||
| else if (params.headers) { | ||
| requestOptions.headers = params.headers; | ||
| } | ||
| const result = await fetchWithL402(params.url, requestOptions, { | ||
| wallet: { | ||
| sendPayment: async (invoice) => { | ||
| const result = await client.payInvoice({ invoice }); | ||
| return { preimage: result.preimage }; | ||
| }, | ||
| }, | ||
| }); | ||
| const responseContent = await result.text(); | ||
| if (!result.ok) { | ||
| throw new Error(`fetch returned non-OK status: ${result.status} ${responseContent}`); | ||
| } | ||
| return { | ||
| content: responseContent, | ||
| }; | ||
| } |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
1334
0.23%74641
-3.28%181
-16.59%+ Added
- Removed