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

feat: add support for Bytes and RawSlice as inputs and outputs for Contracts #1221

Merged
merged 30 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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/twenty-eagles-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": minor
---

Add support for Bytes and RawSlice
7 changes: 7 additions & 0 deletions packages/abi-coder/src/abi-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { ArrayCoder } from './coders/array';
import { B256Coder } from './coders/b256';
import { B512Coder } from './coders/b512';
import { BooleanCoder } from './coders/boolean';
import { ByteCoder } from './coders/byte';
import { EnumCoder } from './coders/enum';
import { NumberCoder } from './coders/number';
import { OptionCoder } from './coders/option';
import { RawSliceCoder } from './coders/raw-slice';
import { StringCoder } from './coders/string';
import { StructCoder } from './coders/struct';
import { TupleCoder } from './coders/tuple';
Expand All @@ -23,6 +25,7 @@ import {
tupleRegEx,
OPTION_CODER_TYPE,
VEC_CODER_TYPE,
BYTE_CODER_TYPE,
} from './constants';
import type { JsonAbi, JsonAbiArgument } from './json-abi';
import { ResolvedAbiType } from './resolved-abi-type';
Expand Down Expand Up @@ -58,12 +61,16 @@ export abstract class AbiCoder {
case 'u64':
case 'raw untyped ptr':
return new U64Coder();
case 'raw untyped slice':
return new RawSliceCoder();
case 'bool':
return new BooleanCoder();
case 'b256':
return new B256Coder();
case 'struct B512':
return new B512Coder();
case BYTE_CODER_TYPE:
return new ByteCoder();
default:
break;
}
Expand Down
47 changes: 47 additions & 0 deletions packages/abi-coder/src/coders/byte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Uint8ArrayWithDynamicData } from '../utilities';

import { ByteCoder } from './byte';

describe('ByteCoder', () => {
it('should encode a byte', () => {
const coder = new ByteCoder();
const expected: Uint8ArrayWithDynamicData = new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 3,
]);
expected.dynamicData = {
0: new Uint8Array([1, 2, 3, 0, 0, 0, 0, 0]),
};

const actual = coder.encode([1, 2, 3]);

expect(actual).toStrictEqual(expected);
});

it('should encode a byte [full word]', () => {
const coder = new ByteCoder();
const expected: Uint8ArrayWithDynamicData = new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 8,
]);
expected.dynamicData = {
0: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]),
};

const actual = coder.encode([1, 2, 3, 4, 5, 6, 7, 8]);

expect(actual).toStrictEqual(expected);
});

it('should decode a byte', () => {
const coder = new ByteCoder();
const input = new Uint8Array([
0, 0, 0, 0, 3, 255, 255, 225, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 10, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9,
]);
const expected = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

const [actual, newOffset] = coder.decode(input, 0);

expect(actual).toEqual(expected);
expect(newOffset).toEqual(24);
});
});
62 changes: 62 additions & 0 deletions packages/abi-coder/src/coders/byte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { arrayify, concat, type BytesLike } from '@ethersproject/bytes';
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
import { bn } from '@fuel-ts/math';

import { WORD_SIZE } from '../constants';
import type { Uint8ArrayWithDynamicData } from '../utilities';
import { BASE_VECTOR_OFFSET, concatWithDynamicData } from '../utilities';

import { Coder } from './abstract-coder';
import { U64Coder } from './u64';

export class ByteCoder extends Coder<number[], Uint8Array> {
static memorySize = 1;
constructor() {
super('struct', 'struct Bytes', BASE_VECTOR_OFFSET);
}

encode(value: number[]): Uint8Array {
if (!Array.isArray(value)) {
this.throwError('expected array value', value);
}

const parts: Uint8Array[] = [];

// pointer (ptr)
const pointer: Uint8ArrayWithDynamicData = new U64Coder().encode(BASE_VECTOR_OFFSET);

// pointer dynamicData, encode the byte vector now and attach to its pointer
const data = this.#getPaddedData(value);
pointer.dynamicData = {
0: concatWithDynamicData([data]),
};

parts.push(pointer);

// capacity (cap)
parts.push(new U64Coder().encode(data.byteLength));

// length (len)
parts.push(new U64Coder().encode(value.length));

return concatWithDynamicData(parts);
}

#getPaddedData(value: number[]): Uint8Array {
const data: Uint8Array[] = [arrayify(value)];

const paddingLength = (WORD_SIZE - (value.length % WORD_SIZE)) % WORD_SIZE;
if (paddingLength) {
data.push(new Uint8Array(paddingLength));
}

return concat(data);
}

decode(data: Uint8Array, offset: number): [Uint8Array, number] {
const len = data.slice(16, 24);
const length = bn(new U64Coder().decode(len, 0)[0]).toNumber();
const byteData = data.slice(BASE_VECTOR_OFFSET, BASE_VECTOR_OFFSET + length * 8);

return [byteData, offset + BASE_VECTOR_OFFSET];
}
}
35 changes: 35 additions & 0 deletions packages/abi-coder/src/coders/raw-slice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { BN } from '@fuel-ts/math';

import type { Uint8ArrayWithDynamicData } from '../utilities';

