-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure backwards compatibility of type signatures
- Loading branch information
Showing
1 changed file
with
25 additions
and
4 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 |
---|---|---|
@@ -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 }; |