-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
148 lines (127 loc) Β· 4.5 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
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
import * as ethers from "ethers";
import { LitContracts } from "@lit-protocol/contracts-sdk";
import { LitNetwork } from "@lit-protocol/constants";
import { LitNodeClient } from "@lit-protocol/lit-node-client";
// @ts-ignore
import IpfsHash from "ipfs-only-hash";
import bs58 from "bs58";
import type { GitHubUser } from "./types";
import { litActionCode } from "./litAction";
export const getEthersSigner = async () => {
console.log("π Connecting to Ethereum account...");
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const ethersSigner = provider.getSigner();
console.log(
"β
Connected Ethereum account:",
await ethersSigner.getAddress()
);
return ethersSigner;
};
let litNodeClient: LitNodeClient | null = null;
export const getLitNodeClient = async (litNetwork: LitNetwork) => {
if (litNodeClient === null) {
console.log(`π Connecting LitNode client to the ${litNetwork} network...`);
litNodeClient = new LitNodeClient({
litNetwork: litNetwork,
debug: Boolean(import.meta.env.VITE_LIT_DEBUG),
});
await litNodeClient.connect();
console.log(`β
Connected LitNode client to the ${litNetwork} network`);
}
return litNodeClient;
};
let litContractClient: LitContracts | null = null;
export const getLitContractsClient = async (
ethersSigner: ethers.providers.JsonRpcSigner,
litNetwork: LitNetwork
) => {
if (litContractClient === null) {
console.log("π Connecting LitContracts client to the network...");
litContractClient = new LitContracts({
signer: ethersSigner,
network: litNetwork,
});
await litContractClient.connect();
console.log("β
Connected LitContracts client to the network");
}
return litContractClient;
};
export const getGithubAuthMethodInfo = (githubUser: GitHubUser) => {
console.log("π Generating Auth Method type and ID...");
const authMethodInfo = {
authMethodType: ethers.utils.keccak256(
ethers.utils.toUtf8Bytes("Lit Developer Guide GitHub Auth Example")
),
authMethodId: ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`github:${githubUser.id}`)
),
};
console.log("β
Generated Auth Method type and ID");
return authMethodInfo;
};
export const getPkpMintCost = async (litContracts: LitContracts) => {
console.log("π Getting PKP mint cost...");
const pkpMintCost = await litContracts.pkpNftContract.read.mintCost();
console.log("β
Got PKP mint cost");
return pkpMintCost;
};
export const getLitActionCodeIpfsCid = async () => {
console.log("π Calculating the IPFS CID for Lit Action code string...");
const litActionIpfsCid = await IpfsHash.of(litActionCode);
console.log(
`β
Calculated IPFS CID: ${litActionIpfsCid}. Hexlified version: 0x${Buffer.from(
bs58.decode(litActionIpfsCid)
).toString("hex")}`
);
return litActionIpfsCid;
};
export const getPkpInfoFromMintReceipt = async (
txReceipt: ethers.ContractReceipt,
litContractsClient: LitContracts
) => {
const pkpMintedEvent = txReceipt!.events!.find(
(event) =>
event.topics[0] ===
"0x3b2cc0657d0387a736293d66389f78e4c8025e413c7a1ee67b7707d4418c46b8"
);
const publicKey = "0x" + pkpMintedEvent!.data.slice(130, 260);
const tokenId = ethers.utils.keccak256(publicKey);
const ethAddress = await litContractsClient.pkpNftContract.read.getEthAddress(
tokenId
);
console.log(`βΉοΈ PKP Public Key: ${publicKey}`);
console.log(`βΉοΈ PKP Token ID: ${tokenId}`);
console.log(`βΉοΈ PKP ETH Address: ${ethAddress}`);
return {
tokenId: ethers.BigNumber.from(tokenId).toString(),
publicKey,
ethAddress,
};
};
export const getCapacityCredit = async (
ethersSigner: ethers.providers.JsonRpcSigner,
litNetwork: LitNetwork
) => {
try {
const litContracts = await getLitContractsClient(ethersSigner, litNetwork);
let capacityTokenId = import.meta.env.VITE_LIT_CAPACITY_CREDIT_TOKEN_ID;
if (capacityTokenId === undefined) {
console.log("π Minting Capacity Credits NFT...");
capacityTokenId = (
await litContracts.mintCapacityCreditsNFT({
requestsPerKilosecond: 10,
daysUntilUTCMidnightExpiration: 1,
})
).capacityTokenIdStr;
console.log(`β
Minted new Capacity Credit with ID: ${capacityTokenId}`);
} else {
console.log(
`βΉοΈ Using provided Capacity Credit with ID: ${capacityTokenId}`
);
}
return capacityTokenId;
} catch (error) {
console.error(error);
}
};