Conditional Signing
Overview
Lit Actions inherit the powerful condition checking ability that Lit Protocol utilizes for access control. You can easily check on or off-chain conditions inside of Lit Actions to generate proofs and condition-based transaction automations.
The below example will check if the user has at least 1 Wei on Ethereum, only returning a signature if they do.
Prerequisites
- Knowlege of SessionSigs
- Knowledge of how to generate an AuthSig
- Basic understanding of Lit Actions
Complete Code Example
The complete code example is available in the Lit Developer Guides Code Repository. There is both a browser and Node.js implementation of the code.
Example Lit Action
This function performs a condition check on the authenticated user's Ethereum balance (the user who signed the Sign-in With Ethereum EIP-4361 message to create the AuthSig
), returning a boolean. This is then used to determine whether or not the PKP will sign dataToSign
or not.
The below example will check if the user has at least 1 Wei on Ethereum, only returning a signature if they do. It uses the checkConditions
function from the Lit Actions SDK. This performs a conditional check on the user's Ethereum balance, checking the balance of the wallet address that signed the Sign-in With Ethereum EIP-4361 when creating the AuthSig
, which is passed in as an argument to the Lit Action. The boolean returned will be used to determine whether the wallet will be used to sign dataToSign
or not.
The toSign
data is required to be an array of 8-bit integers. An example of this is shown below:
dataToSign: ethers.utils.arrayify(ethers.utils.keccak256([1, 2, 3, 4, 5])),
In the below code example, sigShare
is a magic value within a Lit Action that will be automatically returned for you.
const _litActionCode = async () => {
try {
// test an access control condition
const testResult = await Lit.Actions.checkConditions({
conditions,
authSig,
chain,
});
if (!testResult) {
LitActions.setResponse({ response: "address does not have 1 or more Wei on Ethereum Mainnet" });
return;
}
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
} catch (error) {
LitActions.setResponse({ response: error.message });
}
};
const litActionCode = `(${_litActionCode.toString()})();`;
Summary
This guide demonstrates how to use Lit Actions to conditionally sign a message or transaction.
If you'd like to learn more about Lit Actions, check out the Lit Actions SDK, or our Advanced Topics section on Lit Actions.
Not finding the answer you're looking for? Share your feedback on these docs by creating an issue in our GitHub Issues and Reports repository or get support by visiting our Support page.