Skip to content

Commit 373a88b

Browse files
committed
fix: nx passing all node tests
1 parent a86a3ce commit 373a88b

File tree

12 files changed

+1976
-8049
lines changed

12 files changed

+1976
-8049
lines changed

conditional-signing/nodejs/package-lock.json

Lines changed: 0 additions & 4772 deletions
This file was deleted.

conditional-signing/nodejs/yarn.lock

Lines changed: 725 additions & 939 deletions
Large diffs are not rendered by default.

decrypt-api-key-in-action/nodejs/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"$schema": "https://json.schemastore.org/mocharc.json",
3-
"require": "tsx, dotenv/config"
3+
"require": "tsx"
44
}
Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
# `decryptAndCombine` Lit Action Code Example
32

43
This code demonstrates how to use the Lit and Lit Actions SDKs to implement the `decryptAndCombine` method in a Lit Action. This method collects and combines the decryption shares from each of the Lit nodes onto a single Lit node, where the given content is decrypted. In this example, that means the API key remains in [Lit's Trusted Execution Environment (TEE)](https://developer.litprotocol.com/resources/how-it-works#sealed-and-confidential-hardware) and is not exposed to the client.
@@ -47,57 +46,3 @@ Within the `.env` file there are the following ENVs:
4746
### Running the Test
4847

4948
After the `.env` is configured, there is a NPM script in the `package.json` to run the test in the `test/decryptApiKeyInActionTest.spec.ts` file. To run the test, use the `yarn test` command.
50-
=======
51-
# Decrypt and Use an API Key Within a Lit Action
52-
53-
This example shows how you can encrypt an api key on the client with specific decrypt conditions, and then decrypt that api key from within a Lit Action to perform some remote api call with the key.
54-
55-
Before running, there are two variables to configure within `index.ts`:
56-
57-
**Note: ts-node-esm requires NodeJS version 20**
58-
59-
```js
60-
const url = `<your http endpoint for api-key usage>`;
61-
const key = "<your api key>";
62-
```
63-
64-
## Restricting your api key to only be used by a single Lit Action
65-
66-
Within the code you will see the below ACC condition:
67-
68-
```js
69-
const accessControlConditions = [
70-
{
71-
contractAddress: "",
72-
standardContractType: "",
73-
chain,
74-
method: "eth_getBalance",
75-
parameters: [":userAddress", "latest"],
76-
returnValueTest: {
77-
comparator: ">=",
78-
value: "0",
79-
},
80-
},
81-
];
82-
```
83-
84-
You can change the above to the below to use the parameter: `:currentActionId` which will only allow a speific `IPFS ID` to decrypt the key which is explicitly asserted on in the condition `parameters`
85-
86-
```js
87-
const accessControlConditions = [
88-
{
89-
contractAddress: "",
90-
standardContractType: "",
91-
chain,
92-
method: "eth_getBalance",
93-
parameters: [":currentActionId", "latest"],
94-
returnValueTest: {
95-
comparator: "==",
96-
value: "<your ipfs id>",
97-
},
98-
},
99-
];
100-
```
101-
102-
For easy upload of your Lit Action source code you can use the explorer [here](https://explorer.litprotocol.com/create-action)
103-
>>>>>>> 314a19b (refactor: changed dir structure for decrypt-api-key-in-action as per templates)
Lines changed: 0 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
import { LitNodeClient, encryptString } from "@lit-protocol/lit-node-client";
32
import { LitNetwork, LIT_RPC } from "@lit-protocol/constants";
43
import {
@@ -166,180 +165,3 @@ export const decryptApiKey = async (alchemyUrl: string, key: string) => {
166165
litNodeClient!.disconnect();
167166
}
168167
};
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

Comments
 (0)