Skip to content

Commit

Permalink
Merge pull request #2884 from yegorzaremba/hotfix-hexToNumberString-p…
Browse files Browse the repository at this point in the history
…refix

fix(utils): hexToNumberString prefix validation
  • Loading branch information
nivida committed Jun 11, 2019
2 parents 7a8b1fc + 9062ddd commit aaf26c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/web3-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ export const hexToNumber = (value) => {
export const hexToNumberString = (value) => {
if (!value) return value;

if (isString(value)) {
if (!isHexStrict(value)) throw new Error(`Given value "${value}" is not a valid hex string.`);
}

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

Expand Down
5 changes: 4 additions & 1 deletion packages/web3-utils/tests/src/UtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ describe('UtilsTest', () => {
// allow compatiblity
expect(hexToNumberString(100000)).toEqual('100000');

expect(hexToNumberString('100000')).toEqual('100000');
// throw error if the hex string doesn't contain '0x' prefix
expect(() => {
hexToNumberString('100000');
}).toThrow('Given value "100000" is not a valid hex string.');
});

it('calls toTwosComplement and returns the expected results', () => {
Expand Down

0 comments on commit aaf26c8

Please sign in to comment.