Skip to content

Commit ff03458

Browse files
test(wrapped-keys): LIT-3956 - Add testFailStoreEncryptedKeyBatchIsAtomic test case
1 parent d16efa9 commit ff03458

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

local-tests/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import { testExportWrappedKey } from './tests/wrapped-keys/testExportWrappedKey'
105105
import { testSignMessageWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignMessageWithSolanaEncryptedKey';
106106
import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignTransactionWithSolanaEncryptedKey';
107107
import { testBatchGeneratePrivateKeys } from './tests/wrapped-keys/testBatchGeneratePrivateKeys';
108+
import { testFailBatchGeneratePrivateKeysAtomic } from './tests/wrapped-keys/testFailStoreEncryptedKeyBatchIsAtomic';
108109

109110
import { setLitActionsCodeToLocal } from './tests/wrapped-keys/util';
110111
import { testUseEoaSessionSigsToRequestSingleResponse } from './tests/testUseEoaSessionSigsToRequestSingleResponse';
@@ -151,6 +152,7 @@ setLitActionsCodeToLocal();
151152
testFailEthereumSignTransactionWrappedKeyWithMissingParam,
152153
testFailEthereumSignTransactionWrappedKeyWithInvalidParam,
153154
testFailEthereumSignTransactionWrappedKeyInvalidDecryption,
155+
testFailBatchGeneratePrivateKeysAtomic,
154156

155157
// -- import wrapped keys
156158
testFailImportWrappedKeysWithSamePrivateKey,

local-tests/tests/wrapped-keys/testBatchGeneratePrivateKeys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import bs58 from 'bs58';
77
import { ethers } from 'ethers';
88
import { BatchGeneratePrivateKeysActionResult } from '../../../packages/wrapped-keys/src/lib/types';
99

10-
const { batchGeneratePrivateKeys, exportPrivateKey } = api;
10+
const { batchGeneratePrivateKeys } = api;
1111

1212
async function verifySolanaSignature(
1313
solanaResult: BatchGeneratePrivateKeysActionResult,
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)