Skip to content

Commit 91f8068

Browse files
committed
Added MosaicRestrictionTransaction Service
1 parent 66c71d0 commit 91f8068

File tree

5 files changed

+690
-3
lines changed

5 files changed

+690
-3
lines changed
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
import { assert, expect } from 'chai';
2+
import { Listener } from '../../src/infrastructure/Listener';
3+
import { RestrictionHttp } from '../../src/infrastructure/RestrictionHttp';
4+
import { TransactionHttp } from '../../src/infrastructure/TransactionHttp';
5+
import { Account } from '../../src/model/account/Account';
6+
import { NetworkType } from '../../src/model/blockchain/NetworkType';
7+
import { MosaicFlags } from '../../src/model/mosaic/MosaicFlags';
8+
import { MosaicId } from '../../src/model/mosaic/MosaicId';
9+
import { MosaicNonce } from '../../src/model/mosaic/MosaicNonce';
10+
import { MosaicRestrictionType } from '../../src/model/restriction/MosaicRestrictionType';
11+
import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction';
12+
import { Deadline } from '../../src/model/transaction/Deadline';
13+
import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction';
14+
import { MosaicDefinitionTransaction } from '../../src/model/transaction/MosaicDefinitionTransaction';
15+
import { MosaicGlobalRestrictionTransaction } from '../../src/model/transaction/MosaicGlobalRestrictionTransaction';
16+
import { TransactionType } from '../../src/model/transaction/TransactionType';
17+
import { UInt64 } from '../../src/model/UInt64';
18+
import { MosaicRestrictionTransactionService } from '../../src/service/MosaicRestrictionTransactionService';
19+
20+
describe('MosaicRestrictionTransactionService', () => {
21+
const deadline = Deadline.create();
22+
const key = '9876543';
23+
let targetAccount: Account;
24+
let account: Account;
25+
let restrictionHttp: RestrictionHttp;
26+
let transactionHttp: TransactionHttp;
27+
let mosaicId: MosaicId;
28+
let config;
29+
let generationHash: string;
30+
31+
before((done) => {
32+
const path = require('path');
33+
require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
34+
if (err) {
35+
throw err;
36+
}
37+
const json = JSON.parse(data);
38+
config = json;
39+
account = Account.createFromPrivateKey(json.testAccount.privateKey, NetworkType.MIJIN_TEST);
40+
targetAccount = Account.createFromPrivateKey(json.testAccount3.privateKey, NetworkType.MIJIN_TEST);
41+
generationHash = json.generationHash;
42+
restrictionHttp = new RestrictionHttp(json.apiUrl);
43+
transactionHttp = new TransactionHttp(json.apiUrl);
44+
done();
45+
});
46+
});
47+
48+
/**
49+
* =========================
50+
* Setup test data
51+
* =========================
52+
*/
53+
54+
describe('MosaicDefinitionTransaction', () => {
55+
let listener: Listener;
56+
before (() => {
57+
listener = new Listener(config.apiUrl);
58+
return listener.open();
59+
});
60+
after(() => {
61+
return listener.close();
62+
});
63+
it('standalone', (done) => {
64+
const nonce = MosaicNonce.createRandom();
65+
mosaicId = MosaicId.createFromNonce(nonce, account.publicAccount);
66+
const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
67+
Deadline.create(),
68+
nonce,
69+
mosaicId,
70+
MosaicFlags.create( true, true, true),
71+
3,
72+
UInt64.fromUint(1000),
73+
NetworkType.MIJIN_TEST,
74+
);
75+
const signedTransaction = mosaicDefinitionTransaction.signWith(account, generationHash);
76+
listener.confirmed(account.address).subscribe(() => {
77+
done();
78+
});
79+
listener.status(account.address).subscribe((error) => {
80+
console.log('Error:', error);
81+
done();
82+
});
83+
transactionHttp.announce(signedTransaction);
84+
});
85+
});
86+
87+
describe('MosaicGlobalRestrictionTransaction - with referenceMosaicId', () => {
88+
let listener: Listener;
89+
before (() => {
90+
listener = new Listener(config.apiUrl);
91+
return listener.open();
92+
});
93+
after(() => {
94+
return listener.close();
95+
});
96+
97+
it('standalone', (done) => {
98+
const mosaicGlobalRestrictionTransaction = MosaicGlobalRestrictionTransaction.create(
99+
Deadline.create(),
100+
mosaicId,
101+
UInt64.fromNumericString(key),
102+
UInt64.fromUint(0),
103+
MosaicRestrictionType.NONE,
104+
UInt64.fromUint(0),
105+
MosaicRestrictionType.GE,
106+
NetworkType.MIJIN_TEST,
107+
);
108+
const signedTransaction = mosaicGlobalRestrictionTransaction.signWith(account, generationHash);
109+
110+
listener.confirmed(account.address).subscribe(() => {
111+
done();
112+
});
113+
listener.status(account.address).subscribe((error) => {
114+
console.log('Error:', error);
115+
assert(false);
116+
done();
117+
});
118+
transactionHttp.announce(signedTransaction);
119+
});
120+
});
121+
122+
describe('MosaicAddressRestrictionTransaction', () => {
123+
let listener: Listener;
124+
before (() => {
125+
listener = new Listener(config.apiUrl);
126+
return listener.open();
127+
});
128+
after(() => {
129+
return listener.close();
130+
});
131+
it('aggregate', (done) => {
132+
const mosaicAddressRestrictionTransaction = MosaicAddressRestrictionTransaction.create(
133+
Deadline.create(),
134+
mosaicId,
135+
UInt64.fromNumericString(key),
136+
targetAccount.address,
137+
UInt64.fromUint(2),
138+
NetworkType.MIJIN_TEST,
139+
);
140+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
141+
[mosaicAddressRestrictionTransaction.toAggregate(account.publicAccount)],
142+
NetworkType.MIJIN_TEST,
143+
[],
144+
);
145+
const signedTransaction = aggregateTransaction.signWith(account, generationHash);
146+
listener.confirmed(account.address).subscribe(() => {
147+
done();
148+
});
149+
listener.status(account.address).subscribe((error) => {
150+
console.log('Error:', error);
151+
assert(false);
152+
done();
153+
});
154+
transactionHttp.announce(signedTransaction);
155+
});
156+
});
157+
158+
/**
159+
* =========================
160+
* Test
161+
* =========================
162+
*/
163+
describe('Test new services', () => {
164+
it('should create MosaicGlobalRestrictionTransaction', (done) => {
165+
const service = new MosaicRestrictionTransactionService(restrictionHttp);
166+
167+
return service.createMosaicGlobalRestrictionTransaction(
168+
deadline,
169+
NetworkType.MIJIN_TEST,
170+
mosaicId,
171+
key,
172+
'1',
173+
MosaicRestrictionType.GE,
174+
).subscribe((transaction: MosaicGlobalRestrictionTransaction) => {
175+
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_GLOBAL_RESTRICTION);
176+
expect(transaction.previousRestrictionValue.toString()).to.be.equal('0');
177+
expect(transaction.previousRestrictionType).to.be.equal(MosaicRestrictionType.GE);
178+
expect(transaction.newRestrictionValue.toString()).to.be.equal('1');
179+
expect(transaction.newRestrictionType).to.be.equal(MosaicRestrictionType.GE);
180+
expect(transaction.restrictionKey.toString()).to.be.equal(key);
181+
done();
182+
});
183+
});
184+
it('should create MosaicAddressRestrictionTransaction', (done) => {
185+
const service = new MosaicRestrictionTransactionService(restrictionHttp);
186+
187+
return service.createMosaicAddressRestrictionTransaction(
188+
deadline,
189+
NetworkType.MIJIN_TEST,
190+
mosaicId,
191+
key,
192+
targetAccount.address,
193+
'3',
194+
).subscribe((transaction: MosaicAddressRestrictionTransaction) => {
195+
expect(transaction.type).to.be.equal(TransactionType.MOSAIC_ADDRESS_RESTRICTION);
196+
expect(transaction.previousRestrictionValue.toString()).to.be.equal('2');
197+
expect(transaction.newRestrictionValue.toString()).to.be.equal('3');
198+
expect(transaction.targetAddress.plain()).to.be.equal(targetAccount.address.plain());
199+
expect(transaction.restrictionKey.toString()).to.be.equal(key);
200+
done();
201+
});
202+
});
203+
});
204+
205+
describe('Announce MosaicGlobalRestriction through service', () => {
206+
let listener: Listener;
207+
before (() => {
208+
listener = new Listener(config.apiUrl);
209+
return listener.open();
210+
});
211+
after(() => {
212+
return listener.close();
213+
});
214+
it('should create MosaicGlobalRestriction and announce', (done) => {
215+
const service = new MosaicRestrictionTransactionService(restrictionHttp);
216+
217+
return service.createMosaicGlobalRestrictionTransaction(
218+
deadline,
219+
NetworkType.MIJIN_TEST,
220+
mosaicId,
221+
key,
222+
'1',
223+
MosaicRestrictionType.GE,
224+
).subscribe((transaction: MosaicGlobalRestrictionTransaction) => {
225+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
226+
[transaction.toAggregate(account.publicAccount)],
227+
NetworkType.MIJIN_TEST,
228+
[],
229+
);
230+
const signedTransaction = aggregateTransaction.signWith(account, generationHash);
231+
listener.confirmed(account.address).subscribe(() => {
232+
done();
233+
});
234+
listener.status(account.address).subscribe((error) => {
235+
console.log('Error:', error);
236+
assert(false);
237+
done();
238+
});
239+
transactionHttp.announce(signedTransaction);
240+
});
241+
});
242+
});
243+
244+
describe('Announce MosaicAddressRestriction through service', () => {
245+
let listener: Listener;
246+
before (() => {
247+
listener = new Listener(config.apiUrl);
248+
return listener.open();
249+
});
250+
after(() => {
251+
return listener.close();
252+
});
253+
it('should create MosaicAddressRestriction and announce', (done) => {
254+
const service = new MosaicRestrictionTransactionService(restrictionHttp);
255+
256+
return service.createMosaicAddressRestrictionTransaction(
257+
deadline,
258+
NetworkType.MIJIN_TEST,
259+
mosaicId,
260+
key,
261+
targetAccount.address,
262+
'3',
263+
).subscribe((transaction: MosaicAddressRestrictionTransaction) => {
264+
const aggregateTransaction = AggregateTransaction.createComplete(Deadline.create(),
265+
[transaction.toAggregate(account.publicAccount)],
266+
NetworkType.MIJIN_TEST,
267+
[],
268+
);
269+
const signedTransaction = aggregateTransaction.signWith(account, generationHash);
270+
listener.confirmed(account.address).subscribe(() => {
271+
done();
272+
});
273+
listener.status(account.address).subscribe((error) => {
274+
console.log('Error:', error);
275+
assert(false);
276+
done();
277+
});
278+
transactionHttp.announce(signedTransaction);
279+
});
280+
});
281+
});
282+
});

