@getalby/cli 0.0.1 → 0.1.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.
package/README.md CHANGED
@@ -5,10 +5,14 @@ CLI for Nostr Wallet Connect (NIP-47) with lightning tools.
5
5
  ## Usage
6
6
 
7
7
  ```bash
8
- npx @getalby/cli --connection-secret <NWC_CONNECTION_STRING> <command> [options]
8
+ # Pass a file path to a connection secret (preferred)
9
+ npx @getalby/cli -c /path/to/secret.txt <command> [options]
10
+
11
+ # Or pass connection string directly
12
+ npx @getalby/cli -c "nostr+walletconnect://..." <command> [options]
9
13
  ```
10
14
 
11
- The `--connection-secret` (or `-c`) option is required for wallet operations. You can get a connection string from your NWC-compatible wallet (e.g., [Alby](https://getalby.com)).
15
+ The `-c` option auto-detects whether you're passing a connection string or a file path. You can get a connection string from your NWC-compatible wallet (e.g., [Alby](https://getalby.com)).
12
16
 
13
17
  ## Testing Wallet
14
18
 
@@ -110,44 +114,44 @@ npx @getalby/cli request-invoice-from-lightning-address --address "hello@getalby
110
114
 
111
115
  ### Wallet Commands
112
116
 
113
- These require `--connection-secret`:
114
-
115
- | Command | Description | Required Options |
116
- |---------|-------------|------------------|
117
- | `get-balance` | Get wallet balance | - |
118
- | `get-info` | Get wallet info | - |
119
- | `get-wallet-service-info` | Get wallet capabilities | - |
120
- | `get-budget` | Get wallet budget | - |
121
- | `make-invoice` | Create a lightning invoice | `--amount` |
122
- | `pay-invoice` | Pay a lightning invoice | `--invoice` |
123
- | `pay-keysend` | Send a keysend payment | `--pubkey`, `--amount` |
124
- | `lookup-invoice` | Look up an invoice | `--payment-hash` or `--invoice` |
125
- | `list-transactions` | List transactions | - |
126
- | `sign-message` | Sign a message with wallet key | `--message` |
127
- | `wait-for-payment` | Wait for payment notification | `--payment-hash` |
128
- | `fetch-l402` | Fetch L402-protected resource | `--url` |
117
+ These require `-c` or `--connection-secret`:
118
+
119
+ | Command | Description | Required Options |
120
+ | ------------------------- | ------------------------------ | ------------------------------- |
121
+ | `get-balance` | Get wallet balance | - |
122
+ | `get-info` | Get wallet info | - |
123
+ | `get-wallet-service-info` | Get wallet capabilities | - |
124
+ | `get-budget` | Get wallet budget | - |
125
+ | `make-invoice` | Create a lightning invoice | `--amount` |
126
+ | `pay-invoice` | Pay a lightning invoice | `--invoice` |
127
+ | `pay-keysend` | Send a keysend payment | `--pubkey`, `--amount` |
128
+ | `lookup-invoice` | Look up an invoice | `--payment-hash` or `--invoice` |
129
+ | `list-transactions` | List transactions | - |
130
+ | `sign-message` | Sign a message with wallet key | `--message` |
131
+ | `wait-for-payment` | Wait for payment notification | `--payment-hash` |
132
+ | `fetch-l402` | Fetch L402-protected resource | `--url` |
129
133
 
130
134
  ### HOLD Invoice Commands
131
135
 
132
- These require `--connection-secret`:
136
+ These require `-c` or `--connection-secret`:
133
137
 
134
- | Command | Description | Required Options |
135
- |---------|-------------|------------------|
136
- | `make-hold-invoice` | Create a HOLD invoice | `--amount`, `--payment-hash` |
137
- | `settle-hold-invoice` | Settle a HOLD invoice | `--preimage` |
138
- | `cancel-hold-invoice` | Cancel a HOLD invoice | `--payment-hash` |
138
+ | Command | Description | Required Options |
139
+ | --------------------- | --------------------- | ---------------------------- |
140
+ | `make-hold-invoice` | Create a HOLD invoice | `--amount`, `--payment-hash` |
141
+ | `settle-hold-invoice` | Settle a HOLD invoice | `--preimage` |
142
+ | `cancel-hold-invoice` | Cancel a HOLD invoice | `--payment-hash` |
139
143
 
140
144
  ### Lightning Tools
141
145
 
142
146
  These don't require a wallet connection:
143
147
 
144
- | Command | Description | Required Options |
145
- |---------|-------------|------------------|
146
- | `fiat-to-sats` | Convert fiat to sats | `--currency`, `--amount` |
147
- | `sats-to-fiat` | Convert sats to fiat | `--amount`, `--currency` |
148
- | `parse-invoice` | Parse a BOLT-11 invoice | `--invoice` |
149
- | `verify-preimage` | Verify preimage against invoice | `--invoice`, `--preimage` |
150
- | `request-invoice-from-lightning-address` | Request invoice from lightning address | `--address`, `--amount` |
148
+ | Command | Description | Required Options |
149
+ | ---------------------------------------- | -------------------------------------- | ------------------------- |
150
+ | `fiat-to-sats` | Convert fiat to sats | `--currency`, `--amount` |
151
+ | `sats-to-fiat` | Convert sats to fiat | `--amount`, `--currency` |
152
+ | `parse-invoice` | Parse a BOLT-11 invoice | `--invoice` |
153
+ | `verify-preimage` | Verify preimage against invoice | `--invoice`, `--preimage` |
154
+ | `request-invoice-from-lightning-address` | Request invoice from lightning address | `--address`, `--amount` |
151
155
 
152
156
  ## Output
153
157
 
package/build/index.js CHANGED
@@ -24,8 +24,8 @@ const program = new Command();
24
24
  program
25
25
  .name("alby-cli")
26
26
  .description("CLI for Nostr Wallet Connect (NIP-47) with lightning tools")
27
- .version("0.0.0")
28
- .option("-c, --connection-secret <string>", "NWC connection secret (nostr+walletconnect://...)");
27
+ .version("0.1.0")
28
+ .option("-c, --connection-secret <string>", "NWC connection secret (nostr+walletconnect://...) or path to file containing it (preferred)");
29
29
  // Register all commands
30
30
  registerGetBalanceCommand(program);
31
31
  registerGetBudgetCommand(program);
@@ -0,0 +1,57 @@
1
+ import { describe, test, expect, beforeAll, afterAll } from "vitest";
2
+ import { createTestWallet, runCli } from "./helpers.js";
3
+ import { writeFileSync, unlinkSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ describe("Connection Secret Handling", () => {
7
+ let wallet;
8
+ let secretFilePath;
9
+ let badSecretFilePath;
10
+ beforeAll(async () => {
11
+ wallet = await createTestWallet();
12
+ // Create temp file with valid connection secret
13
+ secretFilePath = join(tmpdir(), `nwc-test-secret-${Date.now()}.txt`);
14
+ writeFileSync(secretFilePath, wallet.nwcUrl);
15
+ // Create temp file with invalid/truncated content
16
+ badSecretFilePath = join(tmpdir(), `nwc-test-bad-secret-${Date.now()}.txt`);
17
+ writeFileSync(badSecretFilePath, "nostr+wallet");
18
+ }, 60000);
19
+ afterAll(() => {
20
+ try {
21
+ unlinkSync(secretFilePath);
22
+ }
23
+ catch { }
24
+ try {
25
+ unlinkSync(badSecretFilePath);
26
+ }
27
+ catch { }
28
+ });
29
+ test("accepts connection string directly", () => {
30
+ const result = runCli(`-c "${wallet.nwcUrl}" get-balance`);
31
+ expect(result.success).toBe(true);
32
+ });
33
+ test("reads connection secret from file", () => {
34
+ const result = runCli(`-c "${secretFilePath}" get-balance`);
35
+ expect(result.success).toBe(true);
36
+ });
37
+ test("errors when file does not exist", () => {
38
+ const result = runCli(`-c "/tmp/nonexistent-file-${Date.now()}" get-balance`);
39
+ expect(result.success).toBe(false);
40
+ expect(result.output.error).toContain("Failed to read connection secret file");
41
+ });
42
+ test("errors when file contains invalid connection string", () => {
43
+ const result = runCli(`-c "${badSecretFilePath}" get-balance`);
44
+ expect(result.success).toBe(false);
45
+ expect(result.output.error).toContain("Invalid connection secret");
46
+ });
47
+ test("errors when no connection secret provided", () => {
48
+ const result = runCli("get-balance");
49
+ expect(result.success).toBe(false);
50
+ expect(result.output.error).toContain("--connection-secret is required");
51
+ });
52
+ test("errors when connection string is malformed", () => {
53
+ const result = runCli(`-c "nostr+walletconnect://asdf" get-balance`);
54
+ expect(result.success).toBe(false);
55
+ expect(result.output.error).toContain("Invalid connection secret");
56
+ });
57
+ });
package/build/utils.js CHANGED
@@ -1,12 +1,47 @@
1
1
  import { NWCClient } from "@getalby/sdk";
2
+ import { readFileSync } from "node:fs";
2
3
  export function getClient(program) {
3
4
  const opts = program.opts();
4
- const connectionSecret = opts.connectionSecret;
5
+ let connectionSecret = opts.connectionSecret;
5
6
  if (!connectionSecret) {
6
7
  console.error("Error: --connection-secret is required for this command");
7
8
  process.exit(1);
8
9
  }
9
- return new NWCClient({ nostrWalletConnectUrl: connectionSecret });
10
+ // Auto-detect: if it doesn't start with the protocol, treat as file path
11
+ if (!connectionSecret.startsWith("nostr+walletconnect://")) {
12
+ try {
13
+ connectionSecret = readFileSync(connectionSecret, "utf-8").trim();
14
+ }
15
+ catch (error) {
16
+ console.error(`Error: Failed to read connection secret file "${opts.connectionSecret}": ${error instanceof Error ? error.message : String(error)}`);
17
+ process.exit(1);
18
+ }
19
+ }
20
+ // Validate the connection string format
21
+ if (!connectionSecret.startsWith("nostr+walletconnect://")) {
22
+ console.error(`Error: Invalid connection secret. Expected format: nostr+walletconnect://...\n` +
23
+ `Got: "${connectionSecret.substring(0, 50)}${connectionSecret.length > 50 ? "..." : ""}"\n` +
24
+ `Hint: Make sure the connection string is complete and not truncated.`);
25
+ process.exit(1);
26
+ }
27
+ const client = new NWCClient({ nostrWalletConnectUrl: connectionSecret });
28
+ // Validate client properties
29
+ if (!client.secret || !/^[0-9a-f]{64}$/i.test(client.secret)) {
30
+ console.error(`Error: Invalid connection secret. Missing or invalid secret key.\n` +
31
+ `Hint: Make sure the connection string is complete and not truncated.`);
32
+ process.exit(1);
33
+ }
34
+ if (!client.walletPubkey || !/^[0-9a-f]{64}$/i.test(client.walletPubkey)) {
35
+ console.error(`Error: Invalid connection secret. Missing or invalid wallet pubkey.\n` +
36
+ `Hint: Make sure the connection string is complete and not truncated.`);
37
+ process.exit(1);
38
+ }
39
+ if (!client.relayUrls) {
40
+ console.error(`Error: Invalid connection secret. Missing relay URL.\n` +
41
+ `Hint: Make sure the connection string is complete and not truncated.`);
42
+ process.exit(1);
43
+ }
44
+ return client;
10
45
  }
11
46
  export function output(data) {
12
47
  console.log(JSON.stringify(data, null, 2));
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@getalby/cli",
3
3
  "description": "CLI for Nostr Wallet Connect (NIP-47) with a few additional useful lightning tools",
4
4
  "repository": "https://github.com/getAlby/cli.git",
5
- "version": "0.0.1",
5
+ "version": "0.1.0",
6
6
  "type": "module",
7
7
  "main": "build/index.js",
8
8
  "bin": {