From 3f950ba44d6f21b9d8d70a4f201f82d913896388 Mon Sep 17 00:00:00 2001 From: Fernando Boucquez Date: Wed, 11 Dec 2019 17:30:25 -0300 Subject: [PATCH] Changed MosaicRestrictionTransactionService to use repositories Allow MosaicRestrictionTransactionService to use referenceMosaicId alias Upgraded mockito Fixed handlebars security issue --- package-lock.json | 22 +++---- package.json | 2 +- .../MosaicRestrictionTransactionService.ts | 66 ++++++++++--------- ...osaicRestrictionTransactionservice.spec.ts | 22 +++---- 4 files changed, 58 insertions(+), 54 deletions(-) diff --git a/package-lock.json b/package-lock.json index b108461788..4c4efe546f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2572,9 +2572,9 @@ } }, "handlebars": { - "version": "4.5.1", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", - "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", + "version": "4.5.3", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.3.tgz", + "integrity": "sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -3741,7 +3741,7 @@ }, "is-fullwidth-code-point": { "version": "2.0.0", - "resolved": false, + "resolved": "", "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", "dev": true }, @@ -3779,7 +3779,7 @@ }, "which-module": { "version": "2.0.0", - "resolved": false, + "resolved": "", "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=", "dev": true }, @@ -5151,9 +5151,9 @@ "integrity": "sha512-1m4RA7xVAJrSGrrXGs0L3YTwyvBs2S8PbRHaLZAkFw7JR8oIFwYtysxlBZhYIa7xSyiYJKZ3iGrrk55cGA3i9A==" }, "ts-mockito": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/ts-mockito/-/ts-mockito-2.3.0.tgz", - "integrity": "sha512-IB37YP8DppTHUf/aJeayUa7fZp7mn7HK+NYalzdyoy0z0vOpZwO+HiUKLUkK+c9ERcRlOdwm8Do99ndYsvm+Bw==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/ts-mockito/-/ts-mockito-2.5.0.tgz", + "integrity": "sha512-b3qUeMfghRq5k5jw3xNJcnU9RKhqKnRn0k9v9QkN+YpuawrFuMIiGwzFZCpdi5MHy26o7YPnK8gag2awURl3nA==", "dev": true, "requires": { "lodash": "^4.17.5" @@ -5340,9 +5340,9 @@ } }, "uglify-js": { - "version": "3.6.8", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.8.tgz", - "integrity": "sha512-XhHJ3S3ZyMwP8kY1Gkugqx3CJh2C3O0y8NPiSxtm1tyD/pktLAkFZsFGpuNfTZddKDQ/bbDBLAd2YyA1pbi8HQ==", + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.2.tgz", + "integrity": "sha512-uhRwZcANNWVLrxLfNFEdltoPNhECUR3lc+UdJoG9CBpMcSnKyWA94tc3eAujB1GcMY5Uwq8ZMp4qWpxWYDQmaA==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index c5a0cc22b3..e0ed8f638c 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "mocha": "^4.0.1", "nyc": "^14.1.1", "secure-random": "^1.1.1", - "ts-mockito": "^2.2.7", + "ts-mockito": "^2.4.0", "ts-node": "^5.0.1", "tslint": "^5.8.0", "typescript": "^2.5.3", diff --git a/src/service/MosaicRestrictionTransactionService.ts b/src/service/MosaicRestrictionTransactionService.ts index 3838352e5a..eecdbb3899 100644 --- a/src/service/MosaicRestrictionTransactionService.ts +++ b/src/service/MosaicRestrictionTransactionService.ts @@ -16,11 +16,9 @@ import { Observable, of } from 'rxjs'; import { catchError, map, switchMap } from 'rxjs/operators'; -import { RestrictionMosaicHttp } from '../infrastructure/RestrictionMosaicHttp'; import { Address } from '../model/account/Address'; import { NetworkType } from '../model/blockchain/NetworkType'; import { MosaicId } from '../model/mosaic/MosaicId'; -import { MosaicAddressRestriction } from '../model/restriction/MosaicAddressRestriction'; import { MosaicGlobalRestriction } from '../model/restriction/MosaicGlobalRestriction'; import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRestrictionItem'; import { MosaicRestrictionType } from '../model/restriction/MosaicRestrictionType'; @@ -29,6 +27,8 @@ import { MosaicAddressRestrictionTransaction } from '../model/transaction/Mosaic import { MosaicGlobalRestrictionTransaction } from '../model/transaction/MosaicGlobalRestrictionTransaction'; import { Transaction } from '../model/transaction/Transaction'; import { UInt64 } from '../model/UInt64'; +import { RestrictionMosaicRepository } from "../infrastructure/RestrictionMosaicRespository"; +import { NamespaceId } from "../model/namespace/NamespaceId"; /** * MosaicRestrictionTransactionService service @@ -37,11 +37,12 @@ export class MosaicRestrictionTransactionService { private readonly defaultMosaicAddressRestrictionVaule = UInt64.fromHex('FFFFFFFFFFFFFFFF'); private readonly defaultMosaicGlobalRestrictionVaule = UInt64.fromUint(0); + /** * Constructor - * @param restrictionHttp + * @param restrictionMosaicRepository */ - constructor(private readonly restrictionHttp: RestrictionMosaicHttp) { + constructor(private readonly restrictionMosaicRepository: RestrictionMosaicRepository) { } /** @@ -61,13 +62,13 @@ export class MosaicRestrictionTransactionService { restrictionKey: UInt64, restrictionValue: string, restrictionType: MosaicRestrictionType, - referenceMosaicId: MosaicId = new MosaicId(UInt64.fromUint(0).toDTO()), + referenceMosaicId: MosaicId | NamespaceId = new MosaicId(UInt64.fromUint(0).toDTO()), maxFee: UInt64 = new UInt64([0, 0])): Observable { this.validateInput(restrictionValue); return this.getGlobalRestrictionEntry(mosaicId, restrictionKey).pipe( map((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => { const currentValue = restrictionEntry ? UInt64.fromNumericString(restrictionEntry.restrictionValue) : - this.defaultMosaicGlobalRestrictionVaule; + this.defaultMosaicGlobalRestrictionVaule; const currentType = restrictionEntry ? restrictionEntry.restrictionType : MosaicRestrictionType.NONE; return MosaicGlobalRestrictionTransaction.create( @@ -85,7 +86,7 @@ export class MosaicRestrictionTransactionService { }), catchError((err) => { throw Error(err); - })); + })); } /** @@ -111,11 +112,9 @@ export class MosaicRestrictionTransactionService { if (!restrictionEntry) { throw Error('Global restriction is not valid for RetrictionKey: ' + restrictionKey); } - return this.restrictionHttp.getMosaicAddressRestriction(mosaicId, targetAddress).pipe( - map((addressRestriction: MosaicAddressRestriction) => { - const addressEntry = addressRestriction.restrictions.get(restrictionKey.toHex()); - const currentValue = addressEntry ? UInt64.fromNumericString(addressEntry) : - this.defaultMosaicAddressRestrictionVaule; + return this.getAddressRestrictionEntry(mosaicId, restrictionKey, targetAddress).pipe( + map((optionalValue) => { + const currentValue = optionalValue ? UInt64.fromNumericString(optionalValue) : this.defaultMosaicAddressRestrictionVaule; return MosaicAddressRestrictionTransaction.create( deadline, mosaicId, @@ -126,27 +125,32 @@ export class MosaicRestrictionTransactionService { currentValue, maxFee, ); - }), - catchError((err: Error) => { - const error = JSON.parse(err.message); - if (error && error.statusCode && error.statusCode === 404) { - return of(MosaicAddressRestrictionTransaction.create( - deadline, - mosaicId, - restrictionKey, - targetAddress, - UInt64.fromNumericString(restrictionValue), - networkType, - this.defaultMosaicAddressRestrictionVaule, - maxFee, - )); - } - throw Error(err.message); - })); + })); }), ); } + /** + * Get address global restriction previous value and type + * @param mosaicId - Mosaic identifier + * @param restrictionKey - Mosaic global restriction key + * @param targetAddress - The target address + * @return {Observable} + */ + private getAddressRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64, targetAddress: Address): Observable { + return this.restrictionMosaicRepository.getMosaicAddressRestriction(mosaicId, targetAddress).pipe( + map((mosaicRestriction) => { + return mosaicRestriction.restrictions.get(restrictionKey.toHex()); + }), + catchError((err: Error) => { + const error = JSON.parse(err.message); + if (error && error.statusCode && error.statusCode === 404) { + return of(undefined); + } + throw Error(err.message); + })); + } + /** * Get mosaic global restriction prvious value and type * @param mosaicId - Mosaic identifier @@ -154,7 +158,7 @@ export class MosaicRestrictionTransactionService { * @return {Observable} */ private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable { - return this.restrictionHttp.getMosaicGlobalRestriction(mosaicId).pipe( + return this.restrictionMosaicRepository.getMosaicGlobalRestriction(mosaicId).pipe( map((mosaicRestriction: MosaicGlobalRestriction) => { return mosaicRestriction.restrictions.get(restrictionKey.toHex()); }), @@ -164,7 +168,7 @@ export class MosaicRestrictionTransactionService { return of(undefined); } throw Error(err.message); - })); + })); } /** diff --git a/test/service/MosaicRestrictionTransactionservice.spec.ts b/test/service/MosaicRestrictionTransactionservice.spec.ts index 1ff3e2db21..318bfb804f 100644 --- a/test/service/MosaicRestrictionTransactionservice.spec.ts +++ b/test/service/MosaicRestrictionTransactionservice.spec.ts @@ -14,13 +14,13 @@ * limitations under the License. */ -import {expect} from 'chai'; -import {of as observableOf} from 'rxjs'; -import {deepEqual, instance, mock, when} from 'ts-mockito'; +import { expect } from 'chai'; +import { of as observableOf } from 'rxjs'; +import { deepEqual, instance, mock, when } from 'ts-mockito'; import { KeyGenerator } from '../../src/core/format/KeyGenerator'; -import { RestrictionMosaicHttp } from '../../src/infrastructure/RestrictionMosaicHttp'; +import { RestrictionMosaicRepository } from '../../src/infrastructure/RestrictionMosaicRespository'; import { Account } from '../../src/model/account/Account'; -import {NetworkType} from '../../src/model/blockchain/NetworkType'; +import { NetworkType } from '../../src/model/blockchain/NetworkType'; import { MosaicId } from '../../src/model/mosaic/MosaicId'; import { MosaicAddressRestriction } from '../../src/model/restriction/MosaicAddressRestriction'; import { MosaicGlobalRestriction } from '../../src/model/restriction/MosaicGlobalRestriction'; @@ -31,7 +31,7 @@ import { Deadline } from '../../src/model/transaction/Deadline'; import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction'; import { MosaicGlobalRestrictionTransaction } from '../../src/model/transaction/MosaicGlobalRestrictionTransaction'; import { TransactionType } from '../../src/model/transaction/TransactionType'; -import {UInt64} from '../../src/model/UInt64'; +import { UInt64 } from '../../src/model/UInt64'; import { MosaicRestrictionTransactionService } from '../../src/service/MosaicRestrictionTransactionService'; import { TestingAccount } from '../conf/conf.spec'; @@ -52,18 +52,18 @@ describe('MosaicRestrictionTransactionService', () => { mosaicId = new MosaicId('85BBEA6CC462B244'); mosaicIdWrongKey = new MosaicId('85BBEA6CC462B288'); referenceMosaicId = new MosaicId('1AB129B545561E6A'); - const mockRestrictionHttp = mock(RestrictionMosaicHttp); + const mockRestrictionRepository = mock(); - when(mockRestrictionHttp + when(mockRestrictionRepository .getMosaicGlobalRestriction(deepEqual(mosaicId))) .thenReturn(observableOf(mockGlobalRestriction())); - when(mockRestrictionHttp + when(mockRestrictionRepository .getMosaicGlobalRestriction(deepEqual(mosaicIdWrongKey))) .thenThrow(new Error()); - when(mockRestrictionHttp + when(mockRestrictionRepository .getMosaicAddressRestriction(deepEqual(mosaicId), deepEqual(account.address))) .thenReturn(observableOf(mockAddressRestriction())); - const restrictionHttp = instance(mockRestrictionHttp); + const restrictionHttp = instance(mockRestrictionRepository); mosaicRestrictionTransactionService = new MosaicRestrictionTransactionService(restrictionHttp); });