diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index 5714ec5..7b04f90 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -2,7 +2,7 @@ import {URL} from "whatwg-url" import DDO from "../ddo/DDO" import Config from "../models/Config" import Logger from "../utils/Logger" -import AquariusConnectorProvider from "./AquariusConnectorProvider" +import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider" import SearchQuery from "./query/SearchQuery" export default class Aquarius { @@ -16,7 +16,7 @@ export default class Aquarius { public async getAccessUrl(accessToken: any, payload: any): Promise { - const accessUrl: string = await AquariusConnectorProvider.getConnector() + const accessUrl: string = await WebServiceConnectorProvider.getConnector() .post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload) .then((response: any): string => { if (response.ok) { @@ -39,7 +39,7 @@ export default class Aquarius { public async queryMetadata(query: SearchQuery): Promise { - const result: DDO[] = await AquariusConnectorProvider.getConnector() + const result: DDO[] = await WebServiceConnectorProvider.getConnector() .post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query)) .then((response: any) => { if (response.ok) { @@ -68,7 +68,7 @@ export default class Aquarius { fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort))) fullUrl.searchParams.append("offset", query.offset.toString()) fullUrl.searchParams.append("page", query.page.toString()) - const result: DDO[] = await AquariusConnectorProvider.getConnector() + const result: DDO[] = await WebServiceConnectorProvider.getConnector() .get(fullUrl) .then((response: any) => { if (response.ok) { @@ -92,7 +92,7 @@ export default class Aquarius { public async storeDDO(ddo: DDO): Promise { const fullUrl = this.url + `/api/v1/aquarius/assets/ddo` - const result: DDO = await AquariusConnectorProvider.getConnector() + const result: DDO = await WebServiceConnectorProvider.getConnector() .post(fullUrl, DDO.serialize(ddo)) .then((response: any) => { if (response.ok) { @@ -114,7 +114,7 @@ export default class Aquarius { public async retrieveDDO(did: string): Promise { const fullUrl = this.url + `/api/v1/aquarius/assets/ddo/${did}` - const result = await AquariusConnectorProvider.getConnector() + const result = await WebServiceConnectorProvider.getConnector() .get(fullUrl) .then((response: any) => { if (response.ok) { diff --git a/src/aquarius/AquariusConnectorProvider.ts b/src/aquarius/AquariusConnectorProvider.ts deleted file mode 100644 index 5b00b31..0000000 --- a/src/aquarius/AquariusConnectorProvider.ts +++ /dev/null @@ -1,19 +0,0 @@ -import AquariusConnector from "./AquariusConnector" - -export default class AquariusConnectorProvider { - - public static setConnector(connector: AquariusConnector) { - - AquariusConnectorProvider.connector = connector - } - - public static getConnector(): AquariusConnector { - - if (!AquariusConnectorProvider.connector) { - AquariusConnectorProvider.connector = new AquariusConnector() - } - return AquariusConnectorProvider.connector - } - - private static connector: AquariusConnector = null -} diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index c7ab4fa..e0f1500 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -1,4 +1,5 @@ import Config from "../models/Config" +import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider" export default class Brizo { private url: string @@ -20,4 +21,19 @@ export default class Brizo { // tslint:disable-next-line return `${this.url}/api/v1/brizo/services/compute?pubKey=${pubKey}&serviceId=${serviceId}&algo=${algo}&container=${container}"` } + + public async initializeServiceAgreement(did: string, serviceAgreementId: string, serviceDefinitionId: string, + signature: string, consumerPublicKey: string): Promise { + + return WebServiceConnectorProvider.getConnector().post( + `${this.url}/api/v1/brizo/services/access/initialize`, + { + did, + serviceAgreementId, + serviceDefinitionId, + signature, + consumerPublicKey, + }) + + } } diff --git a/src/examples/InitializeAgreement.ts b/src/examples/InitializeAgreement.ts new file mode 100644 index 0000000..477a1be --- /dev/null +++ b/src/examples/InitializeAgreement.ts @@ -0,0 +1,68 @@ +import DDO from "../ddo/DDO" +import MetaData from "../ddo/MetaData" +import MetaDataBase from "../ddo/MetaDataBase" +import Service from "../ddo/Service" +import {Account, Logger, Ocean} from "../squid" + +(async () => { + const ocean: Ocean = await Ocean.getInstance({ + nodeUri: "http://localhost:8545", + aquariusUri: "http://localhost:5000", + brizoUri: "http://localhost:8030", + parityUri: "http://localhost:9545", + secretStoreUri: "https://secret-store.dev-ocean.com", + threshold: 2, + password: "unittest", + address: "0xed243adfb84a6626eba46178ccb567481c6e655d", + }) + + const publisher: Account = (await ocean.getAccounts())[0] + const consumer: Account = (await ocean.getAccounts())[1] + + const metaData = new MetaData({ + base: { + name: "Office Humidity", + type: "dataset", + description: "Weather information of UK including temperature and humidity", + size: "3.1gb", + dateCreated: "2012-02-01T10:55:11+00:00", + author: "Met Office", + license: "CC-BY", + copyrightHolder: "Met Office", + encoding: "UTF-8", + compression: "zip", + contentType: "text/csv", + // tslint:disable-next-line + workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68", + contentUrls: [ + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + ], + links: [ + {sample1: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/"}, + {sample2: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/"}, + {fieldsDescription: "http://data.ceda.ac.uk/badc/ukcp09/"}, + ], + inLanguage: "en", + tags: "weather, uk, 2011, temperature, humidity", + price: 10, + } as MetaDataBase, + } as MetaData) + + const ddo: DDO = await ocean.registerAsset(metaData, publisher) + Logger.log("did", ddo.id) + + const accessService = ddo.findServiceByType("Access") + + const serviceAgreementSignatureResult: any = await ocean.signServiceAgreement(ddo.id, + accessService.serviceDefinitionId, consumer) + Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId) + Logger.log("ServiceAgreement Signature:", serviceAgreementSignatureResult.serviceAgreementSignature) + + const service: Service = ddo.findServiceByType("Access") + + await ocean.initializeServiceAgreement(ddo.id, service.serviceDefinitionId, + serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementSignature, + consumer) + +})() diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index b83c1b7..3d07354 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -50,6 +50,11 @@ export default class Ocean { return ethAccounts.map((address: string) => new Account(address)) } + public async resolveDID(did): Promise { + + return AquariusProvider.getAquarius().retrieveDDO(did) + } + public async registerAsset(metadata: MetaData, publisher: Account): Promise { const {didRegistry} = this.keeper @@ -157,6 +162,14 @@ export default class Ocean { } } + public async initializeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string, + serviceAgreementSignature: string, consumer: Account) { + const result = await BrizoProvider.getBrizo().initializeServiceAgreement(did, serviceAgreementId, + serviceDefinitionId, serviceAgreementSignature, await consumer.getPublicKey()) + + Logger.log(result) + } + public async executeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string, serviceAgreementSignature: string, consumer: Account, publisher: Account) : Promise { diff --git a/src/aquarius/AquariusConnector.ts b/src/utils/WebServiceConnector.ts similarity index 95% rename from src/aquarius/AquariusConnector.ts rename to src/utils/WebServiceConnector.ts index d4e0ad3..3e1b5d2 100644 --- a/src/aquarius/AquariusConnector.ts +++ b/src/utils/WebServiceConnector.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch" -export default class AquariusConnector { +export default class WebServiceConnector { public async post(url, payload): Promise { return this.fetch(url, { diff --git a/src/utils/WebServiceConnectorProvider.ts b/src/utils/WebServiceConnectorProvider.ts new file mode 100644 index 0000000..2078d6e --- /dev/null +++ b/src/utils/WebServiceConnectorProvider.ts @@ -0,0 +1,19 @@ +import WebServiceConnector from "./WebServiceConnector" + +export default class WebServiceConnectorProvider { + + public static setConnector(connector: WebServiceConnector) { + + WebServiceConnectorProvider.instance = connector + } + + public static getConnector(): WebServiceConnector { + + if (!WebServiceConnectorProvider.instance) { + WebServiceConnectorProvider.instance = new WebServiceConnector() + } + return WebServiceConnectorProvider.instance + } + + private static instance: WebServiceConnector = null +} diff --git a/test/aquarius/Aquarius.test.ts b/test/aquarius/Aquarius.test.ts index 847b6f6..8b8a031 100644 --- a/test/aquarius/Aquarius.test.ts +++ b/test/aquarius/Aquarius.test.ts @@ -1,11 +1,11 @@ import * as assert from "assert" import Aquarius from "../../src/aquarius/Aquarius" -import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider" import SearchQuery from "../../src/aquarius/query/SearchQuery" import DDO from "../../src/ddo/DDO" import IdGenerator from "../../src/ocean/IdGenerator" +import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider" import config from "../config" -import AquariusConnectorMock from "../mocks/AquariusConnector.mock" +import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock" describe("Aquarius", () => { @@ -27,7 +27,7 @@ describe("Aquarius", () => { it("should query metadata", async () => { // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) const result: DDO[] = await aquarius.queryMetadata(query) assert(result) @@ -37,7 +37,7 @@ describe("Aquarius", () => { it("should query metadata and return real ddo", async () => { // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) const result: DDO[] = await aquarius.queryMetadata(query) assert(result) @@ -62,7 +62,7 @@ describe("Aquarius", () => { it("should query metadata by text", async () => { // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) const result: DDO[] = await aquarius.queryMetadataByText(query) assert(result) @@ -72,7 +72,7 @@ describe("Aquarius", () => { it("should query metadata and return real ddo", async () => { // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()])) const result: DDO[] = await aquarius.queryMetadataByText(query) assert(result) @@ -91,7 +91,7 @@ describe("Aquarius", () => { }) // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const result: DDO = await aquarius.storeDDO(ddo) assert(result) @@ -109,7 +109,7 @@ describe("Aquarius", () => { }) // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const storageResult: DDO = await aquarius.storeDDO(ddo) assert(storageResult) diff --git a/test/mocks/Brizo.mock.ts b/test/mocks/Brizo.mock.ts new file mode 100644 index 0000000..3c2ca97 --- /dev/null +++ b/test/mocks/Brizo.mock.ts @@ -0,0 +1,10 @@ +import Brizo from "../../src/brizo/Brizo" + +export default class BrizoMock extends Brizo { + + public async initializeServiceAgreement(did: string, serviceAgreementId: string, serviceDefinitionId: string, + signature: string, consumerPublicKey: string): Promise { + + return true + } +} diff --git a/test/mocks/AquariusConnector.mock.ts b/test/mocks/WebServiceConnector.mock.ts similarity index 78% rename from test/mocks/AquariusConnector.mock.ts rename to test/mocks/WebServiceConnector.mock.ts index 74e2f5b..f5f57fb 100644 --- a/test/mocks/AquariusConnector.mock.ts +++ b/test/mocks/WebServiceConnector.mock.ts @@ -1,7 +1,7 @@ -import AquariusConnector from "../../src/aquarius/AquariusConnector" +import WebServiceConnector from "../../src/utils/WebServiceConnector" // @ts-ignore -export default class AquariusConnectorMock extends AquariusConnector { +export default class WebServiceConnectorMock extends WebServiceConnector { constructor(private returnData: any) { super() diff --git a/test/ocean/Ocean.test.ts b/test/ocean/Ocean.test.ts index 98e4abc..39c0f17 100644 --- a/test/ocean/Ocean.test.ts +++ b/test/ocean/Ocean.test.ts @@ -1,7 +1,7 @@ import {assert} from "chai" -import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider" import AquariusProvider from "../../src/aquarius/AquariusProvider" import SearchQuery from "../../src/aquarius/query/SearchQuery" +import BrizoProvider from "../../src/brizo/BrizoProvider" import ConfigProvider from "../../src/ConfigProvider" import DDO from "../../src/ddo/DDO" import MetaData from "../../src/ddo/MetaData" @@ -10,11 +10,13 @@ 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 WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider" import config from "../config" import TestContractHandler from "../keeper/TestContractHandler" import AquariusMock from "../mocks/Aquarius.mock" -import AquariusConnectorMock from "../mocks/AquariusConnector.mock" +import BrizoMock from "../mocks/Brizo.mock" import SecretStoreMock from "../mocks/SecretStore.mock" +import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock" let ocean: Ocean let accounts: Account[] @@ -25,6 +27,7 @@ describe("Ocean", () => { before(async () => { ConfigProvider.setConfig(config) AquariusProvider.setAquarius(new AquariusMock(config)) + BrizoProvider.setBrizo(new BrizoMock(config)) SecretStoreProvider.setSecretStore(new SecretStoreMock(config)) await TestContractHandler.prepareContracts() ocean = await Ocean.getInstance(config) @@ -56,6 +59,21 @@ describe("Ocean", () => { }) + describe("#resolveDID()", () => { + + it("should resolve a did to a ddo", async () => { + + const metaData: MetaData = new MetaData() + const ddo: DDO = await ocean.registerAsset(metaData, testPublisher) + + const resolvedDDO: DDO = await ocean.resolveDID(ddo.id) + + assert(resolvedDDO) + assert(resolvedDDO.id === ddo.id) + }) + + }) + describe("#registerAsset()", () => { it("should register an asset", async () => { @@ -114,7 +132,7 @@ describe("Ocean", () => { const service: Service = ddo.findServiceByType("Access") // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id, service.serviceDefinitionId, consumer) @@ -138,7 +156,7 @@ describe("Ocean", () => { const service: Service = ddo.findServiceByType("Access") // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, service.serviceDefinitionId, consumer) diff --git a/test/ocean/ServiceAgreement.test.ts b/test/ocean/ServiceAgreement.test.ts index 7157388..86fdbd4 100644 --- a/test/ocean/ServiceAgreement.test.ts +++ b/test/ocean/ServiceAgreement.test.ts @@ -1,5 +1,4 @@ import {assert} from "chai" -import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider" import ConfigProvider from "../../src/ConfigProvider" import DDOCondition from "../../src/ddo/Condition" import DDO from "../../src/ddo/DDO" @@ -12,9 +11,10 @@ import Condition from "../../src/ocean/ServiceAgreements/Condition" import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate" import Access from "../../src/ocean/ServiceAgreements/Templates/Access" +import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider" import config from "../config" import TestContractHandler from "../keeper/TestContractHandler" -import AquariusConnectorMock from "../mocks/AquariusConnector.mock" +import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock" let ocean: Ocean let accounts: Account[] @@ -74,7 +74,8 @@ describe("ServiceAgreement", () => { const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) + const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, serviceAgreementId, consumerAccount) @@ -94,7 +95,7 @@ describe("ServiceAgreement", () => { const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, serviceAgreementId, consumerAccount) @@ -120,7 +121,7 @@ describe("ServiceAgreement", () => { const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, serviceAgreementId, consumerAccount) @@ -146,7 +147,7 @@ describe("ServiceAgreement", () => { const serviceAgreementId: string = IdGenerator.generateId() // @ts-ignore - AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo)) const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, serviceAgreementId, consumerAccount)