clawdentials-mcp 0.1.0 → 0.7.2

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.
Files changed (35) hide show
  1. package/README.md +310 -58
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +225 -18
  4. package/dist/schemas/index.d.ts +141 -0
  5. package/dist/schemas/index.js +54 -0
  6. package/dist/services/firestore.d.ts +45 -2
  7. package/dist/services/firestore.js +410 -6
  8. package/dist/services/payments/alby.d.ts +104 -0
  9. package/dist/services/payments/alby.js +239 -0
  10. package/dist/services/payments/breez.d.ts +91 -0
  11. package/dist/services/payments/breez.js +267 -0
  12. package/dist/services/payments/cashu.d.ts +127 -0
  13. package/dist/services/payments/cashu.js +248 -0
  14. package/dist/services/payments/coinremitter.d.ts +84 -0
  15. package/dist/services/payments/coinremitter.js +176 -0
  16. package/dist/services/payments/index.d.ts +132 -0
  17. package/dist/services/payments/index.js +180 -0
  18. package/dist/services/payments/oxapay.d.ts +89 -0
  19. package/dist/services/payments/oxapay.js +221 -0
  20. package/dist/services/payments/x402.d.ts +61 -0
  21. package/dist/services/payments/x402.js +94 -0
  22. package/dist/services/payments/zbd.d.ts +88 -0
  23. package/dist/services/payments/zbd.js +221 -0
  24. package/dist/tools/admin.d.ts +195 -0
  25. package/dist/tools/admin.js +210 -0
  26. package/dist/tools/agent.d.ts +197 -0
  27. package/dist/tools/agent.js +200 -0
  28. package/dist/tools/escrow.d.ts +74 -16
  29. package/dist/tools/escrow.js +139 -28
  30. package/dist/tools/index.d.ts +3 -0
  31. package/dist/tools/index.js +3 -0
  32. package/dist/tools/payment.d.ts +144 -0
  33. package/dist/tools/payment.js +376 -0
  34. package/dist/types/index.d.ts +44 -1
  35. package/package.json +18 -2