import { RawSliceCoder } from './raw-slice';

describe('RawSliceCoder', () => {
it('should encode a raw-slice', () => {
const coder = new RawSliceCoder();
const expected: Uint8ArrayWithDynamicData = new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 24,
]);
expected.dynamicData = {
0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3]),
};

const actual = coder.encode([1, 2, 3]);

expect(actual).toStrictEqual(expected);
});

it('should decode a raw-slice', () => {
const coder = new RawSliceCoder();
const input = new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0,
0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 9,
]);

const [actual, newOffset] = coder.decode(input, 0);

expect(actual.map((v: BN) => v.toNumber())).toStrictEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(newOffset).toEqual(80);
});
});
46 changes: 46 additions & 0 deletions packages/abi-coder/src/coders/raw-slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { BN } from '@fuel-ts/math';

import { WORD_SIZE } from '../constants';
import type { Uint8ArrayWithDynamicData } from '../utilities';
import { BASE_RAW_SLICE_OFFSET, concatWithDynamicData } from '../utilities';

import { Coder } from './abstract-coder';
import { ArrayCoder } from './array';
import { U64Coder } from './u64';

export class RawSliceCoder extends Coder<number[], BN[]> {
constructor() {
super('raw untyped slice', 'raw untyped slice', BASE_RAW_SLICE_OFFSET);
}

encode(value: number[]): Uint8Array {
if (!Array.isArray(value)) {
this.throwError('expected array value', value);
}

const parts: Uint8Array[] = [];
const coder = new U64Coder();

// pointer (ptr)
const pointer: Uint8ArrayWithDynamicData = new U64Coder().encode(BASE_RAW_SLICE_OFFSET);

// pointer dynamicData, encode the vector now and attach to its pointer
pointer.dynamicData = {
0: concatWithDynamicData(value.map((v) => coder.encode(v))),
};

parts.push(pointer);

// length (len)
parts.push(new U64Coder().encode(value.length * WORD_SIZE));

return concatWithDynamicData(parts);
}

decode(data: Uint8Array, offset: number): [BN[], number] {
const internalCoder = new ArrayCoder(new U64Coder(), data.length / 8);
const decoded = internalCoder.decode(data, offset);

return decoded;
}
}
1 change: 1 addition & 0 deletions packages/abi-coder/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const OPTION_CODER_TYPE = 'enum Option';
export const VEC_CODER_TYPE = 'struct Vec';
export const BYTE_CODER_TYPE = 'struct Bytes';
export const stringRegEx = /str\[(?<length>[0-9]+)\]/;
export const arrayRegEx = /\[(?<item>[\w\s\\[\]]+);\s*(?<length>[0-9]+)\]/;
export const structRegEx = /^struct (?<name>\w+)$/;
Expand Down
4 changes: 4 additions & 0 deletions packages/abi-coder/src/function-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { versions } from '@fuel-ts/versions';
import { AbiCoder } from './abi-coder';
import type { DecodedValue, InputValue } from './coders/abstract-coder';
import type { ArrayCoder } from './coders/array';
import { ByteCoder } from './coders/byte';
import { TupleCoder } from './coders/tuple';
import type { U64Coder } from './coders/u64';
import { VecCoder } from './coders/vec';
Expand Down Expand Up @@ -90,6 +91,9 @@ export class FunctionFragment<
if (heapCoder instanceof VecCoder) {
return heapCoder.coder.encodedLength;
}
if (heapCoder instanceof ByteCoder) {
return ByteCoder.memorySize;
}

return heapCoder.encodedLength;
} catch (e) {
Expand Down
7 changes: 5 additions & 2 deletions packages/abi-coder/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BytesLike } from '@ethersproject/bytes';
import { concat, arrayify } from '@ethersproject/bytes';

import { U64Coder } from './coders/u64';
import { VEC_CODER_TYPE, WORD_SIZE } from './constants';
import { BYTE_CODER_TYPE, VEC_CODER_TYPE, WORD_SIZE } from './constants';

export type DynamicData = {
[pointerIndex: number]: Uint8ArrayWithDynamicData;
Expand All @@ -15,6 +15,9 @@ export type Uint8ArrayWithDynamicData = Uint8Array & {
const VEC_PROPERTY_SPACE = 3; // ptr + cap + length
export const BASE_VECTOR_OFFSET = VEC_PROPERTY_SPACE * WORD_SIZE;

const RAW_SLICE_PROPERTY_SPACE = 2; // ptr + length
export const BASE_RAW_SLICE_OFFSET = RAW_SLICE_PROPERTY_SPACE * WORD_SIZE;

// this is a fork of @ethersproject/bytes:concat
// this collects individual dynamicData data and relocates it to top level
export function concatWithDynamicData(items: ReadonlyArray<BytesLike>): Uint8ArrayWithDynamicData {
Expand Down Expand Up @@ -139,7 +142,7 @@ export const isPointerType = (type: string) => {
}
};

export const isHeapType = (type: string) => type === VEC_CODER_TYPE;
export const isHeapType = (type: string) => type === VEC_CODER_TYPE || type === BYTE_CODER_TYPE;

export function findOrThrow<T>(
arr: readonly T[],
Expand Down
Loading