Keypair

Keypair is the combination of a public key and private key. It handles key generation, signing, and key management.

class Keypair(value: NaclPrivateKey | None = None)
copy

The value is optional. Without a value, a new keypair is generated. If provided, it must be a nacl.public.PrivateKey object. To initialize with a private key as a string or bytes, use from_private_key class method.

Methods

Attributes

Methods

.sign

Signs a message using the keypair's private key.

def sign(message: str | bytes) -> SignedMessage
copy

Returns a SignedMessage object from PyNaCl. The message can be either a UTF-8 string or bytes.

.from_private_key

Class method that creates a Keypair from an existing private key.

@classmethod
def from_private_key(cls, private_key: str | List[int]) -> Keypair
copy

Accepts either a base58-encoded string or a list of integers representing the private key. Returns a new Keypair instance.

.from_file

Class method that loads a Keypair from a JSON file containing the private key.

@staticmethod
def from_file(file_path: str) -> Keypair
copy

Reads a JSON file containing the private key data and returns a new Keypair instance.

Attributes

.public_key

The keypair's public key as a PublicKey instance.

.private_key

The keypair's private key as a PrivateKey instance.

.key_pair

The underlying NaclPrivateKey object used in bytes