Skip to content

Commit 50ab61c

Browse files
committed
JAV-60 [Github: #289] Changed MosaicRestrictionKey to hex string
1 parent 60b9daa commit 50ab61c

File tree

5 files changed

+33
-45
lines changed

5 files changed

+33
-45
lines changed

e2e/service/MosaicRestrictionTransactionService.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assert, expect } from 'chai';
2+
import { KeyGenerator } from '../../src/core/format/KeyGenerator';
23
import { Listener } from '../../src/infrastructure/Listener';
34
import { RestrictionHttp } from '../../src/infrastructure/RestrictionHttp';
45
import { TransactionHttp } from '../../src/infrastructure/TransactionHttp';
@@ -19,7 +20,7 @@ import { MosaicRestrictionTransactionService } from '../../src/service/MosaicRes
1920

2021
describe('MosaicRestrictionTransactionService', () => {
2122
const deadline = Deadline.create();
22-
const key = '9876543';
23+
const key = KeyGenerator.generateUInt64Key('TestKey');
2324
let targetAccount: Account;
2425
let account: Account;
2526
let restrictionHttp: RestrictionHttp;
@@ -98,7 +99,7 @@ describe('MosaicRestrictionTransactionService', () => {
9899
const mosaicGlobalRestrictionTransaction = MosaicGlobalRestrictionTransaction.create(
99100
Deadline.create(),
100101
mosaicId,
101-
UInt64.fromNumericString(key),
102+
key,
102103
UInt64.fromUint(0),
103104
MosaicRestrictionType.NONE,
104105
UInt64.fromUint(0),
@@ -132,7 +133,7 @@ describe('MosaicRestrictionTransactionService', () => {
132133
const mosaicAddressRestrictionTransaction = MosaicAddressRestrictionTransaction.create(
133134
Deadline.create(),
134135
mosaicId,
135-
UInt64.fromNumericString(key),
136+
key,
136137
targetAccount.address,
137138
UInt64.fromUint(2),
138139
NetworkType.MIJIN_TEST,
@@ -177,7 +178,7 @@ describe('MosaicRestrictionTransactionService', () => {
177178
expect(transaction.previousRestrictionType).to.be.equal(MosaicRestrictionType.GE);
178179
expect(transaction.newRestrictionValue.toString()).to.be.equal('1');
179180
expect(transaction.newRestrictionType).to.be.equal(MosaicRestrictionType.GE);
180-
expect(transaction.restrictionKey.toString()).to.be.equal(key);
181+
expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex());
181182
done();
182183
});
183184
});
@@ -196,7 +197,7 @@ describe('MosaicRestrictionTransactionService', () => {
196197
expect(transaction.previousRestrictionValue.toString()).to.be.equal('2');
197198
expect(transaction.newRestrictionValue.toString()).to.be.equal('3');
198199
expect(transaction.targetAddress.plain()).to.be.equal(targetAccount.address.plain());
199-
expect(transaction.restrictionKey.toString()).to.be.equal(key);
200+
expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex());
200201
done();
201202
});
202203
});

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
347347
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
348348
new MosaicId(transactionDTO.mosaicId),
349349
new MosaicId(transactionDTO.referenceMosaicId),
350-
UInt64.fromNumericString(transactionDTO.restrictionKey),
350+
UInt64.fromHex(transactionDTO.restrictionKey),
351351
UInt64.fromNumericString(transactionDTO.previousRestrictionValue),
352352
transactionDTO.previousRestrictionType,
353353
UInt64.fromNumericString(transactionDTO.newRestrictionValue),
@@ -365,7 +365,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
365365
Deadline.createFromDTO(transactionDTO.deadline),
366366
UInt64.fromNumericString(transactionDTO.maxFee || '0'),
367367
new MosaicId(transactionDTO.mosaicId),
368-
UInt64.fromNumericString(transactionDTO.restrictionKey),
368+
UInt64.fromHex(transactionDTO.restrictionKey),
369369
typeof targetAddress === 'object' && targetAddress.hasOwnProperty('address') ?
370370
Address.createFromRawAddress(targetAddress.address) : Address.createFromEncoded(targetAddress),
371371
UInt64.fromNumericString(transactionDTO.previousRestrictionValue),

src/infrastructure/transaction/SerializeTransactionToJSON.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => {
170170
return {
171171
mosaicId: (transaction as MosaicGlobalRestrictionTransaction).mosaicId.toHex(),
172172
referenceMosaicId: (transaction as MosaicGlobalRestrictionTransaction).referenceMosaicId.toHex(),
173-
restrictionKey: (transaction as MosaicGlobalRestrictionTransaction).restrictionKey.toString(),
173+
restrictionKey: (transaction as MosaicGlobalRestrictionTransaction).restrictionKey.toHex(),
174174
previousRestrictionValue: (transaction as MosaicGlobalRestrictionTransaction).previousRestrictionValue.toString(),
175175
previousRestrictionType: (transaction as MosaicGlobalRestrictionTransaction).previousRestrictionType,
176176
newRestrictionValue: (transaction as MosaicGlobalRestrictionTransaction).newRestrictionValue.toString(),
@@ -179,7 +179,7 @@ export const SerializeTransactionToJSON = (transaction: Transaction): any => {
179179
case TransactionType.MOSAIC_ADDRESS_RESTRICTION:
180180
return {
181181
mosaicId: (transaction as MosaicAddressRestrictionTransaction).mosaicId.toHex(),
182-
restrictionKey: (transaction as MosaicAddressRestrictionTransaction).restrictionKey.toString(),
182+
restrictionKey: (transaction as MosaicAddressRestrictionTransaction).restrictionKey.toHex(),
183183
targetAddress: (transaction as MosaicAddressRestrictionTransaction).targetAddress.toDTO(),
184184
previousRestrictionValue: (transaction as MosaicAddressRestrictionTransaction).previousRestrictionValue.toString(),
185185
newRestrictionValue: (transaction as MosaicAddressRestrictionTransaction).newRestrictionValue.toString(),

src/service/MosaicRestrictionTransactionService.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ export class MosaicRestrictionTransactionService {
5858
public createMosaicGlobalRestrictionTransaction(deadline: Deadline,
5959
networkType: NetworkType,
6060
mosaicId: MosaicId,
61-
restrictionKey: string,
61+
restrictionKey: UInt64,
6262
restrictionValue: string,
6363
restrictionType: MosaicRestrictionType,
6464
referenceMosaicId: MosaicId = new MosaicId(UInt64.fromUint(0).toDTO()),
6565
maxFee: UInt64 = new UInt64([0, 0])): Observable<Transaction> {
66-
this.validateInput(restrictionKey, restrictionValue);
66+
this.validateInput(restrictionValue);
6767
return this.getGlobalRestrictionEntry(mosaicId, restrictionKey).pipe(
6868
map((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => {
6969
const currentValue = restrictionEntry ? UInt64.fromNumericString(restrictionEntry.restrictionValue) :
@@ -73,7 +73,7 @@ export class MosaicRestrictionTransactionService {
7373
return MosaicGlobalRestrictionTransaction.create(
7474
deadline,
7575
mosaicId,
76-
UInt64.fromNumericString(restrictionKey),
76+
restrictionKey,
7777
currentValue,
7878
currentType,
7979
UInt64.fromNumericString(restrictionValue),
@@ -101,25 +101,25 @@ export class MosaicRestrictionTransactionService {
101101
public createMosaicAddressRestrictionTransaction(deadline: Deadline,
102102
networkType: NetworkType,
103103
mosaicId: MosaicId,
104-
restrictionKey: string,
104+
restrictionKey: UInt64,
105105
targetAddress: Address,
106106
restrictionValue: string,
107107
maxFee: UInt64 = new UInt64([0, 0])): Observable<Transaction> {
108-
this.validateInput(restrictionKey, restrictionValue);
108+
this.validateInput(restrictionValue);
109109
return this.getGlobalRestrictionEntry(mosaicId, restrictionKey).pipe(
110110
switchMap((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => {
111111
if (!restrictionEntry) {
112112
throw Error('Global restriction is not valid for RetrictionKey: ' + restrictionKey);
113113
}
114114
return this.restrictionHttp.getMosaicAddressRestriction(mosaicId, targetAddress).pipe(
115115
map((addressRestriction: MosaicAddressRestriction) => {
116-
const addressEntry = addressRestriction.restrictions.get(restrictionKey);
116+
const addressEntry = addressRestriction.restrictions.get(restrictionKey.toHex());
117117
const currentValue = addressEntry ? UInt64.fromNumericString(addressEntry) :
118118
this.defaultMosaicAddressRestrictionVaule;
119119
return MosaicAddressRestrictionTransaction.create(
120120
deadline,
121121
mosaicId,
122-
UInt64.fromNumericString(restrictionKey),
122+
restrictionKey,
123123
targetAddress,
124124
UInt64.fromNumericString(restrictionValue),
125125
networkType,
@@ -133,7 +133,7 @@ export class MosaicRestrictionTransactionService {
133133
return of(MosaicAddressRestrictionTransaction.create(
134134
deadline,
135135
mosaicId,
136-
UInt64.fromNumericString(restrictionKey),
136+
restrictionKey,
137137
targetAddress,
138138
UInt64.fromNumericString(restrictionValue),
139139
networkType,
@@ -153,29 +153,25 @@ export class MosaicRestrictionTransactionService {
153153
* @param restrictionKey - Mosaic global restriction key
154154
* @return {Observable<MosaicGlobalRestrictionItem | undefined>}
155155
*/
156-
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: string): Observable<MosaicGlobalRestrictionItem | undefined> {
156+
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable<MosaicGlobalRestrictionItem | undefined> {
157157
return this.restrictionHttp.getMosaicGlobalRestriction(mosaicId).pipe(
158158
map((mosaicRestriction: MosaicGlobalRestriction) => {
159-
return mosaicRestriction.restrictions.get(restrictionKey);
159+
return mosaicRestriction.restrictions.get(restrictionKey.toHex());
160160
}),
161-
catchError((err) => {
162-
if (err.response.statusCode && err.response.statusCode === 404) {
161+
catchError((err: Error) => {
162+
const error = JSON.parse(err.message);
163+
if (error && error.statusCode && error.statusCode === 404) {
163164
return of(undefined);
164165
}
165-
throw Error(err);
166+
throw Error(err.message);
166167
}));
167168
}
168169

169170
/**
170171
* Check if input restriction key and value are invalid or not
171-
* @param key - Restriction key
172172
* @param value - Restriction value
173173
*/
174-
private validateInput(key: string, value: string) {
175-
if (!UInt64.isLongNumericString(key)) {
176-
throw Error(`RestrictionKey: ${key} is not a valid numeric string.`);
177-
}
178-
174+
private validateInput(value: string) {
179175
if (!UInt64.isLongNumericString(value)) {
180176
throw Error(`RestrictionValue: ${value} is not a valid numeric string.`);
181177
}

test/service/MosaicRestrictionTransactionservice.spec.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import {expect} from 'chai';
1818
import {of as observableOf} from 'rxjs';
1919
import {deepEqual, instance, mock, when} from 'ts-mockito';
20+
import { KeyGenerator } from '../../src/core/format/KeyGenerator';
2021
import { RestrictionHttp } from '../../src/infrastructure/RestrictionHttp';
2122
import { Account } from '../../src/model/account/Account';
2223
import {NetworkType} from '../../src/model/blockchain/NetworkType';
@@ -39,8 +40,8 @@ describe('MosaicRestrictionTransactionService', () => {
3940
let mosaicId: MosaicId;
4041
let referenceMosaicId: MosaicId;
4142
let mosaicRestrictionTransactionService: MosaicRestrictionTransactionService;
42-
const key = '123456';
43-
const invalidKey = '9999';
43+
const key = KeyGenerator.generateUInt64Key('TestKey');
44+
const invalidKey = KeyGenerator.generateUInt64Key('9999');
4445
let mosaicIdWrongKey: MosaicId;
4546
const globalRestrictionValue = '1000';
4647
const globalRestrictionType = MosaicRestrictionType.LE;
@@ -76,7 +77,7 @@ describe('MosaicRestrictionTransactionService', () => {
7677
MosaicRestrictionType.LE)
7778
.subscribe((transaction: MosaicGlobalRestrictionTransaction) => {
7879
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION);
79-
expect(transaction.restrictionKey.toString()).to.be.equal(key);
80+
expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex());
8081
expect(transaction.previousRestrictionType).to.be.equal(globalRestrictionType);
8182
expect(transaction.previousRestrictionValue.toString()).to.be.equal(globalRestrictionValue);
8283
expect(transaction.referenceMosaicId.toHex()).to.be.equal(new MosaicId(UInt64.fromUint(0).toDTO()).toHex());
@@ -95,7 +96,7 @@ describe('MosaicRestrictionTransactionService', () => {
9596
referenceMosaicId)
9697
.subscribe((transaction: MosaicGlobalRestrictionTransaction) => {
9798
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION);
98-
expect(transaction.restrictionKey.toString()).to.be.equal(key);
99+
expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex());
99100
expect(transaction.previousRestrictionType).to.be.equal(globalRestrictionType);
100101
expect(transaction.previousRestrictionValue.toString()).to.be.equal(globalRestrictionValue);
101102
expect(transaction.referenceMosaicId.toHex()).to.be.equal(referenceMosaicId.toHex());
@@ -113,7 +114,7 @@ describe('MosaicRestrictionTransactionService', () => {
113114
'2000')
114115
.subscribe((transaction: MosaicAddressRestrictionTransaction) => {
115116
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_ADDRESS_RESTRICTION);
116-
expect(transaction.restrictionKey.toString()).to.be.equal(key);
117+
expect(transaction.restrictionKey.toHex()).to.be.equal(key.toHex());
117118
expect(transaction.targetAddress.plain()).to.be.equal(account.address.plain());
118119
expect(transaction.previousRestrictionValue.toString()).to.be.equal(addressRestrictionValue);
119120
done();
@@ -131,16 +132,6 @@ describe('MosaicRestrictionTransactionService', () => {
131132
MosaicRestrictionType.LE);
132133
}).to.throw(Error, 'RestrictionValue: wrong value is not a valid numeric string.');
133134

134-
expect(() => {
135-
mosaicRestrictionTransactionService.createMosaicGlobalRestrictionTransaction(
136-
Deadline.create(),
137-
NetworkType.MIJIN_TEST,
138-
mosaicId,
139-
'wrong key',
140-
'2000',
141-
MosaicRestrictionType.LE);
142-
}).to.throw(Error, 'RestrictionKey: wrong key is not a valid numeric string.');
143-
144135
expect(() => {
145136
mosaicRestrictionTransactionService.createMosaicAddressRestrictionTransaction(
146137
Deadline.create(),
@@ -170,7 +161,7 @@ describe('MosaicRestrictionTransactionService', () => {
170161
MosaicRestrictionEntryType.GLOBAL,
171162
mosaicId,
172163
new Map<string, MosaicGlobalRestrictionItem>()
173-
.set(key,
164+
.set(key.toHex(),
174165
new MosaicGlobalRestrictionItem(
175166
referenceMosaicId,
176167
globalRestrictionValue,
@@ -187,7 +178,7 @@ describe('MosaicRestrictionTransactionService', () => {
187178
mosaicId,
188179
account.address,
189180
new Map<string, string>()
190-
.set(key,
181+
.set(key.toHex(),
191182
addressRestrictionValue,
192183
),
193184
);

0 commit comments

Comments
 (0)