-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess.service.ts
175 lines (156 loc) · 4.46 KB
/
access.service.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
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
import { Wallet } from "ethers";
import {
LitAccessControlConditionResource,
createSiweMessageWithRecaps,
generateAuthSig,
LitAbility,
AuthSig,
LitResourceAbilityRequest,
} from "@lit-protocol/auth-helpers";
import * as LitJsSdk from "@lit-protocol/lit-node-client";
const accessControlConditions = [
{
contractAddress: "",
standardContractType: "",
chain: "ethereum",
method: "eth_getBalance",
parameters: [":userAddress", "latest"],
returnValueTest: {
comparator: ">=",
value: "1000000000000", // 0.000001 ETH
},
},
];
const chain = "ethereum";
export class Lit {
litNodeClient;
chain;
constructor() {
this.chain = chain;
this.litNodeClient = new LitJsSdk.LitNodeClientNodeJs({
alertWhenUnauthorized: false,
litNetwork: "datil-dev",
debug: true,
});
}
async connect() {
return await this.litNodeClient.connect();
}
async disconnect() {
return await this.litNodeClient.disconnect();
}
async encrypt(message: string) {
await this.connect();
// Encrypt the message
const { ciphertext, dataToEncryptHash } = await LitJsSdk.encryptString(
{
accessControlConditions,
dataToEncrypt: message,
},
this.litNodeClient
);
await this.disconnect();
// Return the ciphertext and dataToEncryptHash
return {
ciphertext,
dataToEncryptHash,
};
}
async decrypt(
ciphertext: string,
dataToEncryptHash: string
): Promise<string> {
// Get the session signatures
await this.connect();
const sessionSigs = await this.getSessionSignatures();
// Decrypt the message
const decryptedString = await LitJsSdk.decryptToString(
{
accessControlConditions,
chain: this.chain,
ciphertext,
dataToEncryptHash,
sessionSigs,
},
this.litNodeClient
);
await this.disconnect();
// Return the decrypted string
return decryptedString;
}
async getDelegationAuthSig() {
if (!process.env.ETHEREUM_PRIVATE_KEY) {
throw new Error("ETHEREUM_PRIVATE_KEY is required");
}
try {
const wallet = new Wallet(process.env.ETHEREUM_PRIVATE_KEY);
const { capacityDelegationAuthSig } =
await this.litNodeClient.createCapacityDelegationAuthSig({
dAppOwnerWallet: wallet,
uses: "1",
capacityTokenId: process.env.LIT_TOKEN_ID,
});
return capacityDelegationAuthSig;
} catch (error) {
console.error("Error connecting to LitContracts:", error);
throw error;
}
}
async getSessionSignatures() {
if (!process.env.ETHEREUM_PRIVATE_KEY) {
throw new Error("ETHEREUM_PRIVATE_KEY is required");
}
// Connect to the wallet
const ethWallet = new Wallet(process.env.ETHEREUM_PRIVATE_KEY);
// Get the latest blockhash
const latestBlockhash = await this.litNodeClient.getLatestBlockhash();
// Define the authNeededCallback function
const authNeededCallback = async (params: {
uri?: string;
expiration?: string;
resourceAbilityRequests?: LitResourceAbilityRequest[];
}): Promise<AuthSig> => {
if (!params.uri) {
throw new Error("uri is required");
}
if (!params.expiration) {
throw new Error("expiration is required");
}
if (!params.resourceAbilityRequests) {
throw new Error("resourceAbilityRequests is required");
}
// Create the SIWE message
const toSign = await createSiweMessageWithRecaps({
uri: params.uri,
expiration: params.expiration,
resources: params.resourceAbilityRequests,
walletAddress: ethWallet.address,
nonce: latestBlockhash,
litNodeClient: this.litNodeClient,
});
// Generate the authSig
const authSig = await generateAuthSig({
signer: ethWallet,
toSign,
});
return authSig;
};
// Define the Lit resource
const litResource = new LitAccessControlConditionResource("*");
// Get the delegation auth sig
const capacityDelegationAuthSig = await this.getDelegationAuthSig();
// Get the session signatures
const sessionSigs = await this.litNodeClient.getSessionSigs({
chain: this.chain,
resourceAbilityRequests: [
{
resource: litResource,
ability: LitAbility.AccessControlConditionDecryption,
},
],
authNeededCallback,
capacityDelegationAuthSig,
});
return sessionSigs;
}
}