Skip to content

Commit

Permalink
Fix hexToNumber and hexToNumberString prefix validation (#3086)
Browse files Browse the repository at this point in the history
* fix hexToNumber & hexToNumberString  prefix validation

* CHANGELOG.md updated
  • Loading branch information
cgewecke authored and nivida committed Oct 15, 2019
1 parent 2e84152 commit 4178371
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ Released with 1.0.0-beta.37 code base.
- Ensure the immutability of the `tx` object passed to function `signTransaction` (#2190)
- Gas check fixed (#2381)
- Signing issues #1998, #2033, and #1074 fixed (#3125)
- Fix hexToNumber and hexToNumberString prefix validation (#3086)
8 changes: 8 additions & 0 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ var hexToNumber = function (value) {
return value;
}

if (_.isString(value) && !isHexStrict(value)) {
throw new Error('Given value "'+value+'" is not a valid hex string.');
}

return toBN(value).toNumber();
};

Expand All @@ -243,6 +247,10 @@ var hexToNumber = function (value) {
var hexToNumberString = function (value) {
if (!value) return value;

if (_.isString(value) && !isHexStrict(value)) {
throw new Error('Given value "'+value+'" is not a valid hex string.');
}

return toBN(value).toString(10);
};

Expand Down
18 changes: 13 additions & 5 deletions test/utils.toNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ var assert = require('assert');
var utils = require('../packages/web3-utils');

describe('lib/utils/utils', function () {
describe('hexToNumberString', function () {
describe('hexToNumber', function () {
it('should return the correct value', function () {

assert.equal(utils.hexToNumberString("0x3e8"), 1000);
assert.equal(utils.hexToNumberString('0x1f0fe294a36'), 2134567897654);
assert.equal(utils.hexToNumber("0x3e8"), 1000);
assert.equal(utils.hexToNumber('0x1f0fe294a36'), 2134567897654);
// allow compatiblity
assert.equal(utils.hexToNumberString(100000), 100000);
assert.equal(utils.hexToNumberString('100000'), 100000);
assert.equal(utils.hexToNumber(100000), 100000);
});

it('should validate hex strings', function() {
try {
utils.hexToNumber('100000');
assert.fail();
} catch (error){
assert(error.message.includes('is not a valid hex string'))
}
})
});
});
10 changes: 9 additions & 1 deletion test/utils.toNumberString.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ describe('lib/utils/utils', function () {
assert.equal(utils.hexToNumberString('0x1f0fe294a36'), '2134567897654');
// allow compatiblity
assert.equal(utils.hexToNumberString(100000), '100000');
assert.equal(utils.hexToNumberString('100000'), '100000');
});

it('should validate hex strings', function() {
try {
utils.hexToNumberString('100000');
assert.fail();
} catch (error){
assert(error.message.includes('is not a valid hex string'))
}
})
});
});

0 comments on commit 4178371

Please sign in to comment.