Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hexToNumber and hexToNumberString prefix validation #3086

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
nivida marked this conversation as resolved.
Show resolved Hide resolved
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);
nivida marked this conversation as resolved.
Show resolved Hide resolved
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'))
}
})
});
});