Skip to content

Commit

Permalink
Merge branch 'main' into wyatt/paying-for-lit
Browse files Browse the repository at this point in the history
  • Loading branch information
spacesailor24 authored Aug 15, 2024
2 parents 4f8c04c + 139b710 commit 5efc82b
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 200 deletions.
22 changes: 11 additions & 11 deletions docs/sdk/access-control/encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,19 @@ const accessControlConditions = [

To encrypt a string, use one of the following functions:

- [encryptString()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptString.html) - Used to encrypt the raw string.
- [encryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptString.html) - Used to encrypt the raw string.
- - [encryptString()](../../) - Used to encrypt the raw string.
- [zipAndEncryptString()](https://v5.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptString.html) - Compresses the string (using [JSZip](https://www.npmjs.com/package/jszip)) before encrypting it. This is useful for saving space, but takes additional time to perform the zip.
- [zipAndEncryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptString.html) - Compresses the string (using [JSZip](https://www.npmjs.com/package/jszip)) before encrypting it. This is useful for saving space, but takes additional time to perform the zip.

To encrypt a file, use:

- [encryptFile()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptFile.html) - Used to encrypt a file without doing any zipping or packing. Because zipping larger files takes time, this function is useful when encrypting large files ( > 20mb). This also requires that you store the file metadata.
- [encryptFileAndZipWithMetadata()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptFileAndZipWithMetadata.html) - Used to encrypt a file and then zip it up with the metadata (using [JSZip](https://www.npmjs.com/package/jszip)). This is useful for when you don't want to store the file metadata separately.
- [zipAndEncryptFiles()](https://v5.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptFiles.html) - Used to zip and encrypt multiple files. This does **not** include the file metadata in the zip, so you must store those yourself.
- [encryptFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFile.html) - Used to encrypt a file without doing any zipping or packing. Because zipping larger files takes time, this function is useful when encrypting large files ( > 20mb). This also requires that you store the file metadata.
- [encryptFileAndZipWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFileAndZipWithMetadata.html) - Used to encrypt a file and then zip it up with the metadata (using [JSZip](https://www.npmjs.com/package/jszip)). This is useful for when you don't want to store the file metadata separately.
- [zipAndEncryptFiles()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptFiles.html) - Used to zip and encrypt multiple files. This does **not** include the file metadata in the zip, so you must store those yourself.

Apart from these, we have one more function which can be used to encrypt both strings and files:

- [encryptToJson()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptToJson.html) - Used to encrypt a string or file and serialize all the metadata required to decrypt i.e. accessControlConditions, evmContractConditions, solRpcConditions, unifiedAccessControlConditions & chain to JSON. It is useful for encrypting/decrypting data in IPFS or other storage without compressing it in a ZIP file.
- [encryptToJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptToJson.html) - Used to encrypt a string or file and serialize all the metadata required to decrypt i.e. accessControlConditions, evmContractConditions, solRpcConditions, unifiedAccessControlConditions & chain to JSON. It is useful for encrypting/decrypting data in IPFS or other storage without compressing it in a ZIP file.

Encryption can be performed entirely client-side and doesn't require making a request to the Lit nodes.

Expand Down Expand Up @@ -525,11 +525,11 @@ If using a `mainnet` in order to keep the wallet which holds the `Capacity Credi

To decrypt use the following functions depending on the function used to encrypt:

- [decryptToString()](https://v5.api-docs.getlit.dev/functions/encryption_src.decryptToString.html) for [encryptString()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptString.html)
- [decryptToZip()](https://v5.api-docs.getlit.dev/functions/encryption_src.decryptToZip.html) for [zipAndEncryptString()](https://v5.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptString.html) & [zipAndEncryptFiles()](https://v5.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptFiles.html)
- [decryptToFile()](https://v5.api-docs.getlit.dev/functions/encryption_src.decryptToFile.html) for [encryptFile()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptFile.html)
- [decryptZipFileWithMetadata()](https://v5.api-docs.getlit.dev/functions/encryption_src.decryptZipFileWithMetadata.html) for [encryptFileAndZipWithMetadata()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptFileAndZipWithMetadata.html)
- [decryptFromJson()](https://v5.api-docs.getlit.dev/functions/encryption_src.decryptFromJson.html) for [encryptToJson()](https://v5.api-docs.getlit.dev/functions/encryption_src.encryptToJson.html)
- [decryptToString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToString.html) for [encryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptString.html)
- [decryptToZip()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToZip.html) for [zipAndEncryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptString.html) & [zipAndEncryptFiles()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptFiles.html)
- [decryptToFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToFile.html) for [encryptFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFile.html)
- [decryptZipFileWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptZipFileWithMetadata.html) for [encryptFileAndZipWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFileAndZipWithMetadata.html)
- [decryptFromJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptFromJson.html) for [encryptToJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptToJson.html)

In the example, we used `encryptString()` to encrypt so we will use `decryptToString()` to decrypt. Pass in the data `accessControlConditions``ciphertext``dataToEncryptHash`, and `authSig`.

Expand Down
22 changes: 11 additions & 11 deletions docs/sdk/access-control/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ const accessControlConditions = [

To encrypt a string, use one of the following functions:

- [encryptString()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptString.html) - Used to encrypt the raw string.
- [zipAndEncryptString()](https://v3.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptString.html) - Compresses the string (using [JSZip](https://www.npmjs.com/package/jszip)) before encrypting it. This is useful for saving space, but takes additional time to perform the zip.
- [encryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptString.html) - Used to encrypt the raw string.
- [zipAndEncryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptString.html) - Compresses the string (using [JSZip](https://www.npmjs.com/package/jszip)) before encrypting it. This is useful for saving space, but takes additional time to perform the zip.

To encrypt a file, use:

- [encryptFile()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptFile.html) - Used to encrypt a file without doing any zipping or packing. Because zipping larger files takes time, this function is useful when encrypting large files ( > 20mb). This also requires that you store the file metadata.
- [encryptFileAndZipWithMetadata()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptFileAndZipWithMetadata.html) - Used to encrypt a file and then zip it up with the metadata (using [JSZip](https://www.npmjs.com/package/jszip)). This is useful for when you don't want to store the file metadata separately.
- [zipAndEncryptFiles()](https://v3.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptFiles.html) - Used to zip and encrypt multiple files. This does **not** include the file metadatas in the zip, so you must store those yourself.
- [encryptFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFile.html) - Used to encrypt a file without doing any zipping or packing. Because zipping larger files takes time, this function is useful when encrypting large files ( > 20mb). This also requires that you store the file metadata.
- [encryptFileAndZipWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFileAndZipWithMetadata.html) - Used to encrypt a file and then zip it up with the metadata (using [JSZip](https://www.npmjs.com/package/jszip)). This is useful for when you don't want to store the file metadata separately.
- [zipAndEncryptFiles()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptFiles.html) - Used to zip and encrypt multiple files. This does **not** include the file metadatas in the zip, so you must store those yourself.

Apart from these, we have one more function which can be used to encrypt both strings and files:

- [encryptToJson()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptToJson.html) - Used to encrypt a string or file and serialise all the metadata required to decrypt i.e. accessControlConditions, evmContractConditions, solRpcConditions, unifiedAccessControlConditions & chain to JSON. It is useful for encrypting/decrypting data in IPFS or other storage without compressing it in a ZIP file.
- [encryptToJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptToJson.html) - Used to encrypt a string or file and serialise all the metadata required to decrypt i.e. accessControlConditions, evmContractConditions, solRpcConditions, unifiedAccessControlConditions & chain to JSON. It is useful for encrypting/decrypting data in IPFS or other storage without compressing it in a ZIP file.

Encryption can be performed entirely client-side and doesn't require making a request to the Lit nodes.

Expand Down Expand Up @@ -510,11 +510,11 @@ If using a `mainnet` in order to keep the wallet which holds the `Capacity Credi

To decrypt use the following functions depending on the function used to encrypt:

- [decryptToString()](https://v3.api-docs.getlit.dev/functions/encryption_src.decryptToString.html) for [encryptString()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptString.html)
- [decryptToZip()](https://v3.api-docs.getlit.dev/functions/encryption_src.decryptToZip.html) for [zipAndEncryptString()](https://v3.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptString.html) & [zipAndEncryptFiles()](https://v3.api-docs.getlit.dev/functions/encryption_src.zipAndEncryptFiles.html)
- [decryptToFile()](https://v3.api-docs.getlit.dev/functions/encryption_src.decryptToFile.html) for [encryptFile()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptFile.html)
- [decryptZipFileWithMetadata()](https://v3.api-docs.getlit.dev/functions/encryption_src.decryptZipFileWithMetadata.html) for [encryptFileAndZipWithMetadata()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptFileAndZipWithMetadata.html)
- [decryptFromJson()](https://v3.api-docs.getlit.dev/functions/encryption_src.decryptFromJson.html) for [encryptToJson()](https://v3.api-docs.getlit.dev/functions/encryption_src.encryptToJson.html)
- [decryptToString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToString.html) for [encryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptString.html)
- [decryptToZip()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToZip.html) for [zipAndEncryptString()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptString.html) & [zipAndEncryptFiles()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.zipAndEncryptFiles.html)
- [decryptToFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptToFile.html) for [encryptFile()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFile.html)
- [decryptZipFileWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptZipFileWithMetadata.html) for [encryptFileAndZipWithMetadata()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptFileAndZipWithMetadata.html)
- [decryptFromJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.decryptFromJson.html) for [encryptToJson()](https://v6-api-doc-lit-js-sdk.vercel.app/functions/encryption_src.encryptToJson.html)

In the example, we used `encryptString()` to encrypt so we will use `decryptToString()` to decrypt. Pass in the data `accessControlConditions``ciphertext``dataToEncryptHash`, and `authSig`.

Expand Down
172 changes: 172 additions & 0 deletions docs/sdk/authentication/session-sigs/get-lit-action-session-sigs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
sidebar_position: 2
---

import FeedbackComponent from "@site/src/pages/feedback.md";
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Generating SessionSigs Using a Lit Action

This guide covers the `getLitActionSessionSigs` function from the Lit SDK. For an overview of what Session Signatures are and how they are to be used, please go [here](./intro).

Using the `getLitActionSessionSigs` function, you can specify the capabilities of your current session on the Lit network.

This function is very similar to [`getPkpSessionSigs`](./get-pkp-session-sigs.md). The `getPkpSessionSigs` function requires you to own a PKP and some form of authentication to prove your identity (e.g. a custom Lit Action, AuthMethod, or AuthSig).

Alternatively, the `getLitActionSessionSigs` function requires the form of authentication to be a Lit Action. Other authentication methods can be included as well, but a Lit Action is required.

Using this arragement, the function executes the Lit Action to determine authorization for the following step.

This function uses the [`signSessionKey`](https://v6-api-doc-lit-js-sdk.vercel.app/classes/lit_node_client_src.LitNodeClientNodeJs.html#signSessionKey) function to sign the session public key using the PKP, which will generate an `AuthSig`.

Once the `AuthSig` has been created, it is then signed by the session keypair. Signing the `AuthSig` with the session keypair creates the Session Signatures.

:::note
The Lit Action is defined with the `litActionCode` or `litActionIpfsId` parameter, and `jsParams` must be provided for executing the action.
:::

## Prerequisites

Before continuing this guide, you should have an understanding of:
- [Session Signatures](./intro)
- [Lit Resources and Abilities](./resources-and-abilities.md)
- [PKPs](../../wallets/minting)

## Parameters and Return Values

To see the parameters and return values of `getLitActionSessionSigs`, please visit our [API Docs](https://v6-api-doc-lit-js-sdk.vercel.app/classes/lit_node_client_src.LitNodeClientNodeJs.html#getLitActionSessionSigs).

## Example Implementation

The full code implementation can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/master/session-signatures/getLitActionSessionSigs).

### Installing the Required Dependencies
<Tabs
defaultValue="npm"
values={[
{label: 'npm', value: 'npm'},
{label: 'yarn', value: 'yarn'},
]}>
<TabItem value="npm">

```bash
npm install \
@lit-protocol/auth-helpers \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/contracts-sdk \
ipfs-only-hash \
ethers@v5
```

</TabItem>

<TabItem value="yarn">

```bash
yarn add \
@lit-protocol/auth-helpers \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/contracts-sdk \
ipfs-only-hash \
ethers@v5
```

</TabItem>
</Tabs>

The `node-localstorage` dependency is only required when executing code outside a browser environment. The SDK will use the native browser storage when in a browser environment. You can learn more about this [here](./intro.md#storing-sessionsigs).

### Initializing an Ethers Signer
The `ETHEREUM_PRIVATE_KEY` environment variable is required.
```ts
import { LIT_RPC } from "@lit-protocol/constants";
import * as ethers from "ethers";

const ethersSigner = new ethers.Wallet(
process.env.ETHEREUM_PRIVATE_KEY,
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
);
```

### Initializing a `LitNodeClient`
Here we are initializing an instance of `LitNodeClient` and connecting it to the `datil-test` Lit network.
```ts
import { LitNodeClient } from "@lit-protocol/lit-node-client";
import { LitNetwork } from "@lit-protocol/constants";

let litNodeClient: LitNodeClient;

litNodeClient = new LitNodeClient({
litNetwork: LitNetwork.DatilTest,
debug: false,
});
await litNodeClient.connect();
```

### Instantiating a `LitContracts` Instance
Here we are initializing an instance of `LitContracts`. This allows us to interact with smart contracts on the Lit network.

```ts
import { LitContracts } from "@lit-protocol/contracts-sdk";
import { LitNetwork } from "@lit-protocol/constants";

const litContracts = new LitContracts({
signer: ethersSigner,
network: LitNetwork.DatilTest,
debug: false,
});
await litContracts.connect();
```

### Generating Session Signatures
In this example, we're enabling our session to use a PKP for signing and to execute Lit Actions.

The current code uses the wildcard (`*`) identifier for `LitPKPResource`, which grants signing abilities to **any** PKP. This should only be used for example implementations or debugging. A more secure implementation would instead use the PKP `tokenId` to grant a specific PKP signing abilities.

The wildcard identifier is also used for `LitActionResource`. This allows the session to execute **any** Lit Action. A more secure implementation would instead have a specific IPFS CID.

To get the Lit resource identifier for other resources, you can use the other methods included in [@lit-protocol/auth-helpers](https://v6-api-doc-lit-js-sdk.vercel.app/modules/auth_helpers_src.html) package.

If you would like to use this function on the `datil` or `datil-test` networks, a `capacityDelegationAuthSig` is required. Please also keep in mind that implementing this requires owning or minting a PKP and defining a Lit Action. How this is done can be found in the full code example.

```ts
import {
LitAbility,
LitActionResource,
LitPKPResource,
} from "@lit-protocol/auth-helpers";

const sessionSignatures = await litNodeClient.getLitActionSessionSigs({
pkpPublicKey: pkp.publicKey,
capabilityAuthSigs: [capacityDelegationAuthSig],
chain: "ethereum",
resourceAbilityRequests: [
{
resource: new LitPKPResource("*"),
ability: LitAbility.PKPSigning,
},
{
resource: new LitActionResource("*"),
ability: LitAbility.LitActionExecution,
},
],
// With this setup you could use either the litActionIpfsId or the litActionCode property
//litActionIpfsId: litActionCodeIpfsCid,
litActionCode: Buffer.from(litActionCode).toString("base64"),
jsParams: {
magicNumber: 42,
},
});
```

## Clearing Local Storage

If you want to clear the session key stored in the browser local storage, you can call the [`disconnectWeb3` method](https://js-sdk.litprotocol.com/functions/auth_browser_src.ethConnect.disconnectWeb3.html).

## Summary
This example shows how to enable a session to use a PKP for signing and to execute Lit Actions.

The full code implementation can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/master/session-signatures/getPkpSessionSigs).
Loading

0 comments on commit 5efc82b

Please sign in to comment.