From 1f6e57fb603a63e91f8584506e95695e52a393af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Thu, 5 Sep 2019 13:32:56 +0200 Subject: [PATCH] Add ComputeExecutionCondition. --- src/keeper/Keeper.ts | 11 ++++- .../conditions/ComputeExecutionCondition.ts | 44 +++++++++++++++++++ src/keeper/contracts/conditions/index.ts | 1 + src/ocean/OceanAgreementsConditions.ts | 28 ++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/keeper/contracts/conditions/ComputeExecutionCondition.ts diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 6e729d5..0ab24d8 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -7,7 +7,8 @@ import { Condition, LockRewardCondition, EscrowReward, - AccessSecretStoreCondition + AccessSecretStoreCondition, + ComputeExecutionCondition } from './contracts/conditions' import { AgreementTemplate, @@ -66,6 +67,9 @@ export class Keeper extends Instantiable { accessSecretStoreCondition: AccessSecretStoreCondition.getInstance( config ), + computeExecutionCondition: ComputeExecutionCondition.getInstance( + config + ), // Templates escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance( config @@ -98,7 +102,9 @@ export class Keeper extends Instantiable { lockRewardCondition: keeper.instances.lockRewardCondition, escrowReward: keeper.instances.escrowReward, accessSecretStoreCondition: - keeper.instances.accessSecretStoreCondition + keeper.instances.accessSecretStoreCondition, + computeExecutionCondition: + keeper.instances.computeExecutionCondition } // Conditions keeper.templates = { @@ -163,6 +169,7 @@ export class Keeper extends Instantiable { lockRewardCondition: LockRewardCondition escrowReward: EscrowReward accessSecretStoreCondition: AccessSecretStoreCondition + computeExecutionCondition: ComputeExecutionCondition } /** diff --git a/src/keeper/contracts/conditions/ComputeExecutionCondition.ts b/src/keeper/contracts/conditions/ComputeExecutionCondition.ts new file mode 100644 index 0000000..24bf07c --- /dev/null +++ b/src/keeper/contracts/conditions/ComputeExecutionCondition.ts @@ -0,0 +1,44 @@ +import { Condition } from './Condition.abstract' +import { zeroX, didZeroX, didPrefixed } from '../../../utils' +import { InstantiableConfig } from '../../../Instantiable.abstract' + +export class ComputeExecutionCondition extends Condition { + public static async getInstance( + config: InstantiableConfig + ): Promise { + return Condition.getInstance( + config, + 'ComputeExecutionCondition', + ComputeExecutionCondition + ) + } + + public hashValues(did: string, computeConsumer: string) { + return super.hashValues(didZeroX(did), zeroX(computeConsumer)) + } + + public fulfill( + agreementId: string, + did: string, + computeConsumer: string, + from?: string + ) { + return super.fulfill( + agreementId, + [didZeroX(did), computeConsumer].map(zeroX), + from + ) + } + + public wasComputeTriggered( + did: string, + computeConsumer: string, + from?: string + ) { + return this.call( + 'wasComputeTriggered', + [didZeroX(did), computeConsumer].map(zeroX), + from + ) + } +} diff --git a/src/keeper/contracts/conditions/index.ts b/src/keeper/contracts/conditions/index.ts index aac7d24..7f78dab 100644 --- a/src/keeper/contracts/conditions/index.ts +++ b/src/keeper/contracts/conditions/index.ts @@ -2,3 +2,4 @@ export * from './Condition.abstract' export { AccessSecretStoreCondition } from './AccessSecretStoreCondition' export { EscrowReward } from './EscrowReward' export { LockRewardCondition } from './LockRewardCondition' +export { ComputeExecutionCondition } from './ComputeExecutionCondition' diff --git a/src/ocean/OceanAgreementsConditions.ts b/src/ocean/OceanAgreementsConditions.ts index 9643bb5..3e9bed7 100644 --- a/src/ocean/OceanAgreementsConditions.ts +++ b/src/ocean/OceanAgreementsConditions.ts @@ -83,6 +83,34 @@ export class OceanAgreementsConditions extends Instantiable { } } + /** + * Authorize the consumer defined in the agreement to execute a remote service associated with this asset. + * @param {string} agreementId Agreement ID. + * @param {string} did Asset ID. + * @param {string} grantee Consumer address. + * @param {Account} from Account of sender. + */ + public async grantServiceExecution( + agreementId: string, + did: string, + grantee: string, + from?: Account + ) { + try { + const { computeExecutionCondition } = this.ocean.keeper.conditions + + const receipt = await computeExecutionCondition.fulfill( + agreementId, + did, + grantee, + from && from.getId() + ) + return !!receipt.events.Fulfilled + } catch { + return false + } + } + /** * Transfer the escrow or locked tokens from the LockRewardCondition contract to the publisher's account. * This should be allowed after access has been given to the consumer and the asset data is downloaded.