forked from LIT-Protocol/policy-AI-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
humanVerificationAction.ts
81 lines (66 loc) · 2.69 KB
/
humanVerificationAction.ts
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
// @ts-nocheck
const _litActionCode = async () => {
try {
const result = await Lit.Actions.runOnce({waitForResponse: true}, async () => {
try {
console.log("🔒 Storing transaction for verification...");
const storeResponse = await fetch(`${baseUrl}/api/database/create-transaction`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount: parseFloat(amount),
status: 'PENDING',
pkpAddress: ethers.utils.computeAddress(`0x${publicKey}`)
})
});
if (!storeResponse.ok) {
const errorText = await storeResponse.text();
console.error("Store transaction response:", errorText);
throw new Error(`HTTP error! status: ${storeResponse.status}`);
}
const storeResult = await storeResponse.json();
console.log("Store transaction response:", storeResult);
if (!storeResult.success) {
throw new Error(storeResult.error || 'Failed to store transaction');
}
console.log("✅ Transaction stored:", storeResult.transaction);
console.log("📧 Sending verification email...");
const loginResponse = await fetch(`${baseUrl}/api/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
txHash: storeResult.transaction.txHash
})
});
if (!loginResponse.ok) {
const errorText = await loginResponse.text();
console.error("Login response:", errorText);
throw new Error(`HTTP error! status: ${loginResponse.status}`);
}
const loginResult = await loginResponse.json();
console.log("Login response:", loginResult);
if (!loginResult.success) {
throw new Error(loginResult.error || 'Failed to send verification email');
}
console.log("📧 Verification email sent successfully");
return "true";
} catch (error) {
console.error("Error in runOnce:", error);
return `Error: ${error.message}`;
}
});
console.log("Final result:", result);
Lit.Actions.setResponse({ response: result });
} catch (error) {
console.error("❌ Error in Lit Action:", error);
Lit.Actions.setResponse({
response: `Error: ${error.message}`
});
}
};
export const litActionCode = `(${_litActionCode.toString()})();`;