Skip to content

Commit

Permalink
ensure backwards compatibility of type signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Nov 2, 2024
1 parent 3c246f7 commit dae516d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import type { BytesLike } from '@metamask/utils';
type HexString = `0x${string}`;
type Bytes = Uint8Array;
type BytesLike = ArrayLike<number> | string;
type HexString = string;

import { createBytes as toBytes, createHex as toHex, concatBytes as concat } from '@metamask/utils';
import { createBytes, createHex, concatBytes, type BytesLike as StrictBytesLike } from '@metamask/utils';

function toBytes(value: BytesLike): Bytes {
if (Array.isArray(value)) {
return createBytes(new Uint8Array(value));
} else {
return createBytes(value as StrictBytesLike);
}
}

function toHex(value: BytesLike): `0x${string}` {
if (Array.isArray(value)) {
return createHex(new Uint8Array(value));
} else {
return createHex(value as StrictBytesLike);
}
}

function concat(values: BytesLike[]): Bytes {
return concatBytes(values.map(toBytes));
}

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

export type { HexString, BytesLike };
export type { HexString, BytesLike, Bytes };
export { toBytes, toHex, concat, compare };

0 comments on commit dae516d

Please sign in to comment.