From e1b42931255366258cfc6d5bd33e0db30b3d10c3 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Mon, 5 Nov 2018 14:56:14 +0100 Subject: [PATCH] use timeouts from template --- src/ddo/Condition.ts | 4 +- src/ddo/Parameter.ts | 5 ++ src/examples/GrantAccess.ts | 2 +- src/examples/Purchase.ts | 2 +- src/keeper/ContractReflector.ts | 1 + src/keeper/contracts/ContractBase.ts | 29 ++++--- .../contracts/conditions/AccessConditions.ts | 4 +- src/models/MethodReflection.ts | 1 + src/ocean/Asset.ts | 4 +- src/ocean/Ocean.ts | 47 +++++----- src/ocean/ServiceAgreementTemplate.ts | 70 --------------- src/ocean/ServiceAgreements/Condition.ts | 6 ++ src/ocean/ServiceAgreements/Method.ts | 5 ++ .../ServiceAgreement.ts | 47 +++++----- .../ServiceAgreementTemplate.ts | 86 +++++++++++++++++++ .../ServiceAgreements/Templates/Default.ts | 26 ++++++ test/ocean/Asset.test.ts | 2 +- test/ocean/ServiceAgreement.test.ts | 53 ++++++------ test/ocean/ServiceAgreementTemplate.test.ts | 19 ++-- 19 files changed, 233 insertions(+), 180 deletions(-) create mode 100644 src/ddo/Parameter.ts delete mode 100644 src/ocean/ServiceAgreementTemplate.ts create mode 100644 src/ocean/ServiceAgreements/Condition.ts create mode 100644 src/ocean/ServiceAgreements/Method.ts rename src/ocean/{ => ServiceAgreements}/ServiceAgreement.ts (75%) create mode 100644 src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts create mode 100644 src/ocean/ServiceAgreements/Templates/Default.ts diff --git a/src/ddo/Condition.ts b/src/ddo/Condition.ts index f6f398f..bdaf18a 100644 --- a/src/ddo/Condition.ts +++ b/src/ddo/Condition.ts @@ -1,6 +1,8 @@ +import Parameter from "./Parameter" + export default class Condition { public name: string = "lockPayment" public timeout: number = 0 public conditionKey: string - public parameters: any + public parameters: Parameter[] } diff --git a/src/ddo/Parameter.ts b/src/ddo/Parameter.ts new file mode 100644 index 0000000..d067ed2 --- /dev/null +++ b/src/ddo/Parameter.ts @@ -0,0 +1,5 @@ +export default class Parameter { + public name: string + public type: string + public value: any +} diff --git a/src/examples/GrantAccess.ts b/src/examples/GrantAccess.ts index 86bc709..e1db596 100644 --- a/src/examples/GrantAccess.ts +++ b/src/examples/GrantAccess.ts @@ -1,5 +1,5 @@ import DDO from "../ddo/DDO" -import ServiceAgreement from "../ocean/ServiceAgreement" +import ServiceAgreement from "../ocean/ServiceAgreements/ServiceAgreement" import {Account, Asset, Logger, Ocean} from "../squid" (async () => { diff --git a/src/examples/Purchase.ts b/src/examples/Purchase.ts index d2973b4..88489c3 100644 --- a/src/examples/Purchase.ts +++ b/src/examples/Purchase.ts @@ -1,5 +1,5 @@ import DDO from "../ddo/DDO" -import ServiceAgreement from "../ocean/ServiceAgreement" +import ServiceAgreement from "../ocean/ServiceAgreements/ServiceAgreement" import {Account, Asset, Logger, Ocean} from "../squid" (async () => { diff --git a/src/keeper/ContractReflector.ts b/src/keeper/ContractReflector.ts index 0f4e651..a3fc502 100644 --- a/src/keeper/ContractReflector.ts +++ b/src/keeper/ContractReflector.ts @@ -12,6 +12,7 @@ export default class ContractReflector { methodName: parts[1], address: contract.getAddress(), signature: contract.getSignatureOfMethod(parts[1]), + inputs: contract.getInputsOfMethod(parts[1]), } as MethodReflection } } diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index d53e79a..fe365b4 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -43,20 +43,15 @@ export default abstract class ContractBase { } public getSignatureOfMethod(methodName: string): string { - - const foundMethod = this.contract.options.jsonInterface.find((method) => { - if (method.name === methodName) { - return method - } - }) - - if (!foundMethod) { - throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`) - } - + const foundMethod = this.searchMethod(methodName) return foundMethod.signature } + public getInputsOfMethod(methodName: string): any[] { + const foundMethod = this.searchMethod(methodName) + return foundMethod.inputs + } + protected async init() { this.contract = await ContractHandler.get(this.contractName) } @@ -95,4 +90,16 @@ export default abstract class ContractBase { } } + private searchMethod(methodName): any { + const foundMethod = this.contract.options.jsonInterface.find((method) => { + if (method.name === methodName) { + return method + } + }) + if (!foundMethod) { + throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`) + } + return foundMethod + } + } diff --git a/src/keeper/contracts/conditions/AccessConditions.ts b/src/keeper/contracts/conditions/AccessConditions.ts index bed38d6..6bd5e65 100644 --- a/src/keeper/contracts/conditions/AccessConditions.ts +++ b/src/keeper/contracts/conditions/AccessConditions.ts @@ -9,10 +9,10 @@ export default class AccessConditions extends ContractBase { return accessConditions } - public async grantAccess(serviceDefinitionId: any, assetId: any, documentKeyId: any, publisherAddress: string) + public async grantAccess(serviceAgreementId: any, assetId: any, documentKeyId: any, publisherAddress: string) : Promise { return this.send("grantAccess", publisherAddress, [ - serviceDefinitionId, "0x" + assetId, "0x" + documentKeyId, + serviceAgreementId, "0x" + assetId, "0x" + documentKeyId, ]) } } diff --git a/src/models/MethodReflection.ts b/src/models/MethodReflection.ts index 3ac4ac6..1802834 100644 --- a/src/models/MethodReflection.ts +++ b/src/models/MethodReflection.ts @@ -3,4 +3,5 @@ export default class MethodReflection { public methodName: string public address: string public signature: string + public inputs: any[] } diff --git a/src/ocean/Asset.ts b/src/ocean/Asset.ts index 02169f5..5a28da7 100644 --- a/src/ocean/Asset.ts +++ b/src/ocean/Asset.ts @@ -2,7 +2,7 @@ import AquariusProvider from "../aquarius/AquariusProvider" import Account from "./Account" import IdGenerator from "./IdGenerator" import OceanBase from "./OceanBase" -import ServiceAgreement from "./ServiceAgreement" +import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" export default class Asset extends OceanBase { @@ -19,7 +19,7 @@ export default class Asset extends OceanBase { const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) const serviceAgreementId: string = IdGenerator.generateId() - const serviceAgreement: ServiceAgreement = await ServiceAgreement.createServiceAgreement(this.getId(), + const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(this.getId(), ddo, serviceAgreementId, consumer, this.publisher) return serviceAgreement diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index d9e65b1..064a1fb 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -1,16 +1,20 @@ import AquariusProvider from "../aquarius/AquariusProvider" import SearchQuery from "../aquarius/query/SearchQuery" import ConfigProvider from "../ConfigProvider" -import Condition from "../ddo/Condition" +import DDOCondition from "../ddo/Condition" import DDO from "../ddo/DDO" +import Parameter from "../ddo/Parameter" import Service from "../ddo/Service" import Keeper from "../keeper/Keeper" import Web3Provider from "../keeper/Web3Provider" import Config from "../models/Config" +import ValuePair from "../models/ValuePair" import Account from "./Account" import Asset from "./Asset" import IdGenerator from "./IdGenerator" -import ServiceAgreementTemplate from "./ServiceAgreementTemplate" +import Condition from "./ServiceAgreements/Condition" +import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate" +import DefaultTemplate from "./ServiceAgreements/Templates/Default" export default class Ocean { @@ -46,35 +50,28 @@ export default class Ocean { const assetId: string = IdGenerator.generateId() const did: string = `did:op:${assetId}` - const methods: string[] = [ - "PaymentConditions.lockPayment", - "AccessConditions.grantAccess", - "PaymentConditions.releasePayment", - "PaymentConditions.refundPayment", - ] - // tslint:disable - const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit - const serviceName = "Access" - const saTemplate: ServiceAgreementTemplate = - await ServiceAgreementTemplate.registerServiceAgreementsTemplate(serviceName, methods, dependencyMatrix, + const serviceAgreementTemplate: ServiceAgreementTemplate = + await ServiceAgreementTemplate.registerServiceAgreementsTemplate(serviceName, DefaultTemplate.methods, asset.publisher) // get condition keys from template - const conditionKeys: string[] = saTemplate.getConditionKeys() + const conditions: Condition[] = serviceAgreementTemplate.getConditions() // create ddo conditions out of the keys - const conditions: Condition[] = conditionKeys.map((conditionKey, i): Condition => { + const ddoConditions: DDOCondition[] = conditions.map((condition: Condition): DDOCondition => { return { - name: methods[i].split(".")[1], + name: condition.methodReflection.methodName, timeout: 100, - conditionKey: conditionKey, - parameters: { - // todo wtf? - assetId: "bytes32", - price: "integer" - }, - } as Condition + conditionKey: condition.condtionKey, + parameters: condition.methodReflection.inputs.map((input: ValuePair) => { + return { + ...input, + value: "xxx", + } as Parameter + }), + + } as DDOCondition }) // create ddo itself @@ -89,8 +86,8 @@ export default class Ocean { // the id of the service agreement? serviceDefinitionId: "0x" + IdGenerator.generateId(), // the id of the service agreement template - templateId: saTemplate.getId(), - conditions, + templateId: serviceAgreementTemplate.getId(), + conditions: ddoConditions, } as Service, ], }) diff --git a/src/ocean/ServiceAgreementTemplate.ts b/src/ocean/ServiceAgreementTemplate.ts deleted file mode 100644 index e2d2e26..0000000 --- a/src/ocean/ServiceAgreementTemplate.ts +++ /dev/null @@ -1,70 +0,0 @@ -import ContractReflector from "../keeper/ContractReflector" -import ServiceAgreement from "../keeper/contracts/ServiceAgreement" -import Web3Provider from "../keeper/Web3Provider" -import MethodReflection from "../models/MethodReflection" -import Account from "./Account" -import OceanBase from "./OceanBase" - -export default class ServiceAgreementTemplate extends OceanBase { - - public static async registerServiceAgreementsTemplate(serviceName: string, methods: string[], - dependencyMatrix: number[], templateOwner: Account): - Promise { - - const methodReflections: MethodReflection[] = - await Promise.all(methods.map(async (method) => { - const methodReflection = await - ContractReflector.reflectContractMethod(method) - return methodReflection - })) - - const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance() - - const receipt = await serviceAgreement.setupAgreementTemplate( - methodReflections, dependencyMatrix, - Web3Provider.getWeb3().utils.fromAscii(serviceName), - templateOwner.getId()) - - const serviceAgreementTemplateId = - receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId - - return new ServiceAgreementTemplate( - serviceAgreementTemplateId, - ServiceAgreementTemplate.generateConditionsKeys(serviceAgreementTemplateId, methodReflections), - templateOwner) - } - - private static generateConditionsKeys(serviceAgreementTemplateId: string, methodReflections: MethodReflection[]): - string[] { - const conditions = [] - for (const methodReflection of methodReflections) { - const values = [ - {type: "bytes32", value: serviceAgreementTemplateId}, - {type: "address", value: methodReflection.address}, - {type: "bytes4", value: methodReflection.signature}, - ] - conditions.push(Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex")) - } - return conditions - } - - private constructor(serviceAgreementTemplateId, private conditionKeys: string[], private owner: Account) { - super(serviceAgreementTemplateId) - } - - /** - * gets the status of a service agreement template - */ - public async getStatus(): Promise { - const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance() - return serviceAgreement.getTemplateStatus(this.getId()) - } - - public getOwner(): Account { - return this.owner - } - - public getConditionKeys(): string[] { - return this.conditionKeys - } -} diff --git a/src/ocean/ServiceAgreements/Condition.ts b/src/ocean/ServiceAgreements/Condition.ts new file mode 100644 index 0000000..68885c2 --- /dev/null +++ b/src/ocean/ServiceAgreements/Condition.ts @@ -0,0 +1,6 @@ +import MethodReflection from "../../models/MethodReflection" + +export default class Condition { + public methodReflection: MethodReflection + public condtionKey +} diff --git a/src/ocean/ServiceAgreements/Method.ts b/src/ocean/ServiceAgreements/Method.ts new file mode 100644 index 0000000..bd05334 --- /dev/null +++ b/src/ocean/ServiceAgreements/Method.ts @@ -0,0 +1,5 @@ +export default class Method { + public path: string + public dependency: number + public timeout: number +} diff --git a/src/ocean/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts similarity index 75% rename from src/ocean/ServiceAgreement.ts rename to src/ocean/ServiceAgreements/ServiceAgreement.ts index 06d7721..90cec13 100644 --- a/src/ocean/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -1,40 +1,38 @@ -import DDO from "../ddo/DDO" -import AccessConditions from "../keeper/contracts/conditions/AccessConditions" -import ServiceAgreementContract from "../keeper/contracts/ServiceAgreement" -import Web3Provider from "../keeper/Web3Provider" -import ValuePair from "../models/ValuePair" -import Account from "./Account" -import OceanBase from "./OceanBase" +import DDO from "../../ddo/DDO" +import AccessConditions from "../../keeper/contracts/conditions/AccessConditions" +import ServiceAgreementContract from "../../keeper/contracts/ServiceAgreement" +import Web3Provider from "../../keeper/Web3Provider" +import ValuePair from "../../models/ValuePair" +import Account from "../Account" +import OceanBase from "../OceanBase" export default class ServiceAgreement extends OceanBase { - public static async createServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account, - publisher: Account): + public static async signServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account, + publisher: Account): Promise { + const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) + const valueHashes = ServiceAgreement.createValueHashes(values) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo) const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId, - consumer) + values, valueHashes, timeoutValues, consumer) - const serviceAgreement: ServiceAgreement = await ServiceAgreement.signServiceAgreement(ddo, - serviceAgreementId, timeoutValues, serviceAgreementHashSignature, consumer, publisher) + const serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo, + serviceAgreementId, values, valueHashes, timeoutValues, serviceAgreementHashSignature, consumer, publisher) return serviceAgreement } - public static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, consumer: Account): + public static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, values: ValuePair[], + valueHashes: string[], timeoutValues: number[], consumer: Account): Promise { - const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) - const valueHashes = ServiceAgreement.createValueHashes(values) - const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => { return condition.conditionKey }) - const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo) - const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId, serviceAgreementId, conditionKeys, valueHashes, timeoutValues) @@ -44,12 +42,11 @@ export default class ServiceAgreement extends OceanBase { return serviceAgreementHashSignature } - private static async signServiceAgreement(ddo: DDO, serviceAgreementId: string, timeoutValues: number[], - serviceAgreementHashSignature: string, consumer: Account, - publisher: Account): Promise { + private static async executeAgreement(ddo: DDO, serviceAgreementId: string, values: ValuePair[], + valueHashes: string[], timeoutValues: number[], + serviceAgreementHashSignature: string, consumer: Account, + publisher: Account): Promise { - const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) - const valueHashes = ServiceAgreement.createValueHashes(values) const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance() const executeAgreementReceipt = await serviceAgreement.executeAgreement( ddo.service[0].templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes, @@ -80,7 +77,8 @@ export default class ServiceAgreement extends OceanBase { } private static hashServiceAgreement(serviceAgreementTemplateId: string, serviceAgreementId: string, - conditionKeys: string[], valueHashes: string[], timeouts: number[]) { + conditionKeys: string[], valueHashes: string[], timeouts: number[]) + : string { const args = [ {type: "bytes32", value: serviceAgreementTemplateId} as ValuePair, {type: "bytes32[]", value: conditionKeys} as ValuePair, @@ -104,6 +102,7 @@ export default class ServiceAgreement extends OceanBase { const values: ValuePair[] = [ {type: "bool", value: true} as ValuePair, {type: "bool", value: false} as ValuePair, + {type: "bool", value: false} as ValuePair, {type: "uint", value: 120} as ValuePair, {type: "string", value: serviceAgreementId} as ValuePair, ] diff --git a/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts b/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts new file mode 100644 index 0000000..3e7a672 --- /dev/null +++ b/src/ocean/ServiceAgreements/ServiceAgreementTemplate.ts @@ -0,0 +1,86 @@ +import ContractReflector from "../../keeper/ContractReflector" +import ServiceAgreement from "../../keeper/contracts/ServiceAgreement" +import Web3Provider from "../../keeper/Web3Provider" +import MethodReflection from "../../models/MethodReflection" +import ValuePair from "../../models/ValuePair" +import Logger from "../../utils/Logger" +import Account from "../Account" +import OceanBase from "../OceanBase" +import Condition from "./Condition" +import Method from "./Method" + +export default class ServiceAgreementTemplate extends OceanBase { + + public static async registerServiceAgreementsTemplate(serviceName: string, methods: Method[], + templateOwner: Account) + : Promise { + + const methodReflections: MethodReflection[] = + await Promise.all(methods.map(async (method: Method) => { + const methodReflection = await + ContractReflector.reflectContractMethod(method.path) + return methodReflection + })) + + const dependencyMatrix: number[] = + await Promise.all(methods.map(async (method: Method) => { + // tslint:disable + return method.dependency | method.timeout + })) + + Logger.log(dependencyMatrix) + const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance() + + const receipt = await serviceAgreement.setupAgreementTemplate( + methodReflections, dependencyMatrix, + Web3Provider.getWeb3().utils.fromAscii(serviceName), + templateOwner.getId()) + + const serviceAgreementTemplateId = + receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId + + const conditions: Condition[] = methodReflections.map((methodReflection) => { + return { + methodReflection, + condtionKey: ServiceAgreementTemplate.generateConditionsKey(serviceAgreementTemplateId, + methodReflection), + } as Condition + }) + + return new ServiceAgreementTemplate( + serviceAgreementTemplateId, + conditions, + templateOwner) + } + + private static generateConditionsKey(serviceAgreementTemplateId: string, methodReflection: MethodReflection) + : string { + const values = [ + {type: "bytes32", value: serviceAgreementTemplateId} as ValuePair, + {type: "address", value: methodReflection.address} as ValuePair, + {type: "bytes4", value: methodReflection.signature} as ValuePair, + ] + return Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex") + } + + private constructor(serviceAgreementTemplateId, private conditions: Condition[], + private owner: Account) { + super(serviceAgreementTemplateId) + } + + /** + * gets the status of a service agreement template + */ + public async getStatus(): Promise { + const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance() + return serviceAgreement.getTemplateStatus(this.getId()) + } + + public getOwner(): Account { + return this.owner + } + + public getConditions(): Condition[] { + return this.conditions + } +} diff --git a/src/ocean/ServiceAgreements/Templates/Default.ts b/src/ocean/ServiceAgreements/Templates/Default.ts new file mode 100644 index 0000000..71043c9 --- /dev/null +++ b/src/ocean/ServiceAgreements/Templates/Default.ts @@ -0,0 +1,26 @@ +import Method from "../Method" + +const methods: Method[] = [ + { + path: "PaymentConditions.lockPayment", + dependency: 0, + timeout: 10, + } as Method, + { + path: "AccessConditions.grantAccess", + dependency: 1, + timeout: 500, + } as Method, + { + path: "PaymentConditions.releasePayment", + dependency: 4, + timeout: 17, + } as Method, + { + path: "PaymentConditions.refundPayment", + dependency: 1, + timeout: 40, + } as Method, +] + +export default {methods} diff --git a/test/ocean/Asset.test.ts b/test/ocean/Asset.test.ts index 747474f..615dc2e 100644 --- a/test/ocean/Asset.test.ts +++ b/test/ocean/Asset.test.ts @@ -7,7 +7,7 @@ import ContractHandler from "../../src/keeper/ContractHandler" import Account from "../../src/ocean/Account" import Asset from "../../src/ocean/Asset" import Ocean from "../../src/ocean/Ocean" -import ServiceAgreement from "../../src/ocean/ServiceAgreement" +import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider" import config from "../config" import AquariusMock from "../mocks/Aquarius.mock" diff --git a/test/ocean/ServiceAgreement.test.ts b/test/ocean/ServiceAgreement.test.ts index 9811564..de12aec 100644 --- a/test/ocean/ServiceAgreement.test.ts +++ b/test/ocean/ServiceAgreement.test.ts @@ -1,15 +1,18 @@ import {assert} from "chai" import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider" import ConfigProvider from "../../src/ConfigProvider" -import Condition from "../../src/ddo/Condition" +import DDOCondition from "../../src/ddo/Condition" import DDO from "../../src/ddo/DDO" +import Parameter from "../../src/ddo/Parameter" import Service from "../../src/ddo/Service" import ContractHandler from "../../src/keeper/ContractHandler" import Account from "../../src/ocean/Account" import IdGenerator from "../../src/ocean/IdGenerator" import Ocean from "../../src/ocean/Ocean" -import ServiceAgreement from "../../src/ocean/ServiceAgreement" -import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate" +import Condition from "../../src/ocean/ServiceAgreements/Condition" +import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" +import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate" +import DefaultTemplate from "../../src/ocean/ServiceAgreements/Templates/Default" import config from "../config" import AquariusConnectorMock from "../mocks/AquariusConnector.mock" @@ -35,43 +38,35 @@ describe("ServiceAgreement", () => { consumerAccount = accounts[2] const resourceName = "superb car data" - const methods: string[] = [ - "PaymentConditions.lockPayment", - "AccessConditions.grantAccess", - "PaymentConditions.releasePayment", - "PaymentConditions.refundPayment", - ] - - // tslint:disable - const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit testServiceAgreementTemplate = - await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods, - dependencyMatrix, templateOwnerAccount) + await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods, + templateOwnerAccount) // get condition keys from template - const conditionKeys: string[] = testServiceAgreementTemplate.getConditionKeys() + const conditions: Condition[] = testServiceAgreementTemplate.getConditions() // create ddo conditions out of the keys - const conditions: Condition[] = conditionKeys.map((conditionKey, i): Condition => { + const ddoConditions: DDOCondition[] = conditions.map((condition): DDOCondition => { return { - name: methods[i].split(".")[1], + name: condition.methodReflection.methodName, timeout: 100, - conditionKey: conditionKey, - parameters: { - // todo wtf? - assetId: "bytes32", - price: "integer" - }, - } as Condition + conditionKey: condition.condtionKey, + parameters: condition.methodReflection.inputs.map((input) => { + return { + ...input, + value: "xx", + }as Parameter + }), + } as DDOCondition }) serviceDefintion = [ { serviceDefinitionId: IdGenerator.generateId(), templateId: testServiceAgreementTemplate.getId(), - conditions, - } as Service + conditions: ddoConditions, + } as Service, ] }) @@ -88,7 +83,7 @@ describe("ServiceAgreement", () => { // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, + await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, publisherAccount) assert(serviceAgreement) @@ -110,7 +105,7 @@ describe("ServiceAgreement", () => { // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, + await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, publisherAccount) assert(serviceAgreement) @@ -131,7 +126,7 @@ describe("ServiceAgreement", () => { // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, + await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount, publisherAccount) assert(serviceAgreement) diff --git a/test/ocean/ServiceAgreementTemplate.test.ts b/test/ocean/ServiceAgreementTemplate.test.ts index d44c7b4..f8914f9 100644 --- a/test/ocean/ServiceAgreementTemplate.test.ts +++ b/test/ocean/ServiceAgreementTemplate.test.ts @@ -3,19 +3,12 @@ import ConfigProvider from "../../src/ConfigProvider" import ContractHandler from "../../src/keeper/ContractHandler" import Account from "../../src/ocean/Account" import Ocean from "../../src/ocean/Ocean" -import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate" +import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate" +import DefaultTemplate from "../../src/ocean/ServiceAgreements/Templates/Default" import config from "../config" let ocean: Ocean let accounts: Account[] -const methods: string[] = [ - "PaymentConditions.lockPayment", - "AccessConditions.grantAccess", - "PaymentConditions.releasePayment", - "PaymentConditions.refundPayment", -] -// tslint:disable -const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit describe("ServiceAgreementTemplate", () => { @@ -32,8 +25,8 @@ describe("ServiceAgreementTemplate", () => { const templateOwner = accounts[0] const resourceName = "consume" const serviceAgreementTemplate: ServiceAgreementTemplate = - await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods, - dependencyMatrix, templateOwner) + await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods, + templateOwner) assert(serviceAgreementTemplate) assert(serviceAgreementTemplate.getId()) @@ -48,8 +41,8 @@ describe("ServiceAgreementTemplate", () => { const resourceName = "consume" const serviceAgreementTemplate: ServiceAgreementTemplate = - await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods, - dependencyMatrix, publisherAccount) + await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, DefaultTemplate.methods, + publisherAccount) assert(serviceAgreementTemplate) const templateStatus = await serviceAgreementTemplate.getStatus()