1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

added connection to brizo

This commit is contained in:
Sebastian Gerske 2018-11-19 12:16:11 +01:00
parent 8a17c2b06c
commit a2bfe0a816
12 changed files with 172 additions and 46 deletions

View File

@ -2,7 +2,7 @@ import {URL} from "whatwg-url"
import DDO from "../ddo/DDO" import DDO from "../ddo/DDO"
import Config from "../models/Config" import Config from "../models/Config"
import Logger from "../utils/Logger" import Logger from "../utils/Logger"
import AquariusConnectorProvider from "./AquariusConnectorProvider" import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
import SearchQuery from "./query/SearchQuery" import SearchQuery from "./query/SearchQuery"
export default class Aquarius { export default class Aquarius {
@ -16,7 +16,7 @@ export default class Aquarius {
public async getAccessUrl(accessToken: any, payload: any): Promise<string> { public async getAccessUrl(accessToken: any, payload: any): Promise<string> {
const accessUrl: string = await AquariusConnectorProvider.getConnector() const accessUrl: string = await WebServiceConnectorProvider.getConnector()
.post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload) .post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload)
.then((response: any): string => { .then((response: any): string => {
if (response.ok) { if (response.ok) {
@ -39,7 +39,7 @@ export default class Aquarius {
public async queryMetadata(query: SearchQuery): Promise<DDO[]> { public async queryMetadata(query: SearchQuery): Promise<DDO[]> {
const result: DDO[] = await AquariusConnectorProvider.getConnector() const result: DDO[] = await WebServiceConnectorProvider.getConnector()
.post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query)) .post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query))
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
@ -68,7 +68,7 @@ export default class Aquarius {
fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort))) fullUrl.searchParams.append("sort", decodeURIComponent(JSON.stringify(query.sort)))
fullUrl.searchParams.append("offset", query.offset.toString()) fullUrl.searchParams.append("offset", query.offset.toString())
fullUrl.searchParams.append("page", query.page.toString()) fullUrl.searchParams.append("page", query.page.toString())
const result: DDO[] = await AquariusConnectorProvider.getConnector() const result: DDO[] = await WebServiceConnectorProvider.getConnector()
.get(fullUrl) .get(fullUrl)
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
@ -92,7 +92,7 @@ export default class Aquarius {
public async storeDDO(ddo: DDO): Promise<DDO> { public async storeDDO(ddo: DDO): Promise<DDO> {
const fullUrl = this.url + `/api/v1/aquarius/assets/ddo` 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)) .post(fullUrl, DDO.serialize(ddo))
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
@ -114,7 +114,7 @@ export default class Aquarius {
public async retrieveDDO(did: string): Promise<DDO> { public async retrieveDDO(did: string): Promise<DDO> {
const fullUrl = this.url + `/api/v1/aquarius/assets/ddo/${did}` const fullUrl = this.url + `/api/v1/aquarius/assets/ddo/${did}`
const result = await AquariusConnectorProvider.getConnector() const result = await WebServiceConnectorProvider.getConnector()
.get(fullUrl) .get(fullUrl)
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {

View File

@ -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
}

View File

@ -1,4 +1,5 @@
import Config from "../models/Config" import Config from "../models/Config"
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
export default class Brizo { export default class Brizo {
private url: string private url: string
@ -20,4 +21,19 @@ export default class Brizo {
// tslint:disable-next-line // tslint:disable-next-line
return `${this.url}/api/v1/brizo/services/compute?pubKey=${pubKey}&serviceId=${serviceId}&algo=${algo}&container=${container}"` 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<any> {
return WebServiceConnectorProvider.getConnector().post(
`${this.url}/api/v1/brizo/services/access/initialize`,
{
did,
serviceAgreementId,
serviceDefinitionId,
signature,
consumerPublicKey,
})
}
} }

View File

@ -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)
})()

View File

@ -50,6 +50,11 @@ export default class Ocean {
return ethAccounts.map((address: string) => new Account(address)) return ethAccounts.map((address: string) => new Account(address))
} }
public async resolveDID(did): Promise<DDO> {
return AquariusProvider.getAquarius().retrieveDDO(did)
}
public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> { public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> {
const {didRegistry} = this.keeper 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, public async executeServiceAgreement(did: string, serviceDefinitionId: string, serviceAgreementId: string,
serviceAgreementSignature: string, consumer: Account, publisher: Account) serviceAgreementSignature: string, consumer: Account, publisher: Account)
: Promise<ServiceAgreement> { : Promise<ServiceAgreement> {

View File

@ -1,6 +1,6 @@
import fetch from "node-fetch" import fetch from "node-fetch"
export default class AquariusConnector { export default class WebServiceConnector {
public async post(url, payload): Promise<any> { public async post(url, payload): Promise<any> {
return this.fetch(url, { return this.fetch(url, {

View File

@ -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
}

View File

@ -1,11 +1,11 @@
import * as assert from "assert" import * as assert from "assert"
import Aquarius from "../../src/aquarius/Aquarius" import Aquarius from "../../src/aquarius/Aquarius"
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
import SearchQuery from "../../src/aquarius/query/SearchQuery" import SearchQuery from "../../src/aquarius/query/SearchQuery"
import DDO from "../../src/ddo/DDO" import DDO from "../../src/ddo/DDO"
import IdGenerator from "../../src/ocean/IdGenerator" import IdGenerator from "../../src/ocean/IdGenerator"
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
import config from "../config" import config from "../config"
import AquariusConnectorMock from "../mocks/AquariusConnector.mock" import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
describe("Aquarius", () => { describe("Aquarius", () => {
@ -27,7 +27,7 @@ describe("Aquarius", () => {
it("should query metadata", async () => { it("should query metadata", async () => {
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()]))
const result: DDO[] = await aquarius.queryMetadata(query) const result: DDO[] = await aquarius.queryMetadata(query)
assert(result) assert(result)
@ -37,7 +37,7 @@ describe("Aquarius", () => {
it("should query metadata and return real ddo", async () => { it("should query metadata and return real ddo", async () => {
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()]))
const result: DDO[] = await aquarius.queryMetadata(query) const result: DDO[] = await aquarius.queryMetadata(query)
assert(result) assert(result)
@ -62,7 +62,7 @@ describe("Aquarius", () => {
it("should query metadata by text", async () => { it("should query metadata by text", async () => {
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()]))
const result: DDO[] = await aquarius.queryMetadataByText(query) const result: DDO[] = await aquarius.queryMetadataByText(query)
assert(result) assert(result)
@ -72,7 +72,7 @@ describe("Aquarius", () => {
it("should query metadata and return real ddo", async () => { it("should query metadata and return real ddo", async () => {
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock([new DDO()])) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock([new DDO()]))
const result: DDO[] = await aquarius.queryMetadataByText(query) const result: DDO[] = await aquarius.queryMetadataByText(query)
assert(result) assert(result)
@ -91,7 +91,7 @@ describe("Aquarius", () => {
}) })
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const result: DDO = await aquarius.storeDDO(ddo) const result: DDO = await aquarius.storeDDO(ddo)
assert(result) assert(result)
@ -109,7 +109,7 @@ describe("Aquarius", () => {
}) })
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const storageResult: DDO = await aquarius.storeDDO(ddo) const storageResult: DDO = await aquarius.storeDDO(ddo)
assert(storageResult) assert(storageResult)

10
test/mocks/Brizo.mock.ts Normal file
View File

@ -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<any> {
return true
}
}

View File

@ -1,7 +1,7 @@
import AquariusConnector from "../../src/aquarius/AquariusConnector" import WebServiceConnector from "../../src/utils/WebServiceConnector"
// @ts-ignore // @ts-ignore
export default class AquariusConnectorMock extends AquariusConnector { export default class WebServiceConnectorMock extends WebServiceConnector {
constructor(private returnData: any) { constructor(private returnData: any) {
super() super()

View File

@ -1,7 +1,7 @@
import {assert} from "chai" import {assert} from "chai"
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
import AquariusProvider from "../../src/aquarius/AquariusProvider" import AquariusProvider from "../../src/aquarius/AquariusProvider"
import SearchQuery from "../../src/aquarius/query/SearchQuery" import SearchQuery from "../../src/aquarius/query/SearchQuery"
import BrizoProvider from "../../src/brizo/BrizoProvider"
import ConfigProvider from "../../src/ConfigProvider" import ConfigProvider from "../../src/ConfigProvider"
import DDO from "../../src/ddo/DDO" import DDO from "../../src/ddo/DDO"
import MetaData from "../../src/ddo/MetaData" import MetaData from "../../src/ddo/MetaData"
@ -10,11 +10,13 @@ import Account from "../../src/ocean/Account"
import Ocean from "../../src/ocean/Ocean" import Ocean from "../../src/ocean/Ocean"
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider" import SecretStoreProvider from "../../src/secretstore/SecretStoreProvider"
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
import config from "../config" import config from "../config"
import TestContractHandler from "../keeper/TestContractHandler" import TestContractHandler from "../keeper/TestContractHandler"
import AquariusMock from "../mocks/Aquarius.mock" 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 SecretStoreMock from "../mocks/SecretStore.mock"
import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
let ocean: Ocean let ocean: Ocean
let accounts: Account[] let accounts: Account[]
@ -25,6 +27,7 @@ describe("Ocean", () => {
before(async () => { before(async () => {
ConfigProvider.setConfig(config) ConfigProvider.setConfig(config)
AquariusProvider.setAquarius(new AquariusMock(config)) AquariusProvider.setAquarius(new AquariusMock(config))
BrizoProvider.setBrizo(new BrizoMock(config))
SecretStoreProvider.setSecretStore(new SecretStoreMock(config)) SecretStoreProvider.setSecretStore(new SecretStoreMock(config))
await TestContractHandler.prepareContracts() await TestContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config) 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()", () => { describe("#registerAsset()", () => {
it("should register an asset", async () => { it("should register an asset", async () => {
@ -114,7 +132,7 @@ describe("Ocean", () => {
const service: Service = ddo.findServiceByType("Access") const service: Service = ddo.findServiceByType("Access")
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id, const serviceAgreementSignature: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer) service.serviceDefinitionId, consumer)
@ -138,7 +156,7 @@ describe("Ocean", () => {
const service: Service = ddo.findServiceByType("Access") const service: Service = ddo.findServiceByType("Access")
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id, const signServiceAgreementResult: any = await ocean.signServiceAgreement(ddo.id,
service.serviceDefinitionId, consumer) service.serviceDefinitionId, consumer)

View File

@ -1,5 +1,4 @@
import {assert} from "chai" import {assert} from "chai"
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
import ConfigProvider from "../../src/ConfigProvider" import ConfigProvider from "../../src/ConfigProvider"
import DDOCondition from "../../src/ddo/Condition" import DDOCondition from "../../src/ddo/Condition"
import DDO from "../../src/ddo/DDO" 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 ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate" import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
import Access from "../../src/ocean/ServiceAgreements/Templates/Access" import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
import config from "../config" import config from "../config"
import TestContractHandler from "../keeper/TestContractHandler" import TestContractHandler from "../keeper/TestContractHandler"
import AquariusConnectorMock from "../mocks/AquariusConnector.mock" import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
let ocean: Ocean let ocean: Ocean
let accounts: Account[] let accounts: Account[]
@ -74,7 +74,8 @@ describe("ServiceAgreement", () => {
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount) serviceAgreementId, consumerAccount)
@ -94,7 +95,7 @@ describe("ServiceAgreement", () => {
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount) serviceAgreementId, consumerAccount)
@ -120,7 +121,7 @@ describe("ServiceAgreement", () => {
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount) serviceAgreementId, consumerAccount)
@ -146,7 +147,7 @@ describe("ServiceAgreement", () => {
const serviceAgreementId: string = IdGenerator.generateId() const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore // @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string = const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId, await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount) serviceAgreementId, consumerAccount)