Signature

Signature#

class rizemind.authentication.signatures.signature.Signature(*, data: bytes)[source]

Bases: BaseModel

Represents an ECDSA signature for Ethereum.

This model provides a structured way to handle the 65-byte signature format, which is a concatenation of the r, s, and v components. It includes properties to access individual components and class methods for convenient instantiation from different formats.

data

The raw 65-byte signature, concatenated as r (32 bytes) + s (32 bytes) + v (1 byte).

Type:

bytes

data: bytes
classmethod from_hex(signature: HexStr) Self[source]

Creates a Signature instance from a hexadecimal string.

Parameters:

signature – The 65-byte signature as a hex string (e.g., ‘0x…’).

Returns:

A new Signature instance.

classmethod from_rsv(r: HexStr, s: HexStr, v: int) Self[source]

Creates a Signature instance from its r, s, and v components.

Parameters:
  • r – The ‘r’ value as a 32-byte hex string.

  • s – The ‘s’ value as a 32-byte hex string.

  • v – The ‘v’ value (recovery ID), must be 27 or 28.

Returns:

A new Signature instance.

Raises:

ValueError – If v is not 27 or 28, or if r or s are not 32 bytes each after conversion from hex.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

property r: HexStr

The ‘r’ value of the ECDSA signature.

Returns:

The first 32 bytes of the signature as a HexStr.

property s: HexStr

The ‘s’ value of the ECDSA signature.

Returns:

The middle 32 bytes (bytes 32-64) of the signature as a HexStr.

to_hex() HexStr[source]

The full 65-byte signature as a hex string.

Returns:

The signature as a HexStr (e.g., ‘0x…’).

to_tuple() tuple[int, bytes, bytes][source]

Converts the signature to a tuple of (v, r, s).

This format is commonly used by Ethereum libraries like eth-account for transaction signing and public key recovery.

Returns:

(v, r_bytes, s_bytes).

Return type:

A tuple containing the signature components

property v: int

The ‘v’ value (recovery identifier) of the ECDSA signature.

Returns:

The last byte (65th) of the signature as an integer.

classmethod validate_signature_length(v: bytes) bytes[source]

Pydantic validator to ensure the signature data is exactly 65 bytes long.

Parameters:

v – The input byte string to validate.

Returns:

The validated 65-byte string.

Raises:

ValueError – If the length of v is not 65.