Skip to content

Update v2 #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/build/getting-started/capacity-credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
description: Learn about capacity credits and how to use them
sidebar_label: Capacity Credits
---

# Capacity Credits

:::info
Capacity Credits are the form of payment for usage of the Lit network. They are required when making decryption requests, PKP signing requests, and when executing Lit Actions.
:::

## Overview

Capacity Credits are the form of payment for usage of the Lit network. They are required when making decryption requests, PKP signing requests, and when executing Lit Actions.

While credits are initially restricted to the account that minted them, you can delegate them to allow other Ethereum addresses to utilize your credits. This is particularly useful when building dApps where you want to cover the usage costs for your users.

## Next Steps

To learn more about Capacity Credits, please explore the following guides:

- **Minting a Credit**
- [Via the Lit Explorer](./capacity-credits/minting/via-lit-explorer.md)
- [Via the Lit Contracts SDK](./capacity-credits/minting/via-lit-contracts-sdk.md)
- **Delegating a Credit**
- [Delegate a Credit](./capacity-credits/delegating/delegate-a-credit.md)
- [Use a Delegated Credit](./capacity-credits/delegating/use-delegated-credit.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
description: Learn how to delegate a capacity credit to an ETH address
sidebar_label: Delegate a Credit
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Delegating a Capacity Credit to an ETH Address

:::info
Capacity Credits are the form of payment for usage of the Lit network. They are required when making decryption requests, PKP signing requests, and when executing Lit Actions.

To learn more about what a Capacity Credit is, and how they're used, please go [here](../../../../learn/paying-for-lit/capacity-credits).
:::

While credits are initially restricted to the account that minted them, you can delegate them to allow other Ethereum addresses to utilize your credits. This is particularly useful when building dApps where you want to cover the usage costs for your users.

To delegate a credit, you'll need to create a _Capacity Delegation Auth Sig_. This Auth Sig is used as proof that an address has authorization to use a specific Capacity Credit to pay for their requests to a Lit network.

This guide will demonstrate how to create a Capacity Delegation Auth Sig using the Lit Contracts SDK.

:::info
The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/v2/capacity-credits/delegating/use-delegated-credit).
:::

## Prerequisites

