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(utils): hexToNumberString prefix validation #2884

Merged
merged 1 commit into from
Jun 11, 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
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