Skip to content

Commit

Permalink
Merge branch 'master' into dp/predicate-gettxid
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiwat10 committed Nov 30, 2023
2 parents 3e34b3c + 2216b6e commit f14dd77
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/soft-timers-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"fuels": patch
"@fuel-ts/interfaces": patch
"@fuel-ts/utils": patch
---

Introduce internal hexlify and arrayify functions
3 changes: 0 additions & 3 deletions packages/fuels/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export { hexlify, getBytesCopy as arrayify } from 'ethers';
export type { BytesLike } from 'ethers';

export { Script } from '@fuel-ts/script';
export * from './cli/index';
export * from '@fuel-ts/abi-coder';
Expand Down
2 changes: 2 additions & 0 deletions packages/interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type Bytes = Uint8Array | number[];

export type RawSlice = Uint8Array | number[];

export type BytesLike = Uint8Array | string;

/**
* @prop value - A 256 bit hash string with the first 12 bytes cleared
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './utils/capitalizeString';
export * from './utils/chunkAndPadBytes';
export * from './utils/concat';
export * from './utils/arrayify';
export * from './utils/hexlify';
export * from './utils/normalizeString';
19 changes: 19 additions & 0 deletions packages/utils/src/utils/arrayify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { arrayify } from './arrayify';

describe('arrayify', () => {
it('returns Uint8Array from Uint8Array', () => {
expect(arrayify(new Uint8Array([0, 1, 2, 3]))).toEqual(new Uint8Array([0, 1, 2, 3]));
});

it('returns Uint8Array from hex string', () => {
expect(arrayify('0x00010203')).toEqual(new Uint8Array([0, 1, 2, 3]));
});

it('returns Uint8Array from Buffer', () => {
expect(arrayify(Buffer.from('20'))).toEqual(new Uint8Array([50, 48]));
});

it('throws for invalid string', () => {
expect(() => arrayify('nope')).toThrow();
});
});
27 changes: 27 additions & 0 deletions packages/utils/src/utils/arrayify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';
import type { BytesLike } from 'ethers';

/**
* Converts a bytes-like value to a `Uint8Array`.
*
* @param value - the value to convert to a Uint8Array
* @returns the Uint8Array
*/
export const arrayify = (value: BytesLike): Uint8Array => {
// Return buffers as a new byte array
if (value instanceof Uint8Array) {
return new Uint8Array(value);
}

if (typeof value === 'string' && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) {
const result = new Uint8Array((value.length - 2) / 2);
let offset = 2;
for (let i = 0; i < result.length; i++) {
result[i] = parseInt(value.substring(offset, offset + 2), 16);
offset += 2;
}
return result;
}

throw new FuelError(ErrorCode.PARSE_FAILED, 'invalid BytesLike value');
};
15 changes: 15 additions & 0 deletions packages/utils/src/utils/hexlify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { hexlify } from './hexlify';

describe('hexlify', () => {
it('returns hex from bytes', () => {
expect(hexlify(new Uint8Array([0, 1, 2, 3]))).toEqual('0x00010203');
});

it('returns hex from string', () => {
expect(hexlify('0x01')).toEqual('0x01');
});

it('throws for invalid string', () => {
expect(() => hexlify('nope')).toThrow();
});
});
18 changes: 18 additions & 0 deletions packages/utils/src/utils/hexlify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { BytesLike } from 'ethers';
import { getBytes } from 'ethers';

const HexCharacters: string = '0123456789abcdef';

/**
* Returns a hex representation of the inputted bytes.
*/
export function hexlify(data: BytesLike): string {
const bytes = getBytes(data);

let result = '0x';
for (let i = 0; i < bytes.length; i++) {
const v = bytes[i];
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
}
return result;
}

0 comments on commit f14dd77

Please sign in to comment.