Skip to content

Commit dae516d

Browse files
committed
ensure backwards compatibility of type signatures
1 parent 3c246f7 commit dae516d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/bytes.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
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;
34

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+
}
526

627
function compare(a: BytesLike, b: BytesLike): number {
728
const diff = BigInt(toHex(a)) - BigInt(toHex(b));
829
return diff > 0 ? 1 : diff < 0 ? -1 : 0;
930
}
1031

11-
export type { HexString, BytesLike };
32+
export type { HexString, BytesLike, Bytes };
1233
export { toBytes, toHex, concat, compare };

0 commit comments

Comments
 (0)