@getalby/cli 0.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.
Files changed (57) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +94 -0
  3. package/build/auth.js +20 -0
  4. package/build/commands/cancel-hold-invoice.js +17 -0
  5. package/build/commands/fetch-l402.js +23 -0
  6. package/build/commands/fiat-to-sats.js +18 -0
  7. package/build/commands/get-balance.js +14 -0
  8. package/build/commands/get-budget.js +14 -0
  9. package/build/commands/get-info.js +14 -0
  10. package/build/commands/get-wallet-service-info.js +14 -0
  11. package/build/commands/list-transactions.js +27 -0
  12. package/build/commands/lookup-invoice.js +23 -0
  13. package/build/commands/make-hold-invoice.js +23 -0
  14. package/build/commands/make-invoice.js +21 -0
  15. package/build/commands/parse-invoice.js +14 -0
  16. package/build/commands/pay-invoice.js +19 -0
  17. package/build/commands/pay-keysend.js +27 -0
  18. package/build/commands/request-invoice.js +20 -0
  19. package/build/commands/sats-to-fiat.js +18 -0
  20. package/build/commands/settle-hold-invoice.js +17 -0
  21. package/build/commands/sign-message.js +17 -0
  22. package/build/commands/verify-preimage.js +18 -0
  23. package/build/commands/wait-for-payment.js +21 -0
  24. package/build/index.js +50 -0
  25. package/build/mcp_server.js +36 -0
  26. package/build/sse.js +50 -0
  27. package/build/streamable_http.js +49 -0
  28. package/build/test/fetch-l402.test.js +7 -0
  29. package/build/test/helpers.js +61 -0
  30. package/build/test/lightning-tools.test.js +36 -0
  31. package/build/test/nwc-hold-invoices.test.js +79 -0
  32. package/build/test/nwc-payments.test.js +42 -0
  33. package/build/test/nwc-readonly.test.js +41 -0
  34. package/build/tools/lightning/fetch_l402.js +32 -0
  35. package/build/tools/lightning/fiat_to_sats.js +10 -0
  36. package/build/tools/lightning/parse_invoice.js +8 -0
  37. package/build/tools/lightning/request_invoice.js +14 -0
  38. package/build/tools/lightning/sats_to_fiat.js +11 -0
  39. package/build/tools/lightning/schemas/invoice.js +20 -0
  40. package/build/tools/lightning/verify_preimage.js +9 -0
  41. package/build/tools/nwc/cancel_hold_invoice.js +8 -0
  42. package/build/tools/nwc/get_balance.js +6 -0
  43. package/build/tools/nwc/get_budget.js +14 -0
  44. package/build/tools/nwc/get_info.js +3 -0
  45. package/build/tools/nwc/get_wallet_service_info.js +3 -0
  46. package/build/tools/nwc/list_transactions.js +18 -0
  47. package/build/tools/nwc/lookup_invoice.js +11 -0
  48. package/build/tools/nwc/make_hold_invoice.js +12 -0
  49. package/build/tools/nwc/make_invoice.js +14 -0
  50. package/build/tools/nwc/pay_invoice.js +12 -0
  51. package/build/tools/nwc/pay_keysend.js +12 -0
  52. package/build/tools/nwc/schemas/transaction.js +26 -0
  53. package/build/tools/nwc/settle_hold_invoice.js +8 -0
  54. package/build/tools/nwc/sign_message.js +6 -0
  55. package/build/tools/nwc/wait_for_payment.js +43 -0
  56. package/build/utils.js +23 -0
  57. package/package.json +47 -0