- An Ethereum wallet with [Lit test tokens](../../../../learn/overview/how-it-works/overview#the-lit-protocol-token) on the Chronicle Yellowstone blockchain
- Test tokens can be obtained using the [faucet](https://chronicle-yellowstone-faucet.getlit.dev/)
- A Lit Capacity Credit
- If you don't have a credit, you can mint one using the [Lit Explorer](../minting/via-lit-explorer.md) or the [Lit Contracts SDK](../minting/via-lit-contracts-sdk.md)

### Required Packages

- `@lit-protocol/constants`
- `@lit-protocol/lit-node-client`
- `ethers@v5`

<Tabs
defaultValue="npm"
values={[
{label: 'npm', value: 'npm'},
{label: 'yarn', value: 'yarn'},
]}>
<TabItem value="npm">

```bash
npm install \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
ethers@v5
```

</TabItem>

<TabItem value="yarn">

```bash
yarn add \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
ethers@v5
```

</TabItem>
</Tabs>

## The Code Example

### Instantiating an Ethers Signer

To delegate a Capacity Credit to another Ethereum address, you'll need to create a Capacity Delegation Auth Sig. The following code uses Ethers.js to create a signer from an Ethereum private key, but any other Ethereum wallet library can be used.

The address corresponding to `process.env.ETHEREUM_PRIVATE_KEY` **needs** to be the owner of the Capacity Credit. This wallet will be used to produce a [ERC-5573](https://eips.ethereum.org/EIPS/eip-5573) message that authorizes usage of the credit later in the guide.

```ts
import ethers from "ethers";
import { LIT_RPC } from "@lit-protocol/constants";

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

### Instantiating a `LitNodeClient` Instance

Next we'll instantiate and connect a `LitNodeClient` client specifying the Lit network the Capacity Credit has been minted for. In this case we'll be delegating a credit that was minted for the [DatilTest](../../../../learn/overview/how-it-works/lit-networks/testnets#the-datil-test-network) network.

```ts
import { LitNodeClient } from "@lit-protocol/lit-node-client";
import { LIT_NETWORK } from "@lit-protocol/constants";

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

:::note
You can learn more about the `@lit-protocol/lit-node-client` package and what it offers using the [API reference docs](https://v7-api-doc-lit-js-sdk.vercel.app/classes/lit_node_client_src.LitNodeClient.html).
:::

## Generating the Capacity Delegation Auth Sig

On the instance of the `LitNodeClient` client, we can call the `createCapacityDelegationAuthSig` method to generate the Auth Sig. This method will create the ERC-5573 message based on the parameters you provide, sign it with the `dAppOwnerWallet`, and return an object containing the `capacityDelegationAuthSig`.

```ts
const { capacityDelegationAuthSig } =
await litNodeClient.createCapacityDelegationAuthSig({
dAppOwnerWallet: ethersSigner,
capacityTokenId,
delegateeAddresses: [delegateeAddress],
uses: "1",
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes
});
```

### Parameters

When calling `createCapacityDelegationAuthSig`, the following parameters are required:

#### `dAppOwnerWallet`

This is the Ethereum wallet that will be used to sign the ERC-5573 message. This wallet must be the owner of the Capacity Credit you're delegating.

#### `capacityTokenId`

This is the ID of the Capacity Credit you're delegating. This is returned to you as `capacityTokenId` when you minted the Capacity Credit.

:::note
You can find a list of Capacity Credits owned by your address by connecting your wallet to the Lit Explorer and navigating to the [Profile page](https://explorer.litprotocol.com/profile).
:::

#### `delegateeAddresses`

This is an array of Ethereum addresses you're delegating the Capacity Credit to.

#### `uses`

Specifies the total number of requests allowed across all delegated addresses. When this limit is reached, the Auth Sig becomes invalid for all delegatees. For example, with `uses: "10"`, if one address uses all 10 requests, no other delegated addresses can use the Auth Sig.

#### `expiration`

This parameter sets a time limit, represented as a UTC timestamp in seconds, for the Auth Sig. It specifies when the Auth Sig will become invalid and can no longer be used.

In the above code, this Auth Sig is being set to expire `10 minutes` after it's created.

### Return Value

`createCapacityDelegationAuthSig` will return an object with the `capacityDelegationAuthSig` property. This Auth Sig object is what you'll attach to your/your users' Session Signatures when making requests to a Lit network.

### Summary

:::info
The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/v2/capacity-credits/delegating/use-delegated-credit).
:::

After running the above code, you will have created a Capacity Delegation Auth Sig that can be used to pay for a Lit network request on behalf of the delegatee addresses.

## Next Steps

- Learn how to [use the Capacity Delegation Auth Sig](./use-delegated-credit) to pay for requests to a Lit network.
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
---
description: Learn how to use a delegated credit to make requests to a Lit network
sidebar_label: Use a Delegated Credit
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Using a Delegated Credit to Make Requests

:::info
Capacity Credits are the form of payment for usage of the Lit network. They are required when making decryption requests, PKP signing requests, and when executing Lit Actions.

To learn more about what a Capacity Credit is, and how they're used, please go [here](../../../../learn/paying-for-lit/capacity-credits).

To learn more about how to delegate a Capacity Credit to an ETH address, go [here](./delegate-a-credit).
:::

When making requests to the Lit network, you must provide [Session Signatures](../../../../learn/authentication/session-sigs). When making requests to the network that require payment, you must also attach a _Capacity Delegation Auth Signature_ to your Session Signatures. This Auth Sig tells the Lit network which Capacity Credit to use for paying for your network usage, and also acts as proof that you have permission to use the Capacity Credit for payment.

This guide will demonstrate how to attach a Capacity Delegation Auth Signature to Session Signatures when making a request to a Lit network.

:::info
The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/v2/capacity-credits/delegating/use-delegated-credit).
:::

## Prerequisites

- An understanding of [Session Signatures](../../../../learn/authentication/session-sigs)
- An understanding of [Capacity Credits](../../../../learn/paying-for-lit/capacity-credits) and what type of Lit network requests require payment
- A Capacity Delegation Auth Sig
- If you don't have a Capacity Delegation Auth Sig, you can create one by following the instructions [here](./delegate-a-credit).

### Required Packages

- `@lit-protocol/constants`
- `@lit-protocol/lit-node-client`
- `@lit-protocol/auth-helpers`
- `ethers@v5`

<Tabs
defaultValue="npm"
values={[
{label: 'npm', value: 'npm'},
{label: 'yarn', value: 'yarn'},
]}>
<TabItem value="npm">

```bash
npm install \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/auth-helpers \
ethers@v5
```

</TabItem>

<TabItem value="yarn">

```bash
yarn add \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/auth-helpers \
ethers@v5
```

</TabItem>
</Tabs>

## The Code Example

### Instantiating an Ethers Signer

This `ethersSigner` will be used to sign the Auth Sig as apart of generating the Session Signatures that the Capacity Delegation Auth Sig will be attached to.

The address corresponding to `process.env.ETHEREUM_PRIVATE_KEY` **needs** to be one of the addresses that the Capacity Delegation Auth Sig authorizes (i.e. the address was included in the `delegateeAddresses` array when the Capacity Delegation Auth Sig was created).

```ts
import ethers from "ethers";
import { LIT_RPC } from "@lit-protocol/constants";

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

### Instantiating a `LitNodeClient` Instance

Next we'll instantiate and connect a `LitNodeClient` client specifying the Lit network the Capacity Credit has been minted for. In this case we'll be using a credit that was minted for the [DatilTest](../../../../learn/overview/how-it-works/lit-networks/testnets#the-datil-test-network) network.

```ts
import { LitNodeClient } from "@lit-protocol/lit-node-client";
import { LIT_NETWORK } from "@lit-protocol/constants";

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

:::note
You can learn more about the `@lit-protocol/lit-node-client` package and what it offers using the [API reference docs](https://v7-api-doc-lit-js-sdk.vercel.app/classes/lit_node_client_src.LitNodeClient.html).
:::

### Generating Session Sigs with the Delegation Auth Sig

Using the [getSessionSigs](https://v7-api-doc-lit-js-sdk.vercel.app/classes/lit_node_client_src.LitNodeClient.html#getSessionSigs) method, we can generate Session Signatures that contain the Capacity Delegation Auth Sig:

```ts
const sessionSigs = await litNodeClient.getSessionSigs({
chain: "ethereum",
expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(),
capabilityAuthSigs: [capacityDelegationAuthSig], // <--- Here is where we
// attach the Capacity Delegation Auth Sig to the Session Signatures
resourceAbilityRequests: [
{
resource: new LitActionResource("*"),
ability: LIT_ABILITY.LitActionExecution,
},
],
authNeededCallback: async ({
resourceAbilityRequests,
expiration,
uri,
}) => {
const toSign = await createSiweMessageWithRecaps({
uri: uri!,
expiration: expiration!,
resources: resourceAbilityRequests!,
walletAddress: ethersSigner.address,
nonce: await litNodeClient.getLatestBlockhash(),
litNodeClient,
});

return await generateAuthSig({
signer: ethersSigner,
toSign,
});
},
});
```

This line from the above code is how we're specifying the Capacity Delegation Auth Sig (that's delegating credit usage to `ethersSigner.address`) to pay for our request later in this guide:

```ts
capabilityAuthSigs: [capacityDelegationAuthSig]
```

:::note
This guide assumes that you've already created a Capacity Delegation Auth Sig and assigned it to a variable with the name: `capacityDelegationAuthSig`.

If you don't have a Capacity Delegation Auth Sig, you can create one by following the instructions [here](./delegate-a-credit).
:::

## Making a Request

After executing the above code, you will now have Session Signatures that contain a Capacity Delegation Auth Signature. These Session Signatures can be used to make any requests to the Lit network that require payment.

Here is an example of using the Session Signatures to execute a simple Lit Action:

```ts
await litNodeClient.executeJs({
sessionSigs,
code: `(() => console.log("It works!"))();`,
});
```

## Summary

:::info
The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/tree/v2/capacity-credits/delegating/use-delegated-credit).
:::

This guide has demonstrated how to attach a Capacity Delegation Auth Signature to Session Signatures, and use those Session Signatures to make a request to the Lit network that requires payment.

## Next Steps
Loading