@zkclaw/sdk 1.0.0 → 2.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/chunk-L5QERYJQ.mjs +350 -0
- package/dist/cli.js +107 -46
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +95 -46
- package/dist/index.mjs +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import {
|
|
3
|
+
createPublicClient,
|
|
4
|
+
http,
|
|
5
|
+
formatUnits,
|
|
6
|
+
hashMessage
|
|
7
|
+
} from "viem";
|
|
8
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
9
|
+
import { base } from "viem/chains";
|
|
10
|
+
var DEFAULT_TOKEN_ADDRESS = "0x000000000000000000000000000000000000dead";
|
|
11
|
+
var DEFAULT_POST_THRESHOLD = BigInt("5000") * BigInt(10 ** 18);
|
|
12
|
+
var DEFAULT_PROMOTE_THRESHOLD = BigInt("2000000") * BigInt(10 ** 18);
|
|
13
|
+
var cachedConfig = null;
|
|
14
|
+
var configFetchPromise = null;
|
|
15
|
+
var ERC20_ABI = [
|
|
16
|
+
{
|
|
17
|
+
name: "balanceOf",
|
|
18
|
+
type: "function",
|
|
19
|
+
stateMutability: "view",
|
|
20
|
+
inputs: [{ name: "account", type: "address" }],
|
|
21
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
var ZKClaw = class {
|
|
25
|
+
constructor(config) {
|
|
26
|
+
this.config = null;
|
|
27
|
+
if (!config.privateKey && !config.account && !config.signer) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"ZKClaw requires one of: privateKey, account, or signer"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (config.signer) {
|
|
33
|
+
this.signer = config.signer;
|
|
34
|
+
} else if (config.account) {
|
|
35
|
+
const account = config.account;
|
|
36
|
+
this.signer = {
|
|
37
|
+
getAddress: () => account.address,
|
|
38
|
+
signMessage: async (message) => {
|
|
39
|
+
if ("signMessage" in account && typeof account.signMessage === "function") {
|
|
40
|
+
return account.signMessage({ message });
|
|
41
|
+
}
|
|
42
|
+
throw new Error("Account does not support signMessage");
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
} else if (config.privateKey) {
|
|
46
|
+
const account = privateKeyToAccount(config.privateKey);
|
|
47
|
+
this.signer = {
|
|
48
|
+
getAddress: () => account.address,
|
|
49
|
+
signMessage: (message) => account.signMessage({ message })
|
|
50
|
+
};
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error("Invalid configuration");
|
|
53
|
+
}
|
|
54
|
+
this.apiUrl = config.apiUrl || "https://zkclaw.com";
|
|
55
|
+
const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL || "https://mainnet.base.org";
|
|
56
|
+
this.publicClient = createPublicClient({
|
|
57
|
+
chain: base,
|
|
58
|
+
transport: http(rpcUrl)
|
|
59
|
+
});
|
|
60
|
+
this.fetchConfig().catch(() => {
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fetch remote config from API (cached)
|
|
65
|
+
*/
|
|
66
|
+
async fetchConfig() {
|
|
67
|
+
if (this.config) return this.config;
|
|
68
|
+
if (cachedConfig) {
|
|
69
|
+
this.config = cachedConfig;
|
|
70
|
+
return cachedConfig;
|
|
71
|
+
}
|
|
72
|
+
if (configFetchPromise) {
|
|
73
|
+
const config = await configFetchPromise;
|
|
74
|
+
this.config = config;
|
|
75
|
+
return config;
|
|
76
|
+
}
|
|
77
|
+
configFetchPromise = (async () => {
|
|
78
|
+
try {
|
|
79
|
+
const response = await fetch(`${this.apiUrl}/api/config`);
|
|
80
|
+
if (!response.ok) throw new Error("Failed to fetch config");
|
|
81
|
+
const config = await response.json();
|
|
82
|
+
cachedConfig = config;
|
|
83
|
+
this.config = config;
|
|
84
|
+
return config;
|
|
85
|
+
} catch {
|
|
86
|
+
const defaultConfig = {
|
|
87
|
+
token: {
|
|
88
|
+
address: DEFAULT_TOKEN_ADDRESS,
|
|
89
|
+
chainId: 8453,
|
|
90
|
+
decimals: 18,
|
|
91
|
+
symbol: "ZKCLAW"
|
|
92
|
+
},
|
|
93
|
+
thresholds: {
|
|
94
|
+
post: 5e3,
|
|
95
|
+
promote: 2e6
|
|
96
|
+
},
|
|
97
|
+
limits: {
|
|
98
|
+
maxPostLength: 320,
|
|
99
|
+
maxImages: { farcaster: 2, twitter: 4 }
|
|
100
|
+
},
|
|
101
|
+
links: {
|
|
102
|
+
buy: `https://app.uniswap.org/swap?outputCurrency=${DEFAULT_TOKEN_ADDRESS}&chain=base`,
|
|
103
|
+
farcaster: "https://farcaster.xyz/zkclaw",
|
|
104
|
+
twitter: "https://x.com/zkclawcom"
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
this.config = defaultConfig;
|
|
108
|
+
return defaultConfig;
|
|
109
|
+
} finally {
|
|
110
|
+
configFetchPromise = null;
|
|
111
|
+
}
|
|
112
|
+
})();
|
|
113
|
+
return configFetchPromise;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get current config (fetches if not cached)
|
|
117
|
+
*/
|
|
118
|
+
async getConfig() {
|
|
119
|
+
return this.fetchConfig();
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the wallet address
|
|
123
|
+
*/
|
|
124
|
+
getAddress() {
|
|
125
|
+
const address = this.signer.getAddress();
|
|
126
|
+
if (typeof address === "string") return address;
|
|
127
|
+
throw new Error("getAddress returned a promise, use getAddressAsync instead");
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get the wallet address (async version)
|
|
131
|
+
*/
|
|
132
|
+
async getAddressAsync() {
|
|
133
|
+
return this.signer.getAddress();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Generate a ZK proof locally
|
|
137
|
+
* Proof generation happens in your Node.js environment using WASM
|
|
138
|
+
*/
|
|
139
|
+
async generateProofLocally() {
|
|
140
|
+
const { AnonBalanceVerifier, BALANCE_THRESHOLDS } = await import("@anon/credentials");
|
|
141
|
+
const address = await this.signer.getAddress();
|
|
142
|
+
const message = `Verify $ZKCLAW balance for anonymous posting
|
|
143
|
+
|
|
144
|
+
Timestamp: ${Date.now()}`;
|
|
145
|
+
const signature = await this.signer.signMessage(message);
|
|
146
|
+
const messageHash = hashMessage(message);
|
|
147
|
+
const verifier = new AnonBalanceVerifier();
|
|
148
|
+
const { input } = await verifier.buildInput(address, BALANCE_THRESHOLDS.POST);
|
|
149
|
+
const actualBalance = input.storageProof[0].value;
|
|
150
|
+
if (actualBalance < BALANCE_THRESHOLDS.POST) {
|
|
151
|
+
const config = await this.fetchConfig();
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Insufficient $ZKCLAW balance. Required: ${config.thresholds.post.toLocaleString()}, got: ${Number(formatUnits(actualBalance, 18)).toLocaleString()}. Buy at: ${config.links.buy}`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const threshold = actualBalance >= BALANCE_THRESHOLDS.PROMOTE ? BALANCE_THRESHOLDS.PROMOTE : BALANCE_THRESHOLDS.POST;
|
|
157
|
+
const { input: finalInput } = await verifier.buildInput(address, threshold);
|
|
158
|
+
const proofResult = await verifier.generateProof({
|
|
159
|
+
...finalInput,
|
|
160
|
+
signature,
|
|
161
|
+
messageHash
|
|
162
|
+
});
|
|
163
|
+
return {
|
|
164
|
+
proof: {
|
|
165
|
+
proof: Array.from(proofResult.proof),
|
|
166
|
+
publicInputs: proofResult.publicInputs
|
|
167
|
+
},
|
|
168
|
+
balance: actualBalance
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
|
|
173
|
+
*
|
|
174
|
+
* @param text - The text content of your post (max 320 characters)
|
|
175
|
+
* @param options - Optional images and embeds
|
|
176
|
+
* @returns Post result with URLs to the published content
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* // Simple post
|
|
181
|
+
* await bot.post('Hello world!')
|
|
182
|
+
*
|
|
183
|
+
* // Post with image
|
|
184
|
+
* await bot.post('Check this out', {
|
|
185
|
+
* images: ['https://example.com/image.png']
|
|
186
|
+
* })
|
|
187
|
+
*
|
|
188
|
+
* // Post with embed
|
|
189
|
+
* await bot.post('Great thread', {
|
|
190
|
+
* embeds: ['https://farcaster.xyz/user/0x123']
|
|
191
|
+
* })
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
async post(text, options) {
|
|
195
|
+
const config = await this.fetchConfig();
|
|
196
|
+
if (!text || text.trim().length === 0) {
|
|
197
|
+
return {
|
|
198
|
+
success: false,
|
|
199
|
+
error: "Text is required"
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (text.length > config.limits.maxPostLength) {
|
|
203
|
+
return {
|
|
204
|
+
success: false,
|
|
205
|
+
error: `Text exceeds ${config.limits.maxPostLength} character limit (got ${text.length})`
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
210
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
211
|
+
const canPromote = balance >= promoteThreshold;
|
|
212
|
+
const body = {
|
|
213
|
+
proof,
|
|
214
|
+
text: text.trim(),
|
|
215
|
+
platforms: {
|
|
216
|
+
farcaster: true,
|
|
217
|
+
twitter: canPromote
|
|
218
|
+
// Auto-enable Twitter if user has enough balance
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
if (options?.images && options.images.length > 0) {
|
|
222
|
+
body.images = options.images;
|
|
223
|
+
}
|
|
224
|
+
if (options?.embeds && options.embeds.length > 0) {
|
|
225
|
+
body.embeds = options.embeds;
|
|
226
|
+
}
|
|
227
|
+
const response = await fetch(`${this.apiUrl}/api/post`, {
|
|
228
|
+
method: "POST",
|
|
229
|
+
headers: { "Content-Type": "application/json" },
|
|
230
|
+
body: JSON.stringify(body)
|
|
231
|
+
});
|
|
232
|
+
if (!response.ok) {
|
|
233
|
+
const errorData = await response.json().catch(() => ({}));
|
|
234
|
+
return {
|
|
235
|
+
success: false,
|
|
236
|
+
error: errorData.error || `HTTP error: ${response.status}`
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const result = await response.json();
|
|
240
|
+
return result;
|
|
241
|
+
} catch (err) {
|
|
242
|
+
return {
|
|
243
|
+
success: false,
|
|
244
|
+
error: err instanceof Error ? err.message : "Failed to generate proof or post"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Check your $ZKCLAW token balance
|
|
250
|
+
*
|
|
251
|
+
* @returns Balance information including whether you can post/promote
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* const balance = await bot.getBalance()
|
|
256
|
+
* console.log(balance.formatted) // "5,000"
|
|
257
|
+
* console.log(balance.canPost) // true
|
|
258
|
+
* console.log(balance.canPromote) // false
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
async getBalance() {
|
|
262
|
+
const config = await this.fetchConfig();
|
|
263
|
+
const address = await this.signer.getAddress();
|
|
264
|
+
const balance = await this.publicClient.readContract({
|
|
265
|
+
address: config.token.address,
|
|
266
|
+
abi: ERC20_ABI,
|
|
267
|
+
functionName: "balanceOf",
|
|
268
|
+
args: [address]
|
|
269
|
+
});
|
|
270
|
+
const balanceBigInt = balance;
|
|
271
|
+
const formatted = Number(formatUnits(balanceBigInt, config.token.decimals)).toLocaleString();
|
|
272
|
+
const postThreshold = BigInt(config.thresholds.post) * BigInt(10 ** config.token.decimals);
|
|
273
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
274
|
+
const canPost = balanceBigInt >= postThreshold;
|
|
275
|
+
const canPromote = balanceBigInt >= promoteThreshold;
|
|
276
|
+
let tier = "none";
|
|
277
|
+
if (canPromote) tier = "promote";
|
|
278
|
+
else if (canPost) tier = "post";
|
|
279
|
+
return {
|
|
280
|
+
balance: balanceBigInt.toString(),
|
|
281
|
+
formatted,
|
|
282
|
+
canPost,
|
|
283
|
+
canPromote,
|
|
284
|
+
tier
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Generate a ZK proof without posting
|
|
289
|
+
*
|
|
290
|
+
* Useful for testing or caching proofs
|
|
291
|
+
*
|
|
292
|
+
* @returns Proof data that can be reused
|
|
293
|
+
*/
|
|
294
|
+
async generateProof() {
|
|
295
|
+
try {
|
|
296
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
297
|
+
const config = await this.fetchConfig();
|
|
298
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
299
|
+
return {
|
|
300
|
+
success: true,
|
|
301
|
+
proof,
|
|
302
|
+
balance: balance.toString(),
|
|
303
|
+
tier: balance >= promoteThreshold ? "promote" : "post"
|
|
304
|
+
};
|
|
305
|
+
} catch (err) {
|
|
306
|
+
return {
|
|
307
|
+
success: false,
|
|
308
|
+
error: err instanceof Error ? err.message : "Failed to generate proof"
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Get the Uniswap link to buy $ZKCLAW tokens
|
|
314
|
+
*/
|
|
315
|
+
async getBuyLink() {
|
|
316
|
+
const config = await this.fetchConfig();
|
|
317
|
+
return config.links.buy;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get the Uniswap link to buy $ZKCLAW tokens (sync version with fallback)
|
|
321
|
+
*/
|
|
322
|
+
getBuyLinkSync() {
|
|
323
|
+
const tokenAddress = this.config?.token.address || DEFAULT_TOKEN_ADDRESS;
|
|
324
|
+
return `https://app.uniswap.org/swap?outputCurrency=${tokenAddress}&chain=base`;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Get token requirements
|
|
328
|
+
*/
|
|
329
|
+
async getRequirements() {
|
|
330
|
+
const config = await this.fetchConfig();
|
|
331
|
+
return {
|
|
332
|
+
post: `${config.thresholds.post.toLocaleString()} $ZKCLAW`,
|
|
333
|
+
promote: `${config.thresholds.promote.toLocaleString()} $ZKCLAW`
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Get token requirements (sync version with defaults)
|
|
338
|
+
*/
|
|
339
|
+
getRequirementsSync() {
|
|
340
|
+
const config = this.config;
|
|
341
|
+
return {
|
|
342
|
+
post: `${(config?.thresholds.post || 5e3).toLocaleString()} $ZKCLAW`,
|
|
343
|
+
promote: `${(config?.thresholds.promote || 2e6).toLocaleString()} $ZKCLAW`
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
export {
|
|
349
|
+
ZKClaw
|
|
350
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
3
25
|
|
|
4
26
|
// src/client.ts
|
|
5
27
|
var import_viem = require("viem");
|
|
@@ -50,7 +72,7 @@ var ZKClaw = class {
|
|
|
50
72
|
throw new Error("Invalid configuration");
|
|
51
73
|
}
|
|
52
74
|
this.apiUrl = config.apiUrl || "https://zkclaw.com";
|
|
53
|
-
const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL ||
|
|
75
|
+
const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL || "https://mainnet.base.org";
|
|
54
76
|
this.publicClient = (0, import_viem.createPublicClient)({
|
|
55
77
|
chain: import_chains.base,
|
|
56
78
|
transport: (0, import_viem.http)(rpcUrl)
|
|
@@ -130,6 +152,42 @@ var ZKClaw = class {
|
|
|
130
152
|
async getAddressAsync() {
|
|
131
153
|
return this.signer.getAddress();
|
|
132
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Generate a ZK proof locally
|
|
157
|
+
* Proof generation happens in your Node.js environment using WASM
|
|
158
|
+
*/
|
|
159
|
+
async generateProofLocally() {
|
|
160
|
+
const { AnonBalanceVerifier, BALANCE_THRESHOLDS } = await import("@anon/credentials");
|
|
161
|
+
const address = await this.signer.getAddress();
|
|
162
|
+
const message = `Verify $ZKCLAW balance for anonymous posting
|
|
163
|
+
|
|
164
|
+
Timestamp: ${Date.now()}`;
|
|
165
|
+
const signature = await this.signer.signMessage(message);
|
|
166
|
+
const messageHash = (0, import_viem.hashMessage)(message);
|
|
167
|
+
const verifier = new AnonBalanceVerifier();
|
|
168
|
+
const { input } = await verifier.buildInput(address, BALANCE_THRESHOLDS.POST);
|
|
169
|
+
const actualBalance = input.storageProof[0].value;
|
|
170
|
+
if (actualBalance < BALANCE_THRESHOLDS.POST) {
|
|
171
|
+
const config = await this.fetchConfig();
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Insufficient $ZKCLAW balance. Required: ${config.thresholds.post.toLocaleString()}, got: ${Number((0, import_viem.formatUnits)(actualBalance, 18)).toLocaleString()}. Buy at: ${config.links.buy}`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
const threshold = actualBalance >= BALANCE_THRESHOLDS.PROMOTE ? BALANCE_THRESHOLDS.PROMOTE : BALANCE_THRESHOLDS.POST;
|
|
177
|
+
const { input: finalInput } = await verifier.buildInput(address, threshold);
|
|
178
|
+
const proofResult = await verifier.generateProof({
|
|
179
|
+
...finalInput,
|
|
180
|
+
signature,
|
|
181
|
+
messageHash
|
|
182
|
+
});
|
|
183
|
+
return {
|
|
184
|
+
proof: {
|
|
185
|
+
proof: Array.from(proofResult.proof),
|
|
186
|
+
publicInputs: proofResult.publicInputs
|
|
187
|
+
},
|
|
188
|
+
balance: actualBalance
|
|
189
|
+
};
|
|
190
|
+
}
|
|
133
191
|
/**
|
|
134
192
|
* Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
|
|
135
193
|
*
|
|
@@ -167,38 +225,45 @@ var ZKClaw = class {
|
|
|
167
225
|
error: `Text exceeds ${config.limits.maxPostLength} character limit (got ${text.length})`
|
|
168
226
|
};
|
|
169
227
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
228
|
+
try {
|
|
229
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
230
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
231
|
+
const canPromote = balance >= promoteThreshold;
|
|
232
|
+
const body = {
|
|
233
|
+
proof,
|
|
234
|
+
text: text.trim(),
|
|
235
|
+
platforms: {
|
|
236
|
+
farcaster: true,
|
|
237
|
+
twitter: canPromote
|
|
238
|
+
// Auto-enable Twitter if user has enough balance
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
if (options?.images && options.images.length > 0) {
|
|
242
|
+
body.images = options.images;
|
|
243
|
+
}
|
|
244
|
+
if (options?.embeds && options.embeds.length > 0) {
|
|
245
|
+
body.embeds = options.embeds;
|
|
246
|
+
}
|
|
247
|
+
const response = await fetch(`${this.apiUrl}/api/post`, {
|
|
248
|
+
method: "POST",
|
|
249
|
+
headers: { "Content-Type": "application/json" },
|
|
250
|
+
body: JSON.stringify(body)
|
|
251
|
+
});
|
|
252
|
+
if (!response.ok) {
|
|
253
|
+
const errorData = await response.json().catch(() => ({}));
|
|
254
|
+
return {
|
|
255
|
+
success: false,
|
|
256
|
+
error: errorData.error || `HTTP error: ${response.status}`
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const result = await response.json();
|
|
260
|
+
return result;
|
|
261
|
+
} catch (err) {
|
|
195
262
|
return {
|
|
196
263
|
success: false,
|
|
197
|
-
error:
|
|
264
|
+
error: err instanceof Error ? err.message : "Failed to generate proof or post"
|
|
198
265
|
};
|
|
199
266
|
}
|
|
200
|
-
const result = await response.json();
|
|
201
|
-
return result;
|
|
202
267
|
}
|
|
203
268
|
/**
|
|
204
269
|
* Check your $ZKCLAW token balance
|
|
@@ -242,31 +307,27 @@ Timestamp: ${timestamp}`;
|
|
|
242
307
|
/**
|
|
243
308
|
* Generate a ZK proof without posting
|
|
244
309
|
*
|
|
245
|
-
* Useful for caching proofs
|
|
310
|
+
* Useful for testing or caching proofs
|
|
246
311
|
*
|
|
247
312
|
* @returns Proof data that can be reused
|
|
248
313
|
*/
|
|
249
314
|
async generateProof() {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
})
|
|
261
|
-
if (!response.ok) {
|
|
262
|
-
const errorData = await response.json().catch(() => ({}));
|
|
315
|
+
try {
|
|
316
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
317
|
+
const config = await this.fetchConfig();
|
|
318
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
319
|
+
return {
|
|
320
|
+
success: true,
|
|
321
|
+
proof,
|
|
322
|
+
balance: balance.toString(),
|
|
323
|
+
tier: balance >= promoteThreshold ? "promote" : "post"
|
|
324
|
+
};
|
|
325
|
+
} catch (err) {
|
|
263
326
|
return {
|
|
264
327
|
success: false,
|
|
265
|
-
error:
|
|
328
|
+
error: err instanceof Error ? err.message : "Failed to generate proof"
|
|
266
329
|
};
|
|
267
330
|
}
|
|
268
|
-
const result = await response.json();
|
|
269
|
-
return result;
|
|
270
331
|
}
|
|
271
332
|
/**
|
|
272
333
|
* Get the Uniswap link to buy $ZKCLAW tokens
|
package/dist/cli.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -124,6 +124,7 @@ type ZKClawRemoteConfig = {
|
|
|
124
124
|
* ZKClaw SDK Client
|
|
125
125
|
*
|
|
126
126
|
* Post anonymously to Farcaster and Twitter using ZK proofs.
|
|
127
|
+
* Proof generation happens locally in your Node.js environment.
|
|
127
128
|
*
|
|
128
129
|
* @example
|
|
129
130
|
* ```typescript
|
|
@@ -159,6 +160,11 @@ declare class ZKClaw {
|
|
|
159
160
|
* Get the wallet address (async version)
|
|
160
161
|
*/
|
|
161
162
|
getAddressAsync(): Promise<string>;
|
|
163
|
+
/**
|
|
164
|
+
* Generate a ZK proof locally
|
|
165
|
+
* Proof generation happens in your Node.js environment using WASM
|
|
166
|
+
*/
|
|
167
|
+
private generateProofLocally;
|
|
162
168
|
/**
|
|
163
169
|
* Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
|
|
164
170
|
*
|
|
@@ -200,7 +206,7 @@ declare class ZKClaw {
|
|
|
200
206
|
/**
|
|
201
207
|
* Generate a ZK proof without posting
|
|
202
208
|
*
|
|
203
|
-
* Useful for caching proofs
|
|
209
|
+
* Useful for testing or caching proofs
|
|
204
210
|
*
|
|
205
211
|
* @returns Proof data that can be reused
|
|
206
212
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -124,6 +124,7 @@ type ZKClawRemoteConfig = {
|
|
|
124
124
|
* ZKClaw SDK Client
|
|
125
125
|
*
|
|
126
126
|
* Post anonymously to Farcaster and Twitter using ZK proofs.
|
|
127
|
+
* Proof generation happens locally in your Node.js environment.
|
|
127
128
|
*
|
|
128
129
|
* @example
|
|
129
130
|
* ```typescript
|
|
@@ -159,6 +160,11 @@ declare class ZKClaw {
|
|
|
159
160
|
* Get the wallet address (async version)
|
|
160
161
|
*/
|
|
161
162
|
getAddressAsync(): Promise<string>;
|
|
163
|
+
/**
|
|
164
|
+
* Generate a ZK proof locally
|
|
165
|
+
* Proof generation happens in your Node.js environment using WASM
|
|
166
|
+
*/
|
|
167
|
+
private generateProofLocally;
|
|
162
168
|
/**
|
|
163
169
|
* Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
|
|
164
170
|
*
|
|
@@ -200,7 +206,7 @@ declare class ZKClaw {
|
|
|
200
206
|
/**
|
|
201
207
|
* Generate a ZK proof without posting
|
|
202
208
|
*
|
|
203
|
-
* Useful for caching proofs
|
|
209
|
+
* Useful for testing or caching proofs
|
|
204
210
|
*
|
|
205
211
|
* @returns Proof data that can be reused
|
|
206
212
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -73,7 +83,7 @@ var ZKClaw = class {
|
|
|
73
83
|
throw new Error("Invalid configuration");
|
|
74
84
|
}
|
|
75
85
|
this.apiUrl = config.apiUrl || "https://zkclaw.com";
|
|
76
|
-
const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL ||
|
|
86
|
+
const rpcUrl = config.rpcUrl || process.env.BASE_RPC_URL || "https://mainnet.base.org";
|
|
77
87
|
this.publicClient = (0, import_viem.createPublicClient)({
|
|
78
88
|
chain: import_chains.base,
|
|
79
89
|
transport: (0, import_viem.http)(rpcUrl)
|
|
@@ -153,6 +163,42 @@ var ZKClaw = class {
|
|
|
153
163
|
async getAddressAsync() {
|
|
154
164
|
return this.signer.getAddress();
|
|
155
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Generate a ZK proof locally
|
|
168
|
+
* Proof generation happens in your Node.js environment using WASM
|
|
169
|
+
*/
|
|
170
|
+
async generateProofLocally() {
|
|
171
|
+
const { AnonBalanceVerifier, BALANCE_THRESHOLDS } = await import("@anon/credentials");
|
|
172
|
+
const address = await this.signer.getAddress();
|
|
173
|
+
const message = `Verify $ZKCLAW balance for anonymous posting
|
|
174
|
+
|
|
175
|
+
Timestamp: ${Date.now()}`;
|
|
176
|
+
const signature = await this.signer.signMessage(message);
|
|
177
|
+
const messageHash = (0, import_viem.hashMessage)(message);
|
|
178
|
+
const verifier = new AnonBalanceVerifier();
|
|
179
|
+
const { input } = await verifier.buildInput(address, BALANCE_THRESHOLDS.POST);
|
|
180
|
+
const actualBalance = input.storageProof[0].value;
|
|
181
|
+
if (actualBalance < BALANCE_THRESHOLDS.POST) {
|
|
182
|
+
const config = await this.fetchConfig();
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Insufficient $ZKCLAW balance. Required: ${config.thresholds.post.toLocaleString()}, got: ${Number((0, import_viem.formatUnits)(actualBalance, 18)).toLocaleString()}. Buy at: ${config.links.buy}`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
const threshold = actualBalance >= BALANCE_THRESHOLDS.PROMOTE ? BALANCE_THRESHOLDS.PROMOTE : BALANCE_THRESHOLDS.POST;
|
|
188
|
+
const { input: finalInput } = await verifier.buildInput(address, threshold);
|
|
189
|
+
const proofResult = await verifier.generateProof({
|
|
190
|
+
...finalInput,
|
|
191
|
+
signature,
|
|
192
|
+
messageHash
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
proof: {
|
|
196
|
+
proof: Array.from(proofResult.proof),
|
|
197
|
+
publicInputs: proofResult.publicInputs
|
|
198
|
+
},
|
|
199
|
+
balance: actualBalance
|
|
200
|
+
};
|
|
201
|
+
}
|
|
156
202
|
/**
|
|
157
203
|
* Post anonymously to Farcaster (and Twitter if you have 2M+ tokens)
|
|
158
204
|
*
|
|
@@ -190,38 +236,45 @@ var ZKClaw = class {
|
|
|
190
236
|
error: `Text exceeds ${config.limits.maxPostLength} character limit (got ${text.length})`
|
|
191
237
|
};
|
|
192
238
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
239
|
+
try {
|
|
240
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
241
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
242
|
+
const canPromote = balance >= promoteThreshold;
|
|
243
|
+
const body = {
|
|
244
|
+
proof,
|
|
245
|
+
text: text.trim(),
|
|
246
|
+
platforms: {
|
|
247
|
+
farcaster: true,
|
|
248
|
+
twitter: canPromote
|
|
249
|
+
// Auto-enable Twitter if user has enough balance
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
if (options?.images && options.images.length > 0) {
|
|
253
|
+
body.images = options.images;
|
|
254
|
+
}
|
|
255
|
+
if (options?.embeds && options.embeds.length > 0) {
|
|
256
|
+
body.embeds = options.embeds;
|
|
257
|
+
}
|
|
258
|
+
const response = await fetch(`${this.apiUrl}/api/post`, {
|
|
259
|
+
method: "POST",
|
|
260
|
+
headers: { "Content-Type": "application/json" },
|
|
261
|
+
body: JSON.stringify(body)
|
|
262
|
+
});
|
|
263
|
+
if (!response.ok) {
|
|
264
|
+
const errorData = await response.json().catch(() => ({}));
|
|
265
|
+
return {
|
|
266
|
+
success: false,
|
|
267
|
+
error: errorData.error || `HTTP error: ${response.status}`
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const result = await response.json();
|
|
271
|
+
return result;
|
|
272
|
+
} catch (err) {
|
|
218
273
|
return {
|
|
219
274
|
success: false,
|
|
220
|
-
error:
|
|
275
|
+
error: err instanceof Error ? err.message : "Failed to generate proof or post"
|
|
221
276
|
};
|
|
222
277
|
}
|
|
223
|
-
const result = await response.json();
|
|
224
|
-
return result;
|
|
225
278
|
}
|
|
226
279
|
/**
|
|
227
280
|
* Check your $ZKCLAW token balance
|
|
@@ -265,31 +318,27 @@ Timestamp: ${timestamp}`;
|
|
|
265
318
|
/**
|
|
266
319
|
* Generate a ZK proof without posting
|
|
267
320
|
*
|
|
268
|
-
* Useful for caching proofs
|
|
321
|
+
* Useful for testing or caching proofs
|
|
269
322
|
*
|
|
270
323
|
* @returns Proof data that can be reused
|
|
271
324
|
*/
|
|
272
325
|
async generateProof() {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
})
|
|
284
|
-
if (!response.ok) {
|
|
285
|
-
const errorData = await response.json().catch(() => ({}));
|
|
326
|
+
try {
|
|
327
|
+
const { proof, balance } = await this.generateProofLocally();
|
|
328
|
+
const config = await this.fetchConfig();
|
|
329
|
+
const promoteThreshold = BigInt(config.thresholds.promote) * BigInt(10 ** config.token.decimals);
|
|
330
|
+
return {
|
|
331
|
+
success: true,
|
|
332
|
+
proof,
|
|
333
|
+
balance: balance.toString(),
|
|
334
|
+
tier: balance >= promoteThreshold ? "promote" : "post"
|
|
335
|
+
};
|
|
336
|
+
} catch (err) {
|
|
286
337
|
return {
|
|
287
338
|
success: false,
|
|
288
|
-
error:
|
|
339
|
+
error: err instanceof Error ? err.message : "Failed to generate proof"
|
|
289
340
|
};
|
|
290
341
|
}
|
|
291
|
-
const result = await response.json();
|
|
292
|
-
return result;
|
|
293
342
|
}
|
|
294
343
|
/**
|
|
295
344
|
* Get the Uniswap link to buy $ZKCLAW tokens
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zkclaw/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "SDK for AI agents to post anonymously on Farcaster and X using ZK proofs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"url": "https://zkclaw.com"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
+
"@anon/credentials": "workspace:*",
|
|
48
49
|
"viem": "^2.21.55"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|