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: validation and handling of unsafe integers #3491

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/orange-moles-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": patch
---

fix: validation and handling of unsafe integers
49 changes: 49 additions & 0 deletions packages/abi-coder/src/encoding/coders/BigNumberCoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@ describe('BigNumberCoder', () => {
expect(actual).toStrictEqual(expected);
});

it('should encode a u64 [max safe integer]', () => {
const coder = new BigNumberCoder('u64');
const value: number = Number.MAX_SAFE_INTEGER;
const expected = new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]);

const data = coder.encode(value);

expect(data).toEqual(expected);
});

it('should throw an error when encoding [number more than max safe integer]', () => {
const coder = new BigNumberCoder('u64');
const value: number = Number.MAX_SAFE_INTEGER + 1;

expect(() => coder.encode(value)).toThrow(
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - number value is too large.')
);
});

it('should encode a u64 [very big number - as string]', () => {
const coder = new BigNumberCoder('u64');
const value: string = '76472027892439376';
const expected = new Uint8Array([1, 15, 174, 231, 121, 200, 89, 80]);

const data = coder.encode(value);

expect(data).toEqual(expected);
});

it('should throw an error when encoding [number more than max safe integer]', () => {
const coder = new BigNumberCoder('u64');
const value: number = 76472027892439376;

expect(() => coder.encode(value)).toThrow(
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - number value is too large.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - number value is too large.')
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - too large. Number can only safely handle up to 53 bits').

JW if it needs more detail

);
});

it('should decode a u64 number', () => {
const coder = new BigNumberCoder('u64');
const expectedValue = 0;
Expand All @@ -34,6 +72,17 @@ describe('BigNumberCoder', () => {
expect(actualLength).toBe(expectedLength);
});

it('should decode a u64 [very big number]', () => {
const coder = new BigNumberCoder('u64');
const data = new Uint8Array([1, 15, 174, 231, 121, 200, 89, 80]);
const expectedValue = bn('76472027892439376');

const [actualValue, actualLength] = coder.decode(data, 0);

expect(actualValue).toEqualBn(expectedValue);
expect(actualLength).toEqual(8);
});

it('should encode u8 max number', () => {
const coder = new BigNumberCoder('u64');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]);
Expand Down
10 changes: 10 additions & 0 deletions packages/abi-coder/src/encoding/coders/BigNumberCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ export class BigNumberCoder extends Coder<BNInput, BN> {
encode(value: BNInput): Uint8Array {
let bytes;

// We throw an error if the value is a number and it's more than the max safe integer
// This is because we can experience some odd behavior with integers more than the max safe integer
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER#description
if (typeof value === 'number' && value > Number.MAX_SAFE_INTEGER) {
throw new FuelError(
ErrorCode.ENCODE_ERROR,
`Invalid ${this.type} type - number value is too large.`
);
}

try {
bytes = toBytes(value, this.encodedLength);
} catch (error) {
Expand Down
Loading