Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Fix hexToNumber and hexToNumberString prefix validation #564

Merged
merged 1 commit into from
Jan 3, 2022
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
9 changes: 9 additions & 0 deletions packages/caver-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
}

Expand Down
22 changes: 12 additions & 10 deletions test/packages/caver.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand All @@ -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)
})
})
Expand All @@ -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 },
Expand All @@ -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)
})
})
})
Expand Down