|
| 1 | +--- |
| 2 | +title: 'Derive Lit Action Public Keys' |
| 3 | +description: 'How to deterministically derive and verify a Lit Action identity without executing it externally.' |
| 4 | +--- |
| 5 | + |
| 6 | +# Derive a Lit Action Public Key Locally |
| 7 | + |
| 8 | +## Question |
| 9 | + |
| 10 | +I want to call `Lit.Actions.signAsAction`. I know the action identity is derived from the Action's IPFS CID, but I cannot find a way to obtain the public key outside of the Action runtime. `Lit.Actions.getActionPublicKey` works within the Action, while `executeJs` only exposes `signatures.<name>.publicKey` after a signing operation. Is there a way to deterministically derive the Action's public key locally without running the Action? |
| 11 | + |
| 12 | +## Answer |
| 13 | + |
| 14 | +Yes. Inside the Lit Action you can deterministically derive the Action identity (and therefore its public key) from the same inputs the nodes use: the Action's IPFS CID and the signing scheme. The snippet below shows the complete flow: |
| 15 | + |
| 16 | +1. Produce the 32-byte message hash the Lit nodes expect. |
| 17 | +2. Call `Lit.Actions.signAsAction` to sign that message with the Action identity. |
| 18 | +3. Derive the Action public key via `Lit.Actions.getActionPublicKey`, passing the Action CID and signing scheme. |
| 19 | +4. Optionally verify the signature with `Lit.Actions.verifyActionSignature`. |
| 20 | + |
| 21 | +```js |
| 22 | +const { sigName, toSign } = jsParams; // 'publicKey' not required; derive it from the Action IPFS CID |
| 23 | +const { keccak256, arrayify } = ethers.utils; |
| 24 | + |
| 25 | +(async () => { |
| 26 | + // 1) Produce a 32-byte hash of the input (Lit Actions expect a 32-byte message for ECDSA schemes) |
| 27 | + const msgBytes = new TextEncoder().encode(toSign); |
| 28 | + const msgHashHex = keccak256(msgBytes); // 0x-prefixed hex string |
| 29 | + const msgHashBytes = arrayify(msgHashHex); // Uint8Array |
| 30 | + |
| 31 | + // 2) Sign as the current Lit Action (deterministic Action identity, not a PKP) |
| 32 | + // Supported schemes include 'EcdsaK256Sha256' (secp256k1) among others. |
| 33 | + const signingScheme = 'EcdsaK256Sha256'; |
| 34 | + const signature = await Lit.Actions.signAsAction({ |
| 35 | + toSign: msgHashBytes, |
| 36 | + sigName, |
| 37 | + signingScheme, |
| 38 | + }); |
| 39 | + |
| 40 | + // 3) Derive this Action's public key deterministically from its IPFS CID + scheme |
| 41 | + // This does not require a PKP and is always the same for a given (CID, scheme). |
| 42 | + const actionIpfsCid = Lit.Auth.actionIpfsIdStack[0]; |
| 43 | + const actionPublicKey = await Lit.Actions.getActionPublicKey({ |
| 44 | + signingScheme, |
| 45 | + actionIpfsCid, |
| 46 | + }); |
| 47 | + |
| 48 | + // 4) (Optional) Verify that the signature was produced by this Action identity |
| 49 | + const verified = await Lit.Actions.verifyActionSignature({ |
| 50 | + signingScheme, |
| 51 | + actionIpfsCid, |
| 52 | + toSign: msgHashBytes, |
| 53 | + signOutput: signature, |
| 54 | + }); |
| 55 | + |
| 56 | + // 5) Return a structured response for clients to consume |
| 57 | + Lit.Actions.setResponse({ |
| 58 | + response: JSON.stringify({ |
| 59 | + sigName, |
| 60 | + signingScheme, |
| 61 | + message: toSign, |
| 62 | + messageHash: msgHashHex, |
| 63 | + signature, // string; format depends on scheme |
| 64 | + actionPublicKey, // string; hex or JSON depending on scheme |
| 65 | + verified, // boolean |
| 66 | + }), |
| 67 | + }); |
| 68 | +})(); |
| 69 | +``` |
| 70 | + |
| 71 | +This approach keeps the derivation entirely within the Lit Action context. Because the public key depends only on the Action CID and signing scheme, you can rely on `Lit.Actions.getActionPublicKey` for a deterministic identity without needing to execute the Action externally first. |
0 commit comments