|
| 1 | +import { log } from '@lit-protocol/misc'; |
| 2 | +import { randomBytes } from 'crypto'; |
| 3 | + |
| 4 | +import { api } from '@lit-protocol/wrapped-keys'; |
| 5 | +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; |
| 6 | +import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs'; |
| 7 | + |
| 8 | +const { generatePrivateKey, getEncryptedKey, updateEncryptedKey } = api; |
| 9 | + |
| 10 | +/** |
| 11 | + * Test Commands: |
| 12 | + * ✅ NETWORK=datil-dev yarn test:local --filter=testUpdateWrappedKey |
| 13 | + * ✅ NETWORK=datil-test yarn test:local --filter=testUpdateWrappedKey |
| 14 | + * ✅ NETWORK=custom yarn test:local --filter=testUpdateWrappedKey |
| 15 | + */ |
| 16 | +export const testUpdateWrappedKey = async (devEnv: TinnyEnvironment) => { |
| 17 | + const alice = await devEnv.createRandomPerson(); |
| 18 | + |
| 19 | + try { |
| 20 | + console.log('1. Fetch PKP session sigs'); |
| 21 | + const pkpSessionSigs = await getPkpSessionSigs( |
| 22 | + devEnv, |
| 23 | + alice, |
| 24 | + null, |
| 25 | + new Date(Date.now() + 1000 * 60 * 10).toISOString() |
| 26 | + ); // 10 mins expiry |
| 27 | + |
| 28 | + console.log('2. Generate a wrapped key'); |
| 29 | + const { id, pkpAddress } = await generatePrivateKey({ |
| 30 | + pkpSessionSigs, |
| 31 | + network: 'evm', |
| 32 | + litNodeClient: devEnv.litNodeClient, |
| 33 | + memo: 'Test update key', |
| 34 | + }); |
| 35 | + console.log({ id, pkpAddress }); |
| 36 | + |
| 37 | + console.log('3. Fetch initial encrypted key (without versions)'); |
| 38 | + const getEncryptedKeyFirst = await getEncryptedKey({ |
| 39 | + pkpSessionSigs, |
| 40 | + litNodeClient: devEnv.litNodeClient, |
| 41 | + id, |
| 42 | + }); |
| 43 | + console.log({ getEncryptedKeyFirst }); |
| 44 | + const newCiphertext1 = randomBytes(48).toString('base64'); |
| 45 | + const newCiphertext2 = randomBytes(48).toString('base64'); |
| 46 | + |
| 47 | + console.log('4. Update encrypted key with new ciphertext/memo'); |
| 48 | + const updateEncryptedKeyFirst = await updateEncryptedKey({ |
| 49 | + pkpSessionSigs, |
| 50 | + litNodeClient: devEnv.litNodeClient, |
| 51 | + id, |
| 52 | + ciphertext: newCiphertext1, |
| 53 | + memo: 'rotated memo', |
| 54 | + }); |
| 55 | + console.log({ updateEncryptedKeyFirst }); |
| 56 | + |
| 57 | + if (updateEncryptedKeyFirst.pkpAddress !== pkpAddress) { |
| 58 | + throw new Error('Updated key pkpAddress mismatch'); |
| 59 | + } |
| 60 | + |
| 61 | + console.log('5. Second update to generate another version'); |
| 62 | + const updateEncryptedKeySecond = await updateEncryptedKey({ |
| 63 | + pkpSessionSigs, |
| 64 | + litNodeClient: devEnv.litNodeClient, |
| 65 | + id, |
| 66 | + ciphertext: newCiphertext2, |
| 67 | + memo: 'rotated memo v2', |
| 68 | + }); |
| 69 | + console.log({ updateEncryptedKeySecond }); |
| 70 | + |
| 71 | + console.log('6. Fetch updated key including versions'); |
| 72 | + const getEncryptedKeyUpdated = await getEncryptedKey({ |
| 73 | + pkpSessionSigs, |
| 74 | + litNodeClient: devEnv.litNodeClient, |
| 75 | + id, |
| 76 | + includeVersions: true, |
| 77 | + }); |
| 78 | + console.log({ getEncryptedKeyUpdated }); |
| 79 | + |
| 80 | + if (getEncryptedKeyUpdated.ciphertext !== newCiphertext2) { |
| 81 | + throw new Error('Ciphertext was not updated'); |
| 82 | + } |
| 83 | + if ( |
| 84 | + !getEncryptedKeyUpdated.versions || |
| 85 | + getEncryptedKeyUpdated.versions.length !== 2 |
| 86 | + ) { |
| 87 | + throw new Error('Versions array missing or incorrect length'); |
| 88 | + } |
| 89 | + if ( |
| 90 | + getEncryptedKeyUpdated.versions[0].ciphertext !== |
| 91 | + getEncryptedKeyFirst.ciphertext |
| 92 | + ) { |
| 93 | + throw new Error('Initial version ciphertext mismatch'); |
| 94 | + } |
| 95 | + if (getEncryptedKeyUpdated.versions[1].ciphertext !== newCiphertext1) { |
| 96 | + throw new Error('First update version ciphertext mismatch'); |
| 97 | + } |
| 98 | + if ( |
| 99 | + !getEncryptedKeyUpdated.updatedAt || |
| 100 | + !getEncryptedKeyUpdated.versions[0].updatedAt || |
| 101 | + !getEncryptedKeyUpdated.versions[1].updatedAt |
| 102 | + ) { |
| 103 | + throw new Error('updatedAt timestamps not set'); |
| 104 | + } |
| 105 | + |
| 106 | + log('✅ testUpdateWrappedKey'); |
| 107 | + } finally { |
| 108 | + devEnv.releasePrivateKeyFromUser(alice); |
| 109 | + } |
| 110 | +}; |
0 commit comments