forked from LIT-Protocol/policy-AI-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
117 lines (100 loc) · 3.58 KB
/
utils.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
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
import { LIT_CHAINS, LIT_RPC, LIT_ABILITY } from "@lit-protocol/constants";
import { LitPKPResource, LitActionResource } from "@lit-protocol/auth-helpers";
import { LitNodeClient } from "@lit-protocol/lit-node-client"
import { EthWalletProvider} from "@lit-protocol/lit-auth-client";
import * as ethers from 'ethers';
const ETHEREUM_PRIVATE_KEY = process.env.NEXT_PUBLIC_ETHEREUM_PRIVATE_KEY;
const LIT_PKP_PUBLIC_KEY = process.env.NEXT_PUBLIC_LIT_PKP_PUBLIC_KEY;
export async function getYellowstoneChainMetrics() {
try {
const provider = new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE);
const gasPrice = await provider.getGasPrice();
const gasPriceGwei = parseFloat(ethers.utils.formatUnits(gasPrice, "gwei"));
const block = await provider.getBlock("latest");
const transactionCount = block.transactions.length;
let networkLoad = "Low";
if (transactionCount > 100) networkLoad = "High";
else if (transactionCount > 50) networkLoad = "Medium";
return {
gasPrice: gasPriceGwei,
networkLoad,
transactionCount
};
} catch (error) {
console.error('Failed to fetch chain metrics:', error);
return {
gasPrice: 0,
networkLoad: "Unknown",
transactionCount: 0
};
}
}
export const getChainInfo = (
chain: string
): { rpcUrl: string; chainId: number } => {
if (LIT_CHAINS[chain] === undefined)
throw new Error(`Chain: ${chain} is not supported by Lit`);
return {
rpcUrl: LIT_CHAINS[chain].rpcUrls[0],
chainId: LIT_CHAINS[chain].chainId,
};
};
export async function getPkpSessionSigs(litNodeClient: LitNodeClient) {
const ethersWallet = new ethers.Wallet(
ETHEREUM_PRIVATE_KEY!,
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
);
const authMethod = await EthWalletProvider.authenticate({signer: ethersWallet, litNodeClient});
let pkpInfo = {
publicKey: LIT_PKP_PUBLIC_KEY!,
ethAddress: ethers.utils.computeAddress(`0x${LIT_PKP_PUBLIC_KEY}`),
};
const sessionSigs = await litNodeClient.getPkpSessionSigs({
pkpPublicKey: pkpInfo.publicKey,
authMethods: [authMethod],
resourceAbilityRequests: [
{
resource: new LitPKPResource("*"),
ability: LIT_ABILITY.PKPSigning,
},
{
resource: new LitActionResource("*"),
ability: LIT_ABILITY.LitActionExecution,
},
],
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(),
});
console.log("✅ Got PKP Session Sigs");
return sessionSigs;
}
export async function authenticateToken(token: string, txHash: string) {
try {
const response = await fetch('/api/auth/authenticate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ token })
});
const result = await response.json();
if (!result.success) {
throw new Error(result.error);
}
await fetch('/api/database/update-transaction', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
status: 'AUTHENTICATED',
approved: false,
txHash: txHash
})
});
console.log("✅ Authentication successful");
return result.session;
} catch (error) {
console.error("Authentication failed:", error);
return null;
}
}