Skip to content

Commit d2c502d

Browse files
authored
Merge pull request #477 from NEMStudios/task/g474_padding_mosaic_nonce_hex
Added padding in mosaicNonce.createFromHex
2 parents d682aac + 3ccea26 commit d2c502d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/model/mosaic/MosaicNonce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class MosaicNonce {
4545
* @return {MosaicNonce}
4646
*/
4747
public static createFromHex(hex: string): MosaicNonce {
48-
const uint8 = convert.hexToUint8(hex);
48+
const uint8 = convert.hexToUint8(hex.padStart(8, '0'));
4949

5050
if (uint8.length !== 4) {
5151
throw new Error('Expected 4 bytes for Nonce and got ' + hex.length + ' instead.');

test/model/mosaic/MosaicNonce.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ describe('MosaicNonce', () => {
4747
deepEqual(nonce.toDTO(), 0);
4848
});
4949

50+
it('should create nonce from hexadecimal notation throw exception', () => {
51+
expect(() => {
52+
MosaicNonce.createFromHex('111100000000');
53+
}).to.throw(Error, 'Expected 4 bytes for Nonce and got ' + '111100000000'.length + ' instead.');
54+
});
55+
56+
it('should create nonce from hexadecimal notation with uint32 input - 0 value', () => {
57+
const nonce = MosaicNonce.createFromHex((0).toString(16));
58+
expect(nonce.nonce).to.not.be.null;
59+
deepEqual(nonce.nonce, new Uint8Array([0x0, 0x0, 0x0, 0x0]));
60+
deepEqual(nonce.toDTO(), 0);
61+
});
62+
63+
it('should create nonce from hexadecimal notation with uint32 input', () => {
64+
const nonce = MosaicNonce.createFromHex((11).toString(16));
65+
expect(nonce.nonce).to.not.be.null;
66+
deepEqual(nonce.nonce, new Uint8Array([0, 0, 0, 11]));
67+
deepEqual(nonce.toDTO(), 184549376);
68+
});
69+
5070
describe('toHex()', () => {
5171
it('should return string value of nonce', () => {
5272
const nonce = new MosaicNonce(new Uint8Array([0x0, 0x0, 0x0, 0x0]));

0 commit comments

Comments
 (0)