@@ -0,0 +1,14 @@
1
+ export async function getBudget(client) {
2
+ const budget = await client.getBudget();
3
+ if (!("used_budget" in budget)) {
4
+ return {};
5
+ }
6
+ return {
7
+ used_budget_in_sats: Math.floor(budget.used_budget / 1000),
8
+ total_budget_in_sats: budget.total_budget
9
+ ? Math.floor(budget.total_budget / 1000)
10
+ : undefined,
11
+ renews_at: budget.renews_at,
12
+ renewal_period: budget.renewal_period,
13
+ };
14
+ }
@@ -0,0 +1,3 @@
1
+ export async function getInfo(client) {
2
+ return await client.getInfo();
3
+ }
@@ -0,0 +1,3 @@
1
+ export async function getWalletServiceInfo(client) {
2
+ return await client.getWalletServiceInfo();
3
+ }
@@ -0,0 +1,18 @@
1
+ export async function listTransactions(client, params) {
2
+ const result = await client.listTransactions({
3
+ from: params.from,
4
+ until: params.until,
5
+ limit: params.limit,
6
+ type: params.type,
7
+ unpaid: params.unpaid,
8
+ offset: params.offset,
9
+ });
10
+ return {
11
+ ...result,
12
+ transactions: result.transactions.map(({ amount, fees_paid, ...transaction }) => ({
13
+ ...transaction,
14
+ amount_in_sats: Math.floor(amount / 1000),
15
+ fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) : undefined,
16
+ })),
17
+ };
18
+ }
@@ -0,0 +1,11 @@
1
+ export async function lookupInvoice(client, params) {
2
+ const { amount, fees_paid, ...result } = await client.lookupInvoice({
3
+ invoice: params.invoice,
4
+ payment_hash: params.payment_hash,
5
+ });
6
+ return {
7
+ ...result,
8
+ amount_in_sats: Math.floor(amount / 1000),
9
+ fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) : undefined,
10
+ };
11
+ }
@@ -0,0 +1,12 @@
1
+ export async function makeHoldInvoice(client, params) {
2
+ const { amount, ...result } = await client.makeHoldInvoice({
3
+ amount: params.amount_in_sats * 1000,
4
+ payment_hash: params.payment_hash,
5
+ description: params.description,
6
+ expiry: params.expiry,
7
+ });
8
+ return {
9
+ ...result,
10
+ amount_in_sats: Math.floor(amount / 1000),
11
+ };
12
+ }
@@ -0,0 +1,14 @@
1
+ export async function makeInvoice(client, params) {
2
+ const { amount, fees_paid, ...result } = await client.makeInvoice({
3
+ amount: params.amount_in_sats * 1000,
4
+ description: params.description,
5
+ description_hash: params.description_hash,
6
+ expiry: params.expiry,
7
+ metadata: params.metadata,
8
+ });
9
+ return {
10
+ ...result,
11
+ amount_in_sats: Math.floor(amount / 1000),
12
+ fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) : undefined,
13
+ };
14
+ }
@@ -0,0 +1,12 @@
1
+ export async function payInvoice(client, params) {
2
+ const { fees_paid, preimage, ...result } = await client.payInvoice({
3
+ invoice: params.invoice,
4
+ amount: params.amount_in_sats ? params.amount_in_sats * 1000 : undefined,
5
+ metadata: params.metadata,
6
+ });
7
+ return {
8
+ ...result,
9
+ preimage: preimage || "",
10
+ fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) : undefined,
11
+ };
12
+ }
@@ -0,0 +1,12 @@
1
+ export async function payKeysend(client, params) {
2
+ const { fees_paid, ...result } = await client.payKeysend({
3
+ pubkey: params.pubkey,
4
+ amount: params.amount_in_sats * 1000,
5
+ preimage: params.preimage,
6
+ tlv_records: params.tlv_records,
7
+ });
8
+ return {
9
+ ...result,
10
+ fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) : undefined,
11
+ };
12
+ }
@@ -0,0 +1,26 @@
1
+ import { z } from "zod";
2
+ export const transactionSchema = {
3
+ type: z.enum(["incoming", "outgoing"]).describe("Transaction type"),
4
+ state: z
5
+ .enum(["settled", "pending", "failed"])
6
+ .nullish()
7
+ .describe("Transaction state"),
8
+ invoice: z.string().describe("BOLT-11 invoice"),
9
+ description: z.string().nullish().describe("Invoice description"),
10
+ description_hash: z.string().nullish().describe("Description hash"),
11
+ preimage: z.string().nullish().describe("Preimage of settled payment"),
12
+ payment_hash: z.string().describe("Payment hash"),
13
+ amount_in_sats: z.number().describe("Amount in sats"),
14
+ fees_paid_in_sats: z.number().nullish().describe("Fees paid in sats"),
15
+ settled_at: z.number().nullish().describe("Timestamp, of settled payment"),
16
+ created_at: z.number().describe("Creation unix timestamp"),
17
+ expires_at: z.number().nullish().describe("Expiry unix timestamp"), // TODO: remove nullish once Primal supports it
18
+ settle_deadline: z
19
+ .number()
20
+ .nullish()
21
+ .describe("HOLD invoice settle deadline"),
22
+ metadata: z
23
+ .unknown()
24
+ .nullish()
25
+ .describe("Additional metadata about the transaction"),
26
+ };
@@ -0,0 +1,8 @@
1
+ export async function settleHoldInvoice(client, params) {
2
+ await client.settleHoldInvoice({
3
+ preimage: params.preimage,
4
+ });
5
+ return {
6
+ preimage: params.preimage,
7
+ };
8
+ }
@@ -0,0 +1,6 @@
1
+ export async function signMessage(client, params) {
2
+ const result = await client.signMessage({
3
+ message: params.message,
4
+ });
5
+ return result;
6
+ }
@@ -0,0 +1,43 @@
1
+ export async function waitForPayment(client, params) {
2
+ return new Promise((resolve, reject) => {
3
+ let timeoutId;
4
+ let unsub;
5
+ const cleanup = () => {
6
+ if (timeoutId) {
7
+ clearTimeout(timeoutId);
8
+ }
9
+ if (unsub) {
10
+ unsub();
11
+ }
12
+ };
13
+ if (params.timeout) {
14
+ timeoutId = setTimeout(() => {
15
+ cleanup();
16
+ reject(new Error(`Timeout waiting for payment after ${params.timeout}s`));
17
+ }, params.timeout * 1000);
18
+ }
19
+ const notificationTypes = params.type
20
+ ? [params.type]
21
+ : undefined;
22
+ client
23
+ .subscribeNotifications((notification) => {
24
+ const transaction = notification.notification;
25
+ if (transaction.payment_hash === params.payment_hash) {
26
+ cleanup();
27
+ const { amount, fees_paid, type, ...rest } = transaction;
28
+ resolve({
29
+ notification_type: notification.notification_type,
30
+ type,
31
+ ...rest,
32
+ amount_in_sats: Math.floor(amount / 1000),
33
+ fees_paid_in_sats: typeof fees_paid === "number"
34
+ ? Math.ceil(fees_paid / 1000)
35
+ : undefined,
36
+ });
37
+ }
38
+ }, notificationTypes)
39
+ .then((unsubscribe) => {
40
+ unsub = unsubscribe;
41
+ });
42
+ });
43
+ }
package/build/utils.js ADDED
@@ -0,0 +1,23 @@
1
+ import { NWCClient } from "@getalby/sdk";
2
+ export function getClient(program) {
3
+ const opts = program.opts();
4
+ const connectionSecret = opts.connectionSecret;
5
+ if (!connectionSecret) {
6
+ console.error("Error: --connection-secret is required for this command");
7
+ process.exit(1);
8
+ }
9
+ return new NWCClient({ nostrWalletConnectUrl: connectionSecret });
10
+ }
11
+ export function output(data) {
12
+ console.log(JSON.stringify(data, null, 2));
13
+ }
14
+ export async function handleError(fn) {
15
+ try {
16
+ await fn();
17
+ process.exit(0);
18
+ }
19
+ catch (error) {
20
+ console.error(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }, null, 2));
21
+ process.exit(1);
22
+ }
23
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@getalby/cli",
3
+ "description": "CLI for Nostr Wallet Connect (NIP-47) with a few additional useful lightning tools",
4
+ "repository": "https://github.com/getAlby/cli.git",
5
+ "version": "0.0.0",
6
+ "type": "module",
7
+ "main": "build/index.js",
8
+ "bin": {
9
+ "cli": "build/index.js",
10
+ "alby-cli": "build/index.js"
11
+ },
12
+ "files": [
13
+ "build/**/*"
14
+ ],
15
+ "scripts": {
16
+ "prepack": "yarn build",
17
+ "build": "tsc && chmod 755 build/index.js",
18
+ "start": "node build/index.js",
19
+ "dev": "yarn build && node build/index.js",
20
+ "test": "vitest run",
21
+ "test:watch": "vitest"
22
+ },
23
+ "keywords": [
24
+ "lightning",
25
+ "nostr",
26
+ "wallet",
27
+ "connect",
28
+ "nwc",
29
+ "ai",
30
+ "agent",
31
+ "cli",
32
+ "nip47"
33
+ ],
34
+ "author": "Alby contributors",
35
+ "license": "MIT",
36
+ "dependencies": {
37
+ "@getalby/lightning-tools": "^6.1.0",
38
+ "@getalby/sdk": "^7.0.0",
39
+ "commander": "^13.1.0"
40
+ },
41
+ "devDependencies": {
42
+ "@types/node": "^25.2.0",
43
+ "@webbtc/webln-types": "^3.0.0",
44
+ "typescript": "^5.9.3",
45
+ "vitest": "^4.0.18"
46
+ }
47
+ }