From ba4e2d4656afd6692fd8276d2683deb56687d034 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Wed, 14 Nov 2018 15:44:25 +0100 Subject: [PATCH] adapted changes from squid discussion --- src/ddo/DDO.ts | 29 +++++++++-- src/examples/Search.ts | 4 +- src/ocean/Ocean.ts | 19 +++++--- .../ServiceAgreements/ServiceAgreement.ts | 37 ++++++++------ test/config.ts | 1 + test/ocean/Ocean.test.ts | 24 ++++++---- test/ocean/ServiceAgreement.test.ts | 48 ++++++++++--------- 7 files changed, 102 insertions(+), 60 deletions(-) diff --git a/src/ddo/DDO.ts b/src/ddo/DDO.ts index fa9434d..c286044 100644 --- a/src/ddo/DDO.ts +++ b/src/ddo/DDO.ts @@ -20,9 +20,6 @@ export default class DDO { public authentication: Authentication[] public service: Service[] - // @ts-ignore - private assa: string - public constructor(ddo?: { id?: string, publicKey?: any[], @@ -34,4 +31,30 @@ export default class DDO { this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : [] this.service = ddo ? ddo.service ? ddo.service : [] : [] } + + public findServiceById(serviceDefinitionId: string): Service { + + if (!serviceDefinitionId) { + throw new Error("serviceDefinitionId not set") + } + + const service: Service = this.service.find((s) => { + return s.serviceDefinitionId === serviceDefinitionId + }) + + return service + } + + public findServiceByType(serviceType: string): Service { + + if (!serviceType) { + throw new Error("serviceType not set") + } + + const service: Service = this.service.find((s) => { + return s.type === serviceType + }) + + return service + } } diff --git a/src/examples/Search.ts b/src/examples/Search.ts index d40c8fc..59e420f 100644 --- a/src/examples/Search.ts +++ b/src/examples/Search.ts @@ -14,8 +14,8 @@ import {Logger, Ocean} from "../squid" }) const result: DDO[] = await ocean.searchAssetsByText("Office Humidity") - const names: string[] = result.map((res: DDO): string => { - const service = res.service.find((srv) => srv.type === "Metadata") + const names: string[] = result.map((ddo: DDO): string => { + const service = ddo.findServiceByType("Metadata") if (service && service.metadata) { return service.metadata.base.name } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index b1c1295..9d2bbb5 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -140,29 +140,34 @@ export default class Ocean { return storedDdo } - public async signServiceAgreement(did: string, serviceAgreementId: string, consumer: Account): Promise { + public async signServiceAgreement(did: string, serviceDefinitionId: string, consumer: Account): Promise { const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) const id = did.replace("did:op:", "") + const serviceAgreementId: string = IdGenerator.generateId() try { - const serviceAgrementSignature: string = await ServiceAgreement.signServiceAgreement(id, - ddo, serviceAgreementId, consumer) - return serviceAgrementSignature + const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(id, + ddo, serviceDefinitionId, serviceAgreementId, consumer) + return { + serviceAgreementId, + serviceAgreementSignature, + } } catch (err) { Logger.error("Signing ServiceAgreement failed!", err) } } - public async executeServiceAgreement(did: string, serviceAgreementId: string, serviceAgreementSignature: string, - consumer: Account, publisher: Account): Promise { + public async executeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string, + serviceAgreementSignature: string, consumer: Account, publisher: Account) + : Promise { const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) const id = did.replace("did:op:", "") const serviceAgrement: ServiceAgreement = await ServiceAgreement.executeServiceAgreement(id, - ddo, serviceAgreementId, serviceAgreementSignature, consumer, publisher) + ddo, serviceDefinitionId, serviceAgreementId, serviceAgreementSignature, consumer, publisher) return serviceAgrement } diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index e5143ef..fbb9099 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -1,5 +1,6 @@ import Condition from "../../ddo/Condition" import DDO from "../../ddo/DDO" +import Service from "../../ddo/Service" import Keeper from "../../keeper/Keeper" import Web3Provider from "../../keeper/Web3Provider" import ValuePair from "../../models/ValuePair" @@ -8,46 +9,49 @@ import OceanBase from "../OceanBase" export default class ServiceAgreement extends OceanBase { - public static async signServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, consumer: Account): + public static async signServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string, + serviceAgreementId: string, consumer: Account): Promise { const values: ValuePair[] = ServiceAgreement.getValuesFromDDO(ddo, serviceAgreementId) const valueHashes = ServiceAgreement.createValueHashes(values) const timeoutValues: number[] = ServiceAgreement.getTimeoutValuesFromDDO(ddo) + const service: Service = ddo.findServiceById(serviceDefinitionId) - const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(ddo, serviceAgreementId, + const serviceAgreementHashSignature = await ServiceAgreement.createSAHashSignature(service, serviceAgreementId, values, valueHashes, timeoutValues, consumer) return serviceAgreementHashSignature } - public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceAgreementId: string, - serviceAgreementHashSignature: string, consumer: Account, - publisher: Account): Promise { + public static async executeServiceAgreement(assetId: string, ddo: DDO, serviceDefinitionId: string, + serviceAgreementId: string, serviceAgreementHashSignature: 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 serviceAgreement: ServiceAgreement = await ServiceAgreement.executeAgreement(ddo, - serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature, consumer, publisher) + serviceDefinitionId, serviceAgreementId, valueHashes, timeoutValues, serviceAgreementHashSignature, + consumer, publisher) return serviceAgreement } - private static async createSAHashSignature(ddo: DDO, serviceAgreementId: string, values: ValuePair[], + private static async createSAHashSignature(service: Service, serviceAgreementId: string, values: ValuePair[], valueHashes: string[], timeoutValues: number[], consumer: Account): Promise { - if (!ddo.service[0].templateId) { + if (!service.templateId) { throw new Error("TemplateId not found in ddo.") } - const conditionKeys: string[] = ddo.service[0].conditions.map((condition) => { + const conditionKeys: string[] = service.conditions.map((condition) => { return condition.conditionKey }) - const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(ddo.service[0].templateId, + const serviceAgreementHash = ServiceAgreement.hashServiceAgreement(service.templateId, serviceAgreementId, conditionKeys, valueHashes, timeoutValues) const serviceAgreementHashSignature = @@ -56,18 +60,21 @@ export default class ServiceAgreement extends OceanBase { return serviceAgreementHashSignature } - private static async executeAgreement(ddo: DDO, serviceAgreementId: string, valueHashes: string[], - timeoutValues: number[], serviceAgreementHashSignature: string, - consumer: Account, publisher: Account): Promise { + private static async executeAgreement(ddo: DDO, serviceDefinitionId: string, serviceAgreementId: string, + valueHashes: string[], timeoutValues: number[], + serviceAgreementHashSignature: string, consumer: Account, publisher: Account) + : Promise { const {serviceAgreement} = await Keeper.getInstance() - if (!ddo.service[0].templateId) { + const service: Service = ddo.findServiceById(serviceDefinitionId) + + if (!service.templateId) { throw new Error("TemplateId not found in ddo.") } const executeAgreementReceipt = await serviceAgreement.executeAgreement( - ddo.service[0].templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes, + service.templateId, serviceAgreementHashSignature, consumer.getId(), valueHashes, timeoutValues, serviceAgreementId, ddo.id, publisher.getId()) if (executeAgreementReceipt.events.ExecuteAgreement.returnValues.state === false) { diff --git a/test/config.ts b/test/config.ts index ec2123f..04b8b3e 100644 --- a/test/config.ts +++ b/test/config.ts @@ -2,6 +2,7 @@ import Config from "../src/models/Config" export default { aquariusUri: "http://localhost:5000", + brizoUri: "http://localhost:3000", nodeUri: "http://localhost:8545", parityUri: "http://localhost:9545", secretStoreUri: "https://secret-store.dev-ocean.com", diff --git a/test/ocean/Ocean.test.ts b/test/ocean/Ocean.test.ts index bb813e1..98e4abc 100644 --- a/test/ocean/Ocean.test.ts +++ b/test/ocean/Ocean.test.ts @@ -5,8 +5,8 @@ import SearchQuery from "../../src/aquarius/query/SearchQuery" import ConfigProvider from "../../src/ConfigProvider" import DDO from "../../src/ddo/DDO" import MetaData from "../../src/ddo/MetaData" +import Service from "../../src/ddo/Service" import Account from "../../src/ocean/Account" -import IdGenerator from "../../src/ocean/IdGenerator" import Ocean from "../../src/ocean/Ocean" import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider" @@ -111,15 +111,18 @@ describe("Ocean", () => { const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher) + const service: Service = ddo.findServiceByType("Access") + // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) - const serviceAgreementId: string = IdGenerator.generateId() - const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId, - consumer) + const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id, + service.serviceDefinitionId, consumer) assert(serviceAgreementSignature) - assert(serviceAgreementSignature.startsWith("0x")) + assert(serviceAgreementSignature.serviceAgreementId) + assert(serviceAgreementSignature.serviceAgreementSignature) + assert(serviceAgreementSignature.serviceAgreementSignature.startsWith("0x")) }) }) @@ -132,16 +135,17 @@ describe("Ocean", () => { const consumer = accounts[1] const ddo: DDO = await ocean.registerAsset(new MetaData(), publisher) + const service: Service = ddo.findServiceByType("Access") // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) - const serviceAgreementId: string = IdGenerator.generateId() - const serviceAgreementSignature: string = await ocean.signServiceAgreement(ddo.id, serviceAgreementId, - consumer) + const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, + service.serviceDefinitionId, consumer) - const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, serviceAgreementId, - serviceAgreementSignature, consumer, publisher) + const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, + service.serviceDefinitionId, signServiceAgreementResult.serviceAgreementId, + signServiceAgreementResult.serviceAgreementSignature, consumer, publisher) assert(serviceAgreement) }) diff --git a/test/ocean/ServiceAgreement.test.ts b/test/ocean/ServiceAgreement.test.ts index a11666e..98e8ddb 100644 --- a/test/ocean/ServiceAgreement.test.ts +++ b/test/ocean/ServiceAgreement.test.ts @@ -21,7 +21,7 @@ let accounts: Account[] let publisherAccount: Account let consumerAccount: Account -let serviceDefinition +let service: Service describe("ServiceAgreement", () => { @@ -55,14 +55,12 @@ describe("ServiceAgreement", () => { } as DDOCondition }) - serviceDefinition = [ - { - serviceDefinitionId: IdGenerator.generateId(), - templateId: serviceAgreementTemplate.getId(), - conditions: ddoConditions, - } as Service, - ] - + service = { + type: "Access", + serviceDefinitionId: IdGenerator.generateId(), + templateId: serviceAgreementTemplate.getId(), + conditions: ddoConditions, + } as Service }) describe("#signServiceAgreement()", () => { @@ -70,14 +68,15 @@ describe("ServiceAgreement", () => { const id: string = IdGenerator.generateId() const did: string = `did:op:${id}` - const ddo = new DDO({id: did, service: serviceDefinition}) + const ddo = new DDO({id: did, service: [service]}) const assetId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreementSignature: string = - await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) + await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, consumerAccount) assert(serviceAgreementSignature) assert(serviceAgreementSignature.startsWith("0x")) @@ -89,18 +88,19 @@ describe("ServiceAgreement", () => { const id: string = IdGenerator.generateId() const did: string = `did:op:${id}` - const ddo = new DDO({id: did, service: serviceDefinition}) + const ddo = new DDO({id: did, service: [service]}) const assetId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreementSignature: string = - await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) + await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, consumerAccount) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, - serviceAgreementSignature, consumerAccount, publisherAccount) + await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount) assert(serviceAgreement) const serviceDefinitionId = serviceAgreement.getId() @@ -114,19 +114,20 @@ describe("ServiceAgreement", () => { const id: string = IdGenerator.generateId() const did: string = `did:op:${id}` - const ddo = new DDO({id: did, service: serviceDefinition}) + const ddo = new DDO({id: did, service: [service]}) const assetId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreementSignature: string = - await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) + await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, consumerAccount) assert(serviceAgreementSignature) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, - serviceAgreementSignature, consumerAccount, publisherAccount) + await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount) assert(serviceAgreement) const status = await serviceAgreement.getStatus() @@ -139,19 +140,20 @@ describe("ServiceAgreement", () => { const id: string = IdGenerator.generateId() const did: string = `did:op:${id}` - const ddo = new DDO({id: did, service: serviceDefinition}) + const ddo = new DDO({id: did, service: [service]}) const assetId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) const serviceAgreementSignature: string = - await ServiceAgreement.signServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount) + await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, consumerAccount) assert(serviceAgreementSignature) const serviceAgreement: ServiceAgreement = - await ServiceAgreement.executeServiceAgreement(assetId, ddo, serviceAgreementId, - serviceAgreementSignature, consumerAccount, publisherAccount) + await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId, + serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount) assert(serviceAgreement) const fulfilled: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId())