Skip to content

Commit 85495f6

Browse files
feat(wrapped-keys): LIT-3956 - Use new service method storePrivateKeyBatch() to write keys to store backend in batchGeneratePrivateKeys() client API
1 parent 27c0844 commit 85495f6

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

packages/wrapped-keys/src/lib/api/batch-generate-private-keys.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getKeyTypeFromNetwork } from './utils';
22
import { batchGenerateKeysWithLitAction } from '../lit-actions-client';
33
import { getLitActionCodeOrCidCommon } from '../lit-actions-client/utils';
4-
import { storePrivateKey } from '../service-client';
4+
import { storePrivateKeyBatch } from '../service-client';
55
import {
66
BatchGeneratePrivateKeysActionResult,
77
BatchGeneratePrivateKeysParams,
@@ -41,34 +41,40 @@ export async function batchGeneratePrivateKeys(
4141
pkpSessionSigs,
4242
});
4343

44-
const results = await Promise.all(
45-
actionResults.map(
46-
async (result): Promise<BatchGeneratePrivateKeysActionResult> => {
47-
const { generateEncryptedPrivateKey, network } = result;
44+
const keyParamsBatch = actionResults.map((keyData) => {
45+
const { generateEncryptedPrivateKey, network } = keyData;
46+
return {
47+
...generateEncryptedPrivateKey,
48+
keyType: getKeyTypeFromNetwork(network),
49+
};
50+
});
51+
52+
const { ids } = await storePrivateKeyBatch({
53+
pkpAddress,
54+
sessionSig,
55+
storedKeyMetadataBatch: keyParamsBatch,
56+
litNetwork: litNodeClient.config.litNetwork,
57+
});
4858

49-
const signature = result.signMessage?.signature;
59+
const results = actionResults.map(
60+
(actionResult, ndx): BatchGeneratePrivateKeysActionResult => {
61+
const {
62+
generateEncryptedPrivateKey: { memo, publicKey },
63+
} = actionResult;
64+
const id = ids[ndx]; // Result of writes is in same order as provided
5065

51-
const { id } = await storePrivateKey({
52-
sessionSig,
53-
storedKeyMetadata: {
54-
...generateEncryptedPrivateKey,
55-
keyType: getKeyTypeFromNetwork(network),
56-
pkpAddress,
57-
},
58-
litNetwork: litNodeClient.config.litNetwork,
59-
});
66+
const signature = actionResult.signMessage?.signature;
6067

61-
return {
62-
...(signature ? { signMessage: { signature } } : {}),
63-
generateEncryptedPrivateKey: {
64-
memo: generateEncryptedPrivateKey.memo,
65-
id,
66-
generatedPublicKey: generateEncryptedPrivateKey.publicKey,
67-
pkpAddress,
68-
},
69-
};
70-
}
71-
)
68+
return {
69+
...(signature ? { signMessage: { signature } } : {}),
70+
generateEncryptedPrivateKey: {
71+
memo: memo,
72+
id,
73+
generatedPublicKey: publicKey,
74+
pkpAddress,
75+
},
76+
};
77+
}
7278
);
7379

7480
return { pkpAddress, results };

packages/wrapped-keys/src/lib/api/store-encrypted-key-batch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export async function storeEncryptedKeyBatch(
4646
);
4747

4848
return storePrivateKeyBatch({
49+
pkpAddress,
4950
storedKeyMetadataBatch,
5051
sessionSig: getFirstSessionSig(pkpSessionSigs),
5152
litNetwork: litNodeClient.config.litNetwork,

packages/wrapped-keys/src/lib/service-client/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ export interface StoreKeyParams extends BaseApiParams {
3131
}
3232

3333
export interface StoreKeyBatchParams extends BaseApiParams {
34+
pkpAddress: string;
3435
storedKeyMetadataBatch: Pick<
3536
StoredKeyData,
36-
| 'pkpAddress'
37-
| 'publicKey'
38-
| 'keyType'
39-
| 'dataToEncryptHash'
40-
| 'ciphertext'
41-
| 'memo'
37+
'publicKey' | 'keyType' | 'dataToEncryptHash' | 'ciphertext' | 'memo'
4238
>[];
4339
}
4440

0 commit comments

Comments
 (0)