mirror of
https://github.com/Routstr/routstrd.git
synced 2026-08-10 12:13:08 +00:00
feat(cli): suffix client names with last 7 chars of npub in remote mode
- clients add: appends _<npub_suffix> to name when daemonUrl+nsec are set - clients list: filters to only clients matching our npub suffix, strips suffix for display - clients delete: auto-appends suffix to id before sending if not already present - adds getNpubSuffix() helper in daemon-client.ts
This commit is contained in:
+42
-5
@@ -7,6 +7,7 @@ import {
|
||||
isDaemonRunning,
|
||||
loadConfig,
|
||||
getDaemonBaseUrl,
|
||||
getNpubSuffix,
|
||||
} from "./utils/daemon-client";
|
||||
import { existsSync, mkdirSync } from "fs";
|
||||
import { execSync } from "child_process";
|
||||
@@ -726,6 +727,9 @@ clientsCmd
|
||||
.action(async () => {
|
||||
await ensureDaemonRunning();
|
||||
|
||||
const config = await loadConfig();
|
||||
const suffix = getNpubSuffix(config);
|
||||
|
||||
const result = await callDaemon("/clients");
|
||||
if (result.error) {
|
||||
console.log(result.error);
|
||||
@@ -745,13 +749,31 @@ clientsCmd
|
||||
}
|
||||
| undefined;
|
||||
|
||||
if (!output?.clients || output.clients.length === 0) {
|
||||
let clients = output?.clients || [];
|
||||
|
||||
if (suffix) {
|
||||
const suffixStr = `_${suffix}`;
|
||||
clients = clients.filter(
|
||||
(c) => c.name.endsWith(suffixStr) || c.id.endsWith(suffixStr),
|
||||
);
|
||||
clients = clients.map((c) => ({
|
||||
...c,
|
||||
name: c.name.endsWith(suffixStr)
|
||||
? c.name.slice(0, -suffixStr.length)
|
||||
: c.name,
|
||||
id: c.id.endsWith(suffixStr)
|
||||
? c.id.slice(0, -suffixStr.length)
|
||||
: c.id,
|
||||
}));
|
||||
}
|
||||
|
||||
if (clients.length === 0) {
|
||||
console.log("No clients found.");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Clients (${output.totalCount} total):\n`);
|
||||
for (const client of output.clients) {
|
||||
console.log(`Clients (${clients.length} total):\n`);
|
||||
for (const client of clients) {
|
||||
const createdAt = new Date(client.createdAt).toISOString();
|
||||
const lastUsed = client.lastUsed
|
||||
? new Date(client.lastUsed).toISOString()
|
||||
@@ -770,9 +792,19 @@ clientsCmd
|
||||
.action(async (id: string) => {
|
||||
await ensureDaemonRunning();
|
||||
|
||||
const config = await loadConfig();
|
||||
const suffix = getNpubSuffix(config);
|
||||
let resolvedId = id;
|
||||
if (suffix) {
|
||||
const suffixStr = `_${suffix}`;
|
||||
if (!id.endsWith(suffixStr)) {
|
||||
resolvedId = `${id}${suffixStr}`;
|
||||
}
|
||||
}
|
||||
|
||||
const result = await callDaemon("/clients/delete", {
|
||||
method: "POST",
|
||||
body: { id },
|
||||
body: { id: resolvedId },
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
@@ -850,9 +882,14 @@ clientsCmd
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const suffix = getNpubSuffix(config);
|
||||
const resolvedName = suffix
|
||||
? `${options.name}_${suffix}`
|
||||
: options.name;
|
||||
|
||||
const result = await callDaemon("/clients/add", {
|
||||
method: "POST",
|
||||
body: { name: options.name },
|
||||
body: { name: resolvedName },
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import {
|
||||
createNIP98Authorization,
|
||||
parseSecretKey,
|
||||
npubFromSecretKey,
|
||||
type HttpMethod,
|
||||
} from "./nip98";
|
||||
|
||||
@@ -97,6 +98,17 @@ export async function isDaemonRunning(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export function getNpubSuffix(config: RoutstrdConfig): string | null {
|
||||
if (!config.daemonUrl || !config.nsec) return null;
|
||||
try {
|
||||
const secretKey = parseSecretKey(config.nsec);
|
||||
const npub = npubFromSecretKey(secretKey);
|
||||
return npub.slice(-7);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function startDaemonProcess(): Promise<void> {
|
||||
// Ensure logs directory exists (logger handles date-based files)
|
||||
if (!existsSync(LOGS_DIR)) {
|
||||
|
||||
Reference in New Issue
Block a user