ECDSA
Title: ECDSA
Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol)
Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
Utility library for verifying and recovering signers from compact signatures. High-S signatures are rejected to prevent signature malleability. Based on OpenZeppelin v5 and Solady patterns.
Functions
recover
Returns the address that signed a hashed message with signature.
Reverts with the appropriate error for invalid signatures.
function recover(bytes32 hash, bytes memory signature) internal pure returns (address signer);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| signature | bytes | The signature bytes (65 bytes). |
Returns
| Name | Type | Description |
|---|---|---|
| signer | address | The recovered signer address. |
recover
Returns the address that signed a hashed message with compact (r, vs) signature.
EIP-2098 compact signature format.
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address signer);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| r | bytes32 | The r component of the signature. |
| vs | bytes32 | The combined v and s component (yParity bit in high bit of vs). |
Returns
| Name | Type | Description |
|---|---|---|
| signer | address | The recovered signer address. |
recover
Returns the address that signed a hashed message with (v, r, s) components.
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address signer);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| v | uint8 | The recovery byte (27 or 28). |
| r | bytes32 | The r component of the signature. |
| s | bytes32 | The s component of the signature. |
Returns
| Name | Type | Description |
|---|---|---|
| signer | address | The recovered signer address. |
tryRecover
Attempts to recover the signer from a full signature, returning an error enum instead of reverting.
function tryRecover(bytes32 hash, bytes memory signature)
internal
pure
returns (address recovered, RecoverError err, bytes32 errArg);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| signature | bytes | The signature bytes. |
Returns
| Name | Type | Description |
|---|---|---|
| recovered | address | The recovered signer (address(0) on error). |
| err | RecoverError | The error enum value. |
| errArg | bytes32 | Additional error argument (s value for InvalidSignatureS, length for InvalidSignatureLength). |
tryRecover
Attempts to recover the signer from an EIP-2098 compact (r, vs) signature.
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs)
internal
pure
returns (address recovered, RecoverError err, bytes32 errArg);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| r | bytes32 | The r component. |
| vs | bytes32 | The combined vs component (high bit is yParity, lower 255 bits are s). |
Returns
| Name | Type | Description |
|---|---|---|
| recovered | address | The recovered signer address. |
| err | RecoverError | The error enum value. |
| errArg | bytes32 | Additional error argument. |
tryRecover
Attempts to recover the signer from (v, r, s) components.
function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s)
internal
pure
returns (address recovered, RecoverError err, bytes32 errArg);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The hash of the signed message. |
| v | uint8 | The recovery byte. |
| r | bytes32 | The r component. |
| s | bytes32 | The s component. |
Returns
| Name | Type | Description |
|---|---|---|
| recovered | address | The recovered signer address. |
| err | RecoverError | The error enum value. |
| errArg | bytes32 | Additional error argument. |
toEthSignedMessageHash
Returns the Ethereum signed message hash of a 32-byte hash.
Produces the hash that would have been signed by eth_sign / personal_sign.
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| hash | bytes32 | The original message hash. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 | The prefixed hash: keccak256("\x19Ethereum Signed Message:\n32" ++ hash). |
toEthSignedMessageHash
Returns the Ethereum signed message hash of arbitrary bytes.
Produces keccak256("\x19Ethereum Signed Message:\nlength" ++ s).
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| s | bytes | The message bytes. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 | The prefixed hash. |
toTypedDataHash
Returns the EIP-712 typed data hash.
Produces keccak256("\x19\x01" ++ domainSeparator ++ structHash).
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| domainSeparator | bytes32 | The EIP-712 domain separator. |
| structHash | bytes32 | The hash of the typed data struct. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 | The final digest. |
_throwError
Throws the appropriate error for a given RecoverError.
function _throwError(RecoverError err, bytes32 errArg) private pure;Parameters
| Name | Type | Description |
|---|---|---|
| err | RecoverError | |
| errArg | bytes32 |
_toString
Converts a uint256 to its decimal ASCII bytes representation.
function _toString(uint256 value) private pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| value | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
Errors
ECDSAInvalidSignature
error ECDSAInvalidSignature();ECDSAInvalidSignatureLength
error ECDSAInvalidSignatureLength(uint256 length);Parameters
| Name | Type | Description |
|---|---|---|
| length | uint256 |
ECDSAInvalidSignatureS
error ECDSAInvalidSignatureS(bytes32 s);Parameters
| Name | Type | Description |
|---|---|---|
| s | bytes32 |
Enums
RecoverError
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS
}Variants
| Name | Description |
|---|---|
| NoError | |
| InvalidSignature | |
| InvalidSignatureLength | |
| InvalidSignatureS |