KeySecret represents an Ed25519 secret key used for signing transactions in Tashi Vertex. Keys can be generated, serialized to Base58-encoded DER format, and used to derive their corresponding public keys.
Methods
generate
pub fn generate() -> Self
Generates a new secret key suitable for signing transactions.
This function creates a cryptographically secure random Ed25519 secret key. Each generated key is unique and suitable for production use.
Returns
A new KeySecret instance.
Example
use tashi_vertex::KeySecret;
let secret = KeySecret::generate();
println!("Secret key: {}", secret); // Base58-encoded DER format
public
pub fn public(&self) -> KeyPublic
Derives the corresponding public key from this secret key.
Returns
A KeyPublic instance that can be shared with other network participants.
Example
let secret = KeySecret::generate();
let public = secret.public();
println!("Public key: {}", public);
from_der
pub fn from_der(der: &[u8]) -> crate::Result<Self>
Parses a secret key from DER-encoded bytes.
DER-encoded secret key bytes (51 bytes for Ed25519 keys)
Returns
Ok(KeySecret) if parsing succeeds
Err(Error) if the DER format is invalid
Example
let der_bytes: &[u8] = &[/* 51 bytes of DER-encoded key */];
let secret = KeySecret::from_der(der_bytes)?;
to_der
pub fn to_der(&self, output: &mut [u8]) -> crate::Result<()>
Formats the secret key to DER format, writing to the provided output buffer.
Output buffer to write DER-encoded bytes (must be at least 51 bytes)
Returns
Ok(()) if serialization succeeds
Err(Error) if the output buffer is too small
to_der_vec
pub fn to_der_vec(&self) -> crate::Result<Vec<u8>>
Formats the secret key to DER format as a newly allocated vector.
Returns
Ok(Vec<u8>) containing 51 bytes of DER-encoded key data
Err(Error) if serialization fails
Example
let secret = KeySecret::generate();
let der_bytes = secret.to_der_vec()?;
assert_eq!(der_bytes.len(), 51);
Parsing from strings
KeySecret implements FromStr, allowing you to parse Base58-encoded keys directly:
use std::str::FromStr;
let secret_str = "aSq9DsNNvGhYPQA7q4E1izcs45NYgwqWvBEd...";
let secret = KeySecret::from_str(secret_str)?;
// Or using the parse() method
let secret: KeySecret = secret_str.parse()?;
KeySecret implements Display and Debug, formatting the key as a Base58-encoded DER string:
let secret = KeySecret::generate();
println!("Secret: {}", secret); // Base58-encoded DER (69 characters)
println!("Debug: {:?}", secret); // Quoted Base58 string
Secret keys should be kept secure and never shared publicly. Store them encrypted at rest and transmit them only over secure channels.
- Algorithm: Ed25519
- DER length: 51 bytes
- Base58 length: 69 characters
- Encoding: Base58-encoded DER format for string representation
See also
- KeyPublic - Ed25519 public key for verification
- Base58 - Encoding/decoding utilities