|
| 1 | +import { AuthContext, LitClientInstance } from '../../../types'; |
| 2 | + |
| 3 | +type ExecuteJsContext = { |
| 4 | + litClient: LitClientInstance; |
| 5 | + aliceEoaAuthContext: AuthContext; |
| 6 | +}; |
| 7 | + |
| 8 | +export const decryptAndCombineLitAction = ` |
| 9 | +(async () => { |
| 10 | + const results = { |
| 11 | + step1_getCurrentCid: null, |
| 12 | + step2_generateEntropy: null, |
| 13 | + step3_encrypt: null, |
| 14 | + step4_decrypt: null, |
| 15 | + step5_verify: null, |
| 16 | + }; |
| 17 | +
|
| 18 | + try { |
| 19 | + // Step 1: Get current action IPFS CID |
| 20 | + const currentCid = Lit.Auth.actionIpfsIdStack[0]; |
| 21 | + results.step1_getCurrentCid = { |
| 22 | + success: true, |
| 23 | + cid: currentCid, |
| 24 | + }; |
| 25 | +
|
| 26 | + // Step 2: Generate entropy (32 random bytes) |
| 27 | + const entropyHex = await Lit.Actions.runOnce( |
| 28 | + { waitForResponse: true, name: "generateEntropy" }, |
| 29 | + async () => { |
| 30 | + return ethers.utils.hexlify(ethers.utils.randomBytes(32)); |
| 31 | + } |
| 32 | + ); |
| 33 | + const entropy = ethers.utils.arrayify(entropyHex); |
| 34 | + results.step2_generateEntropy = { |
| 35 | + success: true, |
| 36 | + entropyPreview: entropyHex.substring(0, 20) + "...", |
| 37 | + entropyLength: entropy.length, |
| 38 | + }; |
| 39 | +
|
| 40 | + // Step 3: Encrypt with access control locked to current IPFS CID |
| 41 | + const accessControlConditions = [ |
| 42 | + { |
| 43 | + contractAddress: "", |
| 44 | + standardContractType: "", |
| 45 | + chain: "ethereum", |
| 46 | + method: "", |
| 47 | + parameters: [":currentActionIpfsId"], |
| 48 | + returnValueTest: { |
| 49 | + comparator: "=", |
| 50 | + value: currentCid, |
| 51 | + }, |
| 52 | + }, |
| 53 | + ]; |
| 54 | +
|
| 55 | + let encryptResult = await Lit.Actions.runOnce( |
| 56 | + { waitForResponse: true, name: "encrypt" }, |
| 57 | + async () => { |
| 58 | + return JSON.stringify( |
| 59 | + await Lit.Actions.encrypt({ |
| 60 | + accessControlConditions, |
| 61 | + to_encrypt: ethers.utils.toUtf8Bytes(entropyHex), |
| 62 | + }) |
| 63 | + ); |
| 64 | + } |
| 65 | + ); |
| 66 | + encryptResult = JSON.parse(encryptResult); |
| 67 | +
|
| 68 | + // Convert ciphertext to base64 for transmission |
| 69 | + let ciphertextStr; |
| 70 | + if (encryptResult.ciphertext instanceof Uint8Array) { |
| 71 | + const binaryStr = Array.from(encryptResult.ciphertext) |
| 72 | + .map((byte) => String.fromCharCode(byte)) |
| 73 | + .join(""); |
| 74 | + ciphertextStr = btoa(binaryStr); |
| 75 | + } else { |
| 76 | + ciphertextStr = encryptResult.ciphertext; |
| 77 | + } |
| 78 | +
|
| 79 | + // Convert dataToEncryptHash to hex |
| 80 | + let dataHashStr; |
| 81 | + if (encryptResult.dataToEncryptHash instanceof Uint8Array) { |
| 82 | + dataHashStr = ethers.utils.hexlify(encryptResult.dataToEncryptHash); |
| 83 | + } else { |
| 84 | + dataHashStr = encryptResult.dataToEncryptHash; |
| 85 | + } |
| 86 | +
|
| 87 | + results.step3_encrypt = { |
| 88 | + success: true, |
| 89 | + ciphertextLength: ciphertextStr.length, |
| 90 | + ciphertextPreview: ciphertextStr.substring(0, 30) + "...", |
| 91 | + dataToEncryptHash: dataHashStr, |
| 92 | + }; |
| 93 | +
|
| 94 | + // Step 4: Decrypt using decryptAndCombine |
| 95 | + const decryptResult = await Lit.Actions.decryptAndCombine({ |
| 96 | + accessControlConditions, |
| 97 | + ciphertext: encryptResult.ciphertext, |
| 98 | + dataToEncryptHash: encryptResult.dataToEncryptHash, |
| 99 | + authSig: null, |
| 100 | + chain: "ethereum", |
| 101 | + }); |
| 102 | +
|
| 103 | + // Convert decrypted result to hex for comparison |
| 104 | + let decryptedHex; |
| 105 | + if (decryptResult instanceof Uint8Array) { |
| 106 | + decryptedHex = ethers.utils.hexlify(decryptResult); |
| 107 | + } else if (typeof decryptResult === "string") { |
| 108 | + decryptedHex = decryptResult; |
| 109 | + } else { |
| 110 | + decryptedHex = "unknown format"; |
| 111 | + } |
| 112 | +
|
| 113 | + results.step4_decrypt = { |
| 114 | + success: true, |
| 115 | + decryptedDataPreview: decryptedHex.substring(0, 20) + "...", |
| 116 | + decryptedLength: decryptResult.length, |
| 117 | + }; |
| 118 | +
|
| 119 | + // Step 5: Verify original matches decrypted |
| 120 | + const matches = entropyHex === decryptedHex; |
| 121 | + results.step5_verify = { |
| 122 | + success: true, |
| 123 | + matches, |
| 124 | + originalPreview: entropyHex.substring(0, 20) + "...", |
| 125 | + decryptedPreview: decryptedHex.substring(0, 20) + "...", |
| 126 | + }; |
| 127 | +
|
| 128 | + Lit.Actions.setResponse({ |
| 129 | + response: JSON.stringify( |
| 130 | + { |
| 131 | + success: true, |
| 132 | + currentCid, |
| 133 | + results, |
| 134 | + }, |
| 135 | + null, |
| 136 | + 2 |
| 137 | + ), |
| 138 | + }); |
| 139 | + } catch (error) { |
| 140 | + Lit.Actions.setResponse({ |
| 141 | + response: JSON.stringify( |
| 142 | + { |
| 143 | + success: false, |
| 144 | + error: error.message, |
| 145 | + results, |
| 146 | + }, |
| 147 | + null, |
| 148 | + 2 |
| 149 | + ), |
| 150 | + }); |
| 151 | + } |
| 152 | +})();`; |
| 153 | + |
| 154 | +export const createExecuteJsDecryptAndCombineTest = ( |
| 155 | + ctx: ExecuteJsContext, |
| 156 | + getAuthContext: () => AuthContext = () => ctx.aliceEoaAuthContext |
| 157 | +) => { |
| 158 | + return async () => { |
| 159 | + const result = await ctx.litClient.executeJs({ |
| 160 | + code: decryptAndCombineLitAction, |
| 161 | + authContext: getAuthContext(), |
| 162 | + jsParams: {}, |
| 163 | + }); |
| 164 | + |
| 165 | + if (typeof result.response !== 'string') { |
| 166 | + throw new Error('Expected executeJs response to be a string payload'); |
| 167 | + } |
| 168 | + |
| 169 | + const parsed = JSON.parse(result.response); |
| 170 | + |
| 171 | + expect(parsed.success).toBe(true); |
| 172 | + expect(parsed.currentCid).toBeDefined(); |
| 173 | + expect(parsed.results?.step1_getCurrentCid?.success).toBe(true); |
| 174 | + expect(parsed.results?.step2_generateEntropy?.entropyLength).toBe(32); |
| 175 | + expect(parsed.results?.step3_encrypt?.success).toBe(true); |
| 176 | + expect(parsed.results?.step4_decrypt?.success).toBe(true); |
| 177 | + expect(parsed.results?.step5_verify?.matches).toBe(true); |
| 178 | + }; |
| 179 | +}; |
0 commit comments