Skip to content

Commit 0048290

Browse files
feat: add waitForResult option to processProtectedData
1 parent 7a5e5d5 commit 0048290

File tree

4 files changed

+380
-100
lines changed

4 files changed

+380
-100
lines changed

packages/sdk/src/lib/dataProtectorCore/IExecDataProtectorCore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class IExecDataProtectorCore extends IExecDataProtectorModule {
6868
return transferOwnership({ ...args, iexec: this.iexec });
6969
}
7070

71-
async processProtectedData(
72-
args: ProcessProtectedDataParams
73-
): Promise<ProcessProtectedDataResponse> {
71+
async processProtectedData<Params extends ProcessProtectedDataParams>(
72+
args: Params
73+
): Promise<ProcessProtectedDataResponse<Params>> {
7474
await this.init();
7575
await isValidProvider(this.iexec);
7676
return processProtectedData({

packages/sdk/src/lib/dataProtectorCore/processProtectedData.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ import { isAddressInWhitelist } from './smartContract/whitelistContract.read.js'
4343

4444
export type ProcessProtectedData = typeof processProtectedData;
4545

46-
export const processProtectedData = async ({
46+
export const processProtectedData = async <
47+
Params extends ProcessProtectedDataParams
48+
>({
4749
iexec = throwIfMissing(),
4850
defaultWorkerpool,
4951
protectedData,
@@ -61,10 +63,11 @@ export const processProtectedData = async ({
6163
voucherOwner,
6264
encryptResult = false,
6365
pemPrivateKey,
66+
waitForResult = true,
6467
onStatusUpdate = () => {},
65-
}: IExecConsumer &
66-
DefaultWorkerpoolConsumer &
67-
ProcessProtectedDataParams): Promise<ProcessProtectedDataResponse> => {
68+
}: IExecConsumer & DefaultWorkerpoolConsumer & Params): Promise<
69+
ProcessProtectedDataResponse<Params>
70+
> => {
6871
const vProtectedData = addressOrEnsSchema()
6972
.required()
7073
.label('protectedData')
@@ -107,6 +110,9 @@ export const processProtectedData = async ({
107110
const vPemPrivateKey = stringSchema()
108111
.label('pemPrivateKey')
109112
.validateSync(pemPrivateKey);
113+
const vWaitForResult = booleanSchema()
114+
.label('waitForResult')
115+
.validateSync(waitForResult);
110116

111117
// Validate that if pemPrivateKey is provided, encryptResult must be true
112118
if (vPemPrivateKey && !vEncryptResult) {
@@ -282,37 +288,39 @@ export const processProtectedData = async ({
282288
isDone: true,
283289
});
284290

285-
vOnStatusUpdate({
286-
title: 'GENERATE_ENCRYPTION_KEY',
287-
isDone: false,
288-
});
289-
290291
// Handle result encryption
291292
let privateKey: string | undefined;
292293
if (vEncryptResult) {
294+
if (!vPemPrivateKey) {
295+
vOnStatusUpdate({
296+
title: 'GENERATE_ENCRYPTION_KEY',
297+
isDone: false,
298+
});
299+
}
293300
const { publicKey, privateKey: generatedPrivateKey } =
294301
await getFormattedKeyPair({
295302
pemPrivateKey: vPemPrivateKey,
296303
});
297304
privateKey = generatedPrivateKey;
298-
299305
// Notify user if a new key was generated
300306
if (!vPemPrivateKey) {
301307
vOnStatusUpdate({
302308
title: 'GENERATE_ENCRYPTION_KEY',
303309
isDone: true,
304310
payload: {
305-
message: 'New encryption key pair generated',
306311
pemPrivateKey: generatedPrivateKey,
307312
},
308313
});
309-
} else {
310-
vOnStatusUpdate({
311-
title: 'PUSH_ENCRYPTION_KEY',
312-
isDone: false,
313-
});
314314
}
315315

316+
vOnStatusUpdate({
317+
title: 'PUSH_ENCRYPTION_KEY',
318+
isDone: false,
319+
payload: {
320+
publicKey,
321+
},
322+
});
323+
316324
await iexec.result.pushResultEncryptionKey(publicKey, {
317325
forceUpdate: true,
318326
});
@@ -375,6 +383,15 @@ export const processProtectedData = async ({
375383
},
376384
});
377385

386+
if (vWaitForResult === false) {
387+
return {
388+
txHash: txHash,
389+
dealId: dealid,
390+
taskId,
391+
...(privateKey ? { pemPrivateKey: privateKey } : {}),
392+
} as ProcessProtectedDataResponse<Params>;
393+
}
394+
378395
vOnStatusUpdate({
379396
title: 'CONSUME_TASK',
380397
isDone: false,
@@ -415,7 +432,7 @@ export const processProtectedData = async ({
415432
taskId,
416433
result,
417434
...(privateKey ? { pemPrivateKey: privateKey } : {}),
418-
};
435+
} as ProcessProtectedDataResponse<Params>;
419436
} catch (error) {
420437
console.error('[processProtectedData] ERROR', error);
421438
handleIfProtocolError(error);

packages/sdk/src/lib/types/coreTypes.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,31 @@ export type ProcessProtectedDataParams = {
369369
* If not provided and encryptResult is true, a new key pair will be generated.
370370
*/
371371
pemPrivateKey?: string;
372+
373+
/**
374+
* Whether to wait for the result of the processing or not.
375+
* @default = true
376+
*/
377+
waitForResult?: boolean;
378+
372379
/**
373380
* Callback function that will get called at each step of the process
374381
*/
375382
onStatusUpdate?: OnStatusUpdateFn<ProcessProtectedDataStatuses>;
376383
};
377384

378-
export type ProcessProtectedDataResponse = {
385+
export type ProcessProtectedDataResponseBase = {
379386
txHash: string;
380387
dealId: string;
381388
taskId: string;
382-
result: ArrayBuffer;
383389
pemPrivateKey?: string;
384390
};
391+
392+
export type ProcessProtectedDataResponseWithResult =
393+
ProcessProtectedDataResponseBase & {
394+
result: ArrayBuffer;
395+
};
396+
397+
export type ProcessProtectedDataResponse<T> = T extends { waitForResult: false }
398+
? ProcessProtectedDataResponseBase
399+
: ProcessProtectedDataResponseWithResult;

0 commit comments

Comments
 (0)