Skip to content

Commit

Permalink
remove metamask/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Nov 2, 2024
1 parent 6d8567c commit 4b00bc2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"license": "MIT",
"dependencies": {
"@metamask/abi-utils": "^2.0.4",
"@metamask/utils": "^9.3.0",
"ethereum-cryptography": "^3.0.0"
},
"devDependencies": {
Expand Down
25 changes: 15 additions & 10 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
type Bytes = Uint8Array;
type BytesLike = ArrayLike<number> | string;
type BytesLike = readonly number[] | Uint8Array | string;
type HexString = string;

import { createBytes, createHex, concatBytes, type BytesLike as StrictBytesLike } from '@metamask/utils';
import { hexToBytes, bytesToHex, concatBytes } from 'ethereum-cryptography/utils';

function toBytes(value: BytesLike): Bytes {
if (Array.isArray(value)) {
return createBytes(new Uint8Array(value));
if (value instanceof Uint8Array) {
return value;
} else if (typeof value === 'string') {
return hexToBytes(value);
} else {
return createBytes(value as StrictBytesLike);
return new Uint8Array(value);
}
}

function toHex(value: BytesLike): `0x${string}` {
if (Array.isArray(value)) {
return createHex(new Uint8Array(value));
function toHex(value: BytesLike): string {
if (typeof value === 'string') {
hexToBytes(value); // assert hex string
return value;
} else if (value instanceof Uint8Array) {
return bytesToHex(value);
} else {
return createHex(value as StrictBytesLike);
return bytesToHex(new Uint8Array(value));
}
}

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

function compare(a: BytesLike, b: BytesLike): number {
Expand Down

0 comments on commit 4b00bc2

Please sign in to comment.