diff --git a/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts index 08c6b83..c68994b 100644 --- a/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts +++ b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts @@ -164,12 +164,13 @@ describe("Register Escrow Access Secret Store Template", () => { }) describe("Short flow", () => { - const did = `0x${utils.generateId()}` + const did = utils.generateId() let agreementId it("should register a DID", async () => { - await keeper.didRegistry.registerAttribute(did.replace("0x", ""), checksum, url, publisher.getId()) + // This part is executed inside Ocean.assets.create() + await keeper.didRegistry.registerAttribute(did, checksum, url, publisher.getId()) }) it("should create a new agreement (short way)", async () => { diff --git a/src/ddo/Service.ts b/src/ddo/Service.ts index 96ac253..83c696e 100644 --- a/src/ddo/Service.ts +++ b/src/ddo/Service.ts @@ -1,5 +1,3 @@ -import { Condition } from "./Condition" -import { Contract } from "./Contract" import { MetaData } from "./MetaData" import { ServiceAgreementTemplate } from "./ServiceAgreementTemplate" diff --git a/src/ddo/ServiceAgreementTemplate.ts b/src/ddo/ServiceAgreementTemplate.ts index 777dddc..2985066 100644 --- a/src/ddo/ServiceAgreementTemplate.ts +++ b/src/ddo/ServiceAgreementTemplate.ts @@ -1,7 +1,7 @@ export interface ServiceAgreementTemplateParameter { name: string type: string - value: string + value: string | number } export interface ServiceAgreementTemplateEvent { diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index b572210..40de5f5 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -2,7 +2,7 @@ import DIDRegistry from "./contracts/DIDRegistry" import Dispenser from "./contracts/Dispenser" import OceanToken from "./contracts/Token" import { Condition, LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from "./contracts/conditions" -import { EscrowAccessSecretStoreTemplate } from "./contracts/templates" +import { AgreementTemplate, EscrowAccessSecretStoreTemplate } from "./contracts/templates" import { TemplateStoreManager } from "./contracts/managers" import Web3Provider from "./Web3Provider" @@ -104,6 +104,11 @@ export class Keeper { .find(condition => condition.getAddress() === address) } + public getTemplateByName(name: string): AgreementTemplate { + return Object.values(this.templates) + .find(template => template.contractName === name) + } + /** * Returns the network by name. * @return {Promise} Network name. diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index 719581a..9a8aa6e 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -9,7 +9,7 @@ export default abstract class ContractBase { protected static instance = null private contract: Contract = null - private contractName: string + contractName: string constructor(contractName) { this.contractName = contractName diff --git a/src/keeper/contracts/conditions/Condition.abstract.ts b/src/keeper/contracts/conditions/Condition.abstract.ts index 3f7c900..e94ca7b 100644 --- a/src/keeper/contracts/conditions/Condition.abstract.ts +++ b/src/keeper/contracts/conditions/Condition.abstract.ts @@ -39,6 +39,6 @@ export abstract class Condition extends ContractBase { } abortByTimeOut(agreementId: string, from?: string) { - return this.sendFrom("requestTokens", [zeroX(agreementId)], from) + return this.sendFrom("abortByTimeOut", [zeroX(agreementId)], from) } } diff --git a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts index 659c7bb..a7a7ef5 100644 --- a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts +++ b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts @@ -1,6 +1,7 @@ import ContractBase from "../ContractBase" import { Condition } from "../conditions/Condition.abstract" import Keeper from "../../Keeper" +import { DDO } from '../../../ddo/DDO' import { ServiceAgreementTemplate, ServiceAgreementTemplateCondition } from '../../../ddo/ServiceAgreementTemplate' import { zeroX } from "../../../utils" @@ -49,9 +50,10 @@ export abstract class AgreementTemplate extends ContractBase { const keeper = await Keeper.getInstance() return (await this.getConditionTypes()) .map(address => keeper.getConditionByAddress(address)) - } + abstract async getServiceAgreementTemplateValuesMap(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> + abstract getServiceAgreementTemplate(): Promise public async getServiceAgreementTemplateConditions(): Promise { diff --git a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts index 53fef2c..fc01e3d 100644 --- a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts +++ b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts @@ -1,7 +1,9 @@ import { AgreementTemplate } from "./AgreementTemplate.abstract" import { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from '../conditions' import DIDRegistry from '../DIDRegistry' -import { generateId } from '../../../utils/GeneratorHelpers' +import Keeper from "../../Keeper" +import { DDO } from '../../../ddo/DDO' +import { generateId, zeroX } from '../../../utils' import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from "./EscrowAccessSecretStoreTemplate.serviceAgreementTemplate" @@ -62,7 +64,7 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate { const lockRewardCondition = await LockRewardCondition.getInstance() const escrowReward = await EscrowReward.getInstance() - const agreementId = `0x${generateId()}` + const agreementId = zeroX(generateId()) const publisher = await didRegistry.getDIDOwner(did) const conditionIdAccess = await accessSecretStoreCondition.generateIdHash(agreementId, did, from) @@ -87,4 +89,33 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate { return agreementId } + + + async getServiceAgreementTemplateValuesMap(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> { + const keeper = await Keeper.getInstance() + const ddoOwner = ddo.proof && ddo.proof.creator + const amount = ddo.findServiceByType("Metadata").metadata.base.price + + let lockCondition + let releaseCondition + + try { + lockCondition = await keeper.conditions.lockRewardCondition.hashValues(ddoOwner, amount) + } catch(e) { } + + try { + releaseCondition = await keeper.conditions.accessSecretStoreCondition.hashValues(ddo.shortId(), consumer) + } catch(e) { } + + return { + rewardAddress: ddoOwner, + amount: amount.toString(), + documentId: ddo.shortId(), + grantee: consumer, + receiver: consumer, + sender: ddoOwner, + lockCondition, + releaseCondition, + } + } } diff --git a/src/ocean/OceanAgreements.ts b/src/ocean/OceanAgreements.ts index 9b927d5..4c43767 100644 --- a/src/ocean/OceanAgreements.ts +++ b/src/ocean/OceanAgreements.ts @@ -3,7 +3,10 @@ import BrizoProvider from "../brizo/BrizoProvider" import { generateId } from "../utils/GeneratorHelpers" import Account from "./Account" import DID from "./DID" +import { DDO } from "../ddo/DDO" import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" +import { Keeper } from "../keeper/Keeper" +import { zeroX, didPrefixed } from "../utils" import OceanAgreementsConditions from "./OceanAgreementsConditions" @@ -54,46 +57,22 @@ export default class OceanAgreements { serviceDefinitionId: string, consumer: Account, ): Promise { + const keeper = await Keeper.getInstance() + const d: DID = DID.parse(did as string) const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) const agreementId: string = generateId() - const valuesMap = await this.getValuesMapForPrepare(ddo, agreementId, consumer.getId()) + const templateName = ddo.findServiceByType("Access").serviceAgreementTemplate.contractName + const valuesMap = await keeper + .getTemplateByName(templateName) + .getServiceAgreementTemplateValuesMap(ddo, agreementId, consumer.getId()) const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, valuesMap, consumer) return {agreementId, signature} } - // TODO: this map depends on the template, this generation should be reponsability of the template - private async getValuesMapForPrepare(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> { - const keeper = await Keeper.getInstance() - const ddoOwner = ddo.proof && ddo.proof.creator - const amount = ddo.findServiceByType("Metadata").metadata.base.price - - let lockCondition - let releaseCondition - - try { - lockCondition = await keeper.conditions.lockRewardCondition.hashValues(ddoOwner, amount) - } catch(e) { } - - try { - releaseCondition = await keeper.conditions.accessSecretStoreCondition.hashValues(ddo.shortId(), consumer) - } catch(e) { } - - return { - rewardAddress: ddoOwner, - amount: amount.toString(), - documentId: ddo.shortId(), - grantee: consumer, - receiver: consumer, - sender: ddoOwner, - lockCondition, - releaseCondition, - } - } - /** * Submit a service agreement to the publisher to create the agreement on-chain. * @param {string} did Decentralized ID. @@ -147,16 +126,19 @@ export default class OceanAgreements { const d: DID = DID.parse(did) const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) - const serviceAgreement: ServiceAgreement = await ServiceAgreement - .executeServiceAgreement( - d, - ddo, - agreementId, - serviceDefinitionId, - signature, - consumer, - publisher) + // TODO: Use Keeper 0.7+ - return serviceAgreement + // const serviceAgreement: ServiceAgreement = await ServiceAgreement + // .executeServiceAgreement( + // d, + // ddo, + // agreementId, + // serviceDefinitionId, + // signature, + // consumer, + // publisher) + + // return serviceAgreement + return undefined } } diff --git a/src/utils/DDOHelpers.ts b/src/utils/DDOHelpers.ts index 8410860..c1b7b1d 100644 --- a/src/utils/DDOHelpers.ts +++ b/src/utils/DDOHelpers.ts @@ -13,11 +13,11 @@ function fillParameterWithDDO(parameter: ServiceAgreementTemplateParameter, ddo: return ddo.shortId() } - return null + return "" } const value = getValue(parameter.name.replace(/^_/, "")) - return {...parameter, value} + return {...parameter, value: parameter.type.includes("int") ? Number(value) : value} } /**