From 88d60f52aadc45e9e3f6c210948736b8002a0830 Mon Sep 17 00:00:00 2001 From: Daniel Olojakpoke Date: Sun, 12 Jun 2022 12:17:00 +0100 Subject: [PATCH] fix: refactor trust module --- __tests__/auth.spec.js | 2 +- __tests__/e2e/__tests__/wallets.test.js | 2 +- package-lock.json | 2 +- package.json | 2 +- server/handlers/trustHandler.js | 107 ++---- server/models/Trust.js | 288 +++++++++++++++- server/models/Wallet.js | 366 +++------------------ server/repositories/.BaseRepository.js.swp | Bin 12288 -> 0 bytes server/services/TrustService.js | 83 ++++- server/utils/trust-enums.js | 3 +- 10 files changed, 459 insertions(+), 396 deletions(-) delete mode 100644 server/repositories/.BaseRepository.js.swp diff --git a/__tests__/auth.spec.js b/__tests__/auth.spec.js index 32726bbe..106718d7 100644 --- a/__tests__/auth.spec.js +++ b/__tests__/auth.spec.js @@ -10,7 +10,7 @@ const Transfer = require('../server/models/Transfer'); const TrustRelationship = require('../server/models/TrustRelationship'); chai.use(require('chai-uuid')); -describe.only('Authentication', () => { +describe('Authentication', () => { let bearerToken; let bearerTokenB; diff --git a/__tests__/e2e/__tests__/wallets.test.js b/__tests__/e2e/__tests__/wallets.test.js index 699105c3..e81f73ab 100644 --- a/__tests__/e2e/__tests__/wallets.test.js +++ b/__tests__/e2e/__tests__/wallets.test.js @@ -15,7 +15,7 @@ const { password } = testData.wallet; const limit = 50; const url = `/wallets?limit=${limit}`; -describe.only('Wallets (Wallet API)', function () { +describe('Wallets (Wallet API)', function () { before(async () => { bearer = await getSession(wallet, password); }); diff --git a/package-lock.json b/package-lock.json index 3c0d3c53..ae6647e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,7 +58,7 @@ "supertest": "^4.0.2" }, "engines": { - "node": ">= 12 < 13" + "node": ">= 16" } }, "node_modules/@babel/code-frame": { diff --git a/package.json b/package.json index e5b6166b..8a21b8bb 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,6 @@ }, "homepage": "https://github.com/Greenstand/treetracker-wallet-api#readme", "engines": { - "node": ">= 12 < 13" + "node": ">= 16" } } diff --git a/server/handlers/trustHandler.js b/server/handlers/trustHandler.js index 166c03dc..81e5339b 100644 --- a/server/handlers/trustHandler.js +++ b/server/handlers/trustHandler.js @@ -1,7 +1,5 @@ const Joi = require('joi'); -const WalletService = require('../services/WalletService'); const TrustService = require('../services/TrustService'); -const Session = require('../database/Session'); const TrustRelationship = require('../utils/trust-enums'); const trustGetQuerySchema = Joi.object({ @@ -17,6 +15,7 @@ const trustPostSchema = Joi.object({ .required() .valid(...Object.keys(TrustRelationship.ENTITY_TRUST_REQUEST_TYPE)), requestee_wallet: Joi.string().required(), + requester_wallet: Joi.string(), }); const trustRelationshipIdSchema = Joi.object({ @@ -29,66 +28,33 @@ const trustGet = async (req, res) => { }); const { state, type, request_type, limit, offset } = req.query; - const session = new Session(); - const walletService = new WalletService(session); - const trustService = new TrustService(session); - const wallet = await walletService.getById(res.locals.wallet_id); - const trust_relationships = await wallet.getTrustRelationships( + const trustService = new TrustService(); + const trustRelationships = await trustService.getAllTrustRelationships({ + walletId: req.wallet_id, state, type, request_type, - Number(offset || 0), - Number(limit || 0), - ); - const subWallets = await wallet.getSubWallets(); - for (const sw of subWallets) { - const trustRelationships = await sw.getTrustRelationships( - req.query.state, - req.query.type, - req.query.request_type, - ); - for (const tr of trustRelationships) { - if (trust_relationships.every((e) => e.id !== tr.id)) { - trust_relationships.push(tr); - } - } - } - - const trust_relationships_json = []; - for (const t of trust_relationships) { - const j = await trustService.convertToResponse(t); - trust_relationships_json.push(j); - } + offset, + limit, + }); res.status(200).json({ - trust_relationships: trust_relationships_json, + trust_relationships: trustRelationships, }); }; const trustPost = async (req, res) => { await trustPostSchema.validateAsync(req.body, { abortEarly: false }); - const session = new Session(); - const walletService = new WalletService(session); - const trustService = new TrustService(session); - const wallet = await walletService.getById(res.locals.wallet_id); - const requesteeWallet = await walletService.getByName( - req.body.requestee_wallet, - ); - let requesterWallet = wallet; - if (req.body.requester_wallet) { - requesterWallet = await walletService.getByName(req.body.requester_wallet); - } - - const trust_relationship = await wallet.requestTrustFromAWallet( - req.body.trust_request_type, - requesterWallet, - requesteeWallet, - ); - const trust_relationship_json = await trustService.convertToResponse( - trust_relationship, - ); - res.status(200).json(trust_relationship_json); + const trustService = new TrustService(); + const trustRelationship = await trustService.createTrustRelationship({ + walletId: req.wallet_id, + requesteeWallet: req.body.requestee_wallet, + requesterWallet: req.body.requester_wallet, + trustRequestType: req.body.trust_request_type, + }); + + res.status(200).json(trustRelationship); }; const trustRelationshipAccept = async (req, res) => { @@ -97,13 +63,12 @@ const trustRelationshipAccept = async (req, res) => { }); const { trustRelationshipId } = req.params; - const session = new Session(); - const walletService = new WalletService(session); - const trustService = new TrustService(session); - const wallet = await walletService.getById(res.locals.wallet_id); - const json = await wallet.acceptTrustRequestSentToMe(trustRelationshipId); - const json2 = await trustService.convertToResponse(json); - res.status(200).json(json2); + const trustService = new TrustService(); + const json = await trustService.acceptTrustRequestSentToMe({ + trustRelationshipId, + walletId: req.wallet_id, + }); + res.status(200).json(json); }; const trustRelationshipDecline = async (req, res) => { @@ -112,13 +77,12 @@ const trustRelationshipDecline = async (req, res) => { }); const { trustRelationshipId } = req.params; - const session = new Session(); - const walletService = new WalletService(session); - const trustService = new TrustService(session); - const wallet = await walletService.getById(res.locals.wallet_id); - const json = await wallet.declineTrustRequestSentToMe(trustRelationshipId); - const json2 = await trustService.convertToResponse(json); - res.status(200).json(json2); + const trustService = new TrustService(); + const json = await trustService.declineTrustRequestSentToMe({ + walletId: req.wallet_id, + trustRelationshipId, + }); + res.status(200).json(json); }; const trustRelationshipDelete = async (req, res) => { @@ -127,13 +91,12 @@ const trustRelationshipDelete = async (req, res) => { }); const { trustRelationshipId } = req.params; - const session = new Session(); - const walletService = new WalletService(session); - const trustService = new TrustService(session); - const wallet = await walletService.getById(res.locals.wallet_id); - const json = await wallet.cancelTrustRequestSentToMe(trustRelationshipId); - const json2 = await trustService.convertToResponse(json); - res.status(200).json(json2); + const trustService = new TrustService(); + const json = await trustService.cancelTrustRequestSentToMe({ + walletId: req.wallet_id, + trustRelationshipId, + }); + res.status(200).json(json); }; module.exports = { diff --git a/server/models/Trust.js b/server/models/Trust.js index 7cfab30f..73f364b6 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -1,8 +1,12 @@ const TrustRepository = require('../repositories/TrustRepository'); +const HttpError = require('../utils/HttpError'); +const TrustRelationship = require('../utils/trust-enums'); +const Wallet = require('./Wallet'); class Trust { constructor(session) { - this.trustRepository = new TrustRepository(session); + this._session = session; + this._trustRepository = new TrustRepository(session); } /* @@ -42,7 +46,287 @@ class Trust { if (request_type) { filter.and.push({ request_type }); } - return this.trustRepository.getByFilter(filter, { offset, limit }); + return this._trustRepository.getByFilter(filter, { offset, limit }); + } + + /* + * Get all relationships which has been accepted + */ + async getTrustRelationshipsTrusted(walletId) { + return this.getTrustRelationships({ + walletId, + state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted, + }); + } + + /* + * send a trust request to another wallet + */ + async requestTrustFromAWallet({ + requestType, + requesterWallet, + requesteeWallet, + originatorWallet, + }) { + log.debug('request trust...'); + + const walletModel = new Wallet(this._session); + + /* + * Translate the requester/ee to actor/target + */ + const actorWallet = requesterWallet; // case of: manage/send + const targetWallet = requesteeWallet; // case of: mange/send + // if( + // requestType === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.receive || + // requestType === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield){ + // actorWallet = requesteeWallet; + // targetWallet = requesterWallet; + // } + + // check if the orginator can control the actor + const hasControl = await walletModel.hasControlOver( + originatorWallet.id, + actorWallet.id, + ); + + if (!hasControl) { + throw new HttpError(403, 'Have no permission to deal with this actor'); + } + + // check if the target wallet can accept the request + // function below currently empty + // await walletModel.checkTrustRequestSentToMe( + // requestType, + // originatorWallet.id, + // targetWallet.id, + // ); + + // create this request + const trustRelationship = { + type: TrustRelationship.getTrustTypeByRequestType(requestType), + request_type: requestType, + actor_wallet_id: actorWallet.id, + originator_wallet_id: originatorWallet.id, + target_wallet_id: targetWallet.id, + state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested, + }; + await this.checkDuplicateRequest({ + walletId: originatorWallet, + trustRelationship, + }); + const result = await this._trustRepository.create(trustRelationship); + + return { + id: result.id, + actor_wallet: actorWallet.name, + originator_wallet: originatorWallet.name, + target_wallet: targetWallet.name, + type: result.type, + request_type: result.request_type, + state: result.state, + created_at: result.created_at, + updated_at: result.updated_at, + active: result.active, + }; + } + + // check if I (current wallet) can add a new trust like this + async checkDuplicateRequest({ walletId, trustRelationship }) { + const trustRelationships = await this.getTrustRelationships({ walletId }); + if ( + trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.send || + trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.manage + ) { + if ( + trustRelationships.some((e) => { + if ( + (e.request_type === trustRelationship.request_type && + (e.state === + TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested || + e.state === + TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted) && + e.actor_wallet_id === trustRelationship.actor_wallet_id && + e.target_wallet_id === trustRelationship.target_wallet_id) || + (e.request_type !== trustRelationship.request_type && + (e.state === + TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested || + e.state === + TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted) && + e.actor_wallet_id === trustRelationship.target_wallet_id && + e.target_wallet_id === trustRelationship.actor_wallet_id) + ) { + return true; + } + return false; + }) + ) { + log.debug('Has duplicated trust'); + throw new HttpError( + 403, + 'The trust relationship has been requested or trusted', + ); + } + } else { + throw HttpError(500, 'Not supported type'); + } + log.debug('Has no duplicated trust'); + } + + async checkManageCircle({ walletId, trustRelationship }) { + const trustRelationshipTrusted = await this.getTrustRelationshipsTrusted( + walletId, + ); + // just manage type of trust relationship + if (trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.manage) { + // if is manage request + if ( + trustRelationship.request_type === + TrustRelationship.ENTITY_TRUST_TYPE.manage + ) { + if ( + trustRelationshipTrusted.some((e) => { + if ( + (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + e.request_type === + TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage && + e.actor_wallet_id === trustRelationship.target_wallet_id && + e.target_wallet_id === trustRelationship.actor_wallet_id) || + (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + e.request_type === + TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield && + e.actor_wallet_id === trustRelationship.actor_wallet_id && + e.target_wallet_id === trustRelationship.target_wallet_id) + ) { + return true; + } + return false; + }) + ) { + throw new HttpError( + 403, + 'Operation forbidden, because this would lead to a management circle', + ); + } + } else if ( + trustRelationship.request_type === + TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield + ) { + if ( + trustRelationshipTrusted.some((e) => { + if ( + (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + e.request_type === + TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield && + e.actor_wallet_id === trustRelationship.target_wallet_id && + e.target_wallet_id === trustRelationship.actor_wallet_id) || + (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + e.request_type === + TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage && + e.actor_wallet_id === trustRelationship.actor_wallet_id && + e.target_wallet_id === trustRelationship.target_wallet_id) + ) { + return true; + } + return false; + }) + ) { + throw new HttpError( + 403, + 'Operation forbidden, because this would lead to a management circle', + ); + } + } + } + } + + /* + * Get all the trust relationships request to me + */ + async getTrustRelationshipsRequestedToMe(walletId) { + const walletModel = new Wallet(this._session); + const allWallets = await walletModel.getAllWallets(walletId); + const allTrustRelationships = []; + for (const wallet of allWallets) { + const list = await this.getTrustRelationships({ walletId: wallet.id }); + allTrustRelationships.push(...list); + } + const walletIds = [...allWallets.map((e) => e.id)]; + return allTrustRelationships.filter((trustRelationship) => { + return walletIds.includes(trustRelationship.target_wallet_id); + }); + } + + /* + * Accept a trust relationship request + */ + async acceptTrustRequestSentToMe({ trustRelationshipId, walletId }) { + const trustRelationships = await this.getTrustRelationshipsRequestedToMe( + walletId, + ); + const trustRelationship = trustRelationships.reduce((a, c) => { + if (c.id === trustRelationshipId) { + return c; + } + return a; + }, undefined); + + if (!trustRelationship) { + throw new HttpError( + 403, + 'Have no permission to accept this relationship', + ); + } + await this.checkManageCircle({ walletId, trustRelationship }); + trustRelationship.state = TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted; + const json = await this._trustRepository.update(trustRelationship); + return json; + } + + /* + * Decline a trust relationship request + */ + async declineTrustRequestSentToMe({ walletId, trustRelationshipId }) { + const trustRelationships = await this.getTrustRelationshipsRequestedToMe( + walletId, + ); + const trustRelationship = trustRelationships.reduce((a, c) => { + if (c.id === trustRelationshipId) { + return c; + } + return a; + }, undefined); + + if (!trustRelationship) { + throw new HttpError( + 403, + 'Have no permission to decline this relationship', + ); + } + trustRelationship.state = + TrustRelationship.ENTITY_TRUST_STATE_TYPE.canceled_by_target; + const json = await this.trustRepository.update(trustRelationship); + return json; + } + + /* + * Cancel a trust relationship request + */ + async cancelTrustRequestSentToMe({ trustRelationshipId, walletId }) { + const trustRelationships = await this._trustRepository.getByFilter({ + id: trustRelationshipId, + }); + const [trustRelationship] = trustRelationships; + if (trustRetrustRelationship?.originator_wallet_id !== walletId) { + throw new HttpError( + 403, + 'Have no permission to cancel this relationship', + ); + } + trustRelationship.state = + TrustRelationship.ENTITY_TRUST_STATE_TYPE.cancelled_by_originator; + const json = await this._trustRepository.update(trustRelationship); + return { ...trustRelationship, updated_at: json.updated_at }; } } diff --git a/server/models/Wallet.js b/server/models/Wallet.js index 7d35ff6d..e328fd37 100644 --- a/server/models/Wallet.js +++ b/server/models/Wallet.js @@ -63,22 +63,6 @@ class Wallet { }); } - /* - * Get all the trust relationships request to me - */ - async getTrustRelationshipsRequestedToMe() { - const result = await this.getTrustRelationships(); - const subWallets = await this.getSubWallets(); - for (const subWallet of subWallets) { - const list = await subWallet.getTrustRelationships(); - result.push(...list); - } - const walletIds = [this._id, ...subWallets.map((e) => e.getId())]; - return result.filter((trustRelationship) => { - return walletIds.includes(trustRelationship.target_wallet_id); - }); - } - /* * Get all the trust relationships targeted to me, means request * the trust from me @@ -87,19 +71,6 @@ class Wallet { return await this.trustRepository.getByTargetId(this._id); } - /* - * Get all relationships which has been accepted - */ - async getTrustRelationshipsTrusted() { - const result = await this.getTrustRelationships(); - return result.filter((trustRelationship) => { - return ( - trustRelationship.state === - TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted - ); - }); - } - async getById(id) { return this._walletRepository.getById(id); } @@ -108,95 +79,6 @@ class Wallet { return this._walletRepository.getByName(name); } - /* - * send a trust request to another wallet - */ - async requestTrustFromAWallet(requestType, requesterWallet, requesteeWallet) { - log.debug('request trust...'); - Joi.assert( - requestType, - Joi.string() - .required() - .valid(...Object.keys(TrustRelationship.ENTITY_TRUST_REQUEST_TYPE)), - ); - - /* - * Translate the requester/ee to actor/target - */ - const actorWallet = requesterWallet; // case of: manage/send - const targetWallet = requesteeWallet; // case of: mange/send - // if( - // requestType === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.receive || - // requestType === TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield){ - // actorWallet = requesteeWallet; - // targetWallet = requesterWallet; - // } - - // check if the orginator can control the actor - const hasControl = await this.hasControlOver(requesterWallet); - if (!hasControl) { - throw new HttpError(403, 'Have no permission to deal with this actor'); - } - - // check if the target wallet can accept the request - await requesteeWallet.checkTrustRequestSentToMe(requestType, this.id); - - // create this request - const trustRelationship = { - type: TrustRelationship.getTrustTypeByRequestType(requestType), - request_type: requestType, - actor_wallet_id: actorWallet.getId(), - originator_wallet_id: this._id, - target_wallet_id: targetWallet.getId(), - state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested, - }; - await this.checkDuplicateRequest(trustRelationship); - const result = await this.trustRepository.create(trustRelationship); - return result; - } - - // check if I (current wallet) can add a new trust like this - async checkDuplicateRequest(trustRelationship) { - const trustRelationships = await this.getTrustRelationships(); - if ( - trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.send || - trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.manage - ) { - if ( - trustRelationships.some((e) => { - if ( - (e.request_type === trustRelationship.request_type && - (e.state === - TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested || - e.state === - TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted) && - e.actor_wallet_id === trustRelationship.actor_wallet_id && - e.target_wallet_id === trustRelationship.target_wallet_id) || - (e.request_type !== trustRelationship.request_type && - (e.state === - TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested || - e.state === - TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted) && - e.actor_wallet_id === trustRelationship.target_wallet_id && - e.target_wallet_id === trustRelationship.actor_wallet_id) - ) { - return true; - } - return false; - }) - ) { - log.debug('Has duplicated trust'); - throw new HttpError( - 403, - 'The trust relationship has been requested or trusted', - ); - } - } else { - throw HttpError(500, 'Not supported type'); - } - log.debug('Has no duplicated trust'); - } - /* * Check if a request sent to me is acceptable. * @@ -208,140 +90,6 @@ class Wallet { // pass } - /* - * Accept a trust relationship request - */ - async acceptTrustRequestSentToMe(trustRelationshipId) { - const trustRelationships = await this.getTrustRelationshipsRequestedToMe( - this._id, - ); - const trustRelationship = trustRelationships.reduce((a, c) => { - if (c.id === trustRelationshipId) { - return c; - } - return a; - }, undefined); - if (!trustRelationship) { - throw new HttpError( - 403, - 'Have no permission to accept this relationship', - ); - } - await this.checkManageCircle(trustRelationship); - trustRelationship.state = TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted; - const json = await this.trustRepository.update(trustRelationship); - return json; - } - - async checkManageCircle(trustRelationship) { - const trustRelationshipTrusted = await this.getTrustRelationshipsTrusted(); - // just manage type of trust relationship - if (trustRelationship.type === TrustRelationship.ENTITY_TRUST_TYPE.manage) { - // if is mange request - if ( - trustRelationship.request_type === - TrustRelationship.ENTITY_TRUST_TYPE.manage - ) { - if ( - trustRelationshipTrusted.some((e) => { - if ( - (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && - e.request_type === - TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage && - e.actor_wallet_id === trustRelationship.target_wallet_id && - e.target_wallet_id === trustRelationship.actor_wallet_id) || - (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && - e.request_type === - TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield && - e.actor_wallet_id === trustRelationship.actor_wallet_id && - e.target_wallet_id === trustRelationship.target_wallet_id) - ) { - return true; - } - return false; - }) - ) { - throw new HttpError( - 403, - 'Operation forbidden, because this would lead to a management circle', - ); - } - } else if ( - trustRelationship.request_type === - TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield - ) { - if ( - trustRelationshipTrusted.some((e) => { - if ( - (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && - e.request_type === - TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield && - e.actor_wallet_id === trustRelationship.target_wallet_id && - e.target_wallet_id === trustRelationship.actor_wallet_id) || - (e.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && - e.request_type === - TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage && - e.actor_wallet_id === trustRelationship.actor_wallet_id && - e.target_wallet_id === trustRelationship.target_wallet_id) - ) { - return true; - } - return false; - }) - ) { - throw new HttpError( - 403, - 'Operation forbidden, because this would lead to a management circle', - ); - } - } - } - } - - /* - * Decline a trust relationship request - */ - async declineTrustRequestSentToMe(trustRelationshipId) { - const trustRelationships = await this.getTrustRelationshipsRequestedToMe( - this._id, - ); - const trustRelationship = trustRelationships.reduce((a, c) => { - if (c.id === trustRelationshipId) { - return c; - } - return a; - }, undefined); - if (!trustRelationship) { - throw new HttpError( - 403, - 'Have no permission to decline this relationship', - ); - } - trustRelationship.state = - TrustRelationship.ENTITY_TRUST_STATE_TYPE.canceled_by_target; - const json = await this.trustRepository.update(trustRelationship); - return json; - } - - /* - * Cancel a trust relationship request - */ - async cancelTrustRequestSentToMe(trustRelationshipId) { - const trustRelationship = await this.trustRepository.getById( - trustRelationshipId, - ); - if (trustRelationship.originator_wallet_id !== this._id) { - throw new HttpError( - 403, - 'Have no permission to cancel this relationship', - ); - } - trustRelationship.state = - TrustRelationship.ENTITY_TRUST_STATE_TYPE.cancelled_by_originator; - const json = await this.trustRepository.update(trustRelationship); - return json; - } - /* * To check if the indicated trust relationship exist between the source and * target wallet @@ -624,49 +372,15 @@ class Wallet { /* * I have control over given wallet */ - async hasControlOver(wallet) { + async hasControlOver(parentId, childId) { // if the given wallet is me, then pass - if (wallet.getId() === this._id) { + if (parentId === childId) { log.debug('The same wallet, control'); return true; } // check sub wallet - const result = await this.trustRepository.getByFilter({ - or: [ - { - and: [ - { - actor_wallet_id: this._id, - }, - { - request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage, - }, - { - target_wallet_id: wallet.getId(), - }, - { - state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted, - }, - ], - }, - { - and: [ - { - actor_wallet_id: wallet.getId(), - }, - { - request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield, - }, - { - target_wallet_id: this._id, - }, - { - state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted, - }, - ], - }, - ], - }); + const result = await this.getSubWallets(parentId, childId); + if (result.length > 0) { return true; } @@ -1052,36 +766,56 @@ class Wallet { } /* - * Get all wallet managed by me + * Get all wallet managed by me(parentId) + * Optionally get a specific subwallet */ - async getSubWallets(id) { - const trustRelationships = await this._trust.getTrustRelationships({ - walletId: id, - }); - console.log('trustRelationships', trustRelationships); - const subWallets = []; - for (const e of trustRelationships) { - if ( - e.actor_wallet_id === id && - e.request_type === - TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.manage && - e.state === TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted - ) { - const subWallet = await this.getById(e.target_wallet_id); - subWallets.push(subWallet); - } else if ( - e.target_wallet_id === id && - e.request_type === - TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.yield && - e.state === TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted - ) { - const subWallet = await this.getById(e.actor_wallet_id); - subWallets.push(subWallet); - } + async getSubWallets(parentId, childId) { + const filter = { + or: [ + { + and: [ + { + actor_wallet_id: parentId, + }, + { + request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.manage, + }, + + { + state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted, + }, + ], + }, + { + and: [ + { + request_type: TrustRelationship.ENTITY_TRUST_REQUEST_TYPE.yield, + }, + { + target_wallet_id: parentId, + }, + { + state: TrustRelationship.ENTITY_TRUST_STATE_TYPE.trusted, + }, + ], + }, + ], + }; + + if (childId) { + filter.or[0].and.push({ + target_wallet_id: childId, + }); + filter.or[1].and.push({ + actor_wallet_id: childId, + }); } - return subWallets; + const result = await this._trustRepository.getByFilter(filter); + + return result; } + // get wallet itself along with all subwallets async getAllWallets(id, limitOptions) { return this._walletRepository.getAllWallets(id, limitOptions); } diff --git a/server/repositories/.BaseRepository.js.swp b/server/repositories/.BaseRepository.js.swp deleted file mode 100644 index 708b0afb79cc3b06884444f0794bd48fcf354ade..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2%WvF79LFbx&=yiaaHEGzLzUQJ*G@{2sC_nV(5 zsw;CZFFi+3ICBKogM|Db%In+i^mvOB@`ks?Vp+b&RpFj!@fLq9m=eSh&w{#^OwPao`F(3hw!r880`yC}N`QrJ_ zNO;IHchSq(3!Q?dvzd)fYD$a(Mgc-{Q7xZ1L>@mjUu^Y9r;gBvA9~3sV-zq77zK<1 zMggOMQNSo*6fg?>j}#E;7Qs_$b%cm31ci~>dhqkvJsC}0#Y3K#{90!9I& zfKk9GU=+9m74QN=-n@&DiE$W@|Nrm&{r}AagnS4ZU<<5+HE;?X1~=~~h_z`>yJ_8?vYv4Wb4tNb%U;_L)LCELeDyV^F@W*|G+yI|~DtHw< z4~~H`@b$g06I=zSz+>R|gD3;nz&3an)W90J0M3H14iNGYNWmsJ4<^9`H~{v8pZ62; zK1jeaI0q)dVekmp2X5R$$d}*~uni``K`;*P2ERbp55N`hB6t=&0p`IRa6lOp`rm=^ zV@x&*{1+6c%PR@;h&k-_G~ptnr|ID5MJEYq6GCYlP|BIed5l|@M`C4e^<+`nUbWQ& zrD3o@F%{8K66qRvn78I&FtCU^}m6coKPXDwq{ z7ACP%W8&9vr~m#Ue=6#>T~7^)@jz- zYfb;eqC5?HQ=LiDrtLH~7-!a07)+I*(N0!U8ZdnI#i&|{?B;|)pE_XxWmILS;$pF8 z$Adz(ST9X>Ea`T3(H*1q4T?cp(wRz?0aNW!RZ@>FUwNs?b!SJQS^-)r)HDDbBs7 zttHqoP*R@M!;IVrC;mOH>zbc~Q=~#ad4}w7j&3hx^-3#NwLR zxLCF32YR#zEtNwkQ$}sd@`_e&L`j%fqP_Z#l5#X;Pq;c}D>L{YbY>O1u_(kp)r4Ec zE{>5Ns(Lf8%p`oEm0pk%Sl2qc2d*4^Ck<2{-BE@@g2Kw8(TD}N?mevV-{-!rQ9b|s zJ^u|kW*7GjdwIxzs`~aiPzIQG-Qespy1LP6J;yor*YUaICAxmbi)ow)?6@3r9c>MrHrjGF zoNb42%;lQn#xs HW$QlyT+iK( diff --git a/server/services/TrustService.js b/server/services/TrustService.js index 813e051a..aece430a 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -1,6 +1,7 @@ const Trust = require('../models/Trust'); const Session = require('../database/Session'); const Wallet = require('../models/Wallet'); +const WalletService = require('./WalletService'); class TrustService { constructor() { @@ -13,7 +14,7 @@ class TrustService { state, type, request_type, - offset, + offset = 0, limit, }) { return this._trust.getTrustRelationships({ @@ -25,6 +26,86 @@ class TrustService { limit, }); } + + // limit and offset not feasible using the current implementation + // except if done manually or coming up with a single query + async getAllTrustRelationships({ walletId, state, type, request_type }) { + const walletModel = new Wallet(this._session); + const wallets = await walletModel.getAllWallets(walletId); + + const alltrustRelationships = []; + + for (const w of wallets) { + const trustRelationships = await this.getTrustRelationships({ + walletId: w.id, + state, + type, + request_type, + }); + alltrustRelationships.push(...trustRelationships); + } + + // remove possible duplicates + const ids = {}; + const finalTrustRelationships = []; + + for (const tr of alltrustRelationships) { + if (ids[tr.id]) continue; + finalTrustRelationships.push(tr); + ids[tr.id] = 1; + } + + return finalTrustRelationships; + } + + async createTrustRelationship({ + walletId, + requesteeWallet, + requesterWallet, + trustRequestType, + }) { + const walletService = new WalletService(); + const originatorWallet = await walletService.getById(walletId); + const requesteeWalletDetails = await walletService.getByName( + requesteeWallet, + ); + let requesterWalletDetails; + if (requesterWallet) { + requesterWalletDetails = await walletService.getByName(requesterWallet); + } else { + requesterWalletDetails = originatorWallet; + } + + const trustRelationship = await this._trust.requestTrustFromAWallet({ + trustRequestType, + requesterWallet: requesterWalletDetails, + requesteeWallet: requesteeWalletDetails, + originatorWallet, + }); + + return trustRelationship; + } + + async acceptTrustRequestSentToMe({ walletId, trustRelationshipId }) { + return this._trust.acceptTrustRequestSentToMe({ + walletId, + trustRelationshipId, + }); + } + + async declineTrustRequestSentToMe({ walletId, trustRelationshipId }) { + return this._trust.declineTrustRequestSentToMe({ + walletId, + trustRelationshipId, + }); + } + + async cancelTrustRequestSentToMe({ walletId, trustRelationshipId }) { + return this._trust.cancelTrustRequestSentToMe({ + walletId, + trustRelationshipId, + }); + } } module.exports = TrustService; diff --git a/server/utils/trust-enums.js b/server/utils/trust-enums.js index 16821295..6549e9b5 100644 --- a/server/utils/trust-enums.js +++ b/server/utils/trust-enums.js @@ -1,4 +1,5 @@ -const HttpError = require('../utils/HttpError'); +const HttpError = require("./HttpError"); + const TrustRelationship = {}; TrustRelationship.ENTITY_TRUST_TYPE = {