From cfda1e61303f0b3f9326e3213c7c9288a5750f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Wed, 13 Feb 2019 14:32:52 +0100 Subject: [PATCH] added all necessary methods of Ocean.agreements --- integration/ocean/BuyAsset.test.ts | 10 +-- src/ocean/Ocean.ts | 6 +- src/ocean/OceanAgreements.ts | 69 +++++++++++++++++-- src/ocean/OceanAssets.ts | 1 - .../ServiceAgreements/ServiceAgreement.ts | 26 ++++--- test/ocean/Ocean.test.ts | 34 ++------- 6 files changed, 91 insertions(+), 55 deletions(-) diff --git a/integration/ocean/BuyAsset.test.ts b/integration/ocean/BuyAsset.test.ts index 1f1e864..bd0b9be 100644 --- a/integration/ocean/BuyAsset.test.ts +++ b/integration/ocean/BuyAsset.test.ts @@ -14,7 +14,7 @@ describe("Buy Asset", () => { let ddo: DDO let did: DID - let agreementId: string + let serviceAgreementSignatureResult: {agreementId: string, signature: string} let serviceAgreement: ServiceAgreement before(async () => { @@ -83,9 +83,11 @@ describe("Buy Asset", () => { it("should sign the service agreement", async () => { const accessService = ddo.findServiceByType("Access") - agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer) + serviceAgreementSignatureResult = await ocean.agreements.prepare(ddo.id, accessService.serviceDefinitionId, consumer) + const {agreementId, signature} = serviceAgreementSignatureResult assert.match(agreementId, /^[a-f0-9]{64}$/, "Service agreement ID seems not valid") + assert.match(signature, /^0x[a-f0-9]{130}$/, "Service agreement signature seems not valid") }) it("should execute the service agreement", async () => { @@ -94,8 +96,8 @@ describe("Buy Asset", () => { serviceAgreement = await ocean.agreements.create( ddo.id, accessService.serviceDefinitionId, - serviceAgreementSignatureResult.serviceAgreementId, - serviceAgreementSignatureResult.serviceAgreementSignature, + serviceAgreementSignatureResult.agreementId, + serviceAgreementSignatureResult.signature, consumer, publisher, ) diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index d507be7..44cea99 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -116,20 +116,20 @@ export default class Ocean { /** * Signs a service agreement by DID. - * @deprecated Replace by [Ocean.assets.order]{@link #OceanAssets.order} + * @deprecated Replace by [Ocean.agreement.prepare]{@link #OceanAgreement.prepare} * @param {string} did Decentralized ID. * @param {string} serviceDefinitionId Service definition ID. * @param {Account} consumer Consumer account. * @return {Promise} * */ - @deprecated("OceanAssets.order") + @deprecated("OceanAgreement.prepare") public async signServiceAgreement( did: string, serviceDefinitionId: string, consumer: Account, ) { - return await this.assets.order(did, serviceDefinitionId, consumer) + return await this.agreements.prepare(did, serviceDefinitionId, consumer) } /** diff --git a/src/ocean/OceanAgreements.ts b/src/ocean/OceanAgreements.ts index 6f87344..1ff1053 100644 --- a/src/ocean/OceanAgreements.ts +++ b/src/ocean/OceanAgreements.ts @@ -2,6 +2,10 @@ import AquariusProvider from "../aquarius/AquariusProvider" import Account from "./Account" import DID from "./DID" import ServiceAgreement from "./ServiceAgreements/ServiceAgreement" +import IdGenerator from "./IdGenerator" +import BrizoProvider from "../brizo/BrizoProvider" + +export type agreementPreparionResult = {agreementId: string, signature: string} /** * Agreements submodule of Ocean Protocol. @@ -27,20 +31,71 @@ export default class OceanAgreements { private static instance: OceanAgreements = null /** - * Executes a service agreement. + * Creates a consumer signature for the specified asset service. * @param {string} did Decentralized ID. * @param {string} serviceDefinitionId Service definition ID. - * @param {string} serviceAgreementId Service agreement ID. - * @param {string} serviceAgreementSignature Service agreement signature. + * @param {Account} consumer Consumer account. + * @return {Promise} Agreement ID and signaturee. + */ + public async prepare( + did: string, + serviceDefinitionId: string, + consumer: Account, + ): Promise { + + const d: DID = DID.parse(did as string) + const ddo = await AquariusProvider.getAquarius().retrieveDDO(d) + const agreementId: string = IdGenerator.generateId() + + const signature = await ServiceAgreement.signServiceAgreement(ddo, serviceDefinitionId, agreementId, consumer) + + return {agreementId, signature} + } + + + /** + * Submit a service agreement to the publisher to create the agreement on-chain. + * @param {string} did Decentralized ID. + * @param {string} serviceDefinitionId Service definition ID. + * @param {Account} consumer Consumer account. + * @return {Promise} + */ + public async send( + did: string, + agreementId: string, + serviceDefinitionId: string, + signature: string, + consumer: Account, + ): Promise { + + await BrizoProvider + .getBrizo() + .initializeServiceAgreement( + did, + agreementId, + serviceDefinitionId, + signature, + consumer.getId(), + ) + } + + /** + * Create a service agreement on-chain. This should be called by the publisher of the asset. + * Consumer signature will be verified on-chain, but it is recommended to verify the signature + * in this method before submitting on-chain. + * @param {string} did Decentralized ID. + * @param {string} agreementId Service agreement ID. + * @param {string} serviceDefinitionId Service definition ID. + * @param {string} signature Service agreement signature. * @param {Account} consumer Consumer account. * @param {Account} publisher Publisher account. * @return {Promise} */ public async create( did: string, + agreementId: string, serviceDefinitionId: string, - serviceAgreementId: string, - serviceAgreementSignature: string, + signature: string, consumer: Account, publisher: Account, ): Promise { @@ -51,9 +106,9 @@ export default class OceanAgreements { .executeServiceAgreement( d, ddo, + agreementId, serviceDefinitionId, - serviceAgreementId, - serviceAgreementSignature, + signature, consumer, publisher) diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 28dd77c..ebd8035 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -9,7 +9,6 @@ import ContractEvent from "../keeper/Event" import EventListener from "../keeper/EventListener" import Keeper from "../keeper/Keeper" import SecretStoreProvider from "../secretstore/SecretStoreProvider" -import Logger from "../utils/Logger" import Account from "./Account" import DID from "./DID" import IdGenerator from "./IdGenerator" diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index 711c5a7..52d1af2 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -12,10 +12,12 @@ import OceanBase from "../OceanBase" export default class ServiceAgreement extends OceanBase { - public static async signServiceAgreement(ddo: DDO, - serviceDefinitionId: string, - serviceAgreementId: string, - consumer: Account): Promise { + public static async signServiceAgreement( + ddo: DDO, + serviceDefinitionId: string, + serviceAgreementId: string, + consumer: Account, + ): Promise { if (ConfigProvider.getConfig().verbose) { Logger.log("Signing SA with serviceAgreementId", serviceAgreementId) @@ -36,13 +38,15 @@ export default class ServiceAgreement extends OceanBase { return serviceAgreementHashSignature } - public static async executeServiceAgreement(did: DID, - ddo: DDO, - serviceDefinitionId: string, - serviceAgreementId: string, - serviceAgreementHashSignature: string, - consumer: Account, - publisher: Account): Promise { + public static async executeServiceAgreement( + did: DID, + ddo: DDO, + serviceDefinitionId: string, + serviceAgreementId: string, + serviceAgreementHashSignature: string, + consumer: Account, + publisher: Account, + ): Promise { if (ConfigProvider.getConfig().verbose) { Logger.log("Executing SA with serviceAgreementId", serviceAgreementId) diff --git a/test/ocean/Ocean.test.ts b/test/ocean/Ocean.test.ts index f075bfc..e4cf9f1 100644 --- a/test/ocean/Ocean.test.ts +++ b/test/ocean/Ocean.test.ts @@ -9,7 +9,6 @@ import { DDO } from "../../src/ddo/DDO" import { Service } from "../../src/ddo/Service" import Account from "../../src/ocean/Account" import Ocean from "../../src/ocean/Ocean" -import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider" import * as signatureHelpers from "../../src/utils/SignatureHelpers" import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider" @@ -139,36 +138,13 @@ describe("Ocean", () => { await consumer.requestTokens(metadata.base.price) - const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, - service.serviceDefinitionId, consumer) + const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, service.serviceDefinitionId, consumer) assert(signServiceAgreementResult) - assert(signServiceAgreementResult.serviceAgreementId, "no serviceAgreementId") - assert(signServiceAgreementResult.serviceAgreementSignature, "no serviceAgreementSignature") - assert(signServiceAgreementResult.serviceAgreementSignature.startsWith("0x")) - assert(signServiceAgreementResult.serviceAgreementSignature.length === 132) - }) - }) - - describe("#executeServiceAgreement()", () => { - it("should execute a service agreement", async () => { - const publisher = accounts[0] - const consumer = accounts[1] - - const ddo: DDO = await ocean.registerAsset(metadata, publisher) - const service: Service = ddo.findServiceByType("Access") - - // @ts-ignore - WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) - - const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, - service.serviceDefinitionId, consumer) - - const serviceAgreement: ServiceAgreement = await ocean.executeServiceAgreement(ddo.id, - service.serviceDefinitionId, signServiceAgreementResult.serviceAgreementId, - signServiceAgreementResult.serviceAgreementSignature, consumer, publisher) - - assert(serviceAgreement) + assert(signServiceAgreementResult.agreementId, "no agreementId") + assert(signServiceAgreementResult.signature, "no signature") + assert(signServiceAgreementResult.signature.startsWith("0x")) + assert(signServiceAgreementResult.signature.length === 132) }) }) })