Using a Delegation Auth Sig to Make Requests
When making requests to the Lit network, you must provide Session Signatures. 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.
The following code will demonstrate executing a Lit Action, one of the types of requests that requires payment, using Session Signatures that contain a Capacity Delegation Auth Signature.
The full implementation of the code used in this guide can be found here.
Prerequisites
Before continuing, you should have an understanding of:
The address we will be using to make a request to the Lit network needs to have been included in the delegateeAddresses
for the Capacity Delegation Auth Signature used in this guide.
- How to obtain a Capacity Delegation Auth Signature
- How to generate Session Signatures
- How to execute a Lit Action
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:
- npm
- yarn
npm install \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/auth-helpers \
ethers@v5
yarn add \
@lit-protocol/constants \
@lit-protocol/lit-node-client \
@lit-protocol/auth-helpers \
ethers@v5
Instantiating an Ethers Signer
import ethers from "ethers";
import { LIT_RPC, LIT_ABILITY } from "@lit-protocol/constants";
const ethersSigner = new ethers.Wallet(
process.env.ETHEREUM_PRIVATE_KEY,
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
);
The address corresponding to process.env.ETHEREUM_PRIVATE_KEY
needs to have been included in the delegateeAddresses
for the Capacity Delegation Auth Signature used in this guide.
Instantiating a LitNodeClient
Client
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();
You can learn more about the @lit-protocol/lit-node-client
package and what is offers using the API reference docs.
Generating Session Sigs with the Delegation Auth Sig
For more information on how the getSessionSigs
works and it's parameters, please go here.
const sessionSigs = await litNodeClient.getSessionSigs({
chain: "ethereum",
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours
capabilityAuthSigs: [capacityDelegationAuthSig],
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 Signature (that's delegating credit usage to ethersSigner.address
) to pay for our request later in this guide:
capabilityAuthSigs: [capacityDelegationAuthSig]
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:
await litNodeClient.executeJs({
sessionSigs,
code: `(() => console.log("It works!"))();`,
});
Summary
The full implementation of the code used in this guide can be found here.
This guide has demonstrated how to use a Capacity Delegation Auth Signature to generate Session Signatures, and use those Session Signature to make a request to the Lit network that requires payment.