feat: Add zap command for sending Bitcoin Lightning tips

- Implemented the `zap` command in the CLI to allow users to send sats to a user, event, or snippet using a NIP-60 wallet.
- Created a new `zap.ts` file to handle the command logic and integrated it into the MCP server.
- Added wallet balance command to check the balance of a user's wallet.
- Enhanced the MCP server to register the new zap command and wallet balance command.
- Introduced caching for wallets to optimize performance and reduce redundant network requests.
- Updated database schema to include snippets table for storing code snippets.
- Improved logging functionality for better debugging and tracking of operations.
- Added functionality to save snippets to the database upon retrieval.
- Updated project overview documentation to reflect new features and structure.
- Refactored existing commands and logic for better modularity and maintainability.
This commit is contained in:
pablof7z
2025-04-08 18:10:16 +01:00
parent f74736191a
commit 10fbca0824
19 changed files with 723 additions and 35 deletions

View File

@@ -4,6 +4,7 @@ import { registerWotCommand } from './wot.js';
import { registerListUsernamesCommand } from './list-usernames.js';
import { registerMcpCommand } from './mcp.js';
import { registerSetupCommand } from './setup.js';
import { registerZapCommand } from './zap.js';
// Create a new Commander program
const program = new Command();
@@ -20,6 +21,7 @@ registerFindSnippetsCommand(program);
registerWotCommand(program);
registerListUsernamesCommand(program);
registerSetupCommand(program);
registerZapCommand(program);
// Function to run the CLI
export async function runCli(args: string[]) {

View File

@@ -1,15 +1,18 @@
import type { Command } from 'commander';
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { Command } from "commander";
import { readConfig } from "../config.js";
import { addCreatePubkeyCommand } from "../logic/create-pubkey.js";
import { addFetchSnippetByIdCommand } from "../logic/fetch_snippet_by_id.js";
import { addFindSnippetsCommand } from "../logic/find_snippets.js";
import { addFindUserCommand } from "../logic/find_user.js";
import { addListSnippetsCommand } from "../logic/list_snippets.js";
import { addListUsernamesCommand } from "../logic/list_usernames.js";
import { addPublishCodeSnippetCommand } from "../logic/publish-code-snippet.js";
import { addPublishCommand } from "../logic/publish.js";
import { addListSnippetsCommand } from "../logic/list_snippets.js";
import { addFetchSnippetByIdCommand } from "../logic/fetch_snippet_by_id.js";
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { addWalletBalanceCommand } from "../logic/wallet-balance.js";
import { addZapCommand } from "../logic/zap.js";
import { log } from "../utils/log.js";
// Define type for command functions
type CommandFunction = (server: McpServer) => void;
@@ -24,27 +27,32 @@ const commandMap: Record<string, CommandFunction> = {
"list-usernames": addListUsernamesCommand,
"list-snippets": addListSnippetsCommand,
"fetch-snippet-by-id": addFetchSnippetByIdCommand,
zap: addZapCommand,
"wallet-balance": addWalletBalanceCommand,
};
// Global server instance
let mcpServer: McpServer | null = null;
const mcpServer = new McpServer({
name: "Nostr Publisher",
version: "1.0.0",
capabilities: {
resources: {},
},
});
export function registerMcpCommand(program: Command): void {
program
.command('mcp')
.description('Start the MCP server')
.command("mcp")
.description("Start the MCP server")
.action(async () => {
try {
// Create the MCP server
mcpServer = new McpServer({
name: "Nostr Publisher",
version: "1.0.0",
});
// Register all MCP commands
registerMcpCommands(mcpServer);
// Connect the server to the transport
log("Starting MCP server...");
const transport = new StdioServerTransport();
await mcpServer.connect(transport);
} catch (error) {
@@ -76,5 +84,7 @@ export function registerMcpCommands(server: McpServer) {
addListUsernamesCommand(server);
addListSnippetsCommand(server);
addFetchSnippetByIdCommand(server);
addZapCommand(server);
addWalletBalanceCommand(server);
}
}

35
commands/zap.ts Normal file
View File

@@ -0,0 +1,35 @@
import { Command } from "commander";
import { sendZap } from "../logic/zap.js";
/**
* Register the zap command with the Commander program
* @param program The Commander program instance
*/
export function registerZapCommand(program: Command) {
program
.command("zap")
.description("Send sats to a user, event, or snippet using a NIP-60 wallet")
.requiredOption("-a, --amount <amount>", "Amount in sats to send", Number.parseInt)
.option("-r, --recipient <recipient>", "Recipient (username, npub, or pubkey) to zap directly")
.option("-e, --event-id <eventId>", "Event ID to zap")
.option("-t, --title <title>", "Snippet title to look up and zap")
.option("-m, --message <message>", "Thank you message to include with the zap")
.option("-u, --username <username>", "Username to zap from (uses wallet associated with this user)")
.action(async (options) => {
try {
const result = await sendZap(
options.amount,
options.recipient,
options.eventId,
options.title,
options.message,
options.username
);
const message = result?.content?.[0]?.text;
console.log(message || "Zap sent successfully!");
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
}
});
}