Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import {
DEFAULT_RELAY_URLS,
getConnectionSecretPath,
getPendingConnectionRelayPath,
getPendingConnectionSecretPath,
Expand All @@ -28,8 +29,9 @@ export function registerAuthCommand(program: Command) {
)
.option(
"--relay-url <url>",
"Relay URL for the pending connection",
"wss://relay.getalby.com/v1",
`Relay URL for the pending connection (repeat to use multiple relays, default: ${DEFAULT_RELAY_URLS.join(", ")})`,
(value: string, previous: string[]) => previous.concat([value]),
[] as string[],
)
.option("--force", "Overwrite existing connection secret")
.option("--remove-pending", "Remove a pending connection and start fresh")
Expand All @@ -39,7 +41,7 @@ export function registerAuthCommand(program: Command) {
options: {
appName?: string;
force?: boolean;
relayUrl: string;
relayUrl: string[];
removePending?: boolean;
},
) => {
Expand Down Expand Up @@ -83,6 +85,11 @@ export function registerAuthCommand(program: Command) {
process.exit(1);
}

const relayUrls =
options.relayUrl.length > 0
? options.relayUrl
: DEFAULT_RELAY_URLS;

const secret = bytesToHex(generateSecretKey());
const pubkey = getPublicKey(hexToBytes(secret));

Expand All @@ -97,7 +104,11 @@ export function registerAuthCommand(program: Command) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(pendingSecretPath, secret, { mode: 0o600 });
writeFileSync(pendingRelayPath, options.relayUrl, { mode: 0o600 });
// Store one relay per line so multiple relays survive the
// round-trip to the completion step.
writeFileSync(pendingRelayPath, relayUrls.join("\n"), {
mode: 0o600,
});

console.log(
"Click the following URL to approve the connection in your wallet:\n" +
Expand Down
24 changes: 18 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
import { homedir } from "node:os";
import { join } from "node:path";

export const DEFAULT_RELAY_URLS = [
"wss://relay.getalby.com",
"wss://relay2.getalby.com",
];

function sanitizeWalletName(name: string): string {
return name.replace(/[^a-zA-Z0-9_-]/g, "_");
}
Expand Down Expand Up @@ -67,21 +72,28 @@ export async function testAndLogConnection(client: NWCClient) {
export async function completePendingConnection(
pendingSecretPath: string,
connectionSecretPath: string,
relayUrl: string | undefined,
relayUrls: string[] | undefined,
verbose: boolean,
pendingRelayPath?: string,
): Promise<NWCClient> {
const secret = readFileSync(pendingSecretPath, "utf-8").trim();

const DEFAULT_RELAY = "wss://relay.getalby.com/v1";
if (!relayUrl && pendingRelayPath && existsSync(pendingRelayPath)) {
relayUrl = readFileSync(pendingRelayPath, "utf-8").trim();
if (
(!relayUrls || relayUrls.length === 0) &&
pendingRelayPath &&
existsSync(pendingRelayPath)
) {
relayUrls = readFileSync(pendingRelayPath, "utf-8")
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
}
const resolvedRelay = relayUrl ?? DEFAULT_RELAY;
const resolvedRelays =
relayUrls && relayUrls.length > 0 ? relayUrls : DEFAULT_RELAY_URLS;

const nwaClient = new NWAClient({
appSecretKey: secret,
relayUrls: [resolvedRelay],
relayUrls: resolvedRelays,
requestMethods: [],
});

Expand Down
Loading