-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
425 lines (381 loc) · 12.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { http } from "@google-cloud/functions-framework";
import pg from "pg";
import { toGcpAccount } from "@nanidao/gcp-account";
import { mainnet, base, arbitrum } from "viem/chains";
import { createPublicClient, http as viemHttp, concat, pad, toHex } from "viem";
import { getKeyFromNonce, getValidatorKey } from "eip-7582-utils";
// Environment configuration
const types = {
ValidateUserOp: [
{ name: "sender", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "initCode", type: "bytes" },
{ name: "callData", type: "bytes" },
{ name: "accountGasLimits", type: "bytes32" },
{ name: "preVerificationGas", type: "uint256" },
{ name: "gasFees", type: "bytes32" },
{ name: "paymasterAndData", type: "bytes" },
{ name: "validUntil", type: "uint48" },
{ name: "validAfter", type: "uint48" },
],
};
const credentials = process.env.CREDENTIALS.trim();
const CONNECTION_STRING = process.env.DATABASE_URL;
if (!credentials) throw new Error("Missing KMS credentials");
if (!CONNECTION_STRING) throw new Error("Missing DB credentials");
// Database initialization
const pool = new pg.Pool({
connectionString: CONNECTION_STRING,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 10000,
});
// Basic health check endpoint
http("vote", async (req, res) => {
try {
const proposals = await getRecentProposals();
console.log(`Found ${proposals.length} proposals to process`);
const account = await toGcpAccount({ credentials });
console.log("ACCOUNT:", account.address);
const results = [];
// Process each proposal
for (const proposal of proposals) {
try {
console.log(`Processing proposal with hash: ${proposal.userOpHash}`);
const { vote, reason } = await getNaniVote(proposal);
console.log(`Vote for ${proposal.userOpHash}:`, vote);
if (Boolean(vote) === true) {
const signature = await signProposal(account, proposal);
console.log(`Signature for ${proposal.userOpHash}:`, signature);
await addSignature({
signer: account.address,
account: proposal.sender,
hash: proposal.userOpHash,
signature: signature,
reason: reason,
});
results.push({
hash: proposal.userOpHash,
status: "success",
signature,
vote,
reason,
});
} else {
await addSignature({
signer: account.address,
account: proposal.sender,
hash: proposal.userOpHash,
signature: "",
reason: reason,
});
results.push({
hash: proposal.userOpHash,
status: "rejected",
vote,
reason,
});
}
} catch (error) {
console.error(
`Error processing proposal ${proposal.userOpHash}:`,
error,
);
results.push({
hash: proposal.userOpHash,
status: "error",
error: error.message,
});
}
}
res.status(200).json({
message: `Processed ${proposals.length} proposals`,
results,
});
} catch (error) {
console.error("Error in main function:", error);
res.status(500).json({
error: "Internal Server Error",
details: error.message,
});
}
});
const TARGET_SENDER = "0x0000000000001d8a2e7bf6bc369525a2654aa298";
const AI_SIGNER = "0x466d3E0E6D661d6E7626e9dea93c460BD4e15B40";
const getRecentProposals = async (retries = 3) => {
for (let i = 0; i < retries; i++) {
try {
const query = `
SELECT *
FROM proposals
WHERE created_at >= NOW() - INTERVAL '24 hour'
AND sender = $1
AND "userOpHash" NOT IN (
SELECT hash
FROM signatures
WHERE signer = $2
)
ORDER BY created_at ASC
`;
const values = [TARGET_SENDER.toLowerCase(), AI_SIGNER.toLowerCase()];
const result = await pool.query(query, values);
console.log(
`Found ${result.rows.length} proposals for sender ${TARGET_SENDER}`,
);
return result.rows;
} catch (error) {
console.error(`Attempt ${i + 1} failed:`, error);
if (i === retries - 1) throw error;
await new Promise((resolve) => setTimeout(resolve, 1000 * (i + 1))); // Exponential backoff
}
}
};
const addSignature = async (sig) => {
const query = `
INSERT INTO signatures (signer, account, hash, signature, content)
VALUES ($1, $2, $3, $4, $5)
RETURNING *
`;
const values = [
sig.signer.toLowerCase(),
sig.account ? sig.account.toLowerCase() : null,
sig.hash.toLowerCase(),
sig.signature,
sig.reason,
];
try {
const result = await pool.query(query, values);
return result.rows[0];
} catch (error) {
console.error("Error in addSignature:", error);
throw error;
}
};
const getNaniVote = async (proposal) => {
const makeRequest = async (additionalInstruction = "") => {
const response = await fetch("http://nani.ooo/api/chat", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": process.env.NANI_AI_KEY,
},
body: JSON.stringify({
messages: [
{
role: "system",
content: `You are NANI, an AI member of the DAO. You must carefully evaluate whether the following proposal is in the best interest of the DAO. Analyze the proposal and determine if it should be approved. Your response must be in JSON format with 'vote' being true or false and include a 'reason' explaining your decision. Format: {"vote":true/false,"reason":"explanation"}. ${additionalInstruction} Here is the proposal to evaluate: ${proposal.content}`,
},
],
}),
});
const text = await response.text();
try {
console.log("RESPONSE", text);
return JSON.parse(text);
} catch (e) {
return null;
}
};
// First attempt
let result = await makeRequest();
if (result) return result;
// Second attempt with stronger instruction
result = await makeRequest(
"You must respond with ONLY valid JSON, no other text or explanation.",
);
if (result) return result;
// Final attempt with very explicit instruction
result = await makeRequest(
'CRITICAL: Respond with ONLY a JSON object in the exact format {"vote": boolean, "reason": "string"} - no other text whatsoever.',
);
if (result) return result;
throw new Error("Failed to get valid JSON response after 3 attempts");
};
const signProposal = async (account, proposal) => {
let userOperation = {
sender: proposal.sender,
nonce: BigInt(proposal.nonce),
callData: proposal.callData,
verificationGasLimit: BigInt(proposal.verificationGasLimit),
callGasLimit: BigInt(proposal.callGasLimit),
preVerificationGas: BigInt(proposal.preVerificationGas),
maxFeePerGas: BigInt(proposal.maxFeePerGas),
maxPriorityFeePerGas: BigInt(proposal.maxPriorityFeePerGas),
signature: "0x",
factory: proposal?.factory,
factoryData: proposal?.factoryData,
paymaster: proposal?.paymaster,
paymasterPostOpGasLimit: proposal?.paymasterPostOpGasLimit
? BigInt(proposal.paymasterPostOpGasLimit)
: undefined,
paymasterVerificationGasLimit: proposal?.paymasterVerificationGasLimit
? BigInt(proposal.paymasterVerificationGasLimit)
: undefined,
paymasterData: proposal?.paymasterData,
};
let domain = await getDomain(userOperation.sender, proposal.chain);
const gasFees = packGasLimits(userOperation);
const accountGasLimits = packAccountGasLimits(userOperation);
const paymasterAndData = packPaymasterAndData(userOperation);
const initCode = packInitCode(userOperation);
const message = {
sender: userOperation.sender,
nonce: userOperation.nonce,
initCode: initCode,
callData: userOperation.callData,
accountGasLimits,
preVerificationGas: userOperation.preVerificationGas,
gasFees,
paymasterAndData,
validUntil: BigInt(0),
validAfter: BigInt(0),
};
const key = getKeyFromNonce(userOperation.nonce);
if (
key !== "0x0000000000000000000000000000000000000000000000000000000000000000"
) {
if (key === getValidatorKey(getValidator("remote-validator").address)) {
domain = {
name: "RemoteValidator",
version: "1.0.0",
chainId: publicClient.chain.id,
verifyingContract: getValidator("remote-validator").address,
};
}
}
console.log("Signing user operation", message);
const signature = await account.signTypedData({
domain,
types,
primaryType: "ValidateUserOp",
message,
});
return signature;
};
const validatorApps = [
{
id: "remote-validator",
address: "0x0000000000159aAFCA7067005E28665a28B5B4cf",
title: "Scheduler",
icon: "/apps/remote-validator.png",
description:
"Set specific days and times for your transactions to be processed",
},
{
id: "recovery-validator",
address: "0x000000000000a78fb292191473e51dd34700c43d",
title: "Recovery",
icon: "/apps/recovery-validator.png",
description:
"Set backup roles to recover your account in case of an emergency",
},
{
id: "joint-validator",
address: "0x000000000000D3D2b2980A7BC509018E4d88e947",
title: "Joint Ownership",
icon: "/apps/joint-validator.webp",
description:
"Add joint owners to your smart account with full concurrent rights",
},
{
id: "payment-validator",
address: "0x00000000000032CD4FAE890F90e61e6864e44aa7",
title: "Payment Plans",
icon: "/apps/payment-validator.webp",
description: "Add payment plans and delegate token transfer permissions",
},
{
id: "permit-validator",
address: "0x000000000000ab6c9FF3ed50AC4BAF2a20890835",
title: "Custom Permissions",
icon: "/apps/permit-validator.webp",
description: "Add arbitrary permissions within the Permit structure",
},
];
const getValidator = (id) => {
const app = validatorApps.find((app) => app.id === id);
if (!app) {
throw new Error("App not found");
}
return app;
};
function packInitCode(unpackedUserOperation) {
return unpackedUserOperation.factory
? concat([
unpackedUserOperation.factory,
unpackedUserOperation.factoryData || "0x",
])
: "0x";
}
function packAccountGasLimits(unpackedUserOperation) {
return concat([
pad(toHex(unpackedUserOperation.verificationGasLimit), { size: 16 }),
pad(toHex(unpackedUserOperation.callGasLimit), { size: 16 }),
]);
}
function packGasLimits(unpackedUserOperation) {
return concat([
pad(toHex(unpackedUserOperation.maxPriorityFeePerGas), { size: 16 }),
pad(toHex(unpackedUserOperation.maxFeePerGas), { size: 16 }),
]);
}
function packPaymasterAndData(unpackedUserOperation) {
return unpackedUserOperation.paymaster
? concat([
unpackedUserOperation.paymaster,
pad(toHex(unpackedUserOperation.paymasterVerificationGasLimit || 0), {
size: 16,
}),
pad(toHex(unpackedUserOperation.paymasterPostOpGasLimit || 0), {
size: 16,
}),
unpackedUserOperation.paymasterData || "0x",
])
: "0x";
}
const eip712DomainABI = [
{
inputs: [],
name: "eip712Domain",
outputs: [
{ internalType: "bytes1", name: "fields", type: "bytes1" },
{ internalType: "string", name: "name", type: "string" },
{ internalType: "string", name: "version", type: "string" },
{ internalType: "uint256", name: "chainId", type: "uint256" },
{ internalType: "address", name: "verifyingContract", type: "address" },
{ internalType: "bytes32", name: "salt", type: "bytes32" },
{ internalType: "uint256[]", name: "extensions", type: "uint256[]" },
],
stateMutability: "view",
type: "function",
},
];
const getDomain = async (sender, chain) => {
const publicClient = createPublicClient({
chain: resolveChain(chain),
transport: viemHttp(),
});
const data = await publicClient.readContract({
address: sender,
abi: eip712DomainABI,
functionName: "eip712Domain",
});
return {
name: data[1],
version: data[2],
chainId: Number(data[3]),
verifyingContract: data[4],
};
};
const resolveChain = (chain) => {
switch (chain.toLowerCase()) {
case "eth":
return mainnet;
case "arbitrum":
return arbitrum;
case "base":
return base;
default:
throw new Error("Unsupported chain ", chain);
}
};