Keypair

class Keypair

A vanilla Ed25519 key pair.

Calling Keypair() creates a new, random Keypair.

Example

>>> from solders.keypair import Keypair
>>> assert Keypair() != Keypair()
LENGTH = 64
static from_base58_string(s)

Recovers a Keypair from a base58-encoded string.

Parameters:

s (str) – The base58-encoded string.

Returns:

a keypair oject.

Return type:

Keypair

Example

>>> from solders.keypair import Keypair
>>> raw_bytes = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x8a\x88\xe3\xddt\t\xf1\x95\xfdR\xdb-<\xba]r\xcag\t\xbf\x1d\x94\x12\x1b\xf3t\x88\x01\xb4\x0fo\\'
>>> base58_str = "2AXDGYSE4f2sz7tvMMzyHvUfcoJmxudvdhBcmiUSo6iuCXagjUCKEQF21awZnUGxmwD4m9vGXuC3qieHXJQHAcT"
>>> kp = Keypair.from_base58_string(base58_str)
>>> assert kp == Keypair.from_bytes(raw_bytes)
>>> assert str(kp) == base58_str
static from_bytes(raw_bytes)

Recovers a Keypair from bytes.

Parameters:

raw_bytes (bytes) – a 64-byte keypair.

Returns:

a keypair object.

Return type:

Keypair

Example

>>> from solders.keypair import Keypair
>>> kp = Keypair()
>>> assert kp == Keypair.from_bytes(bytes(kp))
static from_json(raw)

Build from a JSON string.

static from_seed(seed)

Generate a keypair from a 32-byte seed.

Parameters:

seed (bytes) – 32-byte seed.

Returns:

The generated keypair.

Return type:

Keypair

Example

>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> seed_bytes = bytes([0] * 32)
>>> from_seed = Keypair.from_seed(seed_bytes)
>>> from_bytes = Keypair.from_bytes(seed_bytes + bytes(from_seed.pubkey()))
>>> assert from_seed == from_bytes
static from_seed_and_derivation_path(seed, dpath)

Generate a keypair from a 32-byte seed and derivation path..

Parameters:
  • seed (bytes) – 32-byte seed.

  • dpath (str) – derivation path.

Returns:

The generated keypair.

Return type:

Keypair

Example

>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> seed_bytes = bytes([0] * 64)
>>> account_index = 0
>>> derivation_path = f"m/44'/501'/0'/{account_index}'"
>>> from_seed = Keypair.from_seed_and_derivation_path(seed_bytes, derivation_path)
static from_seed_phrase_and_passphrase(seed_phrase, passphrase)

Generate a keypair from a seed phrase and passphrase.

Parameters:
  • seed_phrase (string) – Secret seed phrase.

  • passphrase (string) – Passphrase.

Example

>>> from pybip39 import Mnemonic, Seed
>>> from solders.keypair import Keypair
>>> mnemonic = Mnemonic()
>>> passphrase = "42"
>>> seed = Seed(mnemonic, passphrase)
>>> expected_keypair = Keypair.from_seed(bytes(seed)[:32])
>>> keypair = Keypair.from_seed_phrase_and_passphrase(mnemonic.phrase, passphrase)
>>> assert keypair.pubkey() == expected_keypair.pubkey()
is_interactive()

Whether the impelmentation requires user interaction to sign.

Returns:

Always False for this class.

Return type:

bool

pubkey()

Get this keypair’s Pubkey.

Returns:

the pubkey of this keypair.

Return type:

Pubkey

Example

>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> seed_bytes = bytes([0] * 32)
>>> pubkey_bytes = b";j'\xbc\xce\xb6\xa4-b\xa3\xa8\xd0*o\rse2\x15w\x1d\xe2C\xa6:\xc0H\xa1\x8bY\xda)"
>>> kp = Keypair.from_bytes(seed_bytes + pubkey_bytes)
>>> assert kp.pubkey() == Pubkey(pubkey_bytes)
secret()

Gets this Keypair’s secret key.

Returns:

The secret key in 32 bytes.

Return type:

bytes

Example

>>> from solders.keypair import Keypair
>>> kp = Keypair()
>>> assert kp.secret() == bytes(kp)[:32]
sign_message(message)

Sign a mesage with this keypair, producing an Ed25519 signature over the provided message bytes.

Parameters:

message (bytes) – The message to sign.

Returns:

The Ed25519 signature.

Return type:

Signature

Example

>>> from solders.keypair import Keypair
>>> seed = bytes([1] * 32)
>>> keypair = Keypair.from_seed(seed)
>>> msg = b"hello"
>>> sig = keypair.sign_message(msg)
>>> bytes(sig).hex()
'e1430c6ebd0d53573b5c803452174f8991ef5955e0906a09e8fdc7310459e9c82a402526748c3431fe7f0e5faafbf7e703234789734063ee42be17af16438d08'
to_bytes_array()

Returns this Keypair as a byte array.

Returns:

the keypair as a list of 64 u8 ints.

Return type:

list[int]

Example

>>> from solders.keypair import Keypair
>>> raw_bytes = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x8a\x88\xe3\xddt\t\xf1\x95\xfdR\xdb-<\xba]r\xcag\t\xbf\x1d\x94\x12\x1b\xf3t\x88\x01\xb4\x0fo\\'
>>> assert Keypair.from_bytes(raw_bytes).to_bytes_array() == list(raw_bytes)
to_json()

Convert to a JSON string.