Skip to content

Commit bf10cd2

Browse files
authored
Merge pull request #296 from NEMStudios/task/g294_mosaic_ordering
JAV-63 Make sure Mosaic list is ordered in transfer transaction
2 parents dbb5c58 + c545796 commit bf10cd2

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

src/model/UInt64.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class UInt64 {
116116
* @return {string}
117117
*/
118118
public toString(): string {
119-
return this.compact().toString();
119+
return Long.fromBits(this.lower, this.higher, true).toString();
120120
}
121121

122122
/**

src/model/transaction/TransferTransaction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as Long from 'long';
1718
import { Convert, Convert as convert } from '../../core/format';
1819
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
1920
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
@@ -180,11 +181,11 @@ export class TransferTransaction extends Transaction {
180181
* @returns {Mosaic[]}
181182
*/
182183
public sortMosaics(): Mosaic[] {
183-
const sortedMosaics = this.mosaics.sort((a, b) => {
184-
if (Number(a.id[1]) > b.id[1]) { return 1; } else if (a.id[1] < b.id[1]) { return -1; }
185-
return 0;
184+
return this.mosaics.sort((a, b) => {
185+
const long_a = Long.fromBits(a.id.id.lower, a.id.id.higher, true);
186+
const long_b = Long.fromBits(b.id.id.lower, b.id.id.higher, true);
187+
return long_a.compare(long_b);
186188
});
187-
return sortedMosaics;
188189
}
189190

190191
/**

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616

1717
import { expect } from 'chai';
18+
import { CreateTransactionFromPayload } from '../../../src/infrastructure/transaction/CreateTransactionFromPayload';
1819
import { Account } from '../../../src/model/account/Account';
1920
import { Address } from '../../../src/model/account/Address';
2021
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
2122
import { MessageType } from '../../../src/model/message/MessageType';
2223
import { PersistentHarvestingDelegationMessage } from '../../../src/model/message/PersistentHarvestingDelegationMessage';
2324
import { PlainMessage } from '../../../src/model/message/PlainMessage';
25+
import { Mosaic } from '../../../src/model/mosaic/Mosaic';
26+
import { MosaicId } from '../../../src/model/mosaic/MosaicId';
2427
import { NetworkCurrencyMosaic } from '../../../src/model/mosaic/NetworkCurrencyMosaic';
2528
import { NamespaceId } from '../../../src/model/namespace/NamespaceId';
2629
import { Deadline } from '../../../src/model/transaction/Deadline';
@@ -299,4 +302,61 @@ describe('TransferTransaction', () => {
299302
);
300303
}).to.throw();
301304
});
305+
306+
it('should sort the Mosaic array', () => {
307+
const mosaics = [
308+
new Mosaic(new MosaicId(UInt64.fromUint(200).toDTO()), UInt64.fromUint(0)),
309+
new Mosaic(new MosaicId(UInt64.fromUint(100).toDTO()), UInt64.fromUint(0)),
310+
];
311+
312+
const transferTransaction = TransferTransaction.create(
313+
Deadline.create(),
314+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
315+
mosaics,
316+
PlainMessage.create('NEM'),
317+
NetworkType.MIJIN_TEST,
318+
);
319+
320+
expect(transferTransaction.mosaics[0].id.id.compact()).to.be.equal(200);
321+
expect(transferTransaction.mosaics[1].id.id.compact()).to.be.equal(100);
322+
323+
const signedTransaction = transferTransaction.signWith(account, generationHash);
324+
325+
expect(signedTransaction.payload.substring(
326+
304,
327+
signedTransaction.payload.length,
328+
)).to.be.equal(
329+
'64000000000000000000000000000000C8000000000000000000000000000000');
330+
331+
const sorted = CreateTransactionFromPayload(signedTransaction.payload) as TransferTransaction;
332+
expect(sorted.mosaics[0].id.id.compact()).to.be.equal(100);
333+
expect(sorted.mosaics[1].id.id.compact()).to.be.equal(200);
334+
});
335+
336+
it('should sort the Mosaic array - using Hex MosaicId', () => {
337+
const mosaics = [
338+
new Mosaic(new MosaicId('D525AD41D95FCF29'), UInt64.fromUint(5)),
339+
new Mosaic(new MosaicId('77A1969932D987D7'), UInt64.fromUint(6)),
340+
new Mosaic(new MosaicId('67F2B76F28BD36BA'), UInt64.fromUint(10)),
341+
];
342+
343+
const transferTransaction = TransferTransaction.create(
344+
Deadline.create(),
345+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
346+
mosaics,
347+
PlainMessage.create('NEM'),
348+
NetworkType.MIJIN_TEST,
349+
);
350+
351+
expect(transferTransaction.mosaics[0].id.toHex()).to.be.equal('D525AD41D95FCF29');
352+
expect(transferTransaction.mosaics[1].id.toHex()).to.be.equal('77A1969932D987D7');
353+
expect(transferTransaction.mosaics[2].id.toHex()).to.be.equal('67F2B76F28BD36BA');
354+
355+
const signedTransaction = transferTransaction.signWith(account, generationHash);
356+
const sorted = CreateTransactionFromPayload(signedTransaction.payload) as TransferTransaction;
357+
expect(sorted.mosaics[0].id.toHex()).to.be.equal('67F2B76F28BD36BA');
358+
expect(sorted.mosaics[1].id.toHex()).to.be.equal('77A1969932D987D7');
359+
expect(sorted.mosaics[2].id.toHex()).to.be.equal('D525AD41D95FCF29');
360+
361+
});
302362
});

0 commit comments

Comments
 (0)