@@ -15,6 +15,7 @@ import {
1515 filterWorkerpoolOrders ,
1616} from '../../utils/processProtectedData.models.js' ;
1717import { pushRequesterSecret } from '../../utils/pushRequesterSecret.js' ;
18+ import { getFormattedKeyPair } from '../../utils/rsa.js' ;
1819import {
1920 addressOrEnsSchema ,
2021 addressSchema ,
@@ -58,6 +59,8 @@ export const processProtectedData = async ({
5859 workerpool,
5960 useVoucher = false ,
6061 voucherOwner,
62+ encryptResult = false ,
63+ pemPrivateKey,
6164 onStatusUpdate = ( ) => { } ,
6265} : IExecConsumer &
6366 DefaultWorkerpoolConsumer &
@@ -98,6 +101,19 @@ export const processProtectedData = async ({
98101 const vVoucherOwner = addressOrEnsSchema ( )
99102 . label ( 'voucherOwner' )
100103 . validateSync ( voucherOwner ) ;
104+ const vEncryptResult = booleanSchema ( )
105+ . label ( 'encryptResult' )
106+ . validateSync ( encryptResult ) ;
107+ const vPemPrivateKey = stringSchema ( )
108+ . label ( 'pemPrivateKey' )
109+ . validateSync ( pemPrivateKey ) ;
110+
111+ // Validate that if pemPrivateKey is provided, encryptResult must be true
112+ if ( vPemPrivateKey && ! vEncryptResult ) {
113+ throw new Error (
114+ 'pemPrivateKey can only be provided when encryptResult is true'
115+ ) ;
116+ }
101117 try {
102118 const vOnStatusUpdate =
103119 validateOnStatusUpdateCallback <
@@ -263,6 +279,45 @@ export const processProtectedData = async ({
263279 isDone : true ,
264280 } ) ;
265281
282+ // Handle result encryption
283+ let privateKey : string | undefined ;
284+ if ( vEncryptResult ) {
285+ const { publicKey, privateKey : generatedPrivateKey } =
286+ await getFormattedKeyPair ( {
287+ pemPrivateKey : vPemPrivateKey ,
288+ } ) ;
289+ privateKey = generatedPrivateKey ;
290+
291+ // Notify user if a new key was generated
292+ if ( ! vPemPrivateKey ) {
293+ vOnStatusUpdate ( {
294+ title : 'PUSH_ENCRYPTION_KEY' ,
295+ isDone : false ,
296+ payload : {
297+ message :
298+ 'New encryption key pair generated and stored in IndexedDB' ,
299+ } ,
300+ } ) ;
301+ } else {
302+ vOnStatusUpdate ( {
303+ title : 'PUSH_ENCRYPTION_KEY' ,
304+ isDone : false ,
305+ } ) ;
306+ }
307+
308+ await iexec . result . pushResultEncryptionKey ( publicKey , {
309+ forceUpdate : true ,
310+ } ) ;
311+
312+ vOnStatusUpdate ( {
313+ title : 'PUSH_ENCRYPTION_KEY' ,
314+ isDone : true ,
315+ payload : {
316+ publicKey,
317+ } ,
318+ } ) ;
319+ }
320+
266321 vOnStatusUpdate ( {
267322 title : 'REQUEST_TO_PROCESS_PROTECTED_DATA' ,
268323 isDone : false ,
@@ -280,6 +335,7 @@ export const processProtectedData = async ({
280335 iexec_input_files : vInputFiles ,
281336 iexec_secrets : secretsId ,
282337 iexec_args : vArgs ,
338+ ...( vEncryptResult ? { iexec_result_encryption : true } : { } ) ,
283339 } ,
284340 } ) ;
285341 const requestorder = await iexec . order . signRequestorder ( requestorderToSign ) ;
@@ -341,6 +397,7 @@ export const processProtectedData = async ({
341397 iexec,
342398 taskId,
343399 path : vPath ,
400+ pemPrivateKey : privateKey ,
344401 onStatusUpdate : vOnStatusUpdate ,
345402 } ) ;
346403
@@ -349,6 +406,7 @@ export const processProtectedData = async ({
349406 dealId : dealid ,
350407 taskId,
351408 result,
409+ ...( privateKey ? { pemPrivateKey : privateKey } : { } ) ,
352410 } ;
353411 } catch ( error ) {
354412 console . error ( '[processProtectedData] ERROR' , error ) ;
0 commit comments