src/model/UInt64.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,25 @@ export class UInt64 {
6060
* @returns {module:coders/uint64~uint64} The uint64 representation of the input.
6161
*/
6262
public static fromNumericString(input: string): UInt64 {
63-
const input_long = Long.fromString(input, true);
64-
if (! /^\d+$/.test(input) || (input.substr(0, 1) === '0' && input.length > 1) || !Long.isLong(input_long)) {
63+
if (!UInt64.isLongNumericString(input)) {
6564
throw new Error('Input string is not a valid numeric string');
6665
}
67-
66+
const input_long = Long.fromString(input, true);
6867
return new UInt64([input_long.getLowBitsUnsigned(), input_long.getHighBitsUnsigned()]);
6968
}
69+
70+
/**
71+
* Check if input string is a numeric string or not
72+
* @param {string} input A string.
73+
* @returns {boolean}
74+
*/
75+
public static isLongNumericString(input: string): boolean {
76+
const input_long = Long.fromString(input, true);
77+
if (! /^\d+$/.test(input) || (input.substr(0, 1) === '0' && input.length > 1) || !Long.isLong(input_long)) {
78+
return false;
79+
}
80+
return true;
81+
}
7082
/**
7183
* Constructor
7284
* @param uintArray

0 commit comments

Comments
 (0)