Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
66 changes: 35 additions & 31 deletions src/service/MosaicRestrictionTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand All @@ -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) {
}

/**
Expand All @@ -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<Transaction> {
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(
Expand All @@ -85,7 +86,7 @@ export class MosaicRestrictionTransactionService {
}),
catchError((err) => {
throw Error(err);
}));
}));
}

/**
Expand All @@ -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,
Expand All @@ -126,35 +125,40 @@ 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<string | undefined>}
*/
private getAddressRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64, targetAddress: Address): Observable<string | undefined> {
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
* @param restrictionKey - Mosaic global restriction key
* @return {Observable<MosaicGlobalRestrictionItem | undefined>}
*/
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable<MosaicGlobalRestrictionItem | undefined> {
return this.restrictionHttp.getMosaicGlobalRestriction(mosaicId).pipe(
return this.restrictionMosaicRepository.getMosaicGlobalRestriction(mosaicId).pipe(
map((mosaicRestriction: MosaicGlobalRestriction) => {
return mosaicRestriction.restrictions.get(restrictionKey.toHex());
}),
Expand All @@ -164,7 +168,7 @@ export class MosaicRestrictionTransactionService {
return of(undefined);
}
throw Error(err.message);
}));
}));
}

/**
Expand Down
22 changes: 11 additions & 11 deletions test/service/MosaicRestrictionTransactionservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand All @@ -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<RestrictionMosaicRepository>();

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);
});

Expand Down