"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*/]; } }); }); }); }); });