This commit is contained in:
pablof7z
2025-04-09 11:16:58 +01:00
parent 10fbca0824
commit 630ed0cbd0
2 changed files with 71 additions and 10 deletions

View File

@@ -1,12 +1,10 @@
import NDK, { NDKPrivateKeySigner, NDKUser, NDKEvent } from "@nostr-dev-kit/ndk"; import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { NDKCashuWallet } from "@nostr-dev-kit/ndk-wallet"; import { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk";
import { NDKCashuMintList } from "@nostr-dev-kit/ndk";
import { getWallet } from "../lib/cache/wallets.js";
import { z } from "zod"; import { z } from "zod";
import { getUser, saveUser } from "../config.js"; import { getUser, saveUser } from "../config.js";
import { getWallet } from "../lib/cache/wallets.js";
import { log } from "../lib/utils/log.js"; import { log } from "../lib/utils/log.js";
import { ndk } from "../ndk.js"; import { ndk } from "../ndk.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export async function createPubkey({ export async function createPubkey({
username, username,
@@ -32,6 +30,7 @@ export async function createPubkey({
display_name, display_name,
name: display_name, name: display_name,
about: about || "", about: about || "",
picture: `https://robohash.org/${username}`,
}); });
// Create the event // Create the event
@@ -47,23 +46,25 @@ export async function createPubkey({
// Publish the event // Publish the event
await event.publish(); await event.publish();
// --- Setup Cashu Wallet (NIP-60/NIP-61) --- // --- Setup Cashu Wallet (NIP-60/NIP-61) ---
log(`Setting up Cashu wallet for ${username}...`); log(`Setting up Cashu wallet for ${username}...`);
try { try {
// Use getWallet to retrieve or create a wallet for the new pubkey // Use getWallet to retrieve or create a wallet for the new pubkey
const wallet = await getWallet(signer.pubkey); const wallet = await getWallet(signer.pubkey, signer);
if (wallet) { if (wallet) {
log(`Cashu wallet setup complete for ${username}.`); log(`Cashu wallet setup complete for ${username}.`);
log(` -> P2PK: ${wallet.p2pk}`); log(` -> P2PK: ${wallet.p2pk}`);
log(` -> Mints: ${wallet.mints.join(', ')}`); log(` -> Mints: ${wallet.mints.join(", ")}`);
} else { } else {
throw new Error("Failed to create wallet"); throw new Error("Failed to create wallet");
} }
} catch (walletError) { } catch (walletError) {
console.error(`Failed to set up Cashu wallet for ${username}:`, walletError); console.error(
`Failed to set up Cashu wallet for ${username}:`,
walletError
);
// Decide if this should be a fatal error for pubkey creation // Decide if this should be a fatal error for pubkey creation
// For now, log the error and continue // For now, log the error and continue
} }

60
logic/deposit.ts Normal file
View File

@@ -0,0 +1,60 @@
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { getWallet } from "../lib/cache/wallets";
import { getSigner } from "../lib/nostr/utils";
import { log } from "../utils/log";
export async function deposit(username: string, amount: number) {
try {
const signer = await getSigner(username);
const wallet = await getWallet(signer.pubkey, signer);
const depositOperation = wallet?.deposit(amount);
if (!depositOperation) {
throw new Error(
`Failed to initiate deposit for user ${username}. Wallet might not support deposits or user not found.`
);
}
const result = await depositOperation.start();
log(
`Deposit of ${amount} for ${username} started successfully: ${result}`
);
return { qrCode: result };
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : String(error);
console.error(
`Failed to deposit ${amount} for ${username}: ${errorMessage}`
);
// Re-throw the error so the MCP handler can catch it
throw new Error(`Deposit failed for ${username}: ${errorMessage}`);
}
}
export function addDepositCommand(server: McpServer) {
server.tool(
"deposit",
"Initiate a deposit for a user",
{
username: z.string().describe("Username to deposit for"),
amount: z.number().describe("Amount in sats to deposit"),
},
async (input) => {
const { username, amount } = input;
log(`Received deposit request for ${username} amount ${amount}`);
const result = await deposit(username, amount);
if (!result || !result.qrCode) {
throw new Error("Deposit failed to return a QR code result");
}
return {
content: [
{
type: "text",
text: `Deposit initiated. Please scan the QR code or copy the invoice: ${result.qrCode}`,
},
],
};
}
);
}