Skip to content

Commit f231d09

Browse files
authored
Merge pull request #425 from LIT-Protocol/feat/add-la-sign-derive
feat: add derive eth address of la wallet section
2 parents 5ca72e9 + 86cb022 commit f231d09

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

docs/sdk/serverless-signing/sign-as-action.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,74 @@ const response = await litNodeClient.executeJs({
100100

101101
The response will contain a boolean indicating whether the signature is valid.
102102

103+
## Deriving the Lit Action Wallet Address
104+
105+
Since a Lit Action's keypair is deterministically derived from its IPFS CID, you can compute the corresponding Ethereum address for any Lit Action.
106+
107+
### Computing the Address
108+
109+
The Lit Action's public key and Ethereum address can be derived using the Lit Contracts SDK:
110+
111+
```ts
112+
import * as ethers from "ethers";
113+
import { LIT_RPC } from "@lit-protocol/constants";
114+
import { LitContracts } from "@lit-protocol/contracts-sdk";
115+
116+
const deriveLitActionWalletAddress = async ({ litActionIpfsCid }) => {
117+
const ethersSigner = new ethers.Wallet(
118+
ETHEREUM_PRIVATE_KEY,
119+
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
120+
);
121+
122+
const contractClient = new LitContracts({ signer: ethersSigner });
123+
await contractClient.connect();
124+
125+
const derivedKeyId = ethers.utils.keccak256(
126+
ethers.utils.toUtf8Bytes(`lit_action_${litActionIpfsCid}`)
127+
);
128+
129+
const derivedPubkey = await contractClient.pubkeyRouterContract.read.getDerivedPubkey(
130+
contractClient.stakingContract.read.address,
131+
derivedKeyId
132+
);
133+
134+
return {
135+
derivedPubkey,
136+
derivedAddress: ethers.utils.computeAddress(derivedPubkey),
137+
};
138+
};
139+
```
140+
141+
This function:
142+
1. Connects to the Lit Protocol smart contracts
143+
2. Computes the derived key ID using the same formula: `keccak256("lit_action_" + actionIpfsCid)`
144+
3. Queries the PKP router contract to get the public key
145+
4. Computes the Ethereum address from the public key
146+
147+
### Obtaining the Address from Within a Lit Action
148+
149+
You can also obtain the public key and compute the Ethereum address from within a Lit Action itself using the [`getActionPublicKey`](https://naga.actions-docs.litprotocol.com/#getactionpublickey) method:
150+
151+
```jsx
152+
(async () => {
153+
// Derive this Action's public key deterministically from its IPFS CID + scheme
154+
// This does not require a PKP and is always the same for a given (CID, scheme).
155+
const actionIpfsCid = Lit.Auth.actionIpfsIdStack[0];
156+
const actionPublicKey = await Lit.Actions.getActionPublicKey({
157+
signingScheme: 'EcdsaK256Sha256',
158+
actionIpfsCid,
159+
});
160+
161+
Lit.Actions.setResponse({
162+
response: JSON.stringify({
163+
actionPublicKey,
164+
actionAddress: ethers.utils.computeAddress(actionPublicKey),
165+
actionIpfsCid,
166+
}),
167+
});
168+
})();
169+
```
170+
103171
## Use Cases
104172

105173
- **Oracle Attestations**: A price oracle Lit Action can sign market data it fetches, allowing other contracts to verify the data came from the specific oracle action

0 commit comments

Comments
 (0)