UNPKG

@tevm/utils

Version:

A custom implementation of ethereumjs blockchain

286 lines (275 loc) 12.3 kB
export { Log as EthjsLog } from '@ethereumjs/evm'; import { DBObject, DB } from '@ethereumjs/util'; export { AddressLike, BatchDBOp as BatchDbOp, BigIntLike, BytesLike, DB as Db, DBObject as DbObject, EncodingOpts, Account as EthjsAccount, Address as EthjsAddress, GWEI_TO_WEI, JSONRPCWithdrawal as JsonRpcWithdrawal, KECCAK256_RLP, KECCAK256_RLP_ARRAY, KeyEncoding, TypeOutput, ValueEncoding, Withdrawal, WithdrawalData, bytesToUnprefixedHex, bytesToUtf8, concatBytes, createAccount, createAddressFromString, createWithdrawal, ecrecover, equalsBytes, fetchFromProvider, getProvider, randomBytes, setLengthLeft, toType } from '@ethereumjs/util'; import { Address } from 'abitype'; export { Abi, AbiConstructor, AbiEvent, AbiFunction, AbiItemType, AbiParametersToPrimitiveTypes, Address, ExtractAbiEvent, ExtractAbiEventNames, ExtractAbiEvents, ExtractAbiFunction, ExtractAbiFunctionNames, FormatAbi, ParseAbi, formatAbi, parseAbi } from 'abitype'; import { Hex } from 'viem'; export { Account, BlockNumber, BlockTag, ContractConstructorArgs, ContractFunctionName, CreateEventFilterParameters, DecodeFunctionResultReturnType, EncodeDeployDataParameters, EncodeFunctionDataParameters, Filter, GetEventArgs, HDAccount, Hex } from 'viem'; import { PrivateKeyAccount } from 'viem/accounts'; export { mnemonicToAccount } from 'viem/accounts'; export { EncodeEventTopicsParameters, boolToBytes, boolToHex, bytesToBigInt, bytesToBigint, bytesToBool, bytesToHex, bytesToNumber, decodeAbiParameters, decodeErrorResult, decodeEventLog, decodeFunctionData, decodeFunctionResult, encodeAbiParameters, encodeDeployData, encodeErrorResult, encodeEventTopics, encodeFunctionData, encodeFunctionResult, encodePacked, formatEther, formatGwei, formatLog, fromBytes, fromHex, fromRlp, getAddress, hexToBigInt, hexToBool, hexToBytes, hexToNumber, hexToString, isAddress, isBytes, isHex, keccak256, numberToHex, parseEther, parseGwei, serializeTransaction, stringToHex, toBytes, toHex, toRlp } from 'viem/utils'; /** * A simple Bloom filter implementation originally from ethereumjs */ declare class Bloom { bitvector: Uint8Array; /** * Represents a Bloom filter. * @throws {InvalidBytesSizeError} If the byte size of the bitvector is not 256. */ constructor(bitvector?: Uint8Array); /** * Adds an element to a bit vector of a 64 byte bloom filter. * @param e - The element to add * @throws {never} */ add(e: Uint8Array): void; /** * Checks if an element is in the bloom. * @param e - The element to check * @throws {never} */ check(e: Uint8Array): boolean; /** * Checks if multiple topics are in a bloom. * @returns `true` if every topic is in the bloom * @throws {never} */ multiCheck(topics: Uint8Array[]): boolean; /** * Bitwise or blooms together. * @throws {never} */ or(bloom: Bloom): void; } type MemoryDb<TKey extends string | number | Uint8Array = Uint8Array, TValue extends string | Uint8Array | Uint8Array | string | DBObject = Uint8Array> = DB<TKey, TValue>; type CreateMemoryDbFn<TKey extends string | number | Uint8Array = Uint8Array, TValue extends string | Uint8Array | Uint8Array | string | DBObject = Uint8Array> = (initialDb?: Map<TKey, TValue>) => MemoryDb<TKey, TValue>; /** * @internal * A simple ethereumjs DB instance that uses an in memory Map as it's backend * Pass in an initial DB optionally to prepoulate the DB. * @type {import('./CreateMemoryDbFn.js').CreateMemoryDbFn} * @throws {never} */ declare const createMemoryDb: CreateMemoryDbFn; declare const PREFUNDED_PRIVATE_KEYS: readonly ["0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d", "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a", "0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6", "0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a", "0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba", "0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e", "0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356", "0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97", "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6"]; declare const PREFUNDED_PUBLIC_KEYS: readonly ["0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "0x90F79bf6EB2c4f870365E785982E1f101E93b906", "0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65", "0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc", "0x976EA74026E726554dB657fA54763abd0C3a0aa9", "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955", "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f", "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"]; declare const PREFUNDED_ACCOUNTS: [ PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount, PrivateKeyAccount ]; declare const PREFUNDED_SEED: Readonly<{ mnemonic: "test test test test test test test test test test test junk"; derivationPath: "m/44'/60'/0'/0/"; }>; /** * @typedef {Object} Signature * @property {bigint} r - The r value of the signature * @property {bigint} s - The s value of the signature * @property {number} [v] - The recovery id (27 or 28) * @property {0 | 1} [yParity] - The y parity (0 or 1) */ /** * Recovers the public key from a signature * @param {Object} params - The parameters * @param {import('./abitype.js').Hex} params.hash - The message hash * @param {Signature} params.signature - The signature * @returns {import('./abitype.js').Hex} The uncompressed public key (65 bytes) * @throws {Error} If the signature is invalid * @example * ```js * import { recoverPublicKey } from '@tevm/utils' * * const publicKey = recoverPublicKey({ * hash: '0x82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', * signature: { * r: 0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9n, * s: 0x129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66n, * v: 27 * } * }) * ``` */ declare function recoverPublicKey({ hash, signature }: { hash: Hex; signature: Signature; }): Hex; /** * Recovers the address from a signature * @param {Object} params - The parameters * @param {import('./abitype.js').Hex} params.hash - The message hash * @param {Signature} params.signature - The signature * @returns {import('./abitype.js').Address} The recovered address * @throws {Error} If the signature is invalid * @example * ```js * import { recoverAddress } from '@tevm/utils' * * const address = recoverAddress({ * hash: '0x82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', * signature: { * r: 0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9n, * s: 0x129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66n, * v: 27 * } * }) * ``` */ declare function recoverAddress({ hash, signature }: { hash: Hex; signature: Signature; }): Address; /** * Hashes a message according to EIP-191 * @param {string} message - The message to hash * @returns {import('./abitype.js').Hex} The message hash * @example * ```js * import { hashMessage } from '@tevm/utils' * * const hash = hashMessage('Hello world') * // 0x8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede * ``` */ declare function hashMessage(message: string): Hex; /** * Recovers the address from a signed message * @param {Object} params - The parameters * @param {string} params.message - The original message * @param {Signature} params.signature - The signature * @returns {import('./abitype.js').Address} The recovered address * @throws {Error} If the signature is invalid * @example * ```js * import { recoverMessageAddress } from '@tevm/utils' * * const address = recoverMessageAddress({ * message: 'Hello world', * signature: { * r: 0x..., * s: 0x..., * v: 27 * } * }) * ``` */ declare function recoverMessageAddress({ message, signature }: { message: string; signature: Signature; }): Address; /** * Verifies a message signature * @param {Object} params - The parameters * @param {import('./abitype.js').Address} params.address - The expected signer address * @param {string} params.message - The original message * @param {Signature} params.signature - The signature * @returns {boolean} Whether the signature is valid * @example * ```js * import { verifyMessage } from '@tevm/utils' * * const isValid = verifyMessage({ * address: '0xa6fb229e9b0a4e4ef52ea6991adcfc59207c7711', * message: 'Hello world', * signature: { * r: 0x..., * s: 0x..., * v: 27 * } * }) * ``` */ declare function verifyMessage({ address, message, signature }: { address: Address; message: string; signature: Signature; }): boolean; /** * Signs a message with a private key * @param {Object} params - The parameters * @param {import('./abitype.js').Hex} params.privateKey - The private key * @param {string} params.message - The message to sign * @returns {Promise<Signature>} The signature * @example * ```js * import { signMessage } from '@tevm/utils' * * const signature = await signMessage({ * privateKey: '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', * message: 'Hello world' * }) * ``` */ declare function signMessage({ privateKey, message }: { privateKey: Hex; message: string; }): Promise<Signature>; type Signature = { /** * - The r value of the signature */ r: bigint; /** * - The s value of the signature */ s: bigint; /** * - The recovery id (27 or 28) */ v?: number; /** * - The y parity (0 or 1) */ yParity?: 0 | 1; }; declare function invariant(condition: any, error?: Error): asserts condition; /** * A type that represents a JSON-serializable value. */ type JsonSerializable = bigint | string | number | boolean | null | JsonSerializableArray | JsonSerializableObject | JsonSerializableSet; /** * A type that represents a JSON-serializable array. */ type JsonSerializableArray = ReadonlyArray<JsonSerializable>; /** * A type that represents a JSON-serializable object. */ type JsonSerializableObject = { [key: string]: JsonSerializable; }; /** * A type that represents a JSON-serializable set. */ type JsonSerializableSet<T extends bigint | string | number | boolean = bigint | string | number | boolean> = Set<T>; /** * A helper type that converts a bigint to a hex string. */ type BigIntToHex<T> = T extends bigint ? Hex : T; /** * A helper type that converts a set to a hex string. */ type SetToHex<T> = T extends Set<any> ? Hex : T; /** * A helper type that converts a widened JSON-serializable value to a JSON-serializable value. * It replaces bigint with hex strings and sets with arrays. */ type SerializeToJson<T> = T extends JsonSerializableSet<infer S> ? ReadonlyArray<S> : T extends JsonSerializableObject ? { [P in keyof T]: SerializeToJson<T[P]>; } : T extends JsonSerializableArray ? SerializeToJson<T[number]>[] : BigIntToHex<SetToHex<T>>; type GenesisState = Record<string, string | Record<string, any>>; type AsyncEventEmitter<T extends Record<string, any> = {}> = { on<K extends keyof T>(event: K, listener: T[K]): void; once<K extends keyof T>(event: K, listener: T[K]): void; off<K extends keyof T>(event: K, listener: T[K]): void; emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>): boolean; removeAllListeners<K extends keyof T>(event?: K): void; }; export { type AsyncEventEmitter, type BigIntToHex, Bloom, type CreateMemoryDbFn, type GenesisState, type JsonSerializable, type JsonSerializableArray, type JsonSerializableObject, type JsonSerializableSet, type MemoryDb, PREFUNDED_ACCOUNTS, PREFUNDED_PRIVATE_KEYS, PREFUNDED_PUBLIC_KEYS, PREFUNDED_SEED, type SerializeToJson, type SetToHex, createMemoryDb, hashMessage, invariant, recoverAddress, recoverMessageAddress, recoverPublicKey, signMessage, verifyMessage };