|
1 |
| -import type { BytesLike } from '@metamask/utils'; |
2 |
| -type HexString = `0x${string}`; |
| 1 | +type Bytes = Uint8Array; |
| 2 | +type BytesLike = ArrayLike<number> | string; |
| 3 | +type HexString = string; |
3 | 4 |
|
4 |
| -import { createBytes as toBytes, createHex as toHex, concatBytes as concat } from '@metamask/utils'; |
| 5 | +import { createBytes, createHex, concatBytes, type BytesLike as StrictBytesLike } from '@metamask/utils'; |
| 6 | + |
| 7 | +function toBytes(value: BytesLike): Bytes { |
| 8 | + if (Array.isArray(value)) { |
| 9 | + return createBytes(new Uint8Array(value)); |
| 10 | + } else { |
| 11 | + return createBytes(value as StrictBytesLike); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function toHex(value: BytesLike): `0x${string}` { |
| 16 | + if (Array.isArray(value)) { |
| 17 | + return createHex(new Uint8Array(value)); |
| 18 | + } else { |
| 19 | + return createHex(value as StrictBytesLike); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function concat(values: BytesLike[]): Bytes { |
| 24 | + return concatBytes(values.map(toBytes)); |
| 25 | +} |
5 | 26 |
|
6 | 27 | function compare(a: BytesLike, b: BytesLike): number {
|
7 | 28 | const diff = BigInt(toHex(a)) - BigInt(toHex(b));
|
8 | 29 | return diff > 0 ? 1 : diff < 0 ? -1 : 0;
|
9 | 30 | }
|
10 | 31 |
|
11 |
| -export type { HexString, BytesLike }; |
| 32 | +export type { HexString, BytesLike, Bytes }; |
12 | 33 | export { toBytes, toHex, concat, compare };
|
0 commit comments