Pubkey

class Pubkey

A public key.

Parameters:

pubkey_bytes (bytes) – The pubkey in bytes.

Example

>>> from solders.pubkey import Pubkey
>>> pubkey = Pubkey(bytes([1] * 32))
>>> str(pubkey)
'4vJ9JU1bJJE96FWSJKvHsmmFADCg4gpZQff4P3bkLKi'
>>> bytes(pubkey).hex()
'0101010101010101010101010101010101010101010101010101010101010101'
LENGTH = 32
static create_program_address(seeds, program_id)

Derive a program address from seeds and a program ID.

Parameters:
  • seeds (Sequence[bytes]) – The seeds to use.

  • program_id (Pubkey) – The program ID.

Returns:

The derived program address.

Return type:

Pubkey

Example

>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.from_string("BPFLoader1111111111111111111111111111111111")
>>> Pubkey.create_program_address([b"", bytes([1])], program_id)
Pubkey(
    3gF2KMe9KiC6FNVBmfg9i267aMPvK37FewCip4eGBFcT,
)
static create_with_seed(base, seed, program_id)

Derive a pubkey from another key, a seed, and a program ID.

Parameters:
  • base (Pubkey) – The other pubkey to use.

  • seed (str) – The seed string

  • program_id (Pubkey) – The program ID.

Returns:

The derived pubkey.

Return type:

Pubkey

Example

>>> from solders.pubkey import Pubkey
>>> default_public_key = Pubkey.default()
>>> Pubkey.create_with_seed(default_public_key, "limber chicken: 4/45", default_public_key)
Pubkey(
    9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq,
)
static default()

Create a new default pubkey.

Returns:

The default pubkey.

Return type:

Pubkey

Example

>>> from solders.pubkey import Pubkey
>>> Pubkey.default()
Pubkey(
    11111111111111111111111111111111,
)
static find_program_address(seeds, program_id)

Find a valid program derived address and its corresponding bump seed.

Program derived addresses (PDAs) are account keys that only the program, program_id, has the authority to sign. The address is of the same form as a Solana Pubkey, except they are ensured to not be on the ed25519 curve and thus have no associated private key. When performing cross-program invocations the program can “sign” for the key by calling invoke_signed and passing the same seeds used to generate the address, along with the calculated bump seed, which this function returns as the second tuple element. The runtime will verify that the program associated with this address is the caller and thus authorized to be the signer.

The seeds are application-specific, and must be carefully selected to uniquely derive accounts per application requirements. It is common to use static strings and other pubkeys as seeds.

Because the program address must not lie on the ed25519 curve, there may be seed and program id combinations that are invalid. For this reason, an extra seed (the bump seed) is calculated that results in a point off the curve. The bump seed must be passed as an additional seed when calling invoke_signed.

Warning: Because of the way the seeds are hashed there is a potential for program address collisions for the same program id. The seeds are hashed sequentially which means that seeds {“abcdef”}, {“abc”, “def”}, and {“ab”, “cd”, “ef”} will all result in the same program address given the same program id. Since the chance of collision is local to a given program id, the developer of that program must take care to choose seeds that do not collide with each other. For seed schemes that are susceptible to this type of hash collision, a common remedy is to insert separators between seeds, e.g. transforming {“abc”, “def”} into {“abc”, “-”, “def”}.

Parameters:
  • seeds (Sequence[bytes]) – The seeds to use.

  • program_id (Pubkey) – The program ID.

Returns:

The PDA.

Return type:

Pubkey

Example

>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.from_string("BPFLoader1111111111111111111111111111111111")
>>> program_address, nonce = Pubkey.find_program_address([b""], program_id)
>>> program_address
Pubkey(
    EXWkUCz3YJU9TDVk39ogA4TwoVsUi75ZDhH6yT7acPgQ,
)
>>> nonce
255
static from_bytes(raw)

Construct from bytes. Equivalent to Pubkey.__init__ but included for the sake of consistency.

Parameters:

raw (bytes) – the pubkey bytes.

Returns:

a Pubkey object.

Return type:

Pubkey

static from_json(raw)

Build from a JSON string.

static from_string(s)

Retrieve a pubkey from a base58 string.

Returns:

the pubkey obtained from the base58 string.

Return type:

Pubkey

Example

>>> from solders.pubkey import Pubkey
>>> Pubkey.from_string("BPFLoader1111111111111111111111111111111111")
Pubkey(
    BPFLoader1111111111111111111111111111111111,
)
is_on_curve()

Check that the pubkey is on the ed25519 curve.

Returns:

True if the pubkey is on the curve.

Return type:

bool

static new_unique()

Unique pubkey for tests and benchmarks.

Returns:

Randomly generated pubkey.

Return type:

Pubkey

to_json()

Convert to a JSON string.