From be885b2757835f90a246fe8c87d3dfdea694958b Mon Sep 17 00:00:00 2001 From: securekim <35220663+securekim@users.noreply.github.com> Date: Thu, 23 Dec 2021 14:52:52 +0900 Subject: [PATCH] Fix hexToNumber and hexToNumberString prefix validation * Bugfix * Allow compatibility * Modify tests --- packages/caver-utils/src/utils.js | 9 +++++++++ test/packages/caver.utils.js | 22 ++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/caver-utils/src/utils.js b/packages/caver-utils/src/utils.js index 4bdbc6a5..1eea92f2 100644 --- a/packages/caver-utils/src/utils.js +++ b/packages/caver-utils/src/utils.js @@ -338,6 +338,11 @@ const hexToUtf8 = function(hex) { */ const hexToNumber = function(value) { if (!value) return value + + if (typeof value === 'string' && !isHexStrict(value)) { + throw new Error(`Given value "${value}" is not a valid hex string.`) + } + return toBN(value).toNumber() } @@ -356,6 +361,10 @@ const hexToNumber = function(value) { const 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) } diff --git a/test/packages/caver.utils.js b/test/packages/caver.utils.js index ca12f334..b3f1b455 100644 --- a/test/packages/caver.utils.js +++ b/test/packages/caver.utils.js @@ -641,11 +641,10 @@ describe('caver.utils.hexToNumberString', () => { }) context('CAVERJS-UNIT-ETC-121: input: numberString', () => { - const tests = [{ value: '1234', expected: (1234).toString() }] - it('should return numberString', () => { - for (const test of tests) { - expect(caver.utils.hexToNumberString(test.value)).to.be.equal(test.expected) - } + it('should throw an error', () => { + const invalid = '1234' + const errorMessage = `Given value "${invalid}" is not a valid hex string.` + expect(() => caver.utils.hexToNumberString(invalid)).to.throw(errorMessage) }) }) @@ -665,7 +664,7 @@ describe('caver.utils.hexToNumberString', () => { context('CAVERJS-UNIT-ETC-123: input: invalid hexString', () => { it('should throw an error', () => { const invalid = 'zzzz' - const errorMessage = `Error: [number-to-bn] while converting number "${invalid}" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "${invalid}"` + const errorMessage = `Given value "${invalid}" is not a valid hex string.` expect(() => caver.utils.hexToNumberString(invalid)).to.throw(errorMessage) }) }) @@ -676,7 +675,6 @@ describe('caver.utils.hexToNumber', () => { context('CAVERJS-UNIT-ETC-124: input: valid value', () => { const tests = [ { value: 1234, expected: 1234 }, - { value: '1234', expected: 1234 }, { value: 0x1234, expected: 4660 }, { value: 0xea, expected: 234 }, { value: '0xea', expected: 234 }, @@ -690,9 +688,13 @@ describe('caver.utils.hexToNumber', () => { context('CAVERJS-UNIT-ETC-125: input: invalid value', () => { it('should throw an error', () => { - const invalid = 'zzzz' - const errorMessage = `Error: [number-to-bn] while converting number "${invalid}" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "${invalid}"` - expect(() => caver.utils.hexToNumber(invalid)).to.throw(errorMessage) + let invalid = '1234' + let errorMessage = `Given value "${invalid}" is not a valid hex string.` + expect(() => caver.utils.hexToBytes(invalid)).to.throw(errorMessage) + + invalid = 'zzzz' + errorMessage = `Given value "${invalid}" is not a valid hex string.` + expect(() => caver.utils.hexToBytes(invalid)).to.throw(errorMessage) }) }) })