diff --git a/packages/types/src/utils/ExecutionAddress.ts b/packages/types/src/utils/ExecutionAddress.ts deleted file mode 100644 index cd2af4d54dc9..000000000000 --- a/packages/types/src/utils/ExecutionAddress.ts +++ /dev/null @@ -1,48 +0,0 @@ -import {keccak256} from "ethereum-cryptography/keccak.js"; -import {ByteVectorType, fromHexString, toHexString} from "@chainsafe/ssz"; - -export type ByteVector = Uint8Array; - -export class ExecutionAddressType extends ByteVectorType { - constructor() { - super(20, {typeName: "ExecutionAddress"}); - } - toJson(value: ByteVector): unknown { - const str = super.toJson(value) as string; - return toChecksumAddress(str); - } -} - -function isAddressValid(address: string): boolean { - return /^(0x)?[0-9a-f]{40}$/i.test(address); -} - -/** - * Formats an address according to [ERC55](https://eips.ethereum.org/EIPS/eip-55) - */ -export function toChecksumAddress(address: string): string { - if (!isAddressValid(address)) { - throw Error(`Invalid address: ${address}`); - } - - const rawAddress = address.toLowerCase().startsWith("0x") ? address.slice(2) : address; - const chars = rawAddress.split(""); - - // Inspired by https://github.com/ethers-io/ethers.js/blob/cac1da1f912c2ae9ba20f25aa51a91766673cd76/src.ts/address/address.ts#L8 - const expanded = new Uint8Array(chars.length); - for (let i = 0; i < expanded.length; i++) { - expanded[i] = rawAddress[i].charCodeAt(0); - } - - const hashed = keccak256(expanded); - for (let i = 0; i < chars.length; i += 2) { - if (hashed[i >> 1] >> 4 >= 8) { - chars[i] = chars[i].toUpperCase(); - } - if ((hashed[i >> 1] & 0x0f) >= 8) { - chars[i + 1] = chars[i + 1].toUpperCase(); - } - } - - return "0x" + chars.join(""); -} diff --git a/packages/types/src/utils/StringType.ts b/packages/types/src/utils/StringType.ts deleted file mode 100644 index b3ef4f2f1f18..000000000000 --- a/packages/types/src/utils/StringType.ts +++ /dev/null @@ -1,55 +0,0 @@ -import {BasicType} from "@chainsafe/ssz"; - -export class StringType extends BasicType { - readonly typeName = "string"; - byteLength = 0; - fixedSize = 0; - minSize = 0; - maxSize = 0; - - defaultValue(): T { - return "" as T; - } - - // Serialization + deserialization - - value_serializeToBytes(): number { - throw Error("Not supported in String type"); - } - value_deserializeFromBytes(): T { - throw Error("Not supported in String type"); - } - tree_serializeToBytes(): number { - throw Error("Not supported in String type"); - } - tree_deserializeFromBytes(): never { - throw Error("Not supported in String type"); - } - - // Fast tree opts - - tree_getFromNode(): T { - throw Error("Not supported in String type"); - } - tree_setToNode(): void { - throw Error("Not supported in String type"); - } - tree_getFromPackedNode(): T { - throw Error("Not supported in String type"); - } - tree_setToPackedNode(): void { - throw Error("Not supported in String type"); - } - - // JSON - - fromJson(json: unknown): T { - return json as T; - } - - toJson(value: T): unknown { - return value; - } -} - -export const stringType = new StringType(); diff --git a/packages/types/src/utils/executionAddress.ts b/packages/types/src/utils/executionAddress.ts index cd2af4d54dc9..bfda06dc3579 100644 --- a/packages/types/src/utils/executionAddress.ts +++ b/packages/types/src/utils/executionAddress.ts @@ -1,5 +1,5 @@ import {keccak256} from "ethereum-cryptography/keccak.js"; -import {ByteVectorType, fromHexString, toHexString} from "@chainsafe/ssz"; +import {ByteVectorType} from "@chainsafe/ssz"; export type ByteVector = Uint8Array;