diff --git a/.changeset/soft-actors-poke.md b/.changeset/soft-actors-poke.md new file mode 100644 index 00000000000..b17e982da59 --- /dev/null +++ b/.changeset/soft-actors-poke.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/utils": patch +--- + +`concatBytes` now accept `BytesLike` as input. diff --git a/internal/check-imports/src/references.ts b/internal/check-imports/src/references.ts index da95d5d0332..a4302c7945a 100644 --- a/internal/check-imports/src/references.ts +++ b/internal/check-imports/src/references.ts @@ -28,7 +28,7 @@ import { FUEL_NETWORK_URL } from '@fuel-ts/wallet/configs'; // TODO: Add `launchNode` and `launchNodeAndGetWallets` here import { generateTestWallet, seedTestWallet } from '@fuel-ts/wallet/test-utils'; import { english, Language } from '@fuel-ts/wordlists'; -import { ScriptRequest, chunkAndPadBytes, normalizeString, concatBytes } from 'fuels'; +import { ScriptRequest, chunkAndPadBytes, normalizeString, concatBytes, concat, arrayify, hexlify } from 'fuels'; const { log } = console; @@ -142,6 +142,9 @@ log(InputCoinCoder); log(chunkAndPadBytes); log(normalizeString); log(concatBytes); +log(concat); +log(arrayify); +log(hexlify); /** * versions diff --git a/packages/abi-coder/src/coders/byte.ts b/packages/abi-coder/src/coders/byte.ts index 1e1a31d4265..13e4314186d 100644 --- a/packages/abi-coder/src/coders/byte.ts +++ b/packages/abi-coder/src/coders/byte.ts @@ -1,6 +1,6 @@ import { ErrorCode } from '@fuel-ts/errors'; import { bn } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { WORD_SIZE } from '../constants'; import type { Uint8ArrayWithDynamicData } from '../utilities'; @@ -50,7 +50,7 @@ export class ByteCoder extends Coder { data.push(new Uint8Array(paddingLength)); } - return concatBytes(data); + return concat(data); } decode(data: Uint8Array, offset: number): [Uint8Array, number] { diff --git a/packages/abi-coder/src/coders/enum.ts b/packages/abi-coder/src/coders/enum.ts index c6f417bfa80..6f4e3ae1b17 100644 --- a/packages/abi-coder/src/coders/enum.ts +++ b/packages/abi-coder/src/coders/enum.ts @@ -1,6 +1,6 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors'; import { toNumber } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import type { RequireExactlyOne } from 'type-fest'; import { concatWithDynamicData } from '../utilities'; @@ -50,7 +50,7 @@ export class EnumCoder> extends Coder< const caseIndex = Object.keys(this.coders).indexOf(value); const padding = new Uint8Array(this.#encodedValueSize - valueCoder.encodedLength); - return concatBytes([this.#caseIndexCoder.encode(caseIndex), padding, encodedValue]); + return concat([this.#caseIndexCoder.encode(caseIndex), padding, encodedValue]); } encode(value: InputValueOf): Uint8Array { diff --git a/packages/abi-coder/src/coders/stdString.ts b/packages/abi-coder/src/coders/stdString.ts index 756be1eb946..6eef7cb83b1 100644 --- a/packages/abi-coder/src/coders/stdString.ts +++ b/packages/abi-coder/src/coders/stdString.ts @@ -1,5 +1,5 @@ import { bn } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { toUtf8Bytes, toUtf8String } from 'ethers'; import { WORD_SIZE } from '../constants'; @@ -46,7 +46,7 @@ export class StdStringCoder extends Coder { data.push(new Uint8Array(paddingLength)); } - return concatBytes(data); + return concat(data); } decode(data: Uint8Array, offset: number): [string, number] { diff --git a/packages/abi-coder/src/coders/string.ts b/packages/abi-coder/src/coders/string.ts index c6e9b7bb449..d866fa3c393 100644 --- a/packages/abi-coder/src/coders/string.ts +++ b/packages/abi-coder/src/coders/string.ts @@ -1,5 +1,5 @@ import { ErrorCode } from '@fuel-ts/errors'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { toUtf8Bytes, toUtf8String } from 'ethers'; import { Coder } from './abstract-coder'; @@ -23,7 +23,7 @@ export class StringCoder extends Coder { const data2 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const data3 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const data4 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]); - const EXPECTED = concatBytes([data1, data2, data3, data4]); + const EXPECTED = concat([data1, data2, data3, data4]); const RESULT = concatWithDynamicData([data1, data2, data3, data4]); expect(RESULT).toEqual(EXPECTED); @@ -21,7 +21,7 @@ describe('Abi Coder Utilities', () => { const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const someData = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]); - const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([pointer, capacity, length, someData]); + const EXPECTED: Uint8ArrayWithDynamicData = concat([pointer, capacity, length, someData]); EXPECTED.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]) }; const RESULT = concatWithDynamicData([pointer, capacity, length, someData]); @@ -36,7 +36,7 @@ describe('Abi Coder Utilities', () => { const pointer = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 24]); const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); - const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([ + const EXPECTED: Uint8ArrayWithDynamicData = concat([ pointer, capacity, length, @@ -49,7 +49,7 @@ describe('Abi Coder Utilities', () => { 3: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]), }; - const arrayWithVectorData: Uint8ArrayWithDynamicData = concatBytes([pointer, capacity, length]); + const arrayWithVectorData: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]); arrayWithVectorData.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]) }; const RESULT = concatWithDynamicData([arrayWithVectorData, arrayWithVectorData]); @@ -65,7 +65,7 @@ describe('Abi Coder Utilities', () => { const pointer = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 24]); const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); - const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([ + const EXPECTED: Uint8ArrayWithDynamicData = concat([ pointer, capacity, length, @@ -82,21 +82,9 @@ describe('Abi Coder Utilities', () => { 6: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 37]), }; - const arrayWithVectorData1: Uint8ArrayWithDynamicData = concatBytes([ - pointer, - capacity, - length, - ]); - const arrayWithVectorData2: Uint8ArrayWithDynamicData = concatBytes([ - pointer, - capacity, - length, - ]); - const arrayWithVectorData3: Uint8ArrayWithDynamicData = concatBytes([ - pointer, - capacity, - length, - ]); + const arrayWithVectorData1: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]); + const arrayWithVectorData2: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]); + const arrayWithVectorData3: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]); arrayWithVectorData1.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 33]) }; arrayWithVectorData2.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 35]) }; arrayWithVectorData3.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 37]) }; @@ -124,7 +112,7 @@ describe('Abi Coder Utilities', () => { const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const someData = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]); - const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([ + const EXPECTED: Uint8ArrayWithDynamicData = concat([ pointerA, capacity, length, @@ -169,7 +157,7 @@ describe('Abi Coder Utilities', () => { const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]); const data = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]); - const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([ + const EXPECTED: Uint8ArrayWithDynamicData = concat([ otherData, pointer, capacity, diff --git a/packages/abi-coder/src/utilities.ts b/packages/abi-coder/src/utilities.ts index e92a82fc00c..dfa4c051c9c 100644 --- a/packages/abi-coder/src/utilities.ts +++ b/packages/abi-coder/src/utilities.ts @@ -1,5 +1,5 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { getBytesCopy, type BytesLike } from 'ethers'; import { U64Coder } from './coders/u64'; @@ -61,7 +61,7 @@ export function unpackDynamicData( dataOffset: number ): Uint8Array { if (!results.dynamicData) { - return concatBytes([results]); + return concat([results]); } let cumulativeDynamicByteLength = 0; @@ -83,7 +83,7 @@ export function unpackDynamicData( dataOffset + vData.byteLength + cumulativeDynamicByteLength ) : vData; - updatedResults = concatBytes([updatedResults, dataToAppend]); + updatedResults = concat([updatedResults, dataToAppend]); cumulativeDynamicByteLength += dataToAppend.byteLength; }); diff --git a/packages/abi-coder/test/interface.test.ts b/packages/abi-coder/test/interface.test.ts index 8b317029d66..bcb30554023 100644 --- a/packages/abi-coder/test/interface.test.ts +++ b/packages/abi-coder/test/interface.test.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { BN } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { NumberCoder, WORD_SIZE, Interface } from '../src'; import type { JsonAbiConfigurable } from '../src/json-abi'; @@ -28,7 +28,7 @@ import { } from './utils/constants'; function encodeVectorFully(encodedData: Uint8Array[] | Uint8Array, offset: number) { - const data = encodedData instanceof Uint8Array ? encodedData : concatBytes(encodedData); + const data = encodedData instanceof Uint8Array ? encodedData : concat(encodedData); const dataLength = data.length / 8; const length = new NumberCoder('u8').encode(dataLength); const capacity = length; @@ -37,7 +37,7 @@ function encodeVectorFully(encodedData: Uint8Array[] | Uint8Array, offset: numbe return { offset, length: dataLength, - vec: concatBytes([o, length, capacity]), + vec: concat([o, length, capacity]), data, }; } @@ -589,7 +589,7 @@ describe('Abi interface', () => { const data1Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][0]]); const data2Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][1]]); const data3Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][2]]); - const expectedBytes = concatBytes([ + const expectedBytes = concat([ // top level vector pointer, capacity, @@ -629,7 +629,7 @@ describe('Abi interface', () => { const data1 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[0][0]]); const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[0][1]]); - const expectedBytes = concatBytes([pointer, capacity, length, data1, data2]); + const expectedBytes = concat([pointer, capacity, length, data1, data2]); return expectedBytes; }, @@ -655,7 +655,7 @@ describe('Abi interface', () => { const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[1]]); const data3 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[2]]); const data4 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[3]]); - const expectedBytes = concatBytes([ + const expectedBytes = concat([ enumCaseOne, pointer, capacity, @@ -690,7 +690,7 @@ describe('Abi interface', () => { const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[1]]); const data3 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[2]]); const data4 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[3]]); - const expectedBytes = concatBytes([ + const expectedBytes = concat([ u8, pointer, capacity, @@ -715,7 +715,7 @@ describe('Abi interface', () => { const encodedVal = encodedValue instanceof Function ? encodedValue(value, offset) : encodedValue; const expectedEncoded = - encodedVal instanceof Uint8Array ? encodedVal : concatBytes(encodedVal); + encodedVal instanceof Uint8Array ? encodedVal : concat(encodedVal); expect(encoded).toEqual(expectedEncoded); diff --git a/packages/fuels/src/index.test.ts b/packages/fuels/src/index.test.ts index 137941015f2..c5de5b4025a 100644 --- a/packages/fuels/src/index.test.ts +++ b/packages/fuels/src/index.test.ts @@ -13,5 +13,8 @@ describe('index.js', () => { expect(fuels.ScriptResultDecoderError).toBeTruthy(); expect(fuels.Script).toBeTruthy(); expect(fuels.FunctionInvocationScope).toBeTruthy(); + expect(fuels.arrayify).toBeTruthy(); + expect(fuels.hexlify).toBeTruthy(); + expect(fuels.concat).toBeTruthy(); }); }); diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index 95f283a6b97..820f56d2751 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -12,7 +12,7 @@ import type { TransactionResultReturnReceipt, } from '@fuel-ts/providers'; import { ReceiptType } from '@fuel-ts/transactions'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import * as asm from '@fuels/vm-asm'; import { getBytesCopy } from 'ethers'; @@ -160,7 +160,7 @@ const scriptResultDecoder = const nextReturnData: TransactionResultReturnDataReceipt = filtered[ index + 1 ] as TransactionResultReturnDataReceipt; - return concatBytes([encodedScriptReturn, getBytesCopy(nextReturnData.data)]); + return concat([encodedScriptReturn, getBytesCopy(nextReturnData.data)]); } return [encodedScriptReturn]; @@ -288,12 +288,12 @@ export const getContractCallScript = ( scriptData.push(args); // move offset for next call - segmentOffset = dataOffset + concatBytes(scriptData).byteLength; + segmentOffset = dataOffset + concat(scriptData).byteLength; } // get asm instructions const script = getInstructions(paramOffsets, outputInfos); - const finalScriptData = concatBytes(scriptData); + const finalScriptData = concat(scriptData); return { data: finalScriptData, script }; }, () => [new Uint8Array()] diff --git a/packages/program/src/instruction-set.ts b/packages/program/src/instruction-set.ts index 76d99ef96e9..291111a46b8 100644 --- a/packages/program/src/instruction-set.ts +++ b/packages/program/src/instruction-set.ts @@ -1,4 +1,4 @@ -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import type * as asm from '@fuels/vm-asm'; import { hexlify } from 'ethers'; @@ -29,7 +29,7 @@ export class InstructionSet { } toBytes(): Uint8Array { - return concatBytes( + return concat( this.#operations.reduce((instructions, line) => { instructions.push(line.to_bytes()); return instructions; diff --git a/packages/transactions/src/coders/byte-array.ts b/packages/transactions/src/coders/byte-array.ts index 94ce5b9d72e..04ec95dafba 100644 --- a/packages/transactions/src/coders/byte-array.ts +++ b/packages/transactions/src/coders/byte-array.ts @@ -1,5 +1,5 @@ import { Coder } from '@fuel-ts/abi-coder'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { hexlify, getBytesCopy } from 'ethers'; import type { BytesLike } from 'ethers'; @@ -33,7 +33,7 @@ export class ByteArrayCoder extends Coder { parts.push(new Uint8Array(this.#paddingLength)); } - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [string, number] { diff --git a/packages/transactions/src/coders/input.ts b/packages/transactions/src/coders/input.ts index 33729bef937..3ca799889fa 100644 --- a/packages/transactions/src/coders/input.ts +++ b/packages/transactions/src/coders/input.ts @@ -2,7 +2,7 @@ import { Coder, U64Coder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BN } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import type { BytesLike } from 'ethers'; import { getBytesCopy, sha256 } from 'ethers'; @@ -79,7 +79,7 @@ export class InputCoinCoder extends Coder { parts.push(new ByteArrayCoder(value.predicateLength).encode(value.predicate)); parts.push(new ByteArrayCoder(value.predicateDataLength).encode(value.predicateData)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [InputCoin, number] { @@ -169,7 +169,7 @@ export class InputContractCoder extends Coder { parts.push(new TxPointerCoder().encode(value.txPointer)); parts.push(new B256Coder().encode(value.contractID)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [InputContract, number] { @@ -257,7 +257,7 @@ export class InputMessageCoder extends Coder { parts.push(new U64Coder().encode(value.amount)); parts.push(getBytesCopy(value.data || '0x')); - return sha256(concatBytes(parts)); + return sha256(concat(parts)); } static encodeData(messageData?: BytesLike): Uint8Array { @@ -283,7 +283,7 @@ export class InputMessageCoder extends Coder { parts.push(new ByteArrayCoder(value.predicateLength).encode(value.predicate)); parts.push(new ByteArrayCoder(value.predicateDataLength).encode(value.predicateData)); - return concatBytes(parts); + return concat(parts); } static decodeData(messageData: BytesLike): Uint8Array { @@ -379,7 +379,7 @@ export class InputCoder extends Coder { } } - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [Input, number] { diff --git a/packages/transactions/src/coders/output.ts b/packages/transactions/src/coders/output.ts index 83ffb6ae457..4983b54ddae 100644 --- a/packages/transactions/src/coders/output.ts +++ b/packages/transactions/src/coders/output.ts @@ -2,7 +2,7 @@ import { Coder, U64Coder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BN } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; export enum OutputType /* u8 */ { Coin = 0, @@ -34,7 +34,7 @@ export class OutputCoinCoder extends Coder { parts.push(new U64Coder().encode(value.amount)); parts.push(new B256Coder().encode(value.assetId)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [OutputCoin, number] { @@ -82,7 +82,7 @@ export class OutputContractCoder extends Coder { parts.push(new B256Coder().encode(value.balanceRoot)); parts.push(new B256Coder().encode(value.stateRoot)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [OutputContract, number] { @@ -130,7 +130,7 @@ export class OutputChangeCoder extends Coder { parts.push(new U64Coder().encode(value.amount)); parts.push(new B256Coder().encode(value.assetId)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [OutputChange, number] { @@ -178,7 +178,7 @@ export class OutputVariableCoder extends Coder { parts.push(new U64Coder().encode(value.amount)); parts.push(new B256Coder().encode(value.assetId)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [OutputVariable, number] { @@ -226,7 +226,7 @@ export class OutputContractCreatedCoder extends Coder< parts.push(new B256Coder().encode(value.contractId)); parts.push(new B256Coder().encode(value.stateRoot)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [OutputContractCreated, number] { @@ -297,7 +297,7 @@ export class OutputCoder extends Coder { } } - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [Output, number] { diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 036a2b74996..ba80cc8925b 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -2,7 +2,7 @@ import { Coder, U64Coder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BN } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { getBytesCopy, sha256 } from 'ethers'; import { ByteArrayCoder } from './byte-array'; @@ -63,7 +63,7 @@ export class ReceiptCallCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptCall, number] { @@ -132,7 +132,7 @@ export class ReceiptReturnCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptReturn, number] { @@ -192,7 +192,7 @@ export class ReceiptReturnDataCoder extends Coder { parts.push(new U64Coder().encode(value.is)); parts.push(new B256Coder().encode(value.contractId)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptPanic, number] { @@ -312,7 +312,7 @@ export class ReceiptRevertCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptRevert, number] { @@ -375,7 +375,7 @@ export class ReceiptLogCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptLog, number] { @@ -450,7 +450,7 @@ export class ReceiptLogDataCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptLogData, number] { @@ -522,7 +522,7 @@ export class ReceiptTransferCoder extends Coder): Uint8Array { @@ -713,7 +713,7 @@ export class ReceiptMessageOutCoder extends Coder { @@ -792,7 +792,7 @@ export class ReceiptMintCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptMint, number] { @@ -860,7 +860,7 @@ export class ReceiptBurnCoder extends Coder { parts.push(new U64Coder().encode(value.pc)); parts.push(new U64Coder().encode(value.is)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [ReceiptBurn, number] { @@ -979,7 +979,7 @@ export class ReceiptCoder extends Coder { } } - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [Receipt, number] { diff --git a/packages/transactions/src/coders/transaction.ts b/packages/transactions/src/coders/transaction.ts index 39581d3e212..d27084358d1 100644 --- a/packages/transactions/src/coders/transaction.ts +++ b/packages/transactions/src/coders/transaction.ts @@ -3,7 +3,7 @@ import { Coder, ArrayCoder, U64Coder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BN } from '@fuel-ts/math'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { ByteArrayCoder } from './byte-array'; import type { Input } from './input'; @@ -92,7 +92,7 @@ export class TransactionScriptCoder extends Coder { } } - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [Transaction, number] { diff --git a/packages/transactions/src/coders/witness.ts b/packages/transactions/src/coders/witness.ts index 59a6ba265b6..491d0295a7b 100644 --- a/packages/transactions/src/coders/witness.ts +++ b/packages/transactions/src/coders/witness.ts @@ -1,5 +1,5 @@ import { Coder, NumberCoder } from '@fuel-ts/abi-coder'; -import { concatBytes } from '@fuel-ts/utils'; +import { concat } from '@fuel-ts/utils'; import { ByteArrayCoder } from './byte-array'; @@ -26,7 +26,7 @@ export class WitnessCoder extends Coder { parts.push(new NumberCoder('u32').encode(value.dataLength)); parts.push(new ByteArrayCoder(value.dataLength).encode(value.data)); - return concatBytes(parts); + return concat(parts); } decode(data: Uint8Array, offset: number): [Witness, number] { diff --git a/packages/utils/src/index.test.ts b/packages/utils/src/index.test.ts index 186a92614cd..ecc66d9618d 100644 --- a/packages/utils/src/index.test.ts +++ b/packages/utils/src/index.test.ts @@ -5,5 +5,6 @@ describe('index.js', () => { expect(indexMod.normalizeString).toBeTruthy(); expect(indexMod.chunkAndPadBytes).toBeTruthy(); expect(indexMod.concatBytes).toBeTruthy(); + expect(indexMod.concat).toBeTruthy(); }); }); diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e3fc9591411..32edcb3e93e 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,3 +1,3 @@ export * from './utils/normalizeString'; export * from './utils/chunkAndPadBytes'; -export * from './utils/concatBytes'; +export * from './utils/concat'; diff --git a/packages/utils/src/utils/concat.test.ts b/packages/utils/src/utils/concat.test.ts new file mode 100644 index 00000000000..d384d918d4c --- /dev/null +++ b/packages/utils/src/utils/concat.test.ts @@ -0,0 +1,44 @@ +import { getBytesCopy } from 'ethers'; + +import { concat, concatBytes } from './concat'; + +describe('concat', () => { + it('should concatenate multiple BytesLike into a single Uint8Array', () => { + const byteslike1 = '0xff61ba809b36351b'; + const byteslike2 = new Uint8Array([23, 168, 113, 122, 53, 97, 62, 111]); + const byteslike3 = new Uint8Array([139, 90, 68, 118, 86, 63, 133, 37]); + + const input = [byteslike1, byteslike2, byteslike3]; + + const output = concat(input); + + expect(output).toBeInstanceOf(Uint8Array); + expect(output).toStrictEqual( + new Uint8Array([...getBytesCopy(byteslike1), ...byteslike2, ...byteslike3]) + ); + }); +}); + +describe('Concat Bytes', () => { + it('can concatenate multiple Uint8Arrays', () => { + const actual = concatBytes([ + new Uint8Array([1, 2, 3]), + new Uint8Array([4, 5, 6]), + new Uint8Array([7, 8, 9]), + ]); + const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]); + + expect(actual).toEqual(expected); + }); + + it('can concatenate multiple num arrays', () => { + const actual = concatBytes([ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ]); + const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]); + + expect(actual).toEqual(expected); + }); +}); diff --git a/packages/utils/src/utils/concatBytes.ts b/packages/utils/src/utils/concat.ts similarity index 63% rename from packages/utils/src/utils/concatBytes.ts rename to packages/utils/src/utils/concat.ts index c8549509b5b..a0da9f4a1a0 100644 --- a/packages/utils/src/utils/concatBytes.ts +++ b/packages/utils/src/utils/concat.ts @@ -1,3 +1,6 @@ +import type { BytesLike } from 'ethers'; +import { getBytesCopy } from 'ethers'; + /** * Concatenates multiple Uint8Arrays into a single Uint8Array. * @@ -22,3 +25,15 @@ export const concatBytes = ( return concatenated; }; + +/** + * Concatenates multiple BytesLike into a single Uint8Array. + * + * @param arrays - The arrays to concatenate. + * @returns - The concatenated array. + */ +export const concat = (arrays: ReadonlyArray): Uint8Array => { + const bytes = arrays.map((v) => getBytesCopy(v)); + + return concatBytes(bytes); +}; diff --git a/packages/utils/src/utils/concatBytes.test.ts b/packages/utils/src/utils/concatBytes.test.ts deleted file mode 100644 index b8a1bf66d07..00000000000 --- a/packages/utils/src/utils/concatBytes.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { concatBytes } from './concatBytes'; - -describe('concatBytes', () => { - it('can concatenate multiple Uint8Arrays', () => { - const actual = concatBytes([ - new Uint8Array([1, 2, 3]), - new Uint8Array([4, 5, 6]), - new Uint8Array([7, 8, 9]), - ]); - const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]); - - expect(actual).toEqual(expected); - }); - - it('can concatenate multiple num arrays', () => { - const actual = concatBytes([ - [1, 2, 3], - [4, 5, 6], - [7, 8, 9], - ]); - const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]); - - expect(actual).toEqual(expected); - }); -});