From d3190a022dd7c37570ea46a67303b31a6b9ef13b Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 6 Apr 2024 01:24:18 +0530 Subject: [PATCH 01/12] feat: accept Uint8Arrays as inputs for Vecs --- .../src/encoding/coders/v0/VecCoder.test.ts | 5 +- .../src/encoding/coders/v0/VecCoder.ts | 9 ++- .../src/bytecode-sway-lib-test.test.ts | 68 +++++++++++++++++++ .../test/fixtures/forc-projects/Forc.toml | 1 + .../bytecode-sway-lib-test/.gitignore | 2 + .../bytecode-sway-lib-test/Forc.toml | 7 ++ .../bytecode-sway-lib-test/src/main.sw | 28 ++++++++ packages/fuel-gauge/test/fixtures/index.ts | 1 + 8 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw diff --git a/packages/abi-coder/src/encoding/coders/v0/VecCoder.test.ts b/packages/abi-coder/src/encoding/coders/v0/VecCoder.test.ts index 1f2c2d9c54a..24b35237644 100644 --- a/packages/abi-coder/src/encoding/coders/v0/VecCoder.test.ts +++ b/packages/abi-coder/src/encoding/coders/v0/VecCoder.test.ts @@ -35,7 +35,10 @@ describe('VecCoder', () => { const coder = new VecCoder(new BooleanCoder(options)); await expectToThrowFuelError( () => coder.encode('Nope' as never), - new FuelError(ErrorCode.ENCODE_ERROR, 'Expected array value.') + new FuelError( + ErrorCode.ENCODE_ERROR, + 'Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.' + ) ); }); diff --git a/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts b/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts index 776ebf411fb..485f4566e78 100644 --- a/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts @@ -12,6 +12,8 @@ import { BigNumberCoder } from './BigNumberCoder'; type InputValueOf = Array['Input']>; type DecodedValueOf = Array['Decoded']>; +const isUintArray = (value: unknown): value is Uint8Array => value instanceof Uint8Array; + export class VecCoder extends Coder< InputValueOf, DecodedValueOf @@ -24,8 +26,11 @@ export class VecCoder extends Coder< } encode(value: InputValueOf): Uint8Array { - if (!Array.isArray(value)) { - throw new FuelError(ErrorCode.ENCODE_ERROR, `Expected array value.`); + if (!Array.isArray(value) && !isUintArray(value)) { + throw new FuelError( + ErrorCode.ENCODE_ERROR, + `Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.` + ); } const parts: Uint8Array[] = []; diff --git a/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts new file mode 100644 index 00000000000..0e120afd919 --- /dev/null +++ b/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts @@ -0,0 +1,68 @@ +import { FUEL_NETWORK_URL, Predicate, Provider, arrayify } from 'fuels'; + +import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../test/fixtures'; +import { defaultPredicateAbi } from '../test/fixtures/abi/predicate'; +import { defaultPredicateBytecode } from '../test/fixtures/bytecode/predicate'; + +import { getSetupContract } from './utils'; + +test('compute_bytecode_root', async () => { + const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject( + FuelGaugeProjectsEnum.CALL_TEST_CONTRACT + ); + + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const contract = await setupContract(); + + const { logs } = await contract.functions + .compute_bytecode_root(arrayify(bytecodeFromFile)) + .call(); + + const bytecodeRoot: string = logs[0]; + + expect(bytecodeRoot).toBeDefined(); + expect(bytecodeRoot.length).toBe(66); +}); + +test('verify_contract_bytecode', async () => { + const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject( + FuelGaugeProjectsEnum.CALL_TEST_CONTRACT + ); + const setupTestContract = getSetupContract(FuelGaugeProjectsEnum.CALL_TEST_CONTRACT); + const testContract = await setupTestContract(); + + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const contract = await setupContract(); + + const { value } = await contract.functions + .verify_contract_bytecode( + { + value: testContract.id.toB256(), + }, + arrayify(bytecodeFromFile) + ) + .call(); + + expect(value).toBeTruthy(); +}); + +test('compute_predicate_address', async () => { + const provider = await Provider.create(FUEL_NETWORK_URL); + + const predicate = new Predicate({ + bytecode: defaultPredicateBytecode, + abi: defaultPredicateAbi, + provider, + }); + + const address = predicate.address; + + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const contract = await setupContract(); + + const { value } = await contract.functions + .compute_predicate_address(arrayify(defaultPredicateBytecode)) + .call(); + + expect(value.value).toEqual(address.toB256()); +}); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml index 20a414a5c5f..998c90f8c5e 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml @@ -6,6 +6,7 @@ members = [ "advanced-logging", "auth_testing_abi", "auth_testing_contract", + "bytecode-sway-lib-test", "bytes", "call-test-contract", "collision_in_fn_names", diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml new file mode 100644 index 00000000000..8d2b6d24879 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "bytecode-sway-lib-test" + +[dependencies] +bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } \ No newline at end of file diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw new file mode 100644 index 00000000000..3fb485a3429 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw @@ -0,0 +1,28 @@ +contract; + +use bytecode::*; + +abi MyContract { + fn compute_bytecode_root(bytecode_input: Vec); + + fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool; + + fn compute_predicate_address(bytecode: Vec) -> Address; +} + +impl MyContract for Contract { + fn compute_bytecode_root(bytecode_input: Vec) { + let mut bytecode = bytecode_input; + let root = compute_bytecode_root(bytecode); + log(root); + } + + fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool { + verify_contract_bytecode(contract_id, bytecode); + return true; + } + + fn compute_predicate_address(bytecode: Vec) -> Address { + return compute_predicate_address(bytecode); + } +} diff --git a/packages/fuel-gauge/test/fixtures/index.ts b/packages/fuel-gauge/test/fixtures/index.ts index 614ad42837e..34f4f83ae82 100644 --- a/packages/fuel-gauge/test/fixtures/index.ts +++ b/packages/fuel-gauge/test/fixtures/index.ts @@ -9,6 +9,7 @@ export enum FuelGaugeProjectsEnum { AUTH_TESTING_ABI = 'auth_testing_abi', AUTH_TESTING_CONTRACT = 'auth_testing_contract', BYTES = 'bytes', + BYTECODE_SWAY_LIB_TEST = 'bytecode-sway-lib-test', CALL_TEST_CONTRACT = 'call-test-contract', CONFIGURABLE_CONTRACT = 'configurable-contract', COMPLEX_SCRIPT = 'complex-script', From 674c39d57eb65ab0cdc7c113641dd0e93cd1454f Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 6 Apr 2024 01:44:35 +0530 Subject: [PATCH 02/12] fix forc format --- .../fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml index 8d2b6d24879..be23b6d8df5 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml @@ -4,4 +4,4 @@ license = "Apache-2.0" name = "bytecode-sway-lib-test" [dependencies] -bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } \ No newline at end of file +bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } From 0edc5c318a15e8bc43b17e4de4a068e6810e02bd Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 6 Apr 2024 01:50:17 +0530 Subject: [PATCH 03/12] add test group --- packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts index 0e120afd919..710723e7f66 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts @@ -6,6 +6,9 @@ import { defaultPredicateBytecode } from '../test/fixtures/bytecode/predicate'; import { getSetupContract } from './utils'; +/** + * @group node + */ test('compute_bytecode_root', async () => { const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject( FuelGaugeProjectsEnum.CALL_TEST_CONTRACT From 17ca36e03048e4692ec2c3463d14ad7523d855d1 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 6 Apr 2024 03:27:21 +0530 Subject: [PATCH 04/12] add docs --- .../src/guide/types/vector.test.ts | 27 ++++++++++++++++++- .../test/fixtures/forc-projects/Forc.toml | 1 + .../forc-projects/bytecode-input}/.gitignore | 0 .../forc-projects/bytecode-input}/Forc.toml | 2 +- .../forc-projects/bytecode-input/src/main.sw | 16 +++++++++++ .../test/fixtures/forc-projects/index.ts | 1 + apps/docs/src/guide/types/vectors.md | 12 +++++++++ ...test.test.ts => bytecode-sway-lib.test.ts} | 12 ++++----- .../test/fixtures/forc-projects/Forc.toml | 2 +- .../bytecode-sway-lib/.gitignore | 2 ++ .../forc-projects/bytecode-sway-lib/Forc.toml | 7 +++++ .../src/main.sw | 0 packages/fuel-gauge/test/fixtures/index.ts | 2 +- 13 files changed, 73 insertions(+), 11 deletions(-) rename {packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test => apps/docs-snippets/test/fixtures/forc-projects/bytecode-input}/.gitignore (100%) rename {packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test => apps/docs-snippets/test/fixtures/forc-projects/bytecode-input}/Forc.toml (84%) create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw rename packages/fuel-gauge/src/{bytecode-sway-lib-test.test.ts => bytecode-sway-lib.test.ts} (86%) create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/.gitignore create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml rename packages/fuel-gauge/test/fixtures/forc-projects/{bytecode-sway-lib-test => bytecode-sway-lib}/src/main.sw (100%) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index 675ad2c7fea..11d4690d538 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -1,5 +1,7 @@ +import { readFile } from 'fs/promises'; import type { Contract } from 'fuels'; -import { BN, getRandomB256 } from 'fuels'; +import { BN, arrayify, getRandomB256 } from 'fuels'; +import { join } from 'path'; import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects'; import { createAndDeployContractFromProject } from '../../utils'; @@ -52,4 +54,27 @@ describe(__filename, () => { expect(value.ratings).toEqual(employees[1].ratings); expect(value.isActive).toEqual(employees[1].isActive); }); + + it('should successfully execute a contract call with a bytecode input', async () => { + const bytecodeContract = await createAndDeployContractFromProject( + DocSnippetProjectsEnum.BYTECODE_INPUT + ); + + // #region vector-bytecode-input-ts + // #context import { arrayify } from 'fuels'; + const bytecode = await readFile( + join( + __dirname, + '../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin' + ) + ); + + const { value: bytecodeRoot } = await bytecodeContract.functions + .compute_bytecode_root(arrayify(bytecode)) + .call(); + // #endregion vector-bytecode-input-ts + + expect(bytecodeRoot).toBeDefined(); + expect(bytecodeRoot.length).toBe(66); + }); }); diff --git a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml index 5713291305f..d0d87a6a099 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml @@ -31,4 +31,5 @@ members = [ "predicate-signing", "script-signing", "input-output-types", + "bytecode-input", ] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore similarity index 100% rename from packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/.gitignore rename to apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml similarity index 84% rename from packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml rename to apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml index be23b6d8df5..2bf7264eaca 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml @@ -1,7 +1,7 @@ [project] authors = ["Fuel Labs "] license = "Apache-2.0" -name = "bytecode-sway-lib-test" +name = "bytecode-input" [dependencies] bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw new file mode 100644 index 00000000000..b4fa47e738e --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw @@ -0,0 +1,16 @@ +contract; + +use bytecode::*; + +abi MyContract { + fn compute_bytecode_root(bytecode_input: Vec) -> b256; +} + +impl MyContract for Contract { + // #region vector-bytecode-input-sway + fn compute_bytecode_root(bytecode_input: Vec) -> b256 { + let root = compute_bytecode_root(bytecode_input); + return root; + } + // #endregion vector-bytecode-input-sway +} diff --git a/apps/docs-snippets/test/fixtures/forc-projects/index.ts b/apps/docs-snippets/test/fixtures/forc-projects/index.ts index abed8dd766a..dd0ea7f8ac8 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/index.ts +++ b/apps/docs-snippets/test/fixtures/forc-projects/index.ts @@ -32,6 +32,7 @@ export enum DocSnippetProjectsEnum { PREDICATE_SIGNING = 'predicate-signing', SCRIPT_SIGNING = 'script-signing', INPUT_OUTPUT_TYPES = 'input-output-types', + BYTECODE_INPUT = 'bytecode-input', } export const getDocsSnippetsForcProject = (project: DocSnippetProjectsEnum) => diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index cf67d28daec..b26bf332742 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -20,6 +20,18 @@ The code snippet below demonstrates how to call this Sway contract method, which <<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers} +## Working with Bytecode in the SDK + +Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. + +Take the `verify_contract_bytecode` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode), for example. + +<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} + +To pass in some bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `Vec` type and pass it to the function like this: + +<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-bytecode-input-ts{ts:line-numbers} + ## Returning vectors Currently, returning vectors is not supported by Sway. If you try returning a type that is or contains a Vector, you will get a compile-time error. diff --git a/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts similarity index 86% rename from packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts rename to packages/fuel-gauge/src/bytecode-sway-lib.test.ts index 710723e7f66..5092af08524 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib-test.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts @@ -14,7 +14,7 @@ test('compute_bytecode_root', async () => { FuelGaugeProjectsEnum.CALL_TEST_CONTRACT ); - const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB); const contract = await setupContract(); const { logs } = await contract.functions @@ -29,18 +29,16 @@ test('compute_bytecode_root', async () => { test('verify_contract_bytecode', async () => { const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject( - FuelGaugeProjectsEnum.CALL_TEST_CONTRACT + FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB ); - const setupTestContract = getSetupContract(FuelGaugeProjectsEnum.CALL_TEST_CONTRACT); - const testContract = await setupTestContract(); - const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB); const contract = await setupContract(); const { value } = await contract.functions .verify_contract_bytecode( { - value: testContract.id.toB256(), + value: contract.id.toB256(), }, arrayify(bytecodeFromFile) ) @@ -60,7 +58,7 @@ test('compute_predicate_address', async () => { const address = predicate.address; - const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB_TEST); + const setupContract = getSetupContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB); const contract = await setupContract(); const { value } = await contract.functions diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml index 998c90f8c5e..22119f792bf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml @@ -6,7 +6,7 @@ members = [ "advanced-logging", "auth_testing_abi", "auth_testing_contract", - "bytecode-sway-lib-test", + "bytecode-sway-lib", "bytes", "call-test-contract", "collision_in_fn_names", diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/.gitignore b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml new file mode 100644 index 00000000000..8704b5962b1 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "bytecode-sway-lib" + +[dependencies] +bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw similarity index 100% rename from packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib-test/src/main.sw rename to packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw diff --git a/packages/fuel-gauge/test/fixtures/index.ts b/packages/fuel-gauge/test/fixtures/index.ts index 34f4f83ae82..91488c78891 100644 --- a/packages/fuel-gauge/test/fixtures/index.ts +++ b/packages/fuel-gauge/test/fixtures/index.ts @@ -9,7 +9,7 @@ export enum FuelGaugeProjectsEnum { AUTH_TESTING_ABI = 'auth_testing_abi', AUTH_TESTING_CONTRACT = 'auth_testing_contract', BYTES = 'bytes', - BYTECODE_SWAY_LIB_TEST = 'bytecode-sway-lib-test', + BYTECODE_SWAY_LIB = 'bytecode-sway-lib', CALL_TEST_CONTRACT = 'call-test-contract', CONFIGURABLE_CONTRACT = 'configurable-contract', COMPLEX_SCRIPT = 'complex-script', From 1575ba800d78df86846f6d7563d312e90d48e671 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 6 Apr 2024 03:30:50 +0530 Subject: [PATCH 05/12] add changeset --- .changeset/heavy-elephants-wave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/heavy-elephants-wave.md diff --git a/.changeset/heavy-elephants-wave.md b/.changeset/heavy-elephants-wave.md new file mode 100644 index 00000000000..d9cd820fa5c --- /dev/null +++ b/.changeset/heavy-elephants-wave.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/abi-coder": patch +--- + +feat: accept `Uint8Array`s as inputs for `Vec`s, add docs for bytecode inputs From 33319e5766dc4f0dec5c1703ef616062ccd71194 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Tue, 9 Apr 2024 00:17:44 +0530 Subject: [PATCH 06/12] fix a typo Co-authored-by: Peter Smith --- apps/docs/src/guide/types/vectors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index b26bf332742..8dcdaa10263 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -24,7 +24,7 @@ The code snippet below demonstrates how to call this Sway contract method, which Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. -Take the `verify_contract_bytecode` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode), for example. +Take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode.sw), for example. <<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} From 818bdce2358ccbe97b14104d7bb1fa68b78dc154 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Tue, 9 Apr 2024 01:06:13 +0530 Subject: [PATCH 07/12] refactor docs, create util function --- apps/docs-snippets/src/guide/types/vector.test.ts | 15 ++++++++------- .../abi-coder/src/encoding/coders/v0/VecCoder.ts | 11 +++++++---- packages/abi-coder/src/utils/utilities.ts | 2 ++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index 11d4690d538..d11337e9fd8 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -59,15 +59,16 @@ describe(__filename, () => { const bytecodeContract = await createAndDeployContractFromProject( DocSnippetProjectsEnum.BYTECODE_INPUT ); + const bytecodePath = join( + __dirname, + '../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin' + ); // #region vector-bytecode-input-ts - // #context import { arrayify } from 'fuels'; - const bytecode = await readFile( - join( - __dirname, - '../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin' - ) - ); + // #import { arrayify }; + // #context import { readFile } from 'fs/promises'; + + const bytecode = await readFile(bytecodePath); const { value: bytecodeRoot } = await bytecodeContract.functions .compute_bytecode_root(arrayify(bytecode)) diff --git a/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts b/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts index 485f4566e78..9b715e21980 100644 --- a/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/VecCoder.ts @@ -3,7 +3,12 @@ import { bn } from '@fuel-ts/math'; import { MAX_BYTES } from '../../../utils/constants'; import type { Uint8ArrayWithDynamicData } from '../../../utils/utilities'; -import { concatWithDynamicData, BASE_VECTOR_OFFSET, chunkByLength } from '../../../utils/utilities'; +import { + concatWithDynamicData, + BASE_VECTOR_OFFSET, + chunkByLength, + isUint8Array, +} from '../../../utils/utilities'; import type { TypesOfCoder } from '../AbstractCoder'; import { Coder } from '../AbstractCoder'; @@ -12,8 +17,6 @@ import { BigNumberCoder } from './BigNumberCoder'; type InputValueOf = Array['Input']>; type DecodedValueOf = Array['Decoded']>; -const isUintArray = (value: unknown): value is Uint8Array => value instanceof Uint8Array; - export class VecCoder extends Coder< InputValueOf, DecodedValueOf @@ -26,7 +29,7 @@ export class VecCoder extends Coder< } encode(value: InputValueOf): Uint8Array { - if (!Array.isArray(value) && !isUintArray(value)) { + if (!Array.isArray(value) && !isUint8Array(value)) { throw new FuelError( ErrorCode.ENCODE_ERROR, `Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.` diff --git a/packages/abi-coder/src/utils/utilities.ts b/packages/abi-coder/src/utils/utilities.ts index efd37520643..36f8c4d267c 100644 --- a/packages/abi-coder/src/utils/utilities.ts +++ b/packages/abi-coder/src/utils/utilities.ts @@ -179,3 +179,5 @@ export const rightPadToWordSize = (encoded: Uint8Array) => { const padding = new Uint8Array(WORD_SIZE - (encoded.length % WORD_SIZE)); return concatBytes([encoded, padding]); }; + +export const isUint8Array = (value: unknown): value is Uint8Array => value instanceof Uint8Array; From 9906d6b5fce157c192f90032e2a06cc513b8cf6c Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Tue, 9 Apr 2024 23:53:36 +0530 Subject: [PATCH 08/12] reword docs Co-authored-by: Daniel Bate --- apps/docs/src/guide/types/vectors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 8dcdaa10263..f0a9925593a 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -28,7 +28,7 @@ Take the `compute_bytecode_root` function from the [`bytecode` Sway library](htt <<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} -To pass in some bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `Vec` type and pass it to the function like this: +To pass bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `UInt8Array`, the TS compatible type for Sway's `Vec` type and pass it the function like so: <<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-bytecode-input-ts{ts:line-numbers} From fe911ee3de1a663e0d8b520494d2ed6072b794c6 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 10 Apr 2024 16:34:44 +0530 Subject: [PATCH 09/12] remove redundant gitignore --- .../test/fixtures/forc-projects/bytecode-input/.gitignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore deleted file mode 100644 index 77d3844f58c..00000000000 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -out -target From 0067c9202b58c81c5ea5eeae308a5d26003144a0 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 10 Apr 2024 16:53:33 +0530 Subject: [PATCH 10/12] clean import Co-authored-by: Peter Smith --- apps/docs-snippets/src/guide/types/vector.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index d11337e9fd8..80895c61749 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -65,8 +65,7 @@ describe(__filename, () => { ); // #region vector-bytecode-input-ts - // #import { arrayify }; - // #context import { readFile } from 'fs/promises'; + // #import { arrayify, readFile }; const bytecode = await readFile(bytecodePath); From 9f3a6d77610a340b2841dd31a0943194794d52ba Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 10 Apr 2024 17:02:34 +0530 Subject: [PATCH 11/12] enable pr release --- .github/workflows/pr-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index 9b48fc2352a..34ad25146b1 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -8,7 +8,7 @@ jobs: name: "Release PR to npm" runs-on: ubuntu-latest # comment out if:false to enable release PR to npm - if: false + # if: false permissions: write-all steps: - name: Checkout From d4ebfc2af56e88fd976e69bdd0f4cb12b5771bfb Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 10 Apr 2024 17:07:02 +0530 Subject: [PATCH 12/12] disable pr release --- .github/workflows/pr-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-release.yaml b/.github/workflows/pr-release.yaml index 34ad25146b1..9b48fc2352a 100644 --- a/.github/workflows/pr-release.yaml +++ b/.github/workflows/pr-release.yaml @@ -8,7 +8,7 @@ jobs: name: "Release PR to npm" runs-on: ubuntu-latest # comment out if:false to enable release PR to npm - # if: false + if: false permissions: write-all steps: - name: Checkout