This example demonstrates how to generate new cryptographic key pairs for use in a Tashi Vertex node. Each node requires a secret key to sign transactions and participate in the network.
Overview
The key-generate example shows the simplest way to create a new key pair:
- Generate a random secret key using
KeySecret::generate()
- Derive the corresponding public key
- Display both keys in Base58-encoded format
Complete example
use tashi_vertex::KeySecret;
fn main() {
// generate a new secret key to use for this node when signing transactions
let secret = KeySecret::generate();
let public = secret.public();
println!("Secret: {secret}");
println!("Public: {public}");
}
How it works
Generate secret key
Call KeySecret::generate() to create a new random secret key. This key will be used to sign all transactions from your node.let secret = KeySecret::generate();
Derive public key
Extract the public key from the secret key using the public() method. This public key identifies your node to other peers.let public = secret.public();
Display keys
Both keys implement Display and are automatically encoded in Base58 format when printed.println!("Secret: {secret}");
println!("Public: {public}");
Example output
When you run this example, you’ll see output similar to:
Secret: 3yZe7d2vK9mN8pQ5rT6wX2aB4cD1eF7gH9jK3mN6pQ8rS5tV7wY9zA2bC4dE6fG
Public: 7wY9zA2bC4dE6fG8hJ1kL3mN5pQ7rS9tV2wX4yZ6aB8cD1eF3gH5jK7mN9pQ2rS
Each time you run the example, you’ll get a different key pair. Store your secret key securely - it cannot be recovered if lost.
Parsing existing keys
If you already have a key and want to parse it, use the key-parse example:
use clap::Parser;
use tashi_vertex::{KeyPublic, KeySecret};
#[derive(Parser)]
struct Args {
#[clap(long)]
secret: Option<String>,
#[clap(long)]
public: Option<String>,
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
if let Some(secret) = args.secret {
// Decode and parse the secret key from DER encoded Base58 string
let secret: KeySecret = secret.parse()?;
let public = secret.public();
println!("Secret: {secret}");
println!("Public: {public}");
} else if let Some(public) = args.public {
let public: KeyPublic = public.parse()?;
println!("Public: {public}");
} else {
println!("Usage: key-parse --secret KEY\n");
println!(" or: key-parse --public KEY\n");
}
Ok(())
}
Usage
Parse secret key
Parse public key
cargo run --example key-parse -- --secret "3yZe7d2vK9mN8pQ5rT6wX2aB4cD1eF7gH9jK3mN6pQ8rS5tV7wY9zA2bC4dE6fG"
This will parse the secret key and display both the secret and derived public key.cargo run --example key-parse -- --public "7wY9zA2bC4dE6fG8hJ1kL3mN5pQ7rS9tV2wX4yZ6aB8cD1eF3gH5jK7mN9pQ2rS"
This will parse and validate the public key format.
Both KeySecret and KeyPublic types:
- Are encoded in Base58 format when displayed or serialized
- Can be parsed from Base58 strings using the
parse() method
- Use DER encoding internally
- Implement standard traits like
Display, FromStr, and Debug
Next steps
Pingback network
Use your keys to participate in a network
Transaction handling
Learn how to send and receive transactions