-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: implement BytesLike type * feat: implement hexlify and arrayify functions * chore: linting * chore: changeset * chore: fix docs Co-authored-by: Nedim Salkić <[email protected]> * chore: fix assertion Co-authored-by: Nedim Salkić <[email protected]> * chore: litning --------- Co-authored-by: Nedim Salkić <[email protected]> Co-authored-by: Sérgio Torres <[email protected]>
- Loading branch information
1 parent
4d1f623
commit 2216b6e
Showing
8 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |