From 48c4f6459910069c79424ac67fc6a0d28a9d1839 Mon Sep 17 00:00:00 2001 From: ssallam Date: Fri, 17 Jan 2020 15:35:11 +0100 Subject: [PATCH] Fix tests. --- .idea/encodings.xml | 4 + .idea/inspectionProfiles/Project_Default.xml | 6 + .idea/markdown-navigator.xml | 86 ++++ .../markdown-navigator/profiles_settings.xml | 3 + .idea/misc.xml | 10 + .idea/modules.xml | 8 + .idea/squid-js.iml | 12 + .idea/typescript-compiler.xml | 6 + .idea/vcs.xml | 6 + integration/config.js | 59 +++ integration/ocean/AssetOwners.test.js | 209 ++++++++++ integration/ocean/AuthenticationToken.test.js | 183 +++++++++ integration/ocean/ConsumeAsset.test.js | 268 +++++++++++++ integration/ocean/ConsumeAssetBrizo.test.js | 170 ++++++++ integration/ocean/ConsumeBigAsset.test.js | 160 ++++++++ ...terEscrowAccessSecretStoreTemplate.test.js | 376 ++++++++++++++++++ ...sterEscrowComputeExecutionTemplate.test.js | 374 +++++++++++++++++ ...sterEscrowComputeExecutionTemplate.test.ts | 2 +- integration/ocean/SearchAsset.test.js | 182 +++++++++ integration/ocean/SecretStore.test.js | 75 ++++ integration/ocean/Signature.test.js | 115 ++++++ integration/ocean/Signature.test.ts | 2 +- integration/ocean/Versions.test.js | 74 ++++ integration/utils/ddo-metadata-generator.js | 58 +++ integration/utils/index.js | 6 + src/keeper/Keeper.ts | 14 +- .../managers/TemplateStoreManager.ts | 4 +- .../templates/AgreementTemplateBase.ts | 5 +- .../EscrowAccessSecretStoreTemplate.ts | 2 +- .../EscrowComputeExecutionTemplate.ts | 2 +- test/keeper/TestContractHandler.ts | 19 +- .../EscrowAccessSecretStoreTemplate.test.ts | 24 -- 32 files changed, 2469 insertions(+), 55 deletions(-) create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/markdown-navigator.xml create mode 100644 .idea/markdown-navigator/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/squid-js.iml create mode 100644 .idea/typescript-compiler.xml create mode 100644 .idea/vcs.xml create mode 100644 integration/config.js create mode 100644 integration/ocean/AssetOwners.test.js create mode 100644 integration/ocean/AuthenticationToken.test.js create mode 100644 integration/ocean/ConsumeAsset.test.js create mode 100644 integration/ocean/ConsumeAssetBrizo.test.js create mode 100644 integration/ocean/ConsumeBigAsset.test.js create mode 100644 integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.js create mode 100644 integration/ocean/RegisterEscrowComputeExecutionTemplate.test.js create mode 100644 integration/ocean/SearchAsset.test.js create mode 100644 integration/ocean/SecretStore.test.js create mode 100644 integration/ocean/Signature.test.js create mode 100644 integration/ocean/Versions.test.js create mode 100644 integration/utils/ddo-metadata-generator.js create mode 100644 integration/utils/index.js delete mode 100644 test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df7825d --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..f2aa065 --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..db06266 --- /dev/null +++ b/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fa2aa7b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c396ea6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/squid-js.iml b/.idea/squid-js.iml new file mode 100644 index 0000000..1de84ce --- /dev/null +++ b/.idea/squid-js.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/typescript-compiler.xml b/.idea/typescript-compiler.xml new file mode 100644 index 0000000..1a2fb33 --- /dev/null +++ b/.idea/typescript-compiler.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/integration/config.js b/integration/config.js new file mode 100644 index 0000000..d508aa7 --- /dev/null +++ b/integration/config.js @@ -0,0 +1,59 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +exports.__esModule = true; +var hdwallet_provider_1 = __importDefault(require("@truffle/hdwallet-provider")); +var configJson = { + nodeUri: 'http://localhost:8545', + aquariusUri: 'http://aquarius:5000', + brizoUri: 'http://localhost:8030', + secretStoreUri: 'http://localhost:12001', + brizoAddress: '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0', + verbose: false +}; +if (process.env.NETWORK_NAME === 'pacific') { + Object.assign(configJson, { + nodeUri: 'https://pacific.oceanprotocol.com', + aquariusUri: 'https://aquarius.commons.oceanprotocol.com', + brizoUri: 'https://brizo.commons.oceanprotocol.com', + secretStoreUri: 'https://secret-store.oceanprotocol.com', + brizoAddress: '0x008c25ed3594e094db4592f4115d5fa74c4f41ea' + }); +} +if (process.env.NETWORK_NAME === 'nile') { + Object.assign(configJson, { + nodeUri: 'https://nile.dev-ocean.com', + aquariusUri: 'https://aquarius.nile.dev-ocean.com', + brizoUri: 'https://brizo.nile.dev-ocean.com', + secretStoreUri: 'https://secret-store.nile.dev-ocean.com', + brizoAddress: '0x413c9ba0a05b8a600899b41b0c62dd661e689354' + }); +} +if (process.env.NETWORK_NAME === 'duero') { + Object.assign(configJson, { + nodeUri: 'https://duero.dev-ocean.com', + aquariusUri: 'https://aquarius.duero.dev-ocean.com', + brizoUri: 'https://brizo.duero.dev-ocean.com', + secretStoreUri: 'https://secret-store.duero.dev-ocean.com', + brizoAddress: '0x9d4ed58293f71122ad6a733c1603927a150735d0' + }); +} +if (process.env.SEED_WORDS) { + var seedphrase = process.env.SEED_WORDS; + // @ts-ignore + configJson.web3Provider = new hdwallet_provider_1["default"](seedphrase, configJson.nodeUri, 0, 5); +} +exports.config = configJson; +exports.config.forceVerbose = __assign(__assign({}, configJson), { verbose: true }); diff --git a/integration/ocean/AssetOwners.test.js b/integration/ocean/AssetOwners.test.js new file mode 100644 index 0000000..de5768a --- /dev/null +++ b/integration/ocean/AssetOwners.test.js @@ -0,0 +1,209 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var utils_1 = require("../utils"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Asset Owners', function () { + var ocean; + var account1; + var account2; + var metadata = utils_1.getMetadata(); + before(function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _b.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + _a = _b.sent(), account1 = _a[0], account2 = _a[1]; + if (!ocean.keeper.dispenser) { + metadata = utils_1.getMetadata(0); + } + return [2 /*return*/]; + } + }); + }); }); + it('should set the owner of an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddo, owner; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 1: + ddo = _a.sent(); + return [4 /*yield*/, ocean.assets.owner(ddo.id)]; + case 2: + owner = _a.sent(); + chai_1.assert.equal(owner, account1.getId()); + return [2 /*return*/]; + } + }); + }); }); + it('should set the provider of an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddo, isProvider; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 1: + ddo = _a.sent(); + return [4 /*yield*/, ocean.keeper.didRegistry.isDIDProvider(ddo.id, config_1.config.brizoAddress)]; + case 2: + isProvider = _a.sent(); + chai_1.assert.isTrue(isProvider); + return [2 /*return*/]; + } + }); + }); }); + it('should be added correctly a permission on an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddo, _a, _b, _c, _d; + return __generator(this, function (_e) { + switch (_e.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 1: + ddo = _e.sent(); + _b = (_a = chai_1.assert).isFalse; + return [4 /*yield*/, ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())]; + case 2: + _b.apply(_a, [_e.sent()]); + return [4 /*yield*/, ocean.keeper.didRegistry.grantPermission(ddo.id, account2.getId(), account1.getId())]; + case 3: + _e.sent(); + _d = (_c = chai_1.assert).isTrue; + return [4 /*yield*/, ocean.keeper.didRegistry.getPermission(ddo.id, account2.getId())]; + case 4: + _d.apply(_c, [_e.sent()]); + return [2 /*return*/]; + } + }); + }); }); + it('should get the assets owned by a user', function () { return __awaiter(void 0, void 0, void 0, function () { + var initialLength, finalLength; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.ownerAssets(account2.getId())]; + case 1: + initialLength = (_a.sent()).length; + return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 2: + _a.sent(); + return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 3: + _a.sent(); + return [4 /*yield*/, ocean.assets.create(metadata, account2)]; + case 4: + _a.sent(); + return [4 /*yield*/, ocean.assets.ownerAssets(account2.getId())]; + case 5: + finalLength = (_a.sent()).length; + chai_1.assert.equal(finalLength - initialLength, 1); + return [2 /*return*/]; + } + }); + }); }); + it('should get the assets that can be consumed by a user', function () { return __awaiter(void 0, void 0, void 0, function () { + var initialLength, ddo, finalLength1, _a, _b, _c, _d, _e, _f, _g, index, finalLength2; + return __generator(this, function (_h) { + switch (_h.label) { + case 0: return [4 /*yield*/, ocean.assets.consumerAssets(account2.getId())]; + case 1: + initialLength = (_h.sent()).length; + return [4 /*yield*/, ocean.assets.create(metadata, account1)]; + case 2: + ddo = _h.sent(); + return [4 /*yield*/, ocean.assets.consumerAssets(account2.getId())]; + case 3: + finalLength1 = (_h.sent()).length; + chai_1.assert.equal(finalLength1 - initialLength, 0); + _h.label = 4; + case 4: + _h.trys.push([4, 7, , 8]); + _b = (_a = account2).requestTokens; + _c = +metadata.main.price; + _e = (_d = Math).pow; + _f = [10]; + return [4 /*yield*/, ocean.keeper.token.decimals()]; + case 5: return [4 /*yield*/, _b.apply(_a, [_c * _e.apply(_d, _f.concat([-(_h.sent())]))])]; + case 6: + _h.sent(); + return [3 /*break*/, 8]; + case 7: + _g = _h.sent(); + return [3 /*break*/, 8]; + case 8: + index = ddo.findServiceByType('access').index; + return [4 /*yield*/, ocean.assets.order(ddo.id, index, account2) + // Access granted + ]; + case 9: + _h.sent(); + return [4 /*yield*/, ocean.assets.consumerAssets(account2.getId())]; + case 10: + finalLength2 = (_h.sent()).length; + chai_1.assert.equal(finalLength2 - initialLength, 1); + return [2 /*return*/]; + } + }); + }); }); + it('should be able to transfer ownership', function () { return __awaiter(void 0, void 0, void 0, function () { + var id, newOwner; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, account1) + // transfer + ]; + case 1: + id = (_a.sent()).id; + // transfer + return [4 /*yield*/, ocean.assets.transferOwnership(id, account2.getId())]; + case 2: + // transfer + _a.sent(); + return [4 /*yield*/, ocean.keeper.didRegistry.getDIDOwner(id)]; + case 3: + newOwner = _a.sent(); + chai_1.assert.equal(newOwner, account2.getId()); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/AuthenticationToken.test.js b/integration/ocean/AuthenticationToken.test.js new file mode 100644 index 0000000..d5af4b2 --- /dev/null +++ b/integration/ocean/AuthenticationToken.test.js @@ -0,0 +1,183 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Authentication Token', function () { + var ocean; + var account1; + var account2; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + try { + localStorage.clear(); + } + catch (_c) { } + return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _b.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + _a = _b.sent(), account1 = _a[0], account2 = _a[1]; + return [2 /*return*/]; + } + }); + }); }); + after(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + try { + localStorage.clear(); + } + catch (_b) { } + return [2 /*return*/]; + }); + }); }); + it('should generate a token', function () { return __awaiter(void 0, void 0, void 0, function () { + var token; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.auth.get(account1)]; + case 1: + token = _a.sent(); + chai_1.assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i); + return [2 /*return*/]; + } + }); + }); }); + it('should return the account that signed the token', function () { return __awaiter(void 0, void 0, void 0, function () { + var token, address; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.auth.get(account1)]; + case 1: + token = _a.sent(); + return [4 /*yield*/, ocean.auth.check(token)]; + case 2: + address = _a.sent(); + chai_1.assert.equal(address, account1.getId()); + return [2 /*return*/]; + } + }); + }); }); + it('should store the token for a user', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, _b, _c, _d; + return __generator(this, function (_e) { + switch (_e.label) { + case 0: + _b = (_a = chai_1.assert).isUndefined; + return [4 /*yield*/, account1.getToken()]; + case 1: + _b.apply(_a, [_e.sent()]); + return [4 /*yield*/, ocean.auth.store(account1)]; + case 2: + _e.sent(); + _d = (_c = chai_1.assert).match; + return [4 /*yield*/, account1.getToken()]; + case 3: + _d.apply(_c, [_e.sent(), /^0x[a-f0-9]{130}-[0-9]{0,14}/i]); + return [2 /*return*/]; + } + }); + }); }); + it('should restore the token for a user', function () { return __awaiter(void 0, void 0, void 0, function () { + var token; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.auth.restore(account1)]; + case 1: + token = _a.sent(); + chai_1.assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i); + return [2 /*return*/]; + } + }); + }); }); + it('should return undefined when is not stored', function () { return __awaiter(void 0, void 0, void 0, function () { + var token; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.auth.restore(account2)]; + case 1: + token = _a.sent(); + chai_1.assert.isUndefined(token); + return [2 /*return*/]; + } + }); + }); }); + it('should know if the token is stored', function () { return __awaiter(void 0, void 0, void 0, function () { + var acc1Stored, acc2Stored, _a, _b, _c, _d, _e, _f; + return __generator(this, function (_g) { + switch (_g.label) { + case 0: return [4 /*yield*/, ocean.auth.isStored(account1)]; + case 1: + // eslint-disable-next-line + acc1Stored = _g.sent(); + return [4 /*yield*/, ocean.auth.isStored(account2)]; + case 2: + acc2Stored = _g.sent(); + chai_1.assert.isTrue(acc1Stored); + _b = (_a = chai_1.assert).isTrue; + return [4 /*yield*/, account1.isTokenStored()]; + case 3: + _b.apply(_a, [_g.sent()]); + chai_1.assert.isFalse(acc2Stored); + _d = (_c = chai_1.assert).isFalse; + return [4 /*yield*/, account2.isTokenStored()]; + case 4: + _d.apply(_c, [_g.sent()]); + return [4 /*yield*/, account2.authenticate()]; + case 5: + _g.sent(); + return [4 /*yield*/, ocean.auth.isStored(account2)]; + case 6: + acc2Stored = _g.sent(); + chai_1.assert.isTrue(acc2Stored); + _f = (_e = chai_1.assert).isTrue; + return [4 /*yield*/, account2.isTokenStored()]; + case 7: + _f.apply(_e, [_g.sent()]); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/ConsumeAsset.test.js b/integration/ocean/ConsumeAsset.test.js new file mode 100644 index 0000000..d1b43b2 --- /dev/null +++ b/integration/ocean/ConsumeAsset.test.js @@ -0,0 +1,268 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var fs = __importStar(require("fs")); +var config_1 = require("../config"); +var utils_1 = require("../utils"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Consume Asset', function () { + var ocean; + var publisher; + var consumer; + var metadata = utils_1.getMetadata(); + var ddo; + var serviceAgreementSignatureResult; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _b.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + _a = _b.sent(), publisher = _a[0], consumer = _a[1]; + if (!ocean.keeper.dispenser) { + metadata = utils_1.getMetadata(0); + } + return [2 /*return*/]; + } + }); + }); }); + it('should register an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, publisher)]; + case 1: + ddo = _a.sent(); + chai_1.assert.isDefined(ddo, 'Register has not returned a DDO'); + chai_1.assert.match(ddo.id, /^did:op:[a-f0-9]{64}$/, 'DDO id is not valid'); + chai_1.assert.isAtLeast(ddo.authentication.length, 1, 'Default authentication not added'); + chai_1.assert.isDefined(ddo.findServiceByType('access'), "DDO access service doesn't exist"); + return [2 /*return*/]; + } + }); + }); }); + it('should be able to request tokens for consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var initialBalance, claimedTokens, _a, _b, _c, _d, _e, _f, _g; + return __generator(this, function (_h) { + switch (_h.label) { + case 0: return [4 /*yield*/, consumer.getBalance()]; + case 1: + initialBalance = (_h.sent()).ocn; + _a = +metadata.main.price; + _c = (_b = Math).pow; + _d = [10]; + return [4 /*yield*/, ocean.keeper.token.decimals()]; + case 2: + claimedTokens = _a * _c.apply(_b, _d.concat([-(_h.sent())])); + _h.label = 3; + case 3: + _h.trys.push([3, 5, , 6]); + return [4 /*yield*/, consumer.requestTokens(claimedTokens)]; + case 4: + _h.sent(); + return [3 /*break*/, 6]; + case 5: + _e = _h.sent(); + return [3 /*break*/, 6]; + case 6: + _g = (_f = chai_1.assert).equal; + return [4 /*yield*/, consumer.getBalance()]; + case 7: + _g.apply(_f, [(_h.sent()).ocn, + initialBalance + claimedTokens, + 'OCN Tokens not delivered']); + return [2 /*return*/]; + } + }); + }); }); + it('should sign the service agreement', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, agreementId, signature; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + return [4 /*yield*/, ocean.agreements.prepare(ddo.id, accessService.index, consumer)]; + case 1: + serviceAgreementSignatureResult = _a.sent(); + agreementId = serviceAgreementSignatureResult.agreementId, signature = serviceAgreementSignatureResult.signature; + chai_1.assert.match(agreementId, /^0x[a-f0-9]{64}$/, 'Service agreement ID seems not valid'); + chai_1.assert.match(signature, /^0x[a-f0-9]{130}$/, 'Service agreement signature seems not valid'); + return [2 /*return*/]; + } + }); + }); }); + it('should execute the service agreement', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, success; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + return [4 /*yield*/, ocean.agreements.create(ddo.id, serviceAgreementSignatureResult.agreementId, accessService.index, serviceAgreementSignatureResult.signature, consumer, publisher)]; + case 1: + success = _a.sent(); + chai_1.assert.isTrue(success); + return [2 /*return*/]; + } + }); + }); }); + it('should get the agreement conditions status not fulfilled', function () { return __awaiter(void 0, void 0, void 0, function () { + var status; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.agreements.status(serviceAgreementSignatureResult.agreementId)]; + case 1: + status = _a.sent(); + chai_1.assert.deepEqual(status, { + lockReward: src_1.ConditionState.Unfulfilled, + accessSecretStore: src_1.ConditionState.Unfulfilled, + escrowReward: src_1.ConditionState.Unfulfilled + }); + return [2 /*return*/]; + } + }); + }); }); + it('should lock the payment by the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var paid; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.agreements.conditions.lockReward(serviceAgreementSignatureResult.agreementId, ddo.findServiceByType('metadata').attributes.main.price, consumer)]; + case 1: + paid = _a.sent(); + chai_1.assert.isTrue(paid, 'The asset has not been paid correctly'); + return [2 /*return*/]; + } + }); + }); }); + // The test will fail because Brizo grants the access faster + it('should grant the access by the publisher', function () { return __awaiter(void 0, void 0, void 0, function () { + var granted, accessGranted, _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _b.trys.push([0, 3, , 4]); + return [4 /*yield*/, ocean.agreements.conditions.grantAccess(serviceAgreementSignatureResult.agreementId, ddo.id, consumer.getId(), publisher)]; + case 1: + granted = _b.sent(); + chai_1.assert.isTrue(granted, 'The asset has not been granted correctly'); + return [4 /*yield*/, ocean.keeper.conditions.accessSecretStoreCondition.checkPermissions(consumer.getId(), ddo.id)]; + case 2: + accessGranted = _b.sent(); + chai_1.assert.isTrue(accessGranted, 'Consumer has been granted.'); + return [3 /*break*/, 4]; + case 3: + _a = _b.sent(); + return [3 /*break*/, 4]; + case 4: return [2 /*return*/]; + } + }); + }); }); + it('should get the agreement conditions status fulfilled', function () { return __awaiter(void 0, void 0, void 0, function () { + var status; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.agreements.status(serviceAgreementSignatureResult.agreementId)]; + case 1: + status = _a.sent(); + chai_1.assert.deepEqual(status, { + lockReward: src_1.ConditionState.Fulfilled, + accessSecretStore: src_1.ConditionState.Fulfilled, + escrowReward: src_1.ConditionState.Unfulfilled + }); + return [2 /*return*/]; + } + }); + }); }); + it('should consume and store the assets', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, folder, path, files; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + folder = '/tmp/ocean/squid-js-1'; + return [4 /*yield*/, ocean.assets.consume(serviceAgreementSignatureResult.agreementId, ddo.id, accessService.index, consumer, folder)]; + case 1: + path = _a.sent(); + chai_1.assert.include(path, folder, 'The storage path is not correct.'); + return [4 /*yield*/, new Promise(function (resolve) { + fs.readdir(path, function (e, fileList) { + resolve(fileList); + }); + })]; + case 2: + files = _a.sent(); + chai_1.assert.deepEqual(files, ['README.md', 'package.json'], 'Stored files are not correct.'); + return [2 /*return*/]; + } + }); + }); }); + it('should consume and store one asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, folder, path, files; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + folder = '/tmp/ocean/squid-js-2'; + return [4 /*yield*/, ocean.assets.consume(serviceAgreementSignatureResult.agreementId, ddo.id, accessService.index, consumer, folder, 1)]; + case 1: + path = _a.sent(); + chai_1.assert.include(path, folder, 'The storage path is not correct.'); + return [4 /*yield*/, new Promise(function (resolve) { + fs.readdir(path, function (e, fileList) { + resolve(fileList); + }); + })]; + case 2: + files = _a.sent(); + chai_1.assert.deepEqual(files, ['README.md'], 'Stored files are not correct.'); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/ConsumeAssetBrizo.test.js b/integration/ocean/ConsumeAssetBrizo.test.js new file mode 100644 index 0000000..4ba0d24 --- /dev/null +++ b/integration/ocean/ConsumeAssetBrizo.test.js @@ -0,0 +1,170 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var fs = __importStar(require("fs")); +var config_1 = require("../config"); +var utils_1 = require("../utils"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Consume Asset (Brizo)', function () { + var ocean; + var publisher; + var consumer; + var ddo; + var agreementId; + var metadata = utils_1.getMetadata(); + before(function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _b.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + _a = _b.sent(), publisher = _a[0], consumer = _a[1]; + if (!ocean.keeper.dispenser) { + metadata = utils_1.getMetadata(0); + } + return [2 /*return*/]; + } + }); + }); }); + after(function () { + try { + localStorage.clear(); + } + catch (_a) { } + }); + it('should authenticate the accounts', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, publisher.authenticate()]; + case 1: + _a.sent(); + return [4 /*yield*/, consumer.authenticate()]; + case 2: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should regiester an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var steps; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + steps = []; + return [4 /*yield*/, ocean.assets + .create(metadata, publisher) + .next(function (step) { return steps.push(step); })]; + case 1: + ddo = _a.sent(); + chai_1.assert.instanceOf(ddo, src_1.DDO); + chai_1.assert.deepEqual(steps, [0, 1, 2, 3, 4, 5, 6, 7]); + return [2 /*return*/]; + } + }); + }); }); + it('should order the asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, _a, _b, _c, _d, _e, _f, _g, steps; + return __generator(this, function (_h) { + switch (_h.label) { + case 0: + accessService = ddo.findServiceByType('access'); + _h.label = 1; + case 1: + _h.trys.push([1, 4, , 5]); + _b = (_a = consumer).requestTokens; + _c = +metadata.main.price; + _e = (_d = Math).pow; + _f = [10]; + return [4 /*yield*/, ocean.keeper.token.decimals()]; + case 2: return [4 /*yield*/, _b.apply(_a, [_c * _e.apply(_d, _f.concat([-(_h.sent())]))])]; + case 3: + _h.sent(); + return [3 /*break*/, 5]; + case 4: + _g = _h.sent(); + return [3 /*break*/, 5]; + case 5: + steps = []; + return [4 /*yield*/, ocean.assets + .order(ddo.id, accessService.index, consumer) + .next(function (step) { return steps.push(step); })]; + case 6: + agreementId = _h.sent(); + chai_1.assert.isDefined(agreementId); + chai_1.assert.deepEqual(steps, [0, 1, 2, 3]); + return [2 /*return*/]; + } + }); + }); }); + it('should consume and store the assets', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, folder, path, files; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + folder = '/tmp/ocean/squid-js'; + return [4 /*yield*/, ocean.assets.consume(agreementId, ddo.id, accessService.index, consumer, folder)]; + case 1: + path = _a.sent(); + chai_1.assert.include(path, folder, 'The storage path is not correct.'); + return [4 /*yield*/, new Promise(function (resolve) { + fs.readdir(path, function (e, fileList) { + resolve(fileList); + }); + })]; + case 2: + files = _a.sent(); + chai_1.assert.deepEqual(files, ['README.md', 'package.json'], 'Stored files are not correct.'); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/ConsumeBigAsset.test.js b/integration/ocean/ConsumeBigAsset.test.js new file mode 100644 index 0000000..cddf5a2 --- /dev/null +++ b/integration/ocean/ConsumeBigAsset.test.js @@ -0,0 +1,160 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var fs = __importStar(require("fs")); +var config_1 = require("../config"); +var utils_1 = require("../utils"); +var src_1 = require("../../src"); // @oceanprotocol/squid +// Ensure that your network is fast enought and you have some free ram before run it. +xdescribe('Consume Asset (Large size)', function () { + var ocean; + var publisher; + var consumer; + var ddo; + var agreementId; + var baseMetadata = utils_1.getMetadata(); + var metadata = utils_1.getMetadata(); + before(function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _b.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + _a = _b.sent(), publisher = _a[0], consumer = _a[1]; + if (!ocean.keeper.dispenser) { + baseMetadata = utils_1.getMetadata(0); + } + metadata = __assign(__assign({}, baseMetadata), { main: __assign(__assign({}, baseMetadata.main), { files: [ + { + index: 0, + contentType: 'hello/hello', + url: 'https://speed.hetzner.de/1GB.bin' + } + ] }) }); + return [2 /*return*/]; + } + }); + }); }); + it('should regiester an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.create(metadata, publisher)]; + case 1: + ddo = _a.sent(); + chai_1.assert.instanceOf(ddo, src_1.DDO); + return [2 /*return*/]; + } + }); + }); }); + it('should order the asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, _a, _b, _c, _d, _e, _f, _g; + return __generator(this, function (_h) { + switch (_h.label) { + case 0: + accessService = ddo.findServiceByType('access'); + _h.label = 1; + case 1: + _h.trys.push([1, 4, , 5]); + _b = (_a = consumer).requestTokens; + _c = +metadata.main.price; + _e = (_d = Math).pow; + _f = [10]; + return [4 /*yield*/, ocean.keeper.token.decimals()]; + case 2: return [4 /*yield*/, _b.apply(_a, [_c * _e.apply(_d, _f.concat([-(_h.sent())]))])]; + case 3: + _h.sent(); + return [3 /*break*/, 5]; + case 4: + _g = _h.sent(); + return [3 /*break*/, 5]; + case 5: return [4 /*yield*/, ocean.assets.order(ddo.id, accessService.index, consumer)]; + case 6: + agreementId = _h.sent(); + chai_1.assert.isDefined(agreementId); + return [2 /*return*/]; + } + }); + }); }); + it('should consume and store the assets', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessService, folder, path, files; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + accessService = ddo.findServiceByType('access'); + folder = '/tmp/ocean/squid-js'; + return [4 /*yield*/, ocean.assets.consume(agreementId, ddo.id, accessService.index, consumer, folder)]; + case 1: + path = _a.sent(); + chai_1.assert.include(path, folder, 'The storage path is not correct.'); + return [4 /*yield*/, new Promise(function (resolve) { + fs.readdir(path, function (e, fileList) { + resolve(fileList); + }); + })]; + case 2: + files = _a.sent(); + chai_1.assert.deepEqual(files, ['1GB.bin'], 'Stored files are not correct.'); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.js b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.js new file mode 100644 index 0000000..9c82762 --- /dev/null +++ b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.js @@ -0,0 +1,376 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __spreadArrays = (this && this.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +var LockRewardCondition = src_1.conditions.LockRewardCondition, EscrowReward = src_1.conditions.EscrowReward, AccessSecretStoreCondition = src_1.conditions.AccessSecretStoreCondition; +describe('Register Escrow Access Secret Store Template', function () { + var ocean; + var keeper; + var template; + var url = 'https://example.com/did/ocean/test-attr-example.txt'; + var checksum = 'b'.repeat(32); + var escrowAmount = 12; + var templateManagerOwner; + var publisher; + var consumer; + var accessSecretStoreCondition; + var lockRewardCondition; + var escrowReward; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config)]; + case 1: + ocean = _a.sent(); + keeper = ocean.keeper; + template = keeper.templates.escrowAccessSecretStoreTemplate; + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + // Accounts + templateManagerOwner = (_a.sent())[0]; + return [4 /*yield*/, ocean.accounts.list()]; + case 3: + publisher = (_a.sent())[1]; + return [4 /*yield*/, ocean.accounts.list()]; + case 4: + consumer = (_a.sent())[2]; + // Conditions + accessSecretStoreCondition = keeper.conditions.accessSecretStoreCondition; + lockRewardCondition = keeper.conditions.lockRewardCondition; + escrowReward = keeper.conditions.escrowReward; + if (!ocean.keeper.dispenser) { + escrowAmount = 0; + } + return [2 /*return*/]; + } + }); + }); }); + describe('Propose and approve template', function () { + it('should propose the template', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.templateStoreManager.proposeTemplate(template.getId(), consumer.getId(), true) + // TODO: Use a event to detect template mined + ]; + case 1: + _a.sent(); + // TODO: Use a event to detect template mined + return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2 * 1000); })]; + case 2: + // TODO: Use a event to detect template mined + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should approve the template', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.templateStoreManager.approveTemplate(template.getId(), templateManagerOwner.getId(), true) + // TODO: Use a event to detect template mined + ]; + case 1: + _a.sent(); + // TODO: Use a event to detect template mined + return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2 * 1000); })]; + case 2: + // TODO: Use a event to detect template mined + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + }); + describe('Full flow', function () { + var agreementId = "0x" + src_1.utils.generateId(); + var did = "0x" + src_1.utils.generateId(); + var conditionIdAccess; + var conditionIdLock; + var conditionIdEscrow; + it('should register a DID', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.didRegistry.registerAttribute(did, checksum, [], url, publisher.getId())]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should generate the condition IDs', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, _b, _c; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.generateIdHash(agreementId, did, consumer.getId())]; + case 1: + conditionIdAccess = _d.sent(); + _b = (_a = lockRewardCondition).generateIdHash; + _c = [agreementId]; + return [4 /*yield*/, escrowReward.getAddress()]; + case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent(), + escrowAmount]))]; + case 3: + conditionIdLock = _d.sent(); + return [4 /*yield*/, escrowReward.generateIdHash(agreementId, escrowAmount, publisher.getId(), consumer.getId(), conditionIdLock, conditionIdAccess)]; + case 4: + conditionIdEscrow = _d.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should have conditions types', function () { return __awaiter(void 0, void 0, void 0, function () { + var conditionTypes; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.getConditionTypes()]; + case 1: + conditionTypes = _a.sent(); + chai_1.assert.equal(conditionTypes.length, 3, 'Expected 3 conditions.'); + chai_1.assert.deepEqual(__spreadArrays(conditionTypes).sort(), [ + accessSecretStoreCondition.getAddress(), + escrowReward.getAddress(), + lockRewardCondition.getAddress() + ].sort(), "The conditions doesn't match"); + return [2 /*return*/]; + } + }); + }); }); + it('should have condition instances asociated', function () { return __awaiter(void 0, void 0, void 0, function () { + var conditionInstances, conditionClasses; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.getConditionTypes()]; + case 1: + conditionInstances = (_a.sent()).map(function (address) { + return keeper.getConditionByAddress(address); + }); + chai_1.assert.equal(conditionInstances.length, 3, 'Expected 3 conditions.'); + conditionClasses = [ + AccessSecretStoreCondition, + EscrowReward, + LockRewardCondition + ]; + conditionClasses.forEach(function (conditionClass) { + if (!conditionInstances.find(function (condition) { return condition instanceof conditionClass; })) { + throw new Error(conditionClass.name + " is not part of the conditions."); + } + }); + return [2 /*return*/]; + } + }); + }); }); + it('should create a new agreement', function () { return __awaiter(void 0, void 0, void 0, function () { + var agreement; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.agreementStoreManager.createAgreement(agreementId, did, template.getId(), [conditionIdLock, conditionIdAccess, conditionIdEscrow], [0, 0, 0], [0, 0, 0], [consumer.getId(), publisher.getId()], publisher.getId())]; + case 1: + agreement = _a.sent(); + chai_1.assert.isTrue(agreement.status); + return [2 /*return*/]; + } + }); + }); }); + it('should not grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessGranted; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.checkPermissions(consumer.getId(), did)]; + case 1: + accessGranted = _a.sent(); + chai_1.assert.isFalse(accessGranted, 'Consumer has been granted.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill LockRewardCondition', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, fulfill; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _b.trys.push([0, 2, , 3]); + return [4 /*yield*/, consumer.requestTokens(escrowAmount)]; + case 1: + _b.sent(); + return [3 /*break*/, 3]; + case 2: + _a = _b.sent(); + return [3 /*break*/, 3]; + case 3: return [4 /*yield*/, keeper.token.approve(lockRewardCondition.getAddress(), escrowAmount, consumer.getId())]; + case 4: + _b.sent(); + return [4 /*yield*/, lockRewardCondition.fulfill(agreementId, escrowReward.getAddress(), escrowAmount, consumer.getId())]; + case 5: + fulfill = _b.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill AccessSecretStoreCondition', function () { return __awaiter(void 0, void 0, void 0, function () { + var fulfill; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.fulfill(agreementId, did, consumer.getId(), publisher.getId())]; + case 1: + fulfill = _a.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill EscrowReward', function () { return __awaiter(void 0, void 0, void 0, function () { + var fulfill; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, escrowReward.fulfill(agreementId, escrowAmount, publisher.getId(), consumer.getId(), conditionIdLock, conditionIdAccess, consumer.getId())]; + case 1: + fulfill = _a.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessGranted; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.checkPermissions(consumer.getId(), did)]; + case 1: + accessGranted = _a.sent(); + chai_1.assert.isTrue(accessGranted, 'Consumer has not been granted.'); + return [2 /*return*/]; + } + }); + }); }); + }); + describe('Short flow', function () { + var did = src_1.utils.generateId(); + var agreementId; + it('should register a DID', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + // This part is executed inside Ocean.assets.create() + return [4 /*yield*/, keeper.didRegistry.registerAttribute(did, checksum, [], url, publisher.getId())]; + case 1: + // This part is executed inside Ocean.assets.create() + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should create a new agreement (short way)', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.createFullAgreement(did, escrowAmount, consumer.getId(), publisher.getId())]; + case 1: + agreementId = _a.sent(); + chai_1.assert.match(agreementId, /^0x[a-f0-9]{64}$/i); + return [2 /*return*/]; + } + }); + }); }); + it('should not grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessGranted; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.checkPermissions(consumer.getId(), did)]; + case 1: + accessGranted = _a.sent(); + chai_1.assert.isFalse(accessGranted, 'Consumer has been granted.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill the conditions from consumer side', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _b.trys.push([0, 2, , 3]); + return [4 /*yield*/, consumer.requestTokens(escrowAmount)]; + case 1: + _b.sent(); + return [3 /*break*/, 3]; + case 2: + _a = _b.sent(); + return [3 /*break*/, 3]; + case 3: return [4 /*yield*/, ocean.agreements.conditions.lockReward(agreementId, escrowAmount, consumer)]; + case 4: + _b.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill the conditions from publisher side', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.agreements.conditions.grantAccess(agreementId, did, consumer.getId(), publisher)]; + case 1: + _a.sent(); + return [4 /*yield*/, ocean.agreements.conditions.releaseReward(agreementId, escrowAmount, did, consumer.getId(), publisher.getId(), publisher)]; + case 2: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var accessGranted; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, accessSecretStoreCondition.checkPermissions(consumer.getId(), did)]; + case 1: + accessGranted = _a.sent(); + chai_1.assert.isTrue(accessGranted, 'Consumer has not been granted.'); + return [2 /*return*/]; + } + }); + }); }); + }); +}); diff --git a/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.js b/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.js new file mode 100644 index 0000000..3aca324 --- /dev/null +++ b/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.js @@ -0,0 +1,374 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __spreadArrays = (this && this.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +var LockRewardCondition = src_1.conditions.LockRewardCondition, EscrowReward = src_1.conditions.EscrowReward, ComputeExecutionCondition = src_1.conditions.ComputeExecutionCondition; +describe('Register Escrow Compute Execution Template', function () { + var ocean; + var keeper; + var template; + var url = 'https://example.com/did/ocean/test-attr-example.txt'; + var checksum = 'b'.repeat(32); + var escrowAmount = 12; + var templateManagerOwner; + var publisher; + var consumer; + var computeExecutionCondition; + var lockRewardCondition; + var escrowReward; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config)]; + case 1: + ocean = _a.sent(); + keeper = ocean.keeper; + template = keeper.templates.escrowComputeExecutionTemplate; + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + // Accounts + templateManagerOwner = (_a.sent())[0]; + return [4 /*yield*/, ocean.accounts.list()]; + case 3: + publisher = (_a.sent())[1]; + return [4 /*yield*/, ocean.accounts.list()]; + case 4: + consumer = (_a.sent())[2]; + // Conditions + computeExecutionCondition = keeper.conditions.computeExecutionCondition; + lockRewardCondition = keeper.conditions.lockRewardCondition; + escrowReward = keeper.conditions.escrowReward; + if (!ocean.keeper.dispenser) { + escrowAmount = 0; + } + return [2 /*return*/]; + } + }); + }); }); + describe('Propose and approve template', function () { + it('should propose the template', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.templateStoreManager.proposeTemplate(template.getId(), consumer.getId(), true) + // TODO: Use a event to detect template mined + ]; + case 1: + _a.sent(); + // TODO: Use a event to detect template mined + return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2 * 1000); })]; + case 2: + // TODO: Use a event to detect template mined + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should approve the template', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.templateStoreManager.approveTemplate(template.getId(), templateManagerOwner.getId(), true) + // TODO: Use a event to detect template mined + ]; + case 1: + _a.sent(); + // TODO: Use a event to detect template mined + return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2 * 1000); })]; + case 2: + // TODO: Use a event to detect template mined + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + }); + describe('Full flow', function () { + var agreementId = "0x" + src_1.utils.generateId(); + var did = "0x" + src_1.utils.generateId(); + var conditionIdCompute; + var conditionIdLock; + var conditionIdEscrow; + it('should register a DID', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.didRegistry.registerAttribute(did, checksum, [], url, publisher.getId())]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should generate the condition IDs', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, _b, _c; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.generateIdHash(agreementId, did, consumer.getId())]; + case 1: + conditionIdCompute = _d.sent(); + _b = (_a = lockRewardCondition).generateIdHash; + _c = [agreementId]; + return [4 /*yield*/, escrowReward.getAddress()]; + case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([_d.sent(), + escrowAmount]))]; + case 3: + conditionIdLock = _d.sent(); + return [4 /*yield*/, escrowReward.generateIdHash(agreementId, escrowAmount, publisher.getId(), consumer.getId(), conditionIdLock, conditionIdCompute)]; + case 4: + conditionIdEscrow = _d.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should have conditions types', function () { return __awaiter(void 0, void 0, void 0, function () { + var conditionTypes; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.getConditionTypes()]; + case 1: + conditionTypes = _a.sent(); + chai_1.assert.equal(conditionTypes.length, 3, 'Expected 3 conditions.'); + chai_1.assert.deepEqual(__spreadArrays(conditionTypes).sort(), [ + computeExecutionCondition.getAddress(), + escrowReward.getAddress(), + lockRewardCondition.getAddress() + ].sort(), "The conditions doesn't match"); + return [2 /*return*/]; + } + }); + }); }); + it('should have condition instances asociated', function () { return __awaiter(void 0, void 0, void 0, function () { + var conditionInstances, conditionClasses; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.getConditions()]; + case 1: + conditionInstances = _a.sent(); + chai_1.assert.equal(conditionInstances.length, 3, 'Expected 3 conditions.'); + conditionClasses = [ + ComputeExecutionCondition, + EscrowReward, + LockRewardCondition + ]; + conditionClasses.forEach(function (conditionClass) { + if (!conditionInstances.find(function (condition) { return condition instanceof conditionClass; })) { + throw new Error(conditionClass.name + " is not part of the conditions."); + } + }); + return [2 /*return*/]; + } + }); + }); }); + it('should create a new agreement', function () { return __awaiter(void 0, void 0, void 0, function () { + var agreement; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, keeper.agreementStoreManager.createAgreement(agreementId, did, template.getId(), [conditionIdLock, conditionIdCompute, conditionIdEscrow], [0, 0, 0], [0, 0, 0], [consumer.getId(), publisher.getId()], publisher.getId())]; + case 1: + agreement = _a.sent(); + chai_1.assert.isTrue(agreement.status); + return [2 /*return*/]; + } + }); + }); }); + it('should not trigger the compute', function () { return __awaiter(void 0, void 0, void 0, function () { + var computeTriggered; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.wasComputeTriggered(did, consumer.getId())]; + case 1: + computeTriggered = _a.sent(); + chai_1.assert.isFalse(computeTriggered, 'Compute has been triggered.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill LockRewardCondition', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, fulfill; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _b.trys.push([0, 2, , 3]); + return [4 /*yield*/, consumer.requestTokens(escrowAmount)]; + case 1: + _b.sent(); + return [3 /*break*/, 3]; + case 2: + _a = _b.sent(); + return [3 /*break*/, 3]; + case 3: return [4 /*yield*/, keeper.token.approve(lockRewardCondition.getAddress(), escrowAmount, consumer.getId())]; + case 4: + _b.sent(); + return [4 /*yield*/, lockRewardCondition.fulfill(agreementId, escrowReward.getAddress(), escrowAmount, consumer.getId())]; + case 5: + fulfill = _b.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill ComputeExecutionCondition', function () { return __awaiter(void 0, void 0, void 0, function () { + var fulfill; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.fulfill(agreementId, did, consumer.getId(), publisher.getId())]; + case 1: + fulfill = _a.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill EscrowReward', function () { return __awaiter(void 0, void 0, void 0, function () { + var fulfill; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, escrowReward.fulfill(agreementId, escrowAmount, publisher.getId(), consumer.getId(), conditionIdLock, conditionIdCompute, consumer.getId())]; + case 1: + fulfill = _a.sent(); + chai_1.assert.isDefined(fulfill.events.Fulfilled, 'Not Fulfilled event.'); + return [2 /*return*/]; + } + }); + }); }); + it('should grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var computeTriggered; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.wasComputeTriggered(did, consumer.getId())]; + case 1: + computeTriggered = _a.sent(); + chai_1.assert.isTrue(computeTriggered, 'Compute has not been triggered.'); + return [2 /*return*/]; + } + }); + }); }); + }); + describe('Short flow', function () { + var did = src_1.utils.generateId(); + var agreementId; + it('should register a DID', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + // This part is executed inside Ocean.assets.create() + return [4 /*yield*/, keeper.didRegistry.registerAttribute(did, checksum, [], url, publisher.getId())]; + case 1: + // This part is executed inside Ocean.assets.create() + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should create a new agreement (short way)', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, template.createFullAgreement(did, escrowAmount, consumer.getId(), publisher.getId())]; + case 1: + agreementId = _a.sent(); + chai_1.assert.match(agreementId, /^0x[a-f0-9]{64}$/i); + return [2 /*return*/]; + } + }); + }); }); + it('should not grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var computeTriggered; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.wasComputeTriggered(did, consumer.getId())]; + case 1: + computeTriggered = _a.sent(); + chai_1.assert.isFalse(computeTriggered, 'Compute has been triggered.'); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill the conditions from consumer side', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _b.trys.push([0, 2, , 3]); + return [4 /*yield*/, consumer.requestTokens(escrowAmount)]; + case 1: + _b.sent(); + return [3 /*break*/, 3]; + case 2: + _a = _b.sent(); + return [3 /*break*/, 3]; + case 3: return [4 /*yield*/, ocean.agreements.conditions.lockReward(agreementId, escrowAmount, consumer)]; + case 4: + _b.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should fulfill the conditions from computing side', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.agreements.conditions.grantServiceExecution(agreementId, did, consumer.getId(), publisher)]; + case 1: + _a.sent(); + return [4 /*yield*/, ocean.agreements.conditions.releaseReward(agreementId, escrowAmount, did, consumer.getId(), publisher.getId(), publisher)]; + case 2: + _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + it('should grant the access to the consumer', function () { return __awaiter(void 0, void 0, void 0, function () { + var computeTriggered; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, computeExecutionCondition.wasComputeTriggered(did, consumer.getId())]; + case 1: + computeTriggered = _a.sent(); + chai_1.assert.isTrue(computeTriggered, 'Compute has not been triggered.'); + return [2 /*return*/]; + } + }); + }); }); + }); +}); diff --git a/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.ts b/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.ts index 89f05d1..305402f 100644 --- a/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.ts +++ b/integration/ocean/RegisterEscrowComputeExecutionTemplate.test.ts @@ -149,7 +149,7 @@ describe('Register Escrow Compute Execution Template', () => { agreementId, did, template.getId(), - [conditionIdCompute, conditionIdLock, conditionIdEscrow], + [conditionIdLock, conditionIdCompute, conditionIdEscrow], [0, 0, 0], [0, 0, 0], [consumer.getId(), publisher.getId()], diff --git a/integration/ocean/SearchAsset.test.js b/integration/ocean/SearchAsset.test.js new file mode 100644 index 0000000..49b4900 --- /dev/null +++ b/integration/ocean/SearchAsset.test.js @@ -0,0 +1,182 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var utils_1 = require("../utils"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Search Asset', function () { + var ocean; + var publisher; + var testHash = Math.random() + .toString(36) + .substr(2); + var price; + var metadataGenerator = function (name) { + return utils_1.generateMetadata("" + name + testHash, price); + }; + var test1length; + var test2length; + var test3length; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _a.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + publisher = (_a.sent())[0]; + return [2 /*return*/]; + } + }); + }); }); + it('should be able to search the assets', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddos; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.search("Test1" + testHash)]; + case 1: + ddos = (_a.sent()).results; + chai_1.assert.isArray(ddos, 'A search should return an array'); + test1length = ddos.length; + return [4 /*yield*/, ocean.assets.search("Test2" + testHash)]; + case 2: + test2length = (_a.sent()).results.length; + return [4 /*yield*/, ocean.assets.search("Test3" + testHash)]; + case 3: + test3length = (_a.sent()).results.length; + if (!ocean.keeper.dispenser) { + price = 0; + } + return [2 /*return*/]; + } + }); + }); }); + it('should register an asset', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, _b, _c, _d, _e, _f, _g, _h; + return __generator(this, function (_j) { + switch (_j.label) { + case 0: + _b = (_a = chai_1.assert).instanceOf; + return [4 /*yield*/, ocean.assets.create(metadataGenerator('Test1'), publisher)]; + case 1: + _b.apply(_a, [_j.sent(), + src_1.DDO]); + _d = (_c = chai_1.assert).instanceOf; + return [4 /*yield*/, ocean.assets.create(metadataGenerator('Test2'), publisher)]; + case 2: + _d.apply(_c, [_j.sent(), + src_1.DDO]); + _f = (_e = chai_1.assert).instanceOf; + return [4 /*yield*/, ocean.assets.create(metadataGenerator('Test2'), publisher)]; + case 3: + _f.apply(_e, [_j.sent(), + src_1.DDO]); + _h = (_g = chai_1.assert).instanceOf; + return [4 /*yield*/, ocean.assets.create(metadataGenerator('Test3'), publisher)]; + case 4: + _h.apply(_g, [_j.sent(), + src_1.DDO]); + return [2 /*return*/]; + } + }); + }); }); + it('should search by text and see the increment of DDOs', function () { return __awaiter(void 0, void 0, void 0, function () { + var _a, _b, _c, _d; + return __generator(this, function (_e) { + switch (_e.label) { + case 0: + _b = (_a = chai_1.assert).equal; + return [4 /*yield*/, ocean.assets.search("Test2" + testHash)]; + case 1: + _b.apply(_a, [(_e.sent()).results.length - test2length, + 2, + 'Something was wrong searching the assets']); + _d = (_c = chai_1.assert).equal; + return [4 /*yield*/, ocean.assets.search("Test3" + testHash)]; + case 2: + _d.apply(_c, [(_e.sent()).results.length - test3length, + 1, + 'Something was wrong searching the assets']); + return [2 /*return*/]; + } + }); + }); }); + it('should return a list of DDOs', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddos; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.search("Test1" + testHash)]; + case 1: + ddos = (_a.sent()).results; + chai_1.assert.equal(ddos.length - test1length, 1, 'Something was wrong searching the assets'); + ddos.map(function (ddo) { + return chai_1.assert.instanceOf(ddo, src_1.DDO, 'The DDO is not an instance of a DDO'); + }); + return [2 /*return*/]; + } + }); + }); }); + it('should be able to do a query to get a list of DDOs', function () { return __awaiter(void 0, void 0, void 0, function () { + var ddos; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.assets.query({ + page: 1, + offset: 1, + query: { + text: ["Test2" + testHash] + }, + sort: { + text: 1 + } + })]; + case 1: + ddos = (_a.sent()).results; + chai_1.assert.equal(ddos.length, 1, 'Something was wrong searching the assets'); + ddos.map(function (ddo) { + return chai_1.assert.instanceOf(ddo, src_1.DDO, 'The DDO is not an instance of a DDO'); + }); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/SecretStore.test.js b/integration/ocean/SecretStore.test.js new file mode 100644 index 0000000..9fd271c --- /dev/null +++ b/integration/ocean/SecretStore.test.js @@ -0,0 +1,75 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Secret Store', function () { + var ocean; + var account; + var did = src_1.DID.generate(); + var content = { content: 'Test 123' }; + var encryptedContent; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _a.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + account = (_a.sent())[0]; + return [2 /*return*/]; + } + }); + }); }); + it('should encrypt a text', function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.secretStore.encrypt(did.getId(), content, account)]; + case 1: + encryptedContent = _a.sent(); + chai_1.assert.isDefined(encryptedContent); + chai_1.assert.match(encryptedContent, /^0x[a-f0-9]{76}$/i); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/Signature.test.js b/integration/ocean/Signature.test.js new file mode 100644 index 0000000..c89066c --- /dev/null +++ b/integration/ocean/Signature.test.js @@ -0,0 +1,115 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +// WARN: not integration test. It has been done here because constant values +// depends on the first account on spree (only accessible from integration test) +describe('Signature', function () { + var ocean; + var consumer; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config) + // Accounts + ]; + case 1: + ocean = _a.sent(); + return [4 /*yield*/, ocean.accounts.list()]; + case 2: + consumer = (_a.sent())[0]; + return [2 /*return*/]; + } + }); + }); }); + it('hashServiceAgreement should generate the correct signature', function () { + var templateId = "0x" + 'f'.repeat(40); + var agreementId = "0x" + 'e'.repeat(64); + var accessId = "0x" + 'a'.repeat(64); + var lockId = "0x" + 'b'.repeat(64); + var escrowId = "0x" + 'c'.repeat(64); + var hash = ocean.utils.agreements.hashServiceAgreement(templateId, agreementId, [lockId, accessId, escrowId], [0, 0, 0], [0, 0, 0]); + chai_1.assert.equal(hash, '0x67901517c18a3d23e05806fff7f04235cc8ae3b1f82345b8bfb3e4b02b5800c7', 'The signature is not correct.'); + }); + it('signServiceAgreement should generate the correct signature', function () { return __awaiter(void 0, void 0, void 0, function () { + var templates, did, templateId, agreementId, serviceAgreementTemplate, ddo, signature; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + templates = ocean.keeper.templates; + did = "did:op:" + 'c'.repeat(64); + templateId = "0x" + 'f'.repeat(40); + agreementId = "0x" + 'e'.repeat(64); + return [4 /*yield*/, templates.escrowAccessSecretStoreTemplate.getServiceAgreementTemplate()]; + case 1: + serviceAgreementTemplate = _a.sent(); + ddo = new src_1.DDO({ + id: did, + service: [ + { + type: 'access', + index: 0, + purchaseEndpoint: undefined, + serviceEndpoint: undefined, + templateId: templateId, + attributes: { + serviceAgreementTemplate: serviceAgreementTemplate + } + }, + { + type: 'metadata', + index: 1, + attributes: { + main: { + price: 10 + } + } + } + ] + }); + return [4 /*yield*/, ocean.utils.agreements.signServiceAgreement(ddo, 0, agreementId, ["0x" + '1'.repeat(64), "0x" + '2'.repeat(64), "0x" + '3'.repeat(64)], consumer)]; + case 2: + signature = _a.sent(); + chai_1.assert.equal(signature, '0x3aa8a1c48b8e582d694bbd4ba3a29fde573b78da9720dc48baeb831b2163e1fa6e10e983882ebf8a00f4124de2505136354fd146934053f0d58bba4eced5f8d01b', 'The signature is not correct.'); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/ocean/Signature.test.ts b/integration/ocean/Signature.test.ts index a67fe1b..4ca285a 100644 --- a/integration/ocean/Signature.test.ts +++ b/integration/ocean/Signature.test.ts @@ -28,7 +28,7 @@ describe('Signature', () => { const hash = ocean.utils.agreements.hashServiceAgreement( templateId, agreementId, - [accessId, lockId, escrowId], + [lockId, accessId, escrowId], [0, 0, 0], [0, 0, 0] ) diff --git a/integration/ocean/Versions.test.js b/integration/ocean/Versions.test.js new file mode 100644 index 0000000..b9b6073 --- /dev/null +++ b/integration/ocean/Versions.test.js @@ -0,0 +1,74 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +var chai_1 = require("chai"); +var config_1 = require("../config"); +var src_1 = require("../../src"); // @oceanprotocol/squid +describe('Versions', function () { + var ocean; + before(function () { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, src_1.Ocean.getInstance(config_1.config)]; + case 1: + ocean = _a.sent(); + return [2 /*return*/]; + } + }); + }); }); + // TODO: enable again after new versions of Brizo + xit('should return the versions', function () { return __awaiter(void 0, void 0, void 0, function () { + var versions; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, ocean.versions.get()]; + case 1: + versions = _a.sent(); + chai_1.assert.equal(versions.aquarius.status, src_1.OceanPlatformTechStatus.Working); + chai_1.assert.equal(versions.brizo.status, src_1.OceanPlatformTechStatus.Working); + chai_1.assert.equal(versions.squid.status, src_1.OceanPlatformTechStatus.Working); + chai_1.assert.deepEqual(versions.status, { + ok: true, + contracts: true, + network: true + }); + return [2 /*return*/]; + } + }); + }); }); +}); diff --git a/integration/utils/ddo-metadata-generator.js b/integration/utils/ddo-metadata-generator.js new file mode 100644 index 0000000..178e06c --- /dev/null +++ b/integration/utils/ddo-metadata-generator.js @@ -0,0 +1,58 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +exports.__esModule = true; +var metadata = { + main: { + name: undefined, + type: 'dataset', + dateCreated: '2012-10-10T17:00:00Z', + datePublished: '2012-10-10T17:00:00Z', + author: 'Met Office', + license: 'CC-BY', + price: '21' + '0'.repeat(18), + files: [ + { + index: 0, + contentType: 'application/json', + url: 'https://raw.githubusercontent.com/oceanprotocol/squid-js/master/package.json' + }, + { + index: 1, + contentType: 'text/plain', + url: 'https://raw.githubusercontent.com/oceanprotocol/squid-js/master/README.md' + } + ] + }, + additionalInformation: { + description: 'Weather information of UK including temperature and humidity', + copyrightHolder: 'Met Office', + workExample: '423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68', + links: [ + { + name: 'Sample of Asset Data', + type: 'sample', + url: 'https://foo.com/sample.csv' + }, + { + name: 'Data Format Definition', + type: 'format', + url: 'https://foo.com/sample.csv' + } + ], + inLanguage: 'en', + categories: ['Economy', 'Data Science'], + tags: ['weather', 'uk', '2011', 'temperature', 'humidity'] + } +}; +exports.generateMetadata = function (name, price) { return (__assign(__assign({}, metadata), { main: __assign(__assign({}, metadata.main), { name: name, price: (price || 21) + '0'.repeat(18) }), additionalInformation: __assign({}, metadata.additionalInformation) })); }; +exports.getMetadata = function (price) { return exports.generateMetadata('TestAsset', price); }; diff --git a/integration/utils/index.js b/integration/utils/index.js new file mode 100644 index 0000000..e10379b --- /dev/null +++ b/integration/utils/index.js @@ -0,0 +1,6 @@ +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +exports.__esModule = true; +__export(require("./ddo-metadata-generator")); diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 0fd4954..247744c 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -93,13 +93,6 @@ export class Keeper extends Instantiable { accessSecretStoreCondition: keeper.instances.accessSecretStoreCondition, computeExecutionCondition: keeper.instances.computeExecutionCondition } - // Conditions - keeper.templates = { - escrowAccessSecretStoreTemplate: - keeper.instances.escrowAccessSecretStoreTemplate, - escrowComputeExecutionTemplate: - keeper.instances.escrowComputeExecutionTemplate - } // Templates keeper.instances.escrowAccessSecretStoreTemplate = new EscrowAccessSecretStoreTemplate( keeper.instances.templateStoreManager, @@ -113,6 +106,13 @@ export class Keeper extends Instantiable { keeper.instances.didRegistry, keeper.instances.conditions ) + // Conditions + keeper.templates = { + escrowAccessSecretStoreTemplate: + keeper.instances.escrowAccessSecretStoreTemplate, + escrowComputeExecutionTemplate: + keeper.instances.escrowComputeExecutionTemplate + } // Utils keeper.utils = { eventHandler: new EventHandler(config) diff --git a/src/keeper/contracts/managers/TemplateStoreManager.ts b/src/keeper/contracts/managers/TemplateStoreManager.ts index 3bd82f2..5b19026 100644 --- a/src/keeper/contracts/managers/TemplateStoreManager.ts +++ b/src/keeper/contracts/managers/TemplateStoreManager.ts @@ -30,9 +30,9 @@ export class TemplateStoreManager extends ContractBase { } public generateId(templateName: string) { - const args: any = [ + const args: any = [ { type: 'string', value: templateName }, - ] + ] return this.web3.utils.soliditySha3(...args) } diff --git a/src/keeper/contracts/templates/AgreementTemplateBase.ts b/src/keeper/contracts/templates/AgreementTemplateBase.ts index 2d8d32e..4777ded 100644 --- a/src/keeper/contracts/templates/AgreementTemplateBase.ts +++ b/src/keeper/contracts/templates/AgreementTemplateBase.ts @@ -77,13 +77,12 @@ export class AgreementTemplateBase { consumer: string, from?: string ) { - const conditionIds = await this.createFullAgreementData( + return await this.createFullAgreementData( agreementId, ddo.shortId(), ddo.findServiceByType('metadata').attributes.main.price, consumer ) - return conditionIds } public getId() { @@ -94,7 +93,9 @@ export class AgreementTemplateBase { * Create a agreement using EscrowAccessSecretStoreTemplate using only the most important information. * @param {string} did Asset DID. * @param {number} amount Asset price. + * @param {string} consumer ethereum address of consumer. * @param {string} from Consumer address. + * @param {string} agreementId bytes32 agreement id. * * @return {Promise} Agreement ID. */ diff --git a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts index 072f269..4665266 100644 --- a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts +++ b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts @@ -2,7 +2,7 @@ import { AgreementTemplateBase } from './AgreementTemplateBase' import { escrowAccessServiceAgreementTemplate } from './EscrowAccess.serviceAgreementTemplate' export class EscrowAccessSecretStoreTemplate extends AgreementTemplateBase { - public static templateName: 'EscrowAccessSecretStoreTemplate' + public templateName: 'EscrowAccessSecretStoreTemplate' public async getServiceAgreementTemplate() { return escrowAccessServiceAgreementTemplate diff --git a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts b/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts index 6636ad9..d41ef44 100644 --- a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts +++ b/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts @@ -2,7 +2,7 @@ import { AgreementTemplateBase } from './AgreementTemplateBase' import { escrowComputeServiceAgreementTemplate } from './EscrowCompute.serviceAgreementTemplate' export class EscrowComputeExecutionTemplate extends AgreementTemplateBase { - public static templateName: 'EscrowComputeExecutionTemplate' + public templateName: 'EscrowComputeExecutionTemplate' public async getServiceAgreementTemplate() { return escrowComputeServiceAgreementTemplate diff --git a/test/keeper/TestContractHandler.ts b/test/keeper/TestContractHandler.ts index edf73c9..5272d47 100644 --- a/test/keeper/TestContractHandler.ts +++ b/test/keeper/TestContractHandler.ts @@ -89,7 +89,7 @@ export default class TestContractHandler extends ContractHandler { ) // Conditions - const lockRewardCondition = await TestContractHandler.deployContract( + await TestContractHandler.deployContract( 'LockRewardCondition', deployerAddress, [ @@ -98,7 +98,7 @@ export default class TestContractHandler extends ContractHandler { token.options.address ] ) - const accessSecretStoreCondition = await TestContractHandler.deployContract( + await TestContractHandler.deployContract( 'AccessSecretStoreCondition', deployerAddress, [ @@ -109,7 +109,7 @@ export default class TestContractHandler extends ContractHandler { ) // Conditions rewards - const escrowReward = await TestContractHandler.deployContract( + await TestContractHandler.deployContract( 'EscrowReward', deployerAddress, [ @@ -119,19 +119,6 @@ export default class TestContractHandler extends ContractHandler { ] ) - // Templates - await TestContractHandler.deployContract( - 'EscrowAccessSecretStoreTemplate', - deployerAddress, - [ - deployerAddress, - agreementStoreManager.options.address, - didRegistry.options.address, - accessSecretStoreCondition.options.address, - lockRewardCondition.options.address, - escrowReward.options.address - ] - ) } private static async deployContract( diff --git a/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts b/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts deleted file mode 100644 index 1518423..0000000 --- a/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { assert } from 'chai' -import { EscrowAccessSecretStoreTemplate } from '../../../src/keeper/contracts/templates' -import config from '../../config' -import TestContractHandler from '../TestContractHandler' -import { Ocean } from '../../../src/ocean/Ocean' - -let condition: EscrowAccessSecretStoreTemplate - -describe('EscrowAccessSecretStoreTemplate', () => { - before(async () => { - const ocean: Ocean = await Ocean.getInstance(config) - await TestContractHandler.prepareContracts() - condition = ocean.keeper.templates.escrowAccessSecretStoreTemplate - }) - - // describe("#hashValues()", () => { - // it("should hash the values", async () => { - // const address = `0x${"a".repeat(40)}` - // const hash = await condition.hashValues(address, 15) - - // assert.match(hash, /^0x[a-f0-9]{64}$/i) - // }) - // }) -})