UNPKG

@tevm/utils

Version:

A custom implementation of ethereumjs blockchain

161 lines 4.95 kB
/** * @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 * } * }) * ``` */ export function recoverPublicKey({ hash, signature }: { hash: import("./abitype.js").Hex; signature: Signature; }): import("./abitype.js").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 * } * }) * ``` */ export function recoverAddress({ hash, signature }: { hash: import("./abitype.js").Hex; signature: Signature; }): import("./abitype.js").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 * ``` */ export function hashMessage(message: string): import("./abitype.js").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 * } * }) * ``` */ export function recoverMessageAddress({ message, signature }: { message: string; signature: Signature; }): import("./abitype.js").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 * } * }) * ``` */ export function verifyMessage({ address, message, signature }: { address: import("./abitype.js").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' * }) * ``` */ export function signMessage({ privateKey, message }: { privateKey: import("./abitype.js").Hex; message: string; }): Promise<Signature>; export 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; }; //# sourceMappingURL=signature.d.ts.map