Skip to content

Commit 27c0844

Browse files
feat(wrapped-keys): LIT-3956 - Initial implementation of new storeEncryptedKeyBatch() client API method and associated types
1 parent 1595610 commit 27c0844

File tree

7 files changed

+143
-3
lines changed

7 files changed

+143
-3
lines changed

packages/wrapped-keys/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
storeEncryptedKey,
99
listEncryptedKeyMetadata,
1010
batchGeneratePrivateKeys,
11+
storeEncryptedKeyBatch,
1112
} from './lib/api';
1213
import {
1314
CHAIN_ETHEREUM,
@@ -45,11 +46,13 @@ import type {
4546
SignTransactionParamsSupportedEvm,
4647
SignTransactionParamsSupportedSolana,
4748
StoreEncryptedKeyParams,
49+
StoreEncryptedKeyBatchParams,
4850
StoredKeyData,
4951
StoredKeyMetadata,
5052
ListEncryptedKeyMetadataParams,
5153
StoreEncryptedKeyResult,
5254
ImportPrivateKeyResult,
55+
StoreEncryptedKeyBatchResult,
5356
} from './lib/types';
5457

5558
export const constants = {
@@ -70,6 +73,7 @@ export const api = {
7073
signMessageWithEncryptedKey,
7174
signTransactionWithEncryptedKey,
7275
storeEncryptedKey,
76+
storeEncryptedKeyBatch,
7377
batchGeneratePrivateKeys,
7478
};
7579

@@ -101,6 +105,8 @@ export {
101105
SignTransactionWithEncryptedKeyParams,
102106
StoreEncryptedKeyParams,
103107
StoreEncryptedKeyResult,
108+
StoreEncryptedKeyBatchParams,
109+
StoreEncryptedKeyBatchResult,
104110
StoredKeyData,
105111
StoredKeyMetadata,
106112
SupportedNetworks,

packages/wrapped-keys/src/lib/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { listEncryptedKeyMetadata } from './list-encrypted-key-metadata';
77
import { signMessageWithEncryptedKey } from './sign-message-with-encrypted-key';
88
import { signTransactionWithEncryptedKey } from './sign-transaction-with-encrypted-key';
99
import { storeEncryptedKey } from './store-encrypted-key';
10+
import { storeEncryptedKeyBatch } from './store-encrypted-key-batch';
1011

1112
export {
1213
listEncryptedKeyMetadata,
@@ -16,6 +17,7 @@ export {
1617
exportPrivateKey,
1718
signMessageWithEncryptedKey,
1819
storeEncryptedKey,
20+
storeEncryptedKeyBatch,
1921
getEncryptedKey,
2022
batchGeneratePrivateKeys,
2123
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { storePrivateKeyBatch } from '../service-client';
2+
import { StoreKeyBatchParams } from '../service-client/types';
3+
import {
4+
StoredKeyData,
5+
StoreEncryptedKeyBatchParams,
6+
StoreEncryptedKeyBatchResult,
7+
} from '../types';
8+
import { getFirstSessionSig, getPkpAddressFromSessionSig } from '../utils';
9+
10+
/** Stores a batch of encrypted private keys and their metadata to the wrapped keys backend service
11+
*
12+
* @param { StoreEncryptedKeyParams } params Parameters required to fetch the encrypted private key metadata
13+
* @returns { Promise<StoreEncryptedKeyResult> } The encrypted private key and its associated metadata
14+
*/
15+
export async function storeEncryptedKeyBatch(
16+
params: StoreEncryptedKeyBatchParams
17+
): Promise<StoreEncryptedKeyBatchResult> {
18+
const { pkpSessionSigs, litNodeClient, keyBatch } = params;
19+
const sessionSig = getFirstSessionSig(pkpSessionSigs);
20+
const pkpAddress = getPkpAddressFromSessionSig(sessionSig);
21+
22+
const storedKeyMetadataBatch: StoreKeyBatchParams['storedKeyMetadataBatch'] =
23+
keyBatch.map(
24+
({
25+
keyType,
26+
publicKey,
27+
memo,
28+
dataToEncryptHash,
29+
ciphertext,
30+
}): Pick<
31+
StoredKeyData,
32+
| 'pkpAddress'
33+
| 'publicKey'
34+
| 'keyType'
35+
| 'dataToEncryptHash'
36+
| 'ciphertext'
37+
| 'memo'
38+
> => ({
39+
pkpAddress,
40+
publicKey,
41+
memo,
42+
dataToEncryptHash,
43+
ciphertext,
44+
keyType,
45+
})
46+
);
47+
48+
return storePrivateKeyBatch({
49+
storedKeyMetadataBatch,
50+
sessionSig: getFirstSessionSig(pkpSessionSigs),
51+
litNetwork: litNodeClient.config.litNetwork,
52+
});
53+
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { FetchKeyParams, ListKeysParams, StoreKeyParams } from './types';
1+
import {
2+
FetchKeyParams,
3+
ListKeysParams,
4+
StoreKeyBatchParams,
5+
StoreKeyParams,
6+
} from './types';
27
import { generateRequestId, getBaseRequestParams, makeRequest } from './utils';
38
import {
49
StoredKeyData,
510
StoredKeyMetadata,
11+
StoreEncryptedKeyBatchResult,
612
StoreEncryptedKeyResult,
713
} from '../types';
814
import { getPkpAddressFromSessionSig } from '../utils';
@@ -92,3 +98,33 @@ export async function storePrivateKey(
9298

9399
return { pkpAddress, id };
94100
}
101+
102+
/** Stores a batch of up to 25 private key metadata into the wrapped keys service backend
103+
*
104+
* @param { StoreKeyParams } params Parameters required to store the private key metadata
105+
* @returns { Promise<StoreEncryptedKeyResult> } `true` on successful write to the service. Otherwise, this method throws an error.
106+
*/
107+
export async function storePrivateKeyBatch(
108+
params: StoreKeyBatchParams
109+
): Promise<StoreEncryptedKeyBatchResult> {
110+
const { litNetwork, sessionSig, storedKeyMetadataBatch } = params;
111+
112+
const requestId = generateRequestId();
113+
const { baseUrl, initParams } = getBaseRequestParams({
114+
litNetwork,
115+
sessionSig,
116+
method: 'POST',
117+
requestId,
118+
});
119+
120+
const { pkpAddress, ids } = await makeRequest<StoreEncryptedKeyBatchResult>({
121+
url: baseUrl,
122+
init: {
123+
...initParams,
124+
body: JSON.stringify({ keyParamsBatch: storedKeyMetadataBatch }),
125+
},
126+
requestId,
127+
});
128+
129+
return { pkpAddress, ids };
130+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import {
22
fetchPrivateKey,
33
storePrivateKey,
4+
storePrivateKeyBatch,
45
listPrivateKeyMetadata,
56
} from './client';
67

7-
export { fetchPrivateKey, storePrivateKey, listPrivateKeyMetadata };
8+
export {
9+
fetchPrivateKey,
10+
storePrivateKey,
11+
storePrivateKeyBatch,
12+
listPrivateKeyMetadata,
13+
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ export interface StoreKeyParams extends BaseApiParams {
3030
>;
3131
}
3232

33+
export interface StoreKeyBatchParams extends BaseApiParams {
34+
storedKeyMetadataBatch: Pick<
35+
StoredKeyData,
36+
| 'pkpAddress'
37+
| 'publicKey'
38+
| 'keyType'
39+
| 'dataToEncryptHash'
40+
| 'ciphertext'
41+
| 'memo'
42+
>[];
43+
}
44+
3345
export interface BaseRequestParams {
3446
sessionSig: AuthSig;
3547
method: 'GET' | 'POST';

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface StoredKeyData extends StoredKeyMetadata {
7777
dataToEncryptHash: string;
7878
}
7979

80-
/** Fetching a previously persisted key's metadata only requires valid pkpSessionSigs and a LIT Node Client instance configured for the appropriate network.
80+
/** Properties required to persist an encrypted key into the wrapped-keys backend storage service
8181
*
8282
* @typedef StoreEncryptedKeyParams
8383
* @extends BaseApiParams
@@ -101,6 +101,31 @@ export interface StoreEncryptedKeyResult {
101101
pkpAddress: string;
102102
}
103103

104+
/** Properties required to persist a batch of encrypted keys into the wrapped-keys backend storage service
105+
*
106+
* @typedef StoreEncryptedKeyBatchParams
107+
* @extends BaseApiParams
108+
*
109+
*/
110+
export type StoreEncryptedKeyBatchParams = BaseApiParams & {
111+
keyBatch: Pick<
112+
StoredKeyData,
113+
'publicKey' | 'keyType' | 'dataToEncryptHash' | 'ciphertext' | 'memo'
114+
>[];
115+
};
116+
117+
/** Result of storing a batch of private keys in the wrapped keys backend service
118+
* Includes an array of unique identifiers, which are necessary to get the encrypted ciphertext and dataToEncryptHash in the future
119+
*
120+
* @typedef StoreEncryptedKeyBatchResult
121+
* @property { string } pkpAddress The LIT PKP Address that the key was linked to; this is derived from the provided pkpSessionSigs
122+
* @property { string[] } ids Array of the unique identifiers (UUID V4) of the encrypted private keys in the same order provided
123+
*/
124+
export interface StoreEncryptedKeyBatchResult {
125+
ids: string[];
126+
pkpAddress: string;
127+
}
128+
104129
/** Exporting a previously persisted key only requires valid pkpSessionSigs and a LIT Node Client instance configured for the appropriate network.
105130
*
106131
* @typedef ExportPrivateKeyParams

0 commit comments

Comments
 (0)