|
| 1 | +import { log } from '@lit-protocol/misc'; |
| 2 | +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; |
| 3 | +import { api } from '@lit-protocol/wrapped-keys'; |
| 4 | +import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs'; |
| 5 | +import { batchGenerateKeysWithLitAction } from '../../../packages/wrapped-keys/src/lib/lit-actions-client'; |
| 6 | +import { getLitActionCodeOrCidCommon } from '../../../packages/wrapped-keys/src/lib/lit-actions-client/utils'; |
| 7 | +import { |
| 8 | + getFirstSessionSig, |
| 9 | + getKeyTypeFromNetwork, |
| 10 | + getPkpAccessControlCondition, |
| 11 | + getPkpAddressFromSessionSig, |
| 12 | +} from '../../../packages/wrapped-keys/src/lib/api/utils'; |
| 13 | +import { listEncryptedKeyMetadata } from '../../../packages/wrapped-keys/src/lib/api'; |
| 14 | + |
| 15 | +const { storeEncryptedKeyBatch } = api; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test Commands: |
| 19 | + * ✅ NETWORK=datil-dev yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 20 | + * ✅ NETWORK=datil-test yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 21 | + * ✅ NETWORK=localchain yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 22 | + */ |
| 23 | +export const testFailBatchGeneratePrivateKeysAtomic = async ( |
| 24 | + devEnv: TinnyEnvironment |
| 25 | +) => { |
| 26 | + const alice = await devEnv.createRandomPerson(); |
| 27 | + |
| 28 | + try { |
| 29 | + const pkpSessionSigsSigning = await getPkpSessionSigs( |
| 30 | + devEnv, |
| 31 | + alice, |
| 32 | + null, |
| 33 | + new Date(Date.now() + 1000 * 60 * 10).toISOString() |
| 34 | + ); // 10 mins expiry |
| 35 | + |
| 36 | + const solanaMessageToSign = 'This is a test solana message'; |
| 37 | + const evmMessageToSign = 'This is a test evm message'; |
| 38 | + |
| 39 | + const sessionSig = getFirstSessionSig(pkpSessionSigsSigning); |
| 40 | + const pkpAddress = getPkpAddressFromSessionSig(sessionSig); |
| 41 | + |
| 42 | + const allowPkpAddressToDecrypt = getPkpAccessControlCondition(pkpAddress); |
| 43 | + |
| 44 | + const { litActionCode, litActionIpfsCid } = getLitActionCodeOrCidCommon( |
| 45 | + 'batchGenerateEncryptedKeys' |
| 46 | + ); |
| 47 | + |
| 48 | + const actionResults = await batchGenerateKeysWithLitAction({ |
| 49 | + litNodeClient: devEnv.litNodeClient, |
| 50 | + litActionIpfsCid: litActionCode ? undefined : litActionIpfsCid, |
| 51 | + litActionCode: litActionCode ? litActionCode : undefined, |
| 52 | + accessControlConditions: [allowPkpAddressToDecrypt], |
| 53 | + actions: [ |
| 54 | + { |
| 55 | + network: 'evm', |
| 56 | + signMessageParams: { messageToSign: evmMessageToSign }, |
| 57 | + generateKeyParams: { memo: 'Test evm key' }, |
| 58 | + }, |
| 59 | + { |
| 60 | + network: 'solana', |
| 61 | + signMessageParams: { messageToSign: solanaMessageToSign }, |
| 62 | + generateKeyParams: { memo: 'Test solana key' }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + pkpSessionSigs: pkpSessionSigsSigning, |
| 66 | + }); |
| 67 | + |
| 68 | + const keyParamsBatch = actionResults.map((keyData) => { |
| 69 | + const { generateEncryptedPrivateKey, network } = keyData; |
| 70 | + return { |
| 71 | + ...generateEncryptedPrivateKey, |
| 72 | + keyType: getKeyTypeFromNetwork(network), |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + // Intentional failure to persist due to missing publicKey |
| 77 | + delete keyParamsBatch[0].publicKey; |
| 78 | + |
| 79 | + try { |
| 80 | + await storeEncryptedKeyBatch({ |
| 81 | + pkpSessionSigs: pkpSessionSigsSigning, |
| 82 | + litNodeClient: devEnv.litNodeClient, |
| 83 | + keyBatch: keyParamsBatch, |
| 84 | + }); |
| 85 | + |
| 86 | + throw new Error( |
| 87 | + 'storeEncryptedKeyBatch() succeeded but we expected it to fail!' |
| 88 | + ); |
| 89 | + } catch (err) { |
| 90 | + // We expect `storeEncryptedKeyBatch` to fail w/ a specific error |
| 91 | + if ( |
| 92 | + err.message.includes( |
| 93 | + 'storeEncryptedKeyBatch() succeeded but we expected it to fail!' |
| 94 | + ) || |
| 95 | + !err.message.includes( |
| 96 | + 'keyParamsBatch[0]: Missing "publicKey" parameter in request' |
| 97 | + ) |
| 98 | + ) { |
| 99 | + throw err; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + const keys = await listEncryptedKeyMetadata({ |
| 104 | + litNodeClient: devEnv.litNodeClient, |
| 105 | + pkpSessionSigs: pkpSessionSigsSigning, |
| 106 | + }); |
| 107 | + |
| 108 | + console.error( |
| 109 | + 'Got a value back we shouldnt have from listEncryptedKeyMetadata()', |
| 110 | + keys |
| 111 | + ); |
| 112 | + |
| 113 | + throw new Error( |
| 114 | + 'Expected `listEncryptedKeyMetadata() to fail, but it didnt!`' |
| 115 | + ); |
| 116 | + } catch (err) { |
| 117 | + if (err.message.includes('No keys exist for pkpAddress')) { |
| 118 | + log('✅ testFailBatchGeneratePrivateKeysAtomic'); |
| 119 | + } else { |
| 120 | + throw err; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } catch (err) { |
| 125 | + console.log(err.message, err, err.stack); |
| 126 | + throw err; |
| 127 | + } finally { |
| 128 | + devEnv.releasePrivateKeyFromUser(alice); |
| 129 | + } |
| 130 | +}; |
0 commit comments