Skip to content

Init Lit Relayer overview page #322

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions docs/paying-for-lit/capacity-credit-intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Capacity Credits

Capacity Credits are how you pay for usage of the Lit network. They allow you to reserve a specific amount of capacity (i.e. requests a second) on the network over a pre-defined amount of time (e.g. one week). In order to make requests to the Lit network for things like: signing using a PKP, decrypting data, and executing a Lit Action. You'll need to provide the token ID of an active Capacity Credit along with your request.

:::info
For an overview of what requests to the Lit network require payment, go [here](./overview.md#overview-of-what-requires-payment).
:::

Capacity credits are NFT tokens on the [Chronicle Yellowstone](../connecting-to-a-lit-network/lit-blockchains/chronicle-yellowstone.md) rollup blockchain, and can be minted by making a transaction directly to the NFT contract, or by using the Lit Explorer. You can learn more about minting Capacity Credits via a specific method by going to these pages:

- [Minting via the NFT contract](./minting-capacity-credit/via-contract.md)
- [Minting via the Lit Explorer](./minting-capacity-credit/via-explorer.md)

After minting a Capacity Credit, you'll want to learn [how to delegate usage](./delegating-credit.md) of it to either your users, or yourself via a Capacity Delegation Auth Sig.
142 changes: 142 additions & 0 deletions docs/paying-for-lit/delegating-credit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Delegating a Credit

To use a Capacity Credit to pay for usage of the Lit network, you need to create a *Capacity Delegation Auth Sig*. This Auth Sig is used as proof you have authorization to use a specific Capacity Credit to pay for requests to the Lit network like: signing using a PKP, decrypting data, and executing a Lit Action.

:::info
To learn more about what a Capacity Credit is, and how they're used, please go [here](./capacity-credit-intro.md).

For an overview of what requests to the Lit network require payment, go [here](./overview.md#overview-of-what-requires-payment).
:::

As we'll see later in the guide, these Auth Sigs are scoped to specific addresses and will be used to delegate usage of the credit to both yourself and your users to pay for network usage.

The following code will demonstrate how to produce the Capacity Delegation Auth Sig.

:::info
The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/blob/master/paying-for-lit/nodejs/src/delegateCapacityCredit.ts).
:::

## Prerequisites

Before continuing, you'll need to have minted a Capacity Credit. This can be done by following these guides:

- [Minting via the NFT contract](./minting-capacity-credit/via-contract.md)
- [Minting via the Lit Explorer](./minting-capacity-credit/via-explorer.md)

## Setup

### Installing the Required Dependencies

This guide makes use of the following packages and are required to use the following code. You can install the dependencies from NPM using NPM or Yarn:

<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>

### Instantiating an Ethers Signer

```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)
);
```

:::note
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 SIWE](https://eips.ethereum.org/EIPS/eip-5573) message that authorizes usage of the credit later in the guide.
:::

### Instantiating a `LitNodeClient` Client

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

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

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

## Generating the Capacity Delegation Auth Sig

```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

#### `dAppOwnerWallet`

This parameter is a [SignerLike](https://v6-api-doc-lit-js-sdk.vercel.app/interfaces/types_src.SignerLike.html) object (for this guide it's an instance of `ethers.Wallet`) that will be used to sign the ERC-5573 SIWE message that authorizes `delegateeAddresses` to use the Capacity Credit. The Ethereum address of the signer **must** own the Capacity Credit to delegate usage of it.

#### `capacityTokenId`

This parameter is the token ID of the Capacity Credit you're delegating usage of.

#### `delegateeAddresses`

This is an array of Ethereum address that you're authorizing usage of the Capacity Credit to. If you're trying to use the Capacity Credit to pay for your own network usage, you would add your address in this array.

#### `uses`

This parameter sets the total number of times the Auth Sig can be used to pay for network usage across all addresses listed in `delegateeAddresses`. Once the total `uses` is exhausted, the Auth Sig becomes invalid for payment by any delegated address. For example, if `uses` is set to `10` and one delegated address consumes all `10` uses, the Auth Sig can no longer be used for payment by any other delegated addresses.

#### `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

Calling `litNodeClient.createCapacityDelegationAuthSig` will create a ERC-5573 SIWE message and sign it using `dAppOwnerWallet`. The object returned by this function contains the Capacity Delegation Auth Sig that can be used to pay for requests to the 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/blob/master/paying-for-lit/nodejs/src/delegateCapacityCredit.ts).
:::

After running the above code, you will have created a Capacity Delegation Auth Sig that authorizes use of a specific Capacity Credit to a specific set of addresses with restrictions. For an example of using the Auth Sig, go [here](./using-delegated-auth-sig.md).
46 changes: 46 additions & 0 deletions docs/paying-for-lit/lit-relayer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Lit Relayer

