Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/provablehq/snarkvm/llms.txt

Use this file to discover all available pages before exploring further.

This is the simplest possible SnarkVM example. It demonstrates account creation, key derivation, and basic operations with the Aleo blockchain.

Complete example

use snarkvm::prelude::*;
use rand::thread_rng;

fn main() -> Result<()> {
    // Generate a new private key
    let private_key = PrivateKey::<Testnet3>::new(&mut thread_rng())?;
    
    // Derive the view key and address
    let view_key = ViewKey::try_from(&private_key)?;
    let address = Address::try_from(&private_key)?;
    
    // Print the account information
    println!("Welcome to SnarkVM!");
    println!("==================");
    println!("Private Key: {}", private_key);
    println!("View Key:    {}", view_key);
    println!("Address:     {}", address);
    
    Ok(())
}

Step by step

Let’s break down what’s happening:
1

Import the prelude

use snarkvm::prelude::*;
use rand::thread_rng;
The prelude provides all commonly used types. We also import thread_rng for random number generation.
2

Generate a private key

let private_key = PrivateKey::<Testnet3>::new(&mut thread_rng())?;
Creates a new cryptographically secure private key for the Testnet3 network. The private key is the root secret that controls the account.
3

Derive the view key

let view_key = ViewKey::try_from(&private_key)?;
The view key is derived from the private key and can decrypt records without the ability to spend them.
4

Derive the address

let address = Address::try_from(&private_key)?;
The address is the public identifier for the account. It’s safe to share and is used to receive transactions.
5

Display the results

println!("Address: {}", address);
Print the account information. The address will start with aleo1 for all networks.

Expected output

When you run this example, you’ll see output like:
Welcome to SnarkVM!
==================
Private Key: APrivateKey1zkp...
View Key:    AViewKey1...
Address:     aleo1...
The private key is sensitive information. Never share it or commit it to version control.

Variations

Using different networks

You can use MainnetV0 or CanaryV0 instead of Testnet3:
// For mainnet
let private_key = PrivateKey::<MainnetV0>::new(&mut thread_rng())?;

// For canary network
let private_key = PrivateKey::<CanaryV0>::new(&mut thread_rng())?;

Restoring from an existing private key

If you already have a private key string:
let private_key = PrivateKey::<Testnet3>::from_str(
    "APrivateKey1zkp..."
)?;
let address = Address::try_from(&private_key)?;

Next steps

Token transfer

Learn how to transfer credits between accounts

Working with accounts

Deep dive into account management

Build docs developers (and LLMs) love