From 85496f70027705c72f99527858724a1750a4f3f3 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Fri, 14 Dec 2018 16:11:26 +0100 Subject: [PATCH 1/4] added the check for the status of the template that is used to sign the service agreement --- src/ocean/ServiceAgreements/ServiceAgreement.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index d274450..c5e6f37 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -109,6 +109,12 @@ export default class ServiceAgreement extends OceanBase { throw new Error(`TemplateId not found in service "${service.type}" ddo.`) } + const templateActive = await serviceAgreement.getTemplateStatus(service.templateId) + + if (!templateActive) { + throw new Error(`Template with id ${service.templateId} is not active.`) + } + const executeAgreementReceipt = await serviceAgreement .executeAgreement( service.templateId, From 77a410c607cd67b1dbdda3ad68dc8ea9364aa3d9 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Fri, 14 Dec 2018 16:12:23 +0100 Subject: [PATCH 2/4] removed file-saver and added a callback with the content --- src/examples/InitializeAgreement.ts | 4 +- src/ocean/Ocean.ts | 72 ++++++++++++++++------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/examples/InitializeAgreement.ts b/src/examples/InitializeAgreement.ts index 8fde379..c69d4f8 100644 --- a/src/examples/InitializeAgreement.ts +++ b/src/examples/InitializeAgreement.ts @@ -63,6 +63,8 @@ import config from "./config" service.serviceDefinitionId, serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementSignature, - consumer) + (files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`), + consumer, + ) })() diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 23ee5be..b07818b 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -1,4 +1,3 @@ -import {saveAs} from "file-saver" import AquariusProvider from "../aquarius/AquariusProvider" import SearchQuery from "../aquarius/query/SearchQuery" import BrizoProvider from "../brizo/BrizoProvider" @@ -189,39 +188,13 @@ export default class Ocean { ) Logger.log("Completed asset payment, now access should be granted.") }) - const accessEvent: ContractEvent = EventListener.subscribe( - accessService.conditions[1].contractName, - accessService.conditions[1].events[1].name, {}) - accessEvent.listenOnce(async (data) => { - Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.") - const webConnector = WebServiceConnectorProvider.getConnector() - const contentUrls = await SecretStoreProvider.getSecretStore() - .decryptDocument(id, metadataService.metadata.base.contentUrls[0]) - const serviceUrl: string = accessService.serviceEndpoint - Logger.log("Consuming asset files using service url: ", serviceUrl) - for (const cUrl of contentUrls) { - let url: string = serviceUrl + `?url=${cUrl}` - url = url + `&serviceAgreementId=${serviceAgreementId}` - url = url + `&consumerAddress=${consumer.getId()}` - Logger.log("Fetching asset from: ", url) - const response: any = await webConnector.get(url) - const buffer: Buffer = await response.buffer() - const parts: string[] = cUrl.split("/") - const filename: string = parts[parts.length - 1] - Logger.debug(`Got response: filename is ${filename}, url is ${response.url}`) - const target = `${__dirname}/downloads/${filename}` - await saveAs([buffer.toString("utf8")], filename) - Logger.log("saved file to:", target) - } - Logger.log("Done downloading asset files.") - }) return { serviceAgreementId, serviceAgreementSignature, } - } catch (err) { + } catch (err) { Logger.error("Signing ServiceAgreement failed!", err) } } @@ -230,8 +203,47 @@ export default class Ocean { serviceDefinitionId: string, serviceAgreementId: string, serviceAgreementSignature: string, + cb, consumer: Account) { - const result = await BrizoProvider + + const ddo = await AquariusProvider.getAquarius().retrieveDDO(did) + const id = did.replace("did:op:", "") + + const accessService: Service = ddo.findServiceByType("Access") + const metadataService: Service = ddo.findServiceByType("Metadata") + + const accessEvent: ContractEvent = EventListener.subscribe( + accessService.conditions[1].contractName, + accessService.conditions[1].events[1].name, {}) + + accessEvent.listenOnce(async () => { + Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.") + const webConnector = WebServiceConnectorProvider.getConnector() + const contentUrls = await SecretStoreProvider + .getSecretStore() + .decryptDocument(id, metadataService.metadata.base.contentUrls[0]) + const serviceUrl: string = accessService.serviceEndpoint + Logger.log("Consuming asset files using service url: ", serviceUrl) + const files = [] + + for (const cUrl of contentUrls) { + let url: string = serviceUrl + `?url=${cUrl}` + url = url + `&serviceAgreementId=${serviceAgreementId}` + url = url + `&consumerAddress=${consumer.getId()}` + Logger.log("Fetching asset from: ", url) + const response: any = await webConnector.get(url) + const responseBuffer: Buffer = await response.buffer() + const urlParts: string[] = cUrl.split("/") + const filename: string = urlParts[urlParts.length - 1] + Logger.debug(`Got response: filename is ${filename}, url is ${response.url}`) + files.push(responseBuffer.toString("utf8")) + } + Logger.log("Done downloading asset files.") + + cb(files) + }) + + await BrizoProvider .getBrizo() .initializeServiceAgreement( did, @@ -239,8 +251,6 @@ export default class Ocean { serviceDefinitionId, serviceAgreementSignature, consumer.getId()) - - Logger.log(result.status) } public async executeServiceAgreement(did: string, From c806ba172b1430565b507978834813a92dcd7758 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Fri, 14 Dec 2018 16:12:36 +0100 Subject: [PATCH 3/4] update packages --- package-lock.json | 11 +++-------- package.json | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index b3e6d96..a72d3d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -141,9 +141,9 @@ "integrity": "sha512-lQDFUhFgQkAnbKtH5nuYXr+IyUy4HC6Z90Ipc0T8OTS4VcvLqbt7F+24scb8W8K9p3suh+eTuEnfPIbcKJqE7A==" }, "@oceanprotocol/secret-store-client": { - "version": "0.0.13", - "resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.13.tgz", - "integrity": "sha512-uEofITszJh72IAHcjXBzPx1I2xAxNUJ5kEDFako9jhQqz0seR1xGP40gj/fp+cmGU8lIq30dKWeGcYZisf735Q==", + "version": "0.0.14", + "resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.14.tgz", + "integrity": "sha512-Yo2/9MJC1vlWXGIaU+35naYHWWYGbtzTu2t5dW4Ro1JuIRkzxfM3TSNzPUAgsCrUBUxsCsu9pd/RjvPuE45SGw==", "requires": { "node-fetch": "^2.3.0" } @@ -2350,11 +2350,6 @@ "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==", "dev": true }, - "file-saver": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.0.tgz", - "integrity": "sha512-cYM1ic5DAkg25pHKgi5f10ziAM7RJU37gaH1XQlyNDrtUnzhC/dfoV9zf2OmF0RMKi42jG5B0JWBnPQqyj/G6g==" - }, "file-type": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/file-type/-/file-type-5.2.0.tgz", diff --git a/package.json b/package.json index 3d6a0fb..5fc9d1f 100644 --- a/package.json +++ b/package.json @@ -52,12 +52,11 @@ "homepage": "https://github.com/oceanprotocol/squid-js#readme", "dependencies": { "@oceanprotocol/keeper-contracts": "~0.5.0", - "@oceanprotocol/secret-store-client": "~0.0.13", + "@oceanprotocol/secret-store-client": "~0.0.14", "bignumber.js": "^8.0.1", "eth-crypto": "^1.2.7", "eth-ecies": "^1.0.3", "ethereumjs-util": "^6.0.0", - "file-saver": "^2.0.0", "jsonwebtoken": "^8.4.0", "node-fetch": "^2.3.0", "uuid": "^3.3.2", From 3e7d45a67d10caeb1a9f8e2ee67dc48a0f11ad6f Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Fri, 14 Dec 2018 17:06:18 +0100 Subject: [PATCH 4/4] v0.2.4 --- .bumpversion.cfg | 2 +- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index a4db740..edd41f7 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.2.3 +current_version = 0.2.4 [bumpversion:file:package.json] diff --git a/package-lock.json b/package-lock.json index a72d3d9..12af74f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@oceanprotocol/squid", - "version": "0.2.3", + "version": "0.2.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5fc9d1f..8a591f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oceanprotocol/squid", - "version": "0.2.3", + "version": "0.2.4", "description": "JavaScript client library for Ocean Protocol", "main": "./dist/node/squid.js", "browser": "./dist/browser/squid.cjs2.min.js",