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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 @@ -244,6 +248,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'))
}
})
});
});