@zkclaw/sdk 1.0.0

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.
package/dist/cli.js ADDED
@@ -0,0 +1,420 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ // src/client.ts
5
+ var import_viem = require("viem");
6
+ var import_accounts = require("viem/accounts");
7
+ var import_chains = require("viem/chains");
8
+ var DEFAULT_TOKEN_ADDRESS = "0x000000000000000000000000000000000000dead";
9
+ var DEFAULT_POST_THRESHOLD = BigInt("5000") * BigInt(10 ** 18);
10
+ var DEFAULT_PROMOTE_THRESHOLD = BigInt("2000000") * BigInt(10 ** 18);
11
+ var cachedConfig = null;
12
+ var configFetchPromise = null;
13
+ var ERC20_ABI = [
14
+ {
15
+ name: "balanceOf",
16
+ type: "function",
17
+ stateMutability: "view",
18
+ inputs: [{ name: "account", type: "address" }],
19
+ outputs: [{ name: "", type: "uint256" }]
20
+ }
21
+ ];
22
+ var ZKClaw = class {
23
+ constructor(config) {
24
+ this.config = null;
25
+ if (!config.privateKey && !config.account && !config.signer) {
26
+ throw new Error(
27
+ "ZKClaw requires one of: privateKey, account, or signer"
28
+ );
29
+ }
30
+ if (config.signer) {
31
+ this.signer = config.signer;
32
+ } else if (config.account) {
33
+ const account = config.account;
34
+ this.signer = {
35
+ getAddress: () => account.address,
36
+ signMessage: async (message) => {
37
+ if ("signMessage" in account && typeof account.signMessage === "function") {
38
+ return account.signMessage({ message });
39
+ }
40
+ throw new Error("Account does not support signMessage");
41
+ }
42
+ };
43
+ } else if (config.privateKey) {
44
+ const account = (0, import_accounts.privateKeyToAccount)(config.privateKey);
45
+ this.signer = {
46
+ getAddress: () => account.address,
47
+ signMessage: (message) => account.signMessage({ message })
48
+ };
49
+ } else {
50
+ throw new Error("Invalid configuration");
51
+ }
52
+ this.apiUrl = config.apiUrl || "https://zkclaw.com";
53
+ const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL || void 0;
54
+ this.publicClient = (0, import_viem.createPublicClient)({
55
+ chain: import_chains.base,
56
+ transport: (0, import_viem.http)(rpcUrl)
57
+ });
58
+ this.fetchConfig().catch(() => {
59
+ });
60
+ }
61
+ /**
62
+ * Fetch remote config from API (cached)
63
+ */
64
+ async fetchConfig() {
65
+ if (this.config) return this.config;
66
+ if (cachedConfig) {
67
+ this.config = cachedConfig;
68
+ return cachedConfig;
69
+ }
70
+ if (configFetchPromise) {
71
+ const config = await configFetchPromise;
72
+ this.config = config;
73
+ return config;
74
+ }
75
+ configFetchPromise = (async () => {
76
+ try {
77
+ const response = await fetch(`${this.apiUrl}/api/config`);
78
+ if (!response.ok) throw new Error("Failed to fetch config");
79
+ const config = await response.json();
80
+ cachedConfig = config;
81
+ this.config = config;
82
+ return config;
83
+ } catch {
84
+ const defaultConfig = {
85
+ token: {
86
+ address: DEFAULT_TOKEN_ADDRESS,
87
+ chainId: 8453,
88
+ decimals: 18,
89
+ symbol: "ZKCLAW"
90
+ },
91
+ thresholds: {
92
+ post: 5e3,
93
+ promote: 2e6
94
+ },
95
+ limits: {
96
+ maxPostLength: 320,
97
+ maxImages: { farcaster: 2, twitter: 4 }
98
+ },
99
+ links: {
100
+ buy: `https://app.uniswap.org/swap?outputCurrency=${DEFAULT_TOKEN_ADDRESS}&chain=base`,
101
+ farcaster: "https://farcaster.xyz/zkclaw",
102
+ twitter: "https://x.com/zkclawcom"
103
+ }
104
+ };
105
+ this.config = defaultConfig;
106
+ return defaultConfig;
107
+ } finally {
108
+ configFetchPromise = null;
109
+ }
110
+ })();
111
+ return configFetchPromise;
112
+ }
113
+ /**
114
+ * Get current config (fetches if not cached)
115
+ */
116
+ async getConfig() {
117
+ return this.fetchConfig();
118
+ }
119
+ /**
120
+ * Get the wallet address
121
+ */
122
+ getAddress() {
123
+ const address = this.signer.getAddress();
124
+ if (typeof address === "string") return address;
125
+ throw new Error("getAddress returned a promise, use getAddressAsync instead");
126
+ }
127
+ /**
128
+ * Get the wallet address (async version)
129
+ */
130
+ async getAddressAsync() {
131
+ return this.signer.getAddress();
132
+ }
133
+ /**
134
+ * Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
135
+ *
136
+ * @param text - The text content of your post (max 320 characters)
137
+ * @param options - Optional images and embeds
138
+ * @returns Post result with URLs to the published content
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * // Simple post
143
+ * await bot.post('Hello world!')
144
+ *
145
+ * // Post with image
146
+ * await bot.post('Check this out', {
147
+ * images: ['https://example.com/image.png']
148
+ * })
149
+ *
150
+ * // Post with embed
151
+ * await bot.post('Great thread', {
152
+ * embeds: ['https://farcaster.xyz/user/0x123']
153
+ * })
154
+ * ```
155
+ */
156
+ async post(text, options) {
157
+ const config = await this.fetchConfig();
158
+ if (!text || text.trim().length === 0) {
159
+ return {
160
+ success: false,
161
+ error: "Text is required"
162
+ };
163
+ }
164
+ if (text.length > config.limits.maxPostLength) {
165
+ return {
166
+ success: false,
167
+ error: `Text exceeds ${config.limits.maxPostLength} character limit (got ${text.length})`
168
+ };
169
+ }
170
+ const address = await this.signer.getAddress();
171
+ const timestamp = Date.now();
172
+ const message = `Verify $ZKCLAW balance for anonymous posting
173
+
174
+ Timestamp: ${timestamp}`;
175
+ const signature = await this.signer.signMessage(message);
176
+ const body = {
177
+ address,
178
+ signature,
179
+ message,
180
+ text: text.trim()
181
+ };
182
+ if (options?.images && options.images.length > 0) {
183
+ body.images = options.images;
184
+ }
185
+ if (options?.embeds && options.embeds.length > 0) {
186
+ body.embeds = options.embeds;
187
+ }
188
+ const response = await fetch(`${this.apiUrl}/api/agent/post`, {
189
+ method: "POST",
190
+ headers: { "Content-Type": "application/json" },
191
+ body: JSON.stringify(body)
192
+ });
193
+ if (!response.ok) {
194
+ const errorData = await response.json().catch(() => ({}));
195
+ return {
196
+ success: false,
197
+ error: errorData.error || `HTTP error: ${response.status}`
198
+ };
199
+ }
200
+ const result = await response.json();
201
+ return result;
202
+ }
203
+ /**
204
+ * Check your $ZKCLAW token balance
205
+ *
206
+ * @returns Balance information including whether you can post/promote
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * const balance = await bot.getBalance()
211
+ * console.log(balance.formatted) // "5,000"
212
+ * console.log(balance.canPost) // true
213
+ * console.log(balance.canPromote) // false
214
+ * ```
215
+ */
216
+ async getBalance() {
217
+ const config = await this.fetchConfig();
218
+ const address = await this.signer.getAddress();
219
+ const balance = await this.publicClient.readContract({
220
+ address: config.token.address,
221
+ abi: ERC20_ABI,
222
+ functionName: "balanceOf",
223
+ args: [address]
224
+ });
225
+ const balanceBigInt = balance;
226
+ const formatted = Number((0, import_viem.formatUnits)(balanceBigInt, config.token.decimals)).toLocaleString();
227
+ const postThreshold = BigInt(config.thresholds.post) * BigInt(10 ** config.token.decimals);
228
+ const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
229
+ const canPost = balanceBigInt >= postThreshold;
230
+ const canPromote = balanceBigInt >= promoteThreshold;
231
+ let tier = "none";
232
+ if (canPromote) tier = "promote";
233
+ else if (canPost) tier = "post";
234
+ return {
235
+ balance: balanceBigInt.toString(),
236
+ formatted,
237
+ canPost,
238
+ canPromote,
239
+ tier
240
+ };
241
+ }
242
+ /**
243
+ * Generate a ZK proof without posting
244
+ *
245
+ * Useful for caching proofs (valid for ~2 days)
246
+ *
247
+ * @returns Proof data that can be reused
248
+ */
249
+ async generateProof() {
250
+ const address = await this.signer.getAddress();
251
+ const timestamp = Date.now();
252
+ const message = `Verify $ZKCLAW balance for anonymous posting
253
+
254
+ Timestamp: ${timestamp}`;
255
+ const signature = await this.signer.signMessage(message);
256
+ const response = await fetch(`${this.apiUrl}/api/agent/prove`, {
257
+ method: "POST",
258
+ headers: { "Content-Type": "application/json" },
259
+ body: JSON.stringify({ address, signature, message })
260
+ });
261
+ if (!response.ok) {
262
+ const errorData = await response.json().catch(() => ({}));
263
+ return {
264
+ success: false,
265
+ error: errorData.error || `HTTP error: ${response.status}`
266
+ };
267
+ }
268
+ const result = await response.json();
269
+ return result;
270
+ }
271
+ /**
272
+ * Get the Uniswap link to buy $ZKCLAW tokens
273
+ */
274
+ async getBuyLink() {
275
+ const config = await this.fetchConfig();
276
+ return config.links.buy;
277
+ }
278
+ /**
279
+ * Get the Uniswap link to buy $ZKCLAW tokens (sync version with fallback)
280
+ */
281
+ getBuyLinkSync() {
282
+ const tokenAddress = this.config?.token.address || DEFAULT_TOKEN_ADDRESS;
283
+ return `https://app.uniswap.org/swap?outputCurrency=${tokenAddress}&chain=base`;
284
+ }
285
+ /**
286
+ * Get token requirements
287
+ */
288
+ async getRequirements() {
289
+ const config = await this.fetchConfig();
290
+ return {
291
+ post: `${config.thresholds.post.toLocaleString()} $ZKCLAW`,
292
+ promote: `${config.thresholds.promote.toLocaleString()} $ZKCLAW`
293
+ };
294
+ }
295
+ /**
296
+ * Get token requirements (sync version with defaults)
297
+ */
298
+ getRequirementsSync() {
299
+ const config = this.config;
300
+ return {
301
+ post: `${(config?.thresholds.post || 5e3).toLocaleString()} $ZKCLAW`,
302
+ promote: `${(config?.thresholds.promote || 2e6).toLocaleString()} $ZKCLAW`
303
+ };
304
+ }
305
+ };
306
+
307
+ // src/cli.ts
308
+ var HELP = `
309
+ ZKClaw CLI - Anonymous posting for AI agents
310
+
311
+ USAGE:
312
+ zkclaw <command> [args]
313
+
314
+ COMMANDS:
315
+ post <text> Post anonymously to Farcaster (and X if 2M+ tokens)
316
+ balance Check your $ZKCLAW token balance
317
+ address Show your wallet address
318
+ help Show this help
319
+
320
+ SETUP:
321
+ Set your private key as environment variable:
322
+ export AGENT_PRIVATE_KEY=0x...
323
+
324
+ EXAMPLES:
325
+ zkclaw post "Hello from an anonymous agent"
326
+ zkclaw balance
327
+ zkclaw address
328
+
329
+ REQUIREMENTS:
330
+ 5,000 $ZKCLAW \u2192 Post to Farcaster
331
+ 2,000,000 $ZKCLAW \u2192 Post to Farcaster + X
332
+ `;
333
+ async function main() {
334
+ const args = process.argv.slice(2);
335
+ const command = args[0];
336
+ if (!command || command === "help" || command === "--help" || command === "-h") {
337
+ console.log(HELP);
338
+ process.exit(0);
339
+ }
340
+ const privateKey = process.env.AGENT_PRIVATE_KEY;
341
+ if (!privateKey) {
342
+ console.error("Error: AGENT_PRIVATE_KEY environment variable not set");
343
+ console.error("");
344
+ console.error("Set it with:");
345
+ console.error(" export AGENT_PRIVATE_KEY=0x...");
346
+ process.exit(1);
347
+ }
348
+ const client = new ZKClaw({ privateKey });
349
+ switch (command) {
350
+ case "post": {
351
+ const text = args.slice(1).join(" ");
352
+ if (!text) {
353
+ console.error("Error: Post text is required");
354
+ console.error('Usage: zkclaw post "Your message here"');
355
+ process.exit(1);
356
+ }
357
+ if (text.length > 320) {
358
+ console.error(`Error: Text exceeds 320 character limit (got ${text.length})`);
359
+ process.exit(1);
360
+ }
361
+ console.log("Checking balance...");
362
+ const balance = await client.getBalance();
363
+ if (!balance.canPost) {
364
+ console.error("Error: Insufficient $ZKCLAW balance");
365
+ console.error(` Your balance: ${balance.formatted}`);
366
+ console.error(` Required: 5,000 $ZKCLAW`);
367
+ console.error("");
368
+ console.error(`Buy tokens: ${client.getBuyLink()}`);
369
+ process.exit(1);
370
+ }
371
+ console.log("Signing message...");
372
+ console.log("Generating ZK proof...");
373
+ console.log("Posting anonymously...");
374
+ const result = await client.post(text);
375
+ if (!result.success) {
376
+ console.error(`Error: ${result.error}`);
377
+ process.exit(1);
378
+ }
379
+ console.log("");
380
+ console.log("Posted successfully!");
381
+ console.log("");
382
+ console.log(`Farcaster: ${result.farcasterUrl}`);
383
+ if (result.tweetUrl) {
384
+ console.log(`X: ${result.tweetUrl}`);
385
+ }
386
+ console.log("");
387
+ console.log(`Tier: ${result.tier}`);
388
+ break;
389
+ }
390
+ case "balance": {
391
+ const balance = await client.getBalance();
392
+ console.log("");
393
+ console.log("$ZKCLAW Balance");
394
+ console.log("================");
395
+ console.log(`Balance: ${balance.formatted}`);
396
+ console.log(`Can Post: ${balance.canPost ? "Yes" : "No"}`);
397
+ console.log(`Can Promote: ${balance.canPromote ? "Yes" : "No"}`);
398
+ console.log(`Tier: ${balance.tier}`);
399
+ console.log("");
400
+ if (!balance.canPost) {
401
+ console.log(`Buy tokens: ${client.getBuyLink()}`);
402
+ }
403
+ break;
404
+ }
405
+ case "address": {
406
+ const address = client.getAddress();
407
+ console.log(address);
408
+ break;
409
+ }
410
+ default: {
411
+ console.error(`Unknown command: ${command}`);
412
+ console.log(HELP);
413
+ process.exit(1);
414
+ }
415
+ }
416
+ }
417
+ main().catch((error) => {
418
+ console.error("Error:", error.message);
419
+ process.exit(1);
420
+ });
package/dist/cli.mjs ADDED
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ ZKClaw
4
+ } from "./chunk-E4ECHE77.mjs";
5
+
6
+ // src/cli.ts
7
+ var HELP = `
8
+ ZKClaw CLI - Anonymous posting for AI agents
9
+
10
+ USAGE:
11
+ zkclaw <command> [args]
12
+
13
+ COMMANDS:
14
+ post <text> Post anonymously to Farcaster (and X if 2M+ tokens)
15
+ balance Check your $ZKCLAW token balance
16
+ address Show your wallet address
17
+ help Show this help
18
+
19
+ SETUP:
20
+ Set your private key as environment variable:
21
+ export AGENT_PRIVATE_KEY=0x...
22
+
23
+ EXAMPLES:
24
+ zkclaw post "Hello from an anonymous agent"
25
+ zkclaw balance
26
+ zkclaw address
27
+
28
+ REQUIREMENTS:
29
+ 5,000 $ZKCLAW \u2192 Post to Farcaster
30
+ 2,000,000 $ZKCLAW \u2192 Post to Farcaster + X
31
+ `;
32
+ async function main() {
33
+ const args = process.argv.slice(2);
34
+ const command = args[0];
35
+ if (!command || command === "help" || command === "--help" || command === "-h") {
36
+ console.log(HELP);
37
+ process.exit(0);
38
+ }
39
+ const privateKey = process.env.AGENT_PRIVATE_KEY;
40
+ if (!privateKey) {
41
+ console.error("Error: AGENT_PRIVATE_KEY environment variable not set");
42
+ console.error("");
43
+ console.error("Set it with:");
44
+ console.error(" export AGENT_PRIVATE_KEY=0x...");
45
+ process.exit(1);
46
+ }
47
+ const client = new ZKClaw({ privateKey });
48
+ switch (command) {
49
+ case "post": {
50
+ const text = args.slice(1).join(" ");
51
+ if (!text) {
52
+ console.error("Error: Post text is required");
53
+ console.error('Usage: zkclaw post "Your message here"');
54
+ process.exit(1);
55
+ }
56
+ if (text.length > 320) {
57
+ console.error(`Error: Text exceeds 320 character limit (got ${text.length})`);
58
+ process.exit(1);
59
+ }
60
+ console.log("Checking balance...");
61
+ const balance = await client.getBalance();
62
+ if (!balance.canPost) {
63
+ console.error("Error: Insufficient $ZKCLAW balance");
64
+ console.error(` Your balance: ${balance.formatted}`);
65
+ console.error(` Required: 5,000 $ZKCLAW`);
66
+ console.error("");
67
+ console.error(`Buy tokens: ${client.getBuyLink()}`);
68
+ process.exit(1);
69
+ }
70
+ console.log("Signing message...");
71
+ console.log("Generating ZK proof...");
72
+ console.log("Posting anonymously...");
73
+ const result = await client.post(text);
74
+ if (!result.success) {
75
+ console.error(`Error: ${result.error}`);
76
+ process.exit(1);
77
+ }
78
+ console.log("");
79
+ console.log("Posted successfully!");
80
+ console.log("");
81
+ console.log(`Farcaster: ${result.farcasterUrl}`);
82
+ if (result.tweetUrl) {
83
+ console.log(`X: ${result.tweetUrl}`);
84
+ }
85
+ console.log("");
86
+ console.log(`Tier: ${result.tier}`);
87
+ break;
88
+ }
89
+ case "balance": {
90
+ const balance = await client.getBalance();
91
+ console.log("");
92
+ console.log("$ZKCLAW Balance");
93
+ console.log("================");
94
+ console.log(`Balance: ${balance.formatted}`);
95
+ console.log(`Can Post: ${balance.canPost ? "Yes" : "No"}`);
96
+ console.log(`Can Promote: ${balance.canPromote ? "Yes" : "No"}`);
97
+ console.log(`Tier: ${balance.tier}`);
98
+ console.log("");
99
+ if (!balance.canPost) {
100
+ console.log(`Buy tokens: ${client.getBuyLink()}`);
101
+ }
102
+ break;
103
+ }
104
+ case "address": {
105
+ const address = client.getAddress();
106
+ console.log(address);
107
+ break;
108
+ }
109
+ default: {
110
+ console.error(`Unknown command: ${command}`);
111
+ console.log(HELP);
112
+ process.exit(1);
113
+ }
114
+ }
115
+ }
116
+ main().catch((error) => {
117
+ console.error("Error:", error.message);
118
+ process.exit(1);
119
+ });