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]);
Standard length of a Solana public key (32 bytes)
Maximum length of a seed for account derivation (32 bytes)
Default public key (11111111111111111111111111111111)
Factory Methods
From Base58 String
static PublicKey fromBase58Encoded(String base58)
Base58-encoded public key string
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-encoded public key string
From Bytes
static PublicKey createPubKey(byte[] publicKey)
static PublicKey readPubKey(byte[] bytes, int offset)
static PublicKey readPubKey(byte[] bytes)
Byte array containing public key
Offset to start reading from (default: 0)
Conversion Methods
Reference to internal byte array (not copied)
New copy of the byte array
Base58-encoded string representation
Base64-encoded string representation
java.security.PublicKey toJavaPublicKey()
Java security PublicKey for signature verification
Serialization
int write(byte[] out, int off)
Offset in destination array
Number of bytes written (always 32)
Length in bytes (always 32)
Signature Verification
boolean verifySignature(
byte[] msg,
int msgOffset,
int msgLength,
byte[] signature
)
Message bytes that were signed
64-byte Ed25519 signature
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
)
List of seed byte arrays (max 16 seeds, each max 32 bytes)
Program ID for derivation
Derived program address, or null if on curve
Find Program Address
static ProgramDerivedAddress findProgramAddress(
List<byte[]> seeds,
PublicKey programId
)
Program ID for derivation
PDA with public key and nonce (255 down to 0)
Create With Seed
static PublicKey createWithSeed(
PublicKey base,
String seed,
PublicKey programId
)
ASCII seed string (max 32 bytes)
Program ID for derivation
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