The Lit Relayer is an [open-source service](https://github.com/LIT-Protocol/relay-server) currently hosted by Lit to facilitate onboarding into the Lit ecosystem. It helps reduce initial costs by covering or subsidizing certain interactions with the Lit network, such as minting Programmable Key Pairs (PKPs).

While the Relayer eases the onboarding process, it's important to note that its availability is not guaranteed. Users may experience rate limiting and/or congestion due to its shared nature.

As your application moves into production, we recommend implementing this functionality directly into your own application instead of using the Lit Relayer. This will ensure that you can use the Lit network with minimal friction and disruptions in service, as your direct implementation will be much more reliable and scalable.

## Overview of Lit Relayer Functions

The Lit Relayer provides the following functions to help interact with the Lit network:

### PKP Minting

The Relayer can mint PKPs on behalf of users, subsidizing the minting costs. This process verifies the authenticity of various authentication methods, then associates the public key of a newly generated PKP with the authenticated identity.

The currently supported authentication methods include:

- WebAuthn
- Google OAuth
- Discord OAuth
- Stytch Otp
- Ethereum wallet signatures
- One-time passwords (OTP)

The Relayer also supports retrieving all the PKPs associated with authenticated accounts.

### Payment Delegation Database

The Relayer also provides access to the [Payment Delegation Database](./payment-delegation-db.md), please refer to it's documentation to learn more.

## Using the Lit Relayer

To use the Lit Relayer, you must have a valid API key, as all communication with the Relayer is done using HTTP requests to the [Relayer endpoints](https://github.com/LIT-Protocol/relay-server?tab=readme-ov-file#available-endpoints).

If you don't already have an API key, you can request one by filling out [this form](https://docs.google.com/forms/d/e/1FAIpQLSeVraHsp1evK_9j-8LpUBiEJWFn4G5VKjOWBmHFjxFRJZJdrg/viewform).

## Limitations and Considerations

While the Lit Relayer is a useful tool for getting started with Lit Protocol, there are some important considerations:

1. **Rate Limiting**: To prevent abuse, the Relayer implements rate limiting. Your application may encounter usage limits.

2. **Availability**: As a shared service, the Relayer may experience congestion or downtime.

3. **Scalability**: For production applications, it's recommended to implement Relayer functionality directly in your application for better reliability and scalability.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions docs/paying-for-lit/minting-capacity-credit/via-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Via the Lit Contracts SDK

Capacity Credits can be minted by making requests to the NFT contract that is deployed on the [Chronicle Yellowstone](../../connecting-to-a-lit-network/lit-blockchains/chronicle-yellowstone.md) rollup blockchain. The following code will demonstrate how to connect to Chronicle Yellowstone via the Lit RPC URL, and send a transaction to the blockchain to mint a new Capacity Credit.

:::info
To learn more about what a Capacity Credit is, and how they're used, please go [here](../capacity-credit-intro.md).

The full implementation of the code used in this guide can be found [here](https://github.com/LIT-Protocol/developer-guides-code/blob/master/paying-for-lit/nodejs/src/mintCapacityCredit.ts).
:::

## Prerequisites

Before continuing, you'll need access to an Ethereum wallet that has [Lit test tokens](../../connecting-to-a-lit-network/lit-blockchains/chronicle-yellowstone.md#tstlpx-test-token) on the Chronicle Yellowstone blockchain. If you don't already have tokens, you can request some using [the faucet](https://chronicle-yellowstone-faucet.getlit.dev/). The `tstLPX` test token will be sent to your wallet address, allowing you to perform transactions on the rollup.

## Setup

### Installing the Required Dependencies

This guide makes use of the following packages and are required to use the following code. You can install the dependencies from NPM using NPM or Yarn:

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

```bash
npm install \
@lit-protocol/constants \
@lit-protocol/contracts-sdk \
ethers@v5
```

</TabItem>

<TabItem value="yarn">

```bash
yarn add \
@lit-protocol/constants \
@lit-protocol/contracts-sdk \
ethers@v5
```

</TabItem>
</Tabs>

### Instantiating an Ethers Signer

```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 `LitContracts` Client

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

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

You can learn more about the `@lit-protocol/contracts-sdk` package and what is offers using the [API reference docs](https://v6-api-doc-lit-js-sdk.vercel.app/modules/contracts_sdk_src.html).

## Minting a Capacity Credit

```ts
const capacityCreditInfo = await litContractClient.mintCapacityCreditsNFT({
requestsPerKilosecond: 80,
// requestsPerDay: 14400,
// requestsPerSecond: 10,
daysUntilUTCMidnightExpiration: 1,
});
```

### Parameters

When minting a credit, the following parameters are required:

#### `requestsPerX`

This parameter is the capacity you're reserving on the Lit network. This value is the maximum number of requests your Capacity Credit can be used for in a given day. Once your credit has been used for this number of requests, you will receive a Rate Limit error if it's used again before midnight UTC time.

For convenience, any one of the following properties can be used:
- `requestsPerKilosecond`
- `requestsPerDay`
- `requestsPerSecond`

#### `daysUntilUTCMidnightExpiration`

This parameter sets the date the Capacity Credit will expire. The credit expires at 12:00 AM (midnight) Coordinated Universal Time (UTC) on the specified date.

:::note
The actual expiration time in your local timezone may be different due to the UTC conversion. For example, if you're in New York (ET), a credit set to expire on June 1st will actually expire on May 31st at 8:00 PM ET.
:::

### Return Value

Calling `litContractClient.mintCapacityCreditsNFT` will create and sign a transaction to the Chronicle Yellowstone blockchain, paying for both the Capacity Credit and transaction gas in the Lit test token.

After the transaction is processed and included in a block, you will be returned the following Capacity Credit info:

```
{
rliTxHash: string;
capacityTokenId: number;
capacityTokenIdStr: string;
}
```

Where:

- `rliTxHash` Is the transaction hash of the transaction that minted the credit.
- `capacityTokenId` Is the generated ID for the new credit as a `number`.
- `capacityTokenIdStr` Is the generated ID for the new credit as a `string`.

You will use either `capacityTokenId` or `capacityTokenIdStr` to identify the Capacity Credit you would like use when paying for request to the 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/blob/master/paying-for-lit/nodejs/src/mintCapacityCredit.ts).
:::

After running the above code, you will have minted a new Capacity Credit that can be used to pay for usage of the Lit network. To learn more about how to use this credit for payment, please go [here](../delegating-credit.md).
Loading