Public keys, keypairs, and signatures
Transaction signatures bind serialized message bytes to the public-key identities that the message requires. This page explains public-key and keypair handling, signing primitives, and that transaction signing flow. For the wider account model, see Accounts and identity and Account state and layouts.
Public key model
Public key representation
packages/…/publickey.ts defines PublicKey as a fixed 32-byte Ed25519 public key. The same value can be read from a base-58 address string or from supported numeric and byte forms. The class keeps one canonical byte form, returns copies of those bytes, and compares keys by value rather than by object identity.
The base-58 form is the address used at API boundaries. It is also exposed as a Kit Address, so the same public identity can pass to APIs that use the Kit address type. The byte form is used for hashing, signing checks, account layouts, and wire encoding. Borsh encoding uses the reverse byte order, so callers must use the matching encode or decode operation rather than treating a Borsh field as an ordinary address byte array.
References: packages/…/publickey.ts• L23-81 packages/…/publickey.ts• L89-136 packages/…/publickey.ts• L164-194 packages/…/publickey.ts• L196-205
Public key validation
Public key construction validates address strings and rejects invalid numeric or byte input. Short byte values are represented as 32-byte values with leading zero padding. A public key can also be checked for membership on the Ed25519 curve. This check matters because program-derived addresses must not be ordinary curve points.
Public keys are not secret. Their address form can be shared and used to select accounts or match required signer identities. The secret key that proves control of that identity has a separate handling rule described in Keypair lifecycle.
References: packages/…/publickey.ts• L318-323 packages/…/ed25519.ts• L11-19
Public key derivation
Seed-based addresses
Seed-based address derivation creates a public key from a base public key, a text seed, and a program ID. The result is still an address that can be used in the account model, and the program ID defines the owner relationship for the derived account. The seed has a maximum length of 32 bytes.
Program-derived addresses use a different rule. They hash the supplied seeds with the program ID and require the result to be off the Ed25519 curve, so the resulting address has no private key. findProgramAddress searches for a valid bump value when the first candidate is on the curve. The program must use the same seeds and bump when it derives the address again.
The diagram below shows how the two derivation families differ at a high level.
flowchart LR
A[Base public key and text seed] --> B[Seed-based address]
C[Seeds and program ID] --> D[Candidate address]
D --> E{On Ed25519 curve?}
E -- Yes --> F[Try another bump]
F --> D
E -- No --> G[Program-derived address]
References: packages/…/publickey.ts• L207-230 packages/…/publickey.ts• L232-315 packages/…/publickey.ts• L24-31 packages/…/publickey.ts• L275-286
Keypair lifecycle
Keypair generation and restoration
packages/…/keypair.ts represents an account keypair backed by WebCrypto. New keypairs start with 32 random private-key bytes. The library derives the matching public key from those bytes and exposes the result as a PublicKey and as a Kit-compatible address.
An existing keypair can be restored in two forms:
- A 32-byte seed is treated as the private-key input and derives its public key.
- A 64-byte secret key contains 32 private-key bytes followed by 32 public-key bytes. The signer validates and uses that complete key material.
The keypair also exposes a WebCrypto CryptoKeyPair for Kit APIs. Its transaction and message signer interfaces let the same identity participate in transaction signing and in arbitrary message signing without converting the key into a different public API.
References: packages/…/keypair.ts• L16-42 packages/…/keypair.ts• L44-71 packages/…/keypair.ts• L74-101 packages/…/keypair.ts• L113-133
Key material boundaries
Public and private material have different security roles. publicKey and address identify the account and can be shared. secretKey returns a new 64-byte copy that includes private bytes, so any code that reads it has obtained exportable signing material. The WebCrypto key pair provides the signing capability, but the private bytes kept for secret-key compatibility remain sensitive as well.
Callers should keep seeds, secret keys, and exported private-key bytes out of logs, URLs, errors, source control, and untrusted application state. Passing a public key to a verifier is safe; passing a secret key to code that only needs an address creates unnecessary exposure.
References: packages/…/keypair.ts• L29-42 packages/…/keypair.ts• L83-110
Signature primitives
Message signing and verification
The Ed25519 primitives sign exact byte sequences. The signer uses private key material to produce a 64-byte signature, and the verifier checks that signature against the same message bytes and the matching 32-byte public key. The low-level signer builds a signing key from the supplied secret-key bytes, while verification imports the public key for a verify-only operation. A changed byte produces a different signed message and must be signed again.
The keypair API provides both message and transaction signing. signBytes signs raw bytes with the keypair's private key. verifySignature checks raw bytes with that keypair's public key. PublicKey.verifySignature and the lower-level Ed25519 utility provide verification when the verifier has a public key but does not hold the signing key.
Verification returns a Boolean result. It proves that the signature matches the supplied public key and bytes; it does not by itself prove that the bytes represent an approved transaction or that the signer is authorized for a particular account.
References: packages/…/keypair.ts• L134-157 packages/…/publickey.ts• L138-162 packages/…/ed25519.ts• L22-49
Transaction signature flow
Transactions sign the serialized message, not an informal description of the requested action. The transaction message defines which public keys must sign. Signing finds each supplied signer by public-key equality, then stores the resulting signature in that key's required position. A transaction can also accept a signature made outside the transaction object when the public key is one of its required signers.
The flow below shows how key identity connects message bytes to a checked transaction.
flowchart LR
A[Transaction message] --> B[Serialize message bytes]
C[Signer keypair] --> D[Ed25519 sign]
B --> D
D --> E[64-byte signature]
C --> F[Public key identity]
E --> G[Signature slot for F]
F --> G
G --> H[Verify signature over message bytes]
H --> I[Serialized transaction]
Legacy transactions can hold missing signatures while a transaction is assembled. Normal serialization requires all signatures and verifies each present signature. Versioned transactions keep one signature slot for each required signer and reject signers or externally supplied signatures that do not match the message's required public keys.
References: src/…/legacy.ts• L647-728 src/…/legacy.ts• L730-832 src/…/versioned.ts• L18-39 src/…/versioned.ts• L94-125
Signature verification
Verification answers two separate questions: whether a signature matches the message and public key, and whether every required signer supplied a signature. The legacy transaction API exposes both checks through verifySignatures, which can allow a partial set when the caller explicitly chooses that mode. Serialization uses strict checks by default so invalid or missing signatures do not silently reach the wire.
Changing any signed message field after signing changes the bytes being checked. The signer must therefore sign the final message, including its current account keys and recent blockhash, before the transaction is sent.
References: src/…/legacy.ts• L599-604 src/…/legacy.ts• L760-832 src/…/versioned.ts• L43-76
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public key byte order consistent between address conversion, ordinary byte access, and Borsh encoding.
- Keep 32-byte seed handling distinct from 64-byte secret-key restoration. Do not change the private/public byte layout without updating all export and restore paths.
- Keep program-derived address rules aligned with the seed count, seed length, off-curve check, and bump search used by programs and account clients. See Programs and instructions.
- Keep raw message signing and transaction message signing tied to the exact bytes that the verifier will receive.
- Preserve the boundary between public address data and exportable private key material.
- Keep the required-signer order consistent between message compilation, transaction signature slots, and external signature insertion.
- Keep message serialization and signature verification aligned. A change to any signed field changes the bytes that the signature covers.
- Preserve the difference between partial verification during assembly and strict verification during serialization.
- Keep legacy and versioned transaction rules consistent with their different signature-slot models.
- Keep public-key identity indexing consistent between compiled messages and transaction signer lookup.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A public key is encoded or decoded with the wrong byte order | Account data resolves to a different key | Use the public key's matching Borsh or address conversion path |
| A seed or PDA input exceeds its limit | Address derivation fails | Check the seed count and length before deriving |
| A PDA candidate lies on the Ed25519 curve | The candidate is rejected as a program address | Use the bump-search path and persist the selected bump |
| A transaction field changes after signing | Verification fails because the signature covers different message bytes | Sign only after the message is complete and re-sign after any change |
| A signer is not a required transaction signer | The signature cannot be placed in a valid slot | Build the signer set from the transaction's fee payer and signer accounts |
| A secret key is logged or copied into untrusted state | Anyone with the bytes can sign as the account | Keep seeds and secret-key bytes in protected memory and export them only when required |
| A required signature is missing or invalid at serialization | The transaction is rejected locally instead of being sent in its required form | Use the default full-signature and verification checks |
| An externally supplied signature uses the wrong public key | The signature does not match a required signature slot | Match the supplied public key to the message's required signer identities |
Key files
| File | Why to read it |
|---|---|
| packages/…/publickey.ts | Defines public key input, address and byte forms, signature verification, and seed-based or program-derived addresses. |
| packages/…/keypair.ts | Defines keypair generation, restoration, secret-key exposure, and message or transaction signing. |
| packages/…/ed25519.ts | Provides the curve-membership check and the raw Ed25519 signing and verification boundary. |
| src/…/legacy.ts | Shows how message bytes, required signers, signatures, and verification join in legacy transactions. |
| src/…/versioned.ts | Shows the corresponding signature-slot rules for versioned transactions. |
| src/…/account-keys.ts | Shows how public keys become the indexed identities used by compiled messages. |