Skip to main content

PublicKey Interface

software.sava.core.accounts.PublicKey Represents a 32-byte Ed25519 public key used as a Solana account address.

Constants

int PUBLIC_KEY_LENGTH = 32;
int MAX_SEED_LENGTH = 32;
int MAX_SEEDS = 16;
PublicKey NONE = new PublicKeyBytes(new byte[PUBLIC_KEY_LENGTH]);
PUBLIC_KEY_LENGTH
int
Standard length of a Solana public key (32 bytes)
MAX_SEED_LENGTH
int
Maximum length of a seed for account derivation (32 bytes)
NONE
PublicKey
Default public key (11111111111111111111111111111111)

Factory Methods

From Base58 String

static PublicKey fromBase58Encoded(String base58)
base58
String
required
Base58-encoded public key string
return
PublicKey
Decoded PublicKey instance
static PublicKey fromBase58Encoded(char[] base58)
static PublicKey fromBase58Encoded(char[] base58, int from, int len)

From Base64 String

static PublicKey fromBase64Encoded(String base64)
base64
String
required
Base64-encoded public key string

From Bytes

static PublicKey createPubKey(byte[] publicKey)
publicKey
byte[]
required
32-byte public key array
static PublicKey readPubKey(byte[] bytes, int offset)
static PublicKey readPubKey(byte[] bytes)
bytes
byte[]
required
Byte array containing public key
offset
int
Offset to start reading from (default: 0)

Conversion Methods

byte[] toByteArray()
return
byte[]
Reference to internal byte array (not copied)
byte[] copyByteArray()
return
byte[]
New copy of the byte array
String toBase58()
return
String
Base58-encoded string representation
String toBase64()
return
String
Base64-encoded string representation
java.security.PublicKey toJavaPublicKey()
return
java.security.PublicKey
Java security PublicKey for signature verification

Serialization

int write(byte[] out, int off)
out
byte[]
required
Destination byte array
off
int
required
Offset in destination array
return
int
Number of bytes written (always 32)
int l()
return
int
Length in bytes (always 32)

Signature Verification

boolean verifySignature(
    byte[] msg,
    int msgOffset,
    int msgLength,
    byte[] signature
)
msg
byte[]
required
Message bytes that were signed
msgOffset
int
required
Offset in message array
msgLength
int
required
Length of message
signature
byte[]
required
64-byte Ed25519 signature
return
boolean
True if signature is valid
boolean verifySignature(byte[] msg, byte[] signature)
boolean verifySignature(String msg, byte[] signature)
boolean verifySignature(String msg, String signature)

Account Derivation

Create Program Address

static PublicKey createProgramAddress(
    List<byte[]> seeds,
    PublicKey programId
)
seeds
List<byte[]>
required
List of seed byte arrays (max 16 seeds, each max 32 bytes)
programId
PublicKey
required
Program ID for derivation
return
PublicKey
Derived program address, or null if on curve

Find Program Address

static ProgramDerivedAddress findProgramAddress(
    List<byte[]> seeds,
    PublicKey programId
)
seeds
List<byte[]>
required
List of seed byte arrays
programId
PublicKey
required
Program ID for derivation
return
ProgramDerivedAddress
PDA with public key and nonce (255 down to 0)

Create With Seed

static PublicKey createWithSeed(
    PublicKey base,
    String seed,
    PublicKey programId
)
base
PublicKey
required
Base public key
seed
String
required
ASCII seed string (max 32 bytes)
programId
PublicKey
required
Program ID for derivation
return
PublicKey
Derived account address

Static Utilities

static java.security.PublicKey toJavaPublicKey(byte[] publicKey)
static java.security.PublicKey toJavaPublicKey(
    byte[] publicKey,
    int off,
    int len
)
static boolean verifySignature(
    java.security.PublicKey publicKey,
    byte[] msg,
    byte[] signature
)
static boolean verifySignature(
    byte[] publicKey,
    int publicKeyOffset,
    byte[] msg,
    int msgOffset,
    int msgLength,
    byte[] signature
)

Example Usage

import software.sava.core.accounts.PublicKey;
import java.util.List;

// Create from base58
var pubKey = PublicKey.fromBase58Encoded(
    "11111111111111111111111111111111"
);

// Convert to different formats
String base58 = pubKey.toBase58();
String base64 = pubKey.toBase64();
byte[] bytes = pubKey.toByteArray();

// Verify signature
boolean valid = pubKey.verifySignature(messageBytes, signatureBytes);

// Find program derived address
var seeds = List.of(
    "metadata".getBytes(),
    programId.toByteArray(),
    mint.toByteArray()
);
var pda = PublicKey.findProgramAddress(seeds, programId);
var derivedKey = pda.publicKey();
var nonce = pda.nonce();

// Create account with seed
var derived = PublicKey.createWithSeed(
    baseKey,
    "my-seed",
    programId
);

PublicKeyBytes Implementation

software.sava.core.accounts.PublicKeyBytes Default implementation of the PublicKey interface backed by a byte array.

Features

  • Implements Comparable<PublicKey> for sorting
  • Efficient byte array storage
  • Cached hash code for performance
  • Immutable after construction

Build docs developers (and LLMs) love