diff --git a/src/@types/Compute.ts b/src/@types/Compute.ts index 81142c7cb..5c6638b97 100644 --- a/src/@types/Compute.ts +++ b/src/@types/Compute.ts @@ -59,7 +59,17 @@ export interface ComputeOutput { whitelist?: string[] } +export enum EncryptMethod { + AES = 'AES', + ECIES = 'ECIES' +} +export interface BaseFileObject { + type: string + encryptedBy?: string + encryptMethod?: EncryptMethod +} export interface ComputeAsset { + fileObject?: BaseFileObject // C2D v2 documentId: string serviceId: string transferTxId?: string diff --git a/src/services/Provider.ts b/src/services/Provider.ts index fd899f73b..c717ad655 100644 --- a/src/services/Provider.ts +++ b/src/services/Provider.ts @@ -524,7 +524,7 @@ export class Provider { return consumeUrl } - /** Instruct the provider to start a compute job + /** Instruct the provider to start a compute job (Old C2D V1) Kept for now, for backwards compatibility * @param {string} providerUri The provider URI. * @param {Signer} signer The consumer signer object. * @param {string} computeEnv The compute environment. @@ -535,7 +535,7 @@ export class Provider { * @param {ComputeOutput} output The compute job output settings. * @return {Promise} The compute job or jobs. */ - public async computeStart( + public async computeStartV1( providerUri: string, consumer: Signer, computeEnv: string, @@ -607,6 +607,87 @@ export class Provider { return null } + /** Instruct the provider to start a compute job (new C2D V2) + * @param {string} providerUri The provider URI. + * @param {Signer} signer The consumer signer object. + * @param {string} computeEnv The compute environment. + * @param {ComputeAsset} datasets The dataset to start compute on + additionalDatasets (the additional datasets if that is the case) + * @param {ComputeAlgorithm} algorithm The algorithm to start compute with. + * @param {AbortSignal} signal abort signal + * @param {ComputeOutput} output The compute job output settings. + * @return {Promise} The compute job or jobs. + */ + public async computeStart( + providerUri: string, + consumer: Signer, + computeEnv: string, + datasets: ComputeAsset[], + algorithm: ComputeAlgorithm, + signal?: AbortSignal, + output?: ComputeOutput + ): Promise { + const providerEndpoints = await this.getEndpoints(providerUri) + const serviceEndpoints = await this.getServiceEndpoints( + providerUri, + providerEndpoints + ) + const computeStartUrl = this.getEndpointURL(serviceEndpoints, 'computeStart') + ? this.getEndpointURL(serviceEndpoints, 'computeStart').urlPath + : null + + const consumerAddress = await consumer.getAddress() + const nonce = ( + (await this.getNonce( + providerUri, + consumerAddress, + signal, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() + + let signatureMessage = consumerAddress + signatureMessage += datasets[0].documentId + signatureMessage += nonce + const signature = await this.signProviderRequest(consumer, signatureMessage) + const payload = Object() + payload.consumerAddress = consumerAddress + payload.signature = signature + payload.nonce = nonce + payload.environment = computeEnv + payload.datasets = datasets + payload.algorithm = algorithm + + if (output) payload.output = output + if (!computeStartUrl) return null + let response + try { + response = await fetch(computeStartUrl, { + method: 'POST', + body: JSON.stringify(payload), + headers: { 'Content-Type': 'application/json' }, + signal + }) + } catch (e) { + LoggerInstance.error('Compute start failed:') + LoggerInstance.error(e) + LoggerInstance.error('Payload was:', payload) + throw new Error('HTTP request failed calling Provider') + } + if (response?.ok) { + const params = await response.json() + return params + } + LoggerInstance.error( + 'Compute start failed: ', + response.status, + response.statusText, + await response.json() + ) + LoggerInstance.error('Payload was:', payload) + return null + } + /** Instruct the provider to Stop the execution of a to stop a compute job. * @param {string} did the asset did * @param {string} consumerAddress The consumer address.