|
1 | | -<<<<<<< HEAD |
2 | 1 | import { LitNodeClient, encryptString } from "@lit-protocol/lit-node-client"; |
3 | 2 | import { LitNetwork, LIT_RPC } from "@lit-protocol/constants"; |
4 | 3 | import { |
@@ -166,180 +165,3 @@ export const decryptApiKey = async (alchemyUrl: string, key: string) => { |
166 | 165 | litNodeClient!.disconnect(); |
167 | 166 | } |
168 | 167 | }; |
169 | | -======= |
170 | | -//@ts-nocheck |
171 | | -import { LitNodeClient, encryptString } from "@lit-protocol/lit-node-client"; |
172 | | -import { AuthCallbackParams } from "@lit-protocol/types"; |
173 | | -import { LIT_RPC } from "@lit-protocol/constants"; |
174 | | -import { LitAbility, LitAccessControlConditionResource, LitActionResource, createSiweMessageWithRecaps, generateAuthSig } from "@lit-protocol/auth-helpers"; |
175 | | -import {ethers} from 'ethers'; |
176 | | - |
177 | | -const url = `<your http endpoint for api-key usage>`; |
178 | | -const key = '<your api key>'; |
179 | | - |
180 | | -const genActionSource = (url: string) => { |
181 | | - return `(async () => { |
182 | | - const apiKey = await Lit.Actions.decryptAndCombine({ |
183 | | - accessControlConditions, |
184 | | - ciphertext, |
185 | | - dataToEncryptHash, |
186 | | - authSig: null, |
187 | | - chain: 'ethereum', |
188 | | - }); |
189 | | - // Note: uncomment this functionality to use your api key that is for the provided url |
190 | | - /* |
191 | | - const resp = await fetch("${url}", { |
192 | | - 'Authorization': "Bearer " + apiKey |
193 | | - }); |
194 | | - let data = await resp.json(); |
195 | | - */ |
196 | | - Lit.Actions.setResponse({ response: apiKey }); |
197 | | - })();`; |
198 | | -} |
199 | | - |
200 | | -const ONE_WEEK_FROM_NOW = new Date( |
201 | | - Date.now() + 1000 * 60 * 60 * 24 * 7 |
202 | | -).toISOString(); |
203 | | - |
204 | | -const genProvider = () => { |
205 | | - return new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE); |
206 | | -} |
207 | | - |
208 | | -const genWallet = () => { |
209 | | -// known private key for testing |
210 | | -// replace with your own key |
211 | | -return new ethers.Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', genProvider()); |
212 | | -} |
213 | | - |
214 | | -const genAuthSig = async ( |
215 | | - wallet: ethers.Wallet, |
216 | | - client: LitNodeClient, |
217 | | - uri: string, |
218 | | - resources: LitResourceAbilityRequest[] |
219 | | -) => { |
220 | | - |
221 | | - let blockHash = await client.getLatestBlockhash(); |
222 | | - const message = await createSiweMessageWithRecaps({ |
223 | | - walletAddress: wallet.address, |
224 | | - nonce: blockHash, |
225 | | - litNodeClient: client, |
226 | | - resources, |
227 | | - expiration: ONE_WEEK_FROM_NOW, |
228 | | - uri |
229 | | - }) |
230 | | - const authSig = await generateAuthSig({ |
231 | | - signer: wallet, |
232 | | - toSign: message, |
233 | | - address: wallet.address |
234 | | - }); |
235 | | - |
236 | | - |
237 | | - return authSig; |
238 | | -} |
239 | | - |
240 | | -const genSession = async ( |
241 | | - wallet: ethers.Wallet, |
242 | | - client: LitNodeClient, |
243 | | - resources: LitResourceAbilityRequest[]) => { |
244 | | - let sessionSigs = await client.getSessionSigs({ |
245 | | - chain: "ethereum", |
246 | | - resourceAbilityRequests: resources, |
247 | | - authNeededCallback: async (params: AuthCallbackParams) => { |
248 | | - console.log("resourceAbilityRequests:", params.resources); |
249 | | - |
250 | | - if (!params.expiration) { |
251 | | - throw new Error("expiration is required"); |
252 | | - } |
253 | | - |
254 | | - if (!params.resources) { |
255 | | - throw new Error("resourceAbilityRequests is required"); |
256 | | - } |
257 | | - |
258 | | - if (!params.uri) { |
259 | | - throw new Error("uri is required"); |
260 | | - } |
261 | | - |
262 | | - // generate the authSig for the inner signature of the session |
263 | | - // we need capabilities to assure that only one api key may be decrypted |
264 | | - const authSig = genAuthSig(wallet, client, params.uri, params.resourceAbilityRequests ?? []); |
265 | | - return authSig; |
266 | | - } |
267 | | - }); |
268 | | - |
269 | | - return sessionSigs; |
270 | | -} |
271 | | - |
272 | | -const main = async () => { |
273 | | - let client = new LitNodeClient({ |
274 | | - litNetwork: LitNetwork.DatilDev, |
275 | | - debug: true |
276 | | - }); |
277 | | - |
278 | | - const wallet = genWallet(); |
279 | | - const chain = 'ethereum'; |
280 | | - // lit action will allow anyone to decrypt this api key with a valid authSig |
281 | | - const accessControlConditions = [ |
282 | | - { |
283 | | - contractAddress: '', |
284 | | - standardContractType: '', |
285 | | - chain, |
286 | | - method: 'eth_getBalance', |
287 | | - parameters: [':userAddress', 'latest'], |
288 | | - returnValueTest: { |
289 | | - comparator: '>=', |
290 | | - value: '0', |
291 | | - }, |
292 | | - }, |
293 | | - ]; |
294 | | - |
295 | | - |
296 | | - await client.connect(); |
297 | | - /* |
298 | | - Here we are encypting our key for secure use within an action |
299 | | - this code should be run once and the ciphertext and dataToEncryptHash stored for later sending |
300 | | - to the Lit Action in 'jsParams' |
301 | | - */ |
302 | | - const { ciphertext, dataToEncryptHash } = await encryptString( |
303 | | - { |
304 | | - accessControlConditions, |
305 | | - dataToEncrypt: key, |
306 | | - }, |
307 | | - client |
308 | | - ); |
309 | | - |
310 | | - console.log("cipher text:", ciphertext, "hash:", dataToEncryptHash); |
311 | | - const accsResourceString = |
312 | | - await LitAccessControlConditionResource.generateResourceString(accessControlConditions as any, dataToEncryptHash); |
313 | | - const sessionForDecryption = await genSession(wallet, client, [ |
314 | | - { |
315 | | - resource: new LitActionResource('*'), |
316 | | - ability: LitAbility.LitActionExecution, |
317 | | - }, |
318 | | - { |
319 | | - resource: new LitAccessControlConditionResource(accsResourceString), |
320 | | - ability: LitAbility.AccessControlConditionDecryption, |
321 | | - |
322 | | - } |
323 | | - ] |
324 | | - ); |
325 | | - console.log("action source code: ", genActionSource(url)) |
326 | | - /* |
327 | | - Here we use the encrypted key by sending the |
328 | | - ciphertext and dataTiEncryptHash to the action |
329 | | - */ |
330 | | - const res = await client.executeJs({ |
331 | | - sessionSigs: sessionForDecryption, |
332 | | - code: genActionSource(url), |
333 | | - jsParams: { |
334 | | - accessControlConditions, |
335 | | - ciphertext, |
336 | | - dataToEncryptHash |
337 | | - } |
338 | | - }); |
339 | | - |
340 | | - console.log("result from action execution:", res); |
341 | | - client.disconnect(); |
342 | | -} |
343 | | - |
344 | | -await main(); |
345 | | ->>>>>>> 314a19b (refactor: changed dir structure for decrypt-api-key-in-action as per templates) |
0 commit comments