SolanaPrivateKey.fromBytes constructor
Factory method to create a SolanaPrivateKey instance from bytes representing a keypair.
Implementation
factory SolanaPrivateKey.fromBytes(List<int> keypairBytes) {
// Check if the byte length matches the expected length of a Solana keypair.
if (keypairBytes.length !=
(Ed25519KeysConst.privKeyByteLen + Ed25519KeysConst.pubKeyByteLen)) {
throw MessageException(
"Invalid Solana keypair length. A valid keypair must consist of exactly 64 bytes, combining both the seed and public key components.",
details: {"length": keypairBytes.length});
}
// Extract seed bytes and public bytes from the keypair bytes.
final seedBytes = keypairBytes.sublist(0, Ed25519KeysConst.privKeyByteLen);
final publicBytes = keypairBytes.sublist(Ed25519KeysConst.privKeyByteLen);
// Create a private key from the seed bytes.
final privateKey = Ed25519PrivateKey.fromBytes(seedBytes);
// Check if the extracted public bytes match the public key derived from the private key.
if (!BytesUtils.bytesEqual(
privateKey.publicKey.compressed
.sublist(Ed25519KeysConst.pubKeyPrefix.length),
publicBytes)) {
throw const MessageException("Invalid keypair");
}
return SolanaPrivateKey._(privateKey);
}