@@ -3,13 +3,15 @@ import {
33 ListKeysParams ,
44 StoreKeyBatchParams ,
55 StoreKeyParams ,
6+ UpdateKeyParams ,
67} from './types' ;
78import { generateRequestId , getBaseRequestParams , makeRequest } from './utils' ;
89import {
910 StoredKeyData ,
1011 StoredKeyMetadata ,
1112 StoreEncryptedKeyBatchResult ,
1213 StoreEncryptedKeyResult ,
14+ UpdateEncryptedKeyResult ,
1315} from '../types' ;
1416
1517/** Fetches previously stored private key metadata from the wrapped keys service.
@@ -48,7 +50,7 @@ export async function listPrivateKeyMetadata(
4850export async function fetchPrivateKey (
4951 params : FetchKeyParams
5052) : Promise < StoredKeyData > {
51- const { litNetwork, sessionSig, id, pkpAddress } = params ;
53+ const { litNetwork, sessionSig, id, pkpAddress, includeVersions } = params ;
5254
5355 const requestId = generateRequestId ( ) ;
5456 const { baseUrl, initParams } = getBaseRequestParams ( {
@@ -58,8 +60,9 @@ export async function fetchPrivateKey(
5860 requestId,
5961 } ) ;
6062
63+ const query = includeVersions ? '?includeVersions=true' : '' ;
6164 return makeRequest < StoredKeyData > ( {
62- url : `${ baseUrl } /${ pkpAddress } /${ id } ` ,
65+ url : `${ baseUrl } /${ pkpAddress } /${ id } ${ query } ` ,
6366 init : initParams ,
6467 requestId,
6568 } ) ;
@@ -124,3 +127,45 @@ export async function storePrivateKeyBatch(
124127
125128 return { pkpAddress, ids } ;
126129}
130+
131+ /** Updates an existing wrapped key and appends prior state to versions.
132+ *
133+ * @param { UpdateKeyParams } params Parameters required to update the private key metadata
134+ * @returns { Promise<UpdateEncryptedKeyResult> } id/pkpAddress/updatedAt on successful update
135+ */
136+ export async function updatePrivateKey (
137+ params : UpdateKeyParams
138+ ) : Promise < UpdateEncryptedKeyResult > {
139+ const {
140+ litNetwork,
141+ sessionSig,
142+ pkpAddress,
143+ id,
144+ ciphertext,
145+ evmContractConditions,
146+ memo,
147+ } = params ;
148+
149+ const requestId = generateRequestId ( ) ;
150+ const { baseUrl, initParams } = getBaseRequestParams ( {
151+ litNetwork,
152+ sessionSig,
153+ method : 'PUT' ,
154+ requestId,
155+ } ) ;
156+
157+ return makeRequest < UpdateEncryptedKeyResult > ( {
158+ url : `${ baseUrl } /${ pkpAddress } /${ id } ` ,
159+ init : {
160+ ...initParams ,
161+ body : JSON . stringify ( {
162+ ciphertext,
163+ ...( evmContractConditions !== undefined
164+ ? { evmContractConditions }
165+ : { } ) ,
166+ ...( memo !== undefined ? { memo } : { } ) ,
167+ } ) ,
168+ } ,
169+ requestId,
170+ } ) ;
171+ }
0 commit comments