@@ -0,0 +1,94 @@
1
+ /**
2
+ * x402 Payment Service - USDC on Base
3
+ *
4
+ * Uses Coinbase's x402 protocol for instant stablecoin payments.
5
+ * Docs: https://docs.cdp.coinbase.com/x402/welcome
6
+ */
7
+ // Configuration
8
+ const X402_FACILITATOR_URL = process.env.X402_FACILITATOR_URL || 'https://x402.org/facilitator';
9
+ const X402_WALLET_ADDRESS = process.env.X402_WALLET_ADDRESS || '0x7BAC327BF264BF530D002907b375B8C9E04b0212'; // Clawdentials Base wallet
10
+ const X402_NETWORK = process.env.X402_NETWORK || 'eip155:8453'; // Base mainnet (84532 for testnet)
11
+ /**
12
+ * Create a payment request for USDC via x402
13
+ *
14
+ * For x402, the flow is different - it's typically used as middleware
15
+ * where the client pays before accessing a resource. For deposits,
16
+ * we generate payment details that the agent can use to send USDC.
17
+ */
18
+ export async function createX402Deposit(request) {
19
+ if (!X402_WALLET_ADDRESS) {
20
+ return {
21
+ success: false,
22
+ error: 'X402_WALLET_ADDRESS not configured. Set this environment variable to your Base wallet address.',
23
+ };
24
+ }
25
+ // x402 uses a simple payment scheme - we provide the details
26
+ // and the agent sends USDC directly using their wallet
27
+ const amountInWei = BigInt(Math.floor(request.amount * 1e6)).toString(); // USDC has 6 decimals
28
+ const depositId = `x402_${Date.now()}_${Math.random().toString(36).substring(7)}`;
29
+ const expiresAt = new Date(Date.now() + 30 * 60 * 1000); // 30 minutes
30
+ return {
31
+ success: true,
32
+ deposit: {
33
+ id: depositId,
34
+ agentId: request.agentId,
35
+ amount: request.amount,
36
+ currency: 'USDC',
37
+ network: 'base',
38
+ status: 'pending',
39
+ provider: 'x402',
40
+ externalId: depositId,
41
+ paymentAddress: X402_WALLET_ADDRESS,
42
+ paymentUrl: null, // Direct transfer, no hosted page
43
+ createdAt: new Date(),
44
+ expiresAt,
45
+ completedAt: null,
46
+ txHash: null,
47
+ },
48
+ paymentDetails: {
49
+ payTo: X402_WALLET_ADDRESS,
50
+ amount: amountInWei,
51
+ network: X402_NETWORK,
52
+ facilitatorUrl: X402_FACILITATOR_URL,
53
+ },
54
+ };
55
+ }
56
+ /**
57
+ * Verify a payment was received via x402
58
+ *
59
+ * In production, this would check the blockchain or use the x402 facilitator
60
+ * to verify the payment. For now, we provide a manual verification endpoint.
61
+ */
62
+ export async function verifyX402Payment(txHash, expectedAmount) {
63
+ // TODO: Implement actual blockchain verification
64
+ // Options:
65
+ // 1. Use Coinbase's x402 facilitator verification
66
+ // 2. Use Base RPC to check transaction
67
+ // 3. Use a service like Alchemy or Infura
68
+ return {
69
+ success: true,
70
+ verified: false,
71
+ error: 'Automatic verification not yet implemented. Use admin tools to manually credit balance after confirming transaction.',
72
+ };
73
+ }
74
+ /**
75
+ * Send USDC withdrawal via x402/Base
76
+ */
77
+ export async function sendX402Withdrawal(toAddress, amount) {
78
+ // TODO: Implement actual USDC transfer
79
+ // This requires a wallet private key or integration with a custodial service
80
+ return {
81
+ success: false,
82
+ error: 'Automated USDC withdrawals not yet implemented. Process manually and update withdrawal status.',
83
+ };
84
+ }
85
+ export const x402Service = {
86
+ createDeposit: createX402Deposit,
87
+ verifyPayment: verifyX402Payment,
88
+ sendWithdrawal: sendX402Withdrawal,
89
+ config: {
90
+ facilitatorUrl: X402_FACILITATOR_URL,
91
+ walletAddress: X402_WALLET_ADDRESS,
92
+ network: X402_NETWORK,
93
+ },
94
+ };
@@ -0,0 +1,88 @@
1
+ /**
2
+ * ZBD Payment Service - Bitcoin on Lightning
3
+ *
4
+ * Uses ZBD API for instant Bitcoin Lightning payments.
5
+ * Docs: https://zbd.dev
6
+ * Fee: ~1%
7
+ */
8
+ import type { Deposit } from '../../types/index.js';
9
+ export interface ZBDPaymentRequest {
10
+ amount: number;
11
+ agentId: string;
12
+ description?: string;
13
+ }
14
+ export interface ZBDPaymentResponse {
15
+ success: boolean;
16
+ deposit?: Partial<Deposit>;
17
+ invoice?: {
18
+ id: string;
19
+ invoice: string;
20
+ amount: string;
21
+ expiresAt: string;
22
+ };
23
+ error?: string;
24
+ }
25
+ /**
26
+ * Convert USD to millisats
27
+ */
28
+ declare function usdToMillisats(usd: number): string;
29
+ /**
30
+ * Convert millisats to USD
31
+ */
32
+ declare function millisatsToUsd(millisats: string | number): number;
33
+ /**
34
+ * Create a Lightning invoice for BTC deposit
35
+ */
36
+ export declare function createZBDDeposit(request: ZBDPaymentRequest): Promise<ZBDPaymentResponse>;
37
+ /**
38
+ * Check charge/invoice status
39
+ */
40
+ export declare function getChargeStatus(chargeId: string): Promise<{
41
+ success: boolean;
42
+ status?: string;
43
+ paid?: boolean;
44
+ amountUsd?: number;
45
+ error?: string;
46
+ }>;
47
+ /**
48
+ * Process webhook callback from ZBD
49
+ */
50
+ export declare function parseWebhookPayload(body: Record<string, any>): {
51
+ chargeId: string;
52
+ status: string;
53
+ amountUsd: number;
54
+ agentId: string;
55
+ };
56
+ /**
57
+ * Send BTC withdrawal via Lightning
58
+ */
59
+ export declare function sendZBDWithdrawal(destination: string, // Lightning invoice or Lightning Address
60
+ amountUsd: number): Promise<{
61
+ success: boolean;
62
+ paymentId?: string;
63
+ error?: string;
64
+ }>;
65
+ /**
66
+ * Get ZBD wallet balance
67
+ */
68
+ export declare function getWalletBalance(): Promise<{
69
+ success: boolean;
70
+ balanceSats?: number;
71
+ balanceUsd?: number;
72
+ error?: string;
73
+ }>;
74
+ export declare const zbdService: {
75
+ createDeposit: typeof createZBDDeposit;
76
+ getChargeStatus: typeof getChargeStatus;
77
+ parseWebhookPayload: typeof parseWebhookPayload;
78
+ sendWithdrawal: typeof sendZBDWithdrawal;
79
+ getWalletBalance: typeof getWalletBalance;
80
+ usdToMillisats: typeof usdToMillisats;
81
+ millisatsToUsd: typeof millisatsToUsd;
82
+ config: {
83
+ configured: boolean;
84
+ callbackUrl: string;
85
+ satsPerUsd: number;
86
+ };
87
+ };
88
+ export {};
@@ -0,0 +1,221 @@
1
+ /**
2
+ * ZBD Payment Service - Bitcoin on Lightning
3
+ *
4
+ * Uses ZBD API for instant Bitcoin Lightning payments.
5
+ * Docs: https://zbd.dev
6
+ * Fee: ~1%
7
+ */
8
+ // Configuration
9
+ const ZBD_API_KEY = process.env.ZBD_API_KEY || '';
10
+ const ZBD_CALLBACK_URL = process.env.ZBD_CALLBACK_URL || '';
11
+ // API base URL
12
+ const API_BASE = 'https://api.zebedee.io/v0';
13
+ // Current BTC/USD rate (in production, fetch from an API)
14
+ // 1 sat = 0.00000001 BTC
15
+ // At ~$100k/BTC: 1 sat ≈ $0.001, so $1 ≈ 1000 sats
16
+ const SATS_PER_USD = parseInt(process.env.ZBD_SATS_PER_USD || '1000', 10);
17
+ /**
18
+ * Make API request to ZBD
19
+ */
20
+ async function apiRequest(endpoint, method = 'POST', data) {
21
+ if (!ZBD_API_KEY) {
22
+ throw new Error('ZBD_API_KEY not configured');
23
+ }
24
+ const response = await fetch(`${API_BASE}${endpoint}`, {
25
+ method,
26
+ headers: {
27
+ 'Content-Type': 'application/json',
28
+ apikey: ZBD_API_KEY,
29
+ },
30
+ body: data ? JSON.stringify(data) : undefined,
31
+ });
32
+ const result = await response.json();
33
+ if (!result.success) {
34
+ throw new Error(result.message || 'ZBD API error');
35
+ }
36
+ return result.data;
37
+ }
38
+ /**
39
+ * Convert USD to millisats
40
+ */
41
+ function usdToMillisats(usd) {
42
+ const sats = Math.floor(usd * SATS_PER_USD);
43
+ const millisats = sats * 1000;
44
+ return millisats.toString();
45
+ }
46
+ /**
47
+ * Convert millisats to USD
48
+ */
49
+ function millisatsToUsd(millisats) {
50
+ const sats = typeof millisats === 'string' ? parseInt(millisats, 10) / 1000 : millisats / 1000;
51
+ return sats / SATS_PER_USD;
52
+ }
53
+ /**
54
+ * Create a Lightning invoice for BTC deposit
55
+ */
56
+ export async function createZBDDeposit(request) {
57
+ if (!ZBD_API_KEY) {
58
+ return {
59
+ success: false,
60
+ error: 'ZBD_API_KEY not configured. Set this environment variable to your ZBD API key.',
61
+ };
62
+ }
63
+ try {
64
+ const amountMillisats = usdToMillisats(request.amount);
65
+ const chargeData = await apiRequest('/charges', 'POST', {
66
+ amount: amountMillisats,
67
+ description: request.description || `Clawdentials deposit for ${request.agentId}`,
68
+ expiresIn: 600, // 10 minutes (Lightning invoices expire fast)
69
+ internalId: request.agentId,
70
+ callbackUrl: ZBD_CALLBACK_URL,
71
+ });
72
+ const expiresAt = new Date(chargeData.expiresAt);
73
+ return {
74
+ success: true,
75
+ deposit: {
76
+ id: `zbd_${chargeData.id}`,
77
+ agentId: request.agentId,
78
+ amount: request.amount,
79
+ currency: 'BTC',
80
+ network: 'lightning',
81
+ status: 'pending',
82
+ provider: 'zbd',
83
+ externalId: chargeData.id,
84
+ paymentAddress: chargeData.invoice.request, // bolt11 invoice
85
+ paymentUrl: null, // No hosted page, agent pays invoice directly
86
+ createdAt: new Date(),
87
+ expiresAt,
88
+ completedAt: null,
89
+ txHash: null,
90
+ },
91
+ invoice: {
92
+ id: chargeData.id,
93
+ invoice: chargeData.invoice.request,
94
+ amount: amountMillisats,
95
+ expiresAt: chargeData.expiresAt,
96
+ },
97
+ };
98
+ }
99
+ catch (error) {
100
+ return {
101
+ success: false,
102
+ error: error instanceof Error ? error.message : 'Failed to create Lightning invoice',
103
+ };
104
+ }
105
+ }
106
+ /**
107
+ * Check charge/invoice status
108
+ */
109
+ export async function getChargeStatus(chargeId) {
110
+ if (!ZBD_API_KEY) {
111
+ return { success: false, error: 'ZBD_API_KEY not configured' };
112
+ }
113
+ try {
114
+ const data = await apiRequest(`/charges/${chargeId}`, 'GET');
115
+ return {
116
+ success: true,
117
+ status: data.status,
118
+ paid: data.status === 'completed',
119
+ amountUsd: millisatsToUsd(data.amount),
120
+ };
121
+ }
122
+ catch (error) {
123
+ return {
124
+ success: false,
125
+ error: error instanceof Error ? error.message : 'Failed to get charge status',
126
+ };
127
+ }
128
+ }
129
+ /**
130
+ * Process webhook callback from ZBD
131
+ */
132
+ export function parseWebhookPayload(body) {
133
+ return {
134
+ chargeId: body.id,
135
+ status: body.status,
136
+ amountUsd: millisatsToUsd(body.amount),
137
+ agentId: body.internalId,
138
+ };
139
+ }
140
+ /**
141
+ * Send BTC withdrawal via Lightning
142
+ */
143
+ export async function sendZBDWithdrawal(destination, // Lightning invoice or Lightning Address
144
+ amountUsd) {
145
+ if (!ZBD_API_KEY) {
146
+ return { success: false, error: 'ZBD_API_KEY not configured' };
147
+ }
148
+ try {
149
+ const amountMillisats = usdToMillisats(amountUsd);
150
+ // Check if it's a Lightning Address (contains @)
151
+ if (destination.includes('@')) {
152
+ // Pay to Lightning Address
153
+ const data = await apiRequest('/ln-address/send-payment', 'POST', {
154
+ lnAddress: destination,
155
+ amount: amountMillisats,
156
+ comment: 'Clawdentials withdrawal',
157
+ callbackUrl: ZBD_CALLBACK_URL,
158
+ });
159
+ return {
160
+ success: true,
161
+ paymentId: data.id,
162
+ };
163
+ }
164
+ else {
165
+ // Pay to bolt11 invoice
166
+ const data = await apiRequest('/payments', 'POST', {
167
+ invoice: destination,
168
+ description: 'Clawdentials withdrawal',
169
+ internalId: `withdrawal_${Date.now()}`,
170
+ callbackUrl: ZBD_CALLBACK_URL,
171
+ });
172
+ return {
173
+ success: true,
174
+ paymentId: data.id,
175
+ };
176
+ }
177
+ }
178
+ catch (error) {
179
+ return {
180
+ success: false,
181
+ error: error instanceof Error ? error.message : 'Failed to send payment',
182
+ };
183
+ }
184
+ }
185
+ /**
186
+ * Get ZBD wallet balance
187
+ */
188
+ export async function getWalletBalance() {
189
+ if (!ZBD_API_KEY) {
190
+ return { success: false, error: 'ZBD_API_KEY not configured' };
191
+ }
192
+ try {
193
+ const data = await apiRequest('/wallet', 'GET');
194
+ const balanceSats = parseInt(data.balance, 10) / 1000; // millisats to sats
195
+ return {
196
+ success: true,
197
+ balanceSats,
198
+ balanceUsd: balanceSats / SATS_PER_USD,
199
+ };
200
+ }
201
+ catch (error) {
202
+ return {
203
+ success: false,
204
+ error: error instanceof Error ? error.message : 'Failed to get balance',
205
+ };
206
+ }
207
+ }
208
+ export const zbdService = {
209
+ createDeposit: createZBDDeposit,
210
+ getChargeStatus,
211
+ parseWebhookPayload,
212
+ sendWithdrawal: sendZBDWithdrawal,
213
+ getWalletBalance,
214
+ usdToMillisats,
215
+ millisatsToUsd,
216
+ config: {
217
+ configured: !!ZBD_API_KEY,
218
+ callbackUrl: ZBD_CALLBACK_URL,
219
+ satsPerUsd: SATS_PER_USD,
220
+ },
221
+ };
@@ -0,0 +1,195 @@
1
+ import { type AdminCreditBalanceInput, type AdminProcessWithdrawalInput, type AdminListWithdrawalsInput } from '../schemas/index.js';
2
+ import { z } from 'zod';
3
+ declare const adminRefundSchema: z.ZodObject<{
4
+ adminSecret: z.ZodString;
5
+ escrowId: z.ZodString;
6
+ }, "strip", z.ZodTypeAny, {
7
+ escrowId: string;
8
+ adminSecret: string;
9
+ }, {
10
+ escrowId: string;
11
+ adminSecret: string;
12
+ }>;
13
+ type AdminRefundInput = z.infer<typeof adminRefundSchema>;
14
+ export declare const adminTools: {
15
+ admin_credit_balance: {
16
+ description: string;
17
+ inputSchema: z.ZodObject<{
18
+ adminSecret: z.ZodString;
19
+ agentId: z.ZodString;
20
+ amount: z.ZodNumber;
21
+ currency: z.ZodDefault<z.ZodEnum<["USD", "USDC", "BTC"]>>;
22
+ notes: z.ZodOptional<z.ZodString>;
23
+ }, "strip", z.ZodTypeAny, {
24
+ amount: number;
25
+ currency: "USD" | "USDC" | "BTC";
26
+ agentId: string;
27
+ adminSecret: string;
28
+ notes?: string | undefined;
29
+ }, {
30
+ amount: number;
31
+ agentId: string;
32
+ adminSecret: string;
33
+ currency?: "USD" | "USDC" | "BTC" | undefined;
34
+ notes?: string | undefined;
35
+ }>;
36
+ handler: (input: AdminCreditBalanceInput) => Promise<{
37
+ success: boolean;
38
+ error: string;
39
+ message?: undefined;
40
+ agentId?: undefined;
41
+ oldBalance?: undefined;
42
+ credited?: undefined;
43
+ newBalance?: undefined;
44
+ currency?: undefined;
45
+ notes?: undefined;
46
+ } | {
47
+ success: boolean;
48
+ message: string;
49
+ agentId: string;
50
+ oldBalance: number;
51
+ credited: number;
52
+ newBalance: number;
53
+ currency: "USD" | "USDC" | "BTC";
54
+ notes: string | undefined;
55
+ error?: undefined;
56
+ }>;
57
+ };
58
+ admin_list_withdrawals: {
59
+ description: string;
60
+ inputSchema: z.ZodObject<{
61
+ adminSecret: z.ZodString;
62
+ status: z.ZodOptional<z.ZodEnum<["pending", "processing", "completed", "rejected"]>>;
63
+ limit: z.ZodDefault<z.ZodNumber>;
64
+ }, "strip", z.ZodTypeAny, {
65
+ limit: number;
66
+ adminSecret: string;
67
+ status?: "pending" | "processing" | "completed" | "rejected" | undefined;
68
+ }, {
69
+ adminSecret: string;
70
+ status?: "pending" | "processing" | "completed" | "rejected" | undefined;
71
+ limit?: number | undefined;
72
+ }>;
73
+ handler: (input: AdminListWithdrawalsInput) => Promise<{
74
+ success: boolean;
75
+ error: string;
76
+ withdrawals?: undefined;
77
+ count?: undefined;
78
+ } | {
79
+ success: boolean;
80
+ withdrawals: {
81
+ id: string;
82
+ agentId: string;
83
+ amount: number;
84
+ currency: import("../types/index.js").Currency;
85
+ status: import("../types/index.js").WithdrawalStatus;
86
+ paymentMethod: string;
87
+ requestedAt: string;
88
+ processedAt: string | null;
89
+ notes: string | null;
90
+ }[];
91
+ count: number;
92
+ error?: undefined;
93
+ }>;
94
+ };
95
+ admin_process_withdrawal: {
96
+ description: string;
97
+ inputSchema: z.ZodObject<{
98
+ adminSecret: z.ZodString;
99
+ withdrawalId: z.ZodString;
100
+ action: z.ZodEnum<["complete", "reject"]>;
101
+ notes: z.ZodOptional<z.ZodString>;
102
+ }, "strip", z.ZodTypeAny, {
103
+ adminSecret: string;
104
+ withdrawalId: string;
105
+ action: "complete" | "reject";
106
+ notes?: string | undefined;
107
+ }, {
108
+ adminSecret: string;
109
+ withdrawalId: string;
110
+ action: "complete" | "reject";
111
+ notes?: string | undefined;
112
+ }>;
113
+ handler: (input: AdminProcessWithdrawalInput) => Promise<{
114
+ success: boolean;
115
+ error: string;
116
+ message?: undefined;
117
+ withdrawal?: undefined;
118
+ } | {
119
+ success: boolean;
120
+ message: string;
121
+ withdrawal: {
122
+ id: string;
123
+ agentId: string;
124
+ amount: number;
125
+ currency: import("../types/index.js").Currency;
126
+ status: import("../types/index.js").WithdrawalStatus;
127
+ paymentMethod: string;
128
+ processedAt: string | undefined;
129
+ notes: string | null;
130
+ };
131
+ error?: undefined;
132
+ }>;
133
+ };
134
+ admin_refund_escrow: {
135
+ description: string;
136
+ inputSchema: z.ZodObject<{
137
+ adminSecret: z.ZodString;
138
+ escrowId: z.ZodString;
139
+ }, "strip", z.ZodTypeAny, {
140
+ escrowId: string;
141
+ adminSecret: string;
142
+ }, {
143
+ escrowId: string;
144
+ adminSecret: string;
145
+ }>;
146
+ handler: (input: AdminRefundInput) => Promise<{
147
+ success: boolean;
148
+ error: string;
149
+ message?: undefined;
150
+ escrow?: undefined;
151
+ clientNewBalance?: undefined;
152
+ } | {
153
+ success: boolean;
154
+ message: string;
155
+ escrow: {
156
+ id: string;
157
+ status: import("../types/index.js").EscrowStatus;
158
+ amount: number;
159
+ currency: import("../types/index.js").Currency;
160
+ };
161
+ clientNewBalance: number;
162
+ error?: undefined;
163
+ }>;
164
+ };
165
+ admin_nostr_json: {
166
+ description: string;
167
+ inputSchema: z.ZodObject<{
168
+ adminSecret: z.ZodString;
169
+ }, "strip", z.ZodTypeAny, {
170
+ adminSecret: string;
171
+ }, {
172
+ adminSecret: string;
173
+ }>;
174
+ handler: (input: {
175
+ adminSecret: string;
176
+ }) => Promise<{
177
+ success: boolean;
178
+ error: string;
179
+ message?: undefined;
180
+ agentCount?: undefined;
181
+ nostrJson?: undefined;
182
+ nostrJsonString?: undefined;
183
+ } | {
184
+ success: boolean;
185
+ message: string;
186
+ agentCount: number;
187
+ nostrJson: {
188
+ names: Record<string, string>;
189
+ };
190
+ nostrJsonString: string;
191
+ error?: undefined;
192
+ }>;
193
+ };
194
+ };
195
+ export {};