Skip to content

Commit

Permalink
hardening.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Jul 27, 2022
1 parent 77c7282 commit d032d64
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/core/src/dataTypes/BigNumber/BigNumberParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('BigNumberParser', () => {

it('throws an error when NaN passed', () => {
expect(() => parseInt(NaN)).toThrowError('The number NaN cannot be converted to a BigInt');
expect(() => parseInt(null as any)).toThrowError('Value is null');
expect(() => parseInt(undefined as any)).toThrowError('Value is undefined');
expect(() => parseInt('')).toThrowError('Value is empty');
});
});

Expand Down Expand Up @@ -59,20 +62,26 @@ describe('BigNumberParser', () => {
});

it('throws an error when invalid string passed', () => {
function test(input: string, expectedError: string = 'Cannot convert') {
const cannotConvertExpectedError = 'Cannot convert';
const emptyFragmentsExpectedError = 'Value has empty fragments';

function test(input: string, expectedError: string) {
expect(() => parseDecimal(input, 0)).toThrowError(expectedError);
}

test('lorem ipsum');
test('-x');
test('x');
test('##');
test('$$');
test('!');
test('lorem ipsum', cannotConvertExpectedError);
test('-x', cannotConvertExpectedError);
test('x', cannotConvertExpectedError);
test('##', cannotConvertExpectedError);
test('$$', cannotConvertExpectedError);
test('!', cannotConvertExpectedError);
test('.', emptyFragmentsExpectedError);
test('', emptyFragmentsExpectedError);
test('-', emptyFragmentsExpectedError);
test('', emptyFragmentsExpectedError);
test('..', 'Value has more than one dot');
test('.', 'Value has empty fragments');
test('', 'Value has empty fragments');
test('-', 'Value has empty fragments');
test(null as any, 'Value is null');
test(undefined as any, 'Value is undefined');
});

it('throws an error when fractional part is wrong', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/dataTypes/BigNumber/BigNumberParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export type BigNumberPrimitive = number | string | bigint;

export class BigNumberParser {
public static parseInt(value: BigNumberPrimitive): bigint {
assertNotEmpty(value);

if (typeof value === 'string') {
if (value.length === 0) {
throw createError('Value is empty');
}

const isNegativeHex = value.startsWith('-0x');
if (isNegativeHex) {
value = value.substring(1);
Expand All @@ -15,10 +21,13 @@ export class BigNumberParser {
}
return result;
}

return BigInt(value);
}

public static parseDecimal(value: BigNumberPrimitive, decimals: number): bigint {
assertNotEmpty(value);

const multiplier = getMultiplier(decimals);

if (typeof value === 'number') {
Expand Down Expand Up @@ -65,6 +74,15 @@ export class BigNumberParser {
}
}

function assertNotEmpty(value: BigNumberPrimitive) {
if (value === null) {
throw createError('Value is null');
}
if (value === undefined) {
throw createError('Value is undefined');
}
}

function getMultiplier(decimals: number): bigint {
if (decimals < 0) {
throw createError('Invalid decimals');
Expand Down

0 comments on commit d032d64

Please sign in to comment.