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