-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaccount_management.rs
More file actions
120 lines (95 loc) · 4.77 KB
/
Copy pathaccount_management.rs
File metadata and controls
120 lines (95 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Example: Account Management and Key Derivation
//!
//! This example demonstrates how to:
//! 1. Generate random accounts
//! 2. Create accounts from mnemonic phrases (BIP-39)
//! 3. Derive multiple accounts from a single mnemonic
//! 4. Load accounts from private keys
//!
//! Run with: `cargo run --example account_management --features "ed25519,secp256k1,mnemonic,faucet"`
use aptos_sdk::{
Aptos, AptosConfig,
account::{Ed25519Account, Secp256k1Account},
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("=== Account Management Example ===\n");
// 1. Generate a random Ed25519 account
println!("--- 1. Random Account Generation ---");
let random_account = Ed25519Account::generate();
println!("Random Ed25519 Account:");
println!(" Address: {}", random_account.address());
println!(" Public Key: {}", random_account.public_key());
// 2. Generate different key types
println!("\n--- 2. Different Key Types ---");
let secp_account = Secp256k1Account::generate();
println!("Random Secp256k1 Account:");
println!(" Address: {}", secp_account.address());
println!(" Public Key: {}", secp_account.public_key());
// 3. Create account with mnemonic
println!("\n--- 3. Mnemonic-Based Account ---");
// Generate account with a new mnemonic
let (mnemonic_account, mnemonic_phrase) = Ed25519Account::generate_with_mnemonic()?;
println!("Generated 24-word mnemonic:");
println!(" {mnemonic_phrase}");
println!("\n ⚠️ IMPORTANT: Store this phrase securely!");
println!("\nDerived Account (index 0):");
println!(" Address: {}", mnemonic_account.address());
// 4. Derive multiple accounts from same mnemonic
println!("\n--- 4. Multiple Account Derivation ---");
println!("Deriving accounts at different indices from the same mnemonic:\n");
for i in 0..5 {
let account = Ed25519Account::from_mnemonic(&mnemonic_phrase, i)?;
println!(" Account {}: {}", i, account.address());
}
// 5. Load account from existing mnemonic
println!("\n--- 5. Loading from Known Mnemonic ---");
// Standard test mnemonic (DO NOT use for real funds!)
let test_phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
println!("Test mnemonic: {test_phrase}");
let test_account = Ed25519Account::from_mnemonic(test_phrase, 0)?;
println!("Derived address: {}", test_account.address());
println!("\n⚠️ This is a well-known test mnemonic. Never use it for real funds!");
// 6. Load account from private key hex
println!("\n--- 6. Loading from Private Key ---");
// Get the private key bytes and convert to hex
// WARNING: This prints the private key for demonstration purposes ONLY.
// NEVER print, log, or expose private keys in production code!
let pk_bytes = random_account.private_key().to_bytes();
let pk_hex = const_hex::encode(pk_bytes);
println!("[DEMO ONLY - NEVER DO THIS IN PRODUCTION] Private key (hex): {pk_hex}");
// Recreate account from private key hex
let restored_account = Ed25519Account::from_private_key_hex(&pk_hex)?;
println!("Restored account address: {}", restored_account.address());
// Verify they match
assert_eq!(random_account.address(), restored_account.address());
println!("✓ Addresses match! Account successfully restored from private key.");
// 7. Demonstrate using an account on devnet
println!("\n--- 7. Using an Account on Devnet ---");
let config = AptosConfig::devnet();
let aptos = Aptos::new(config)?;
// Generate fresh account for devnet
let devnet_account = Ed25519Account::generate();
println!("New devnet account: {}", devnet_account.address());
// Fund it
println!("Funding account...");
aptos
.fund_account(devnet_account.address(), 100_000_000)
.await?;
// Wait for funding
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
// Check balance
let balance = aptos.get_balance(devnet_account.address()).await?;
println!("Balance: {} APT", balance as f64 / 100_000_000.0);
// 8. Summary of key concepts
println!("\n--- Key Concepts Summary ---");
println!("• Ed25519: Fast, widely-used signature scheme (default for Aptos)");
println!("• Secp256k1: Bitcoin/Ethereum compatible signatures");
println!("• Mnemonic: Human-readable backup of your keys (BIP-39)");
println!("• Index: Allows multiple accounts from one mnemonic");
println!("• Private Key: The secret that controls your account");
println!("• Public Key: Safely shareable, used to derive address");
println!("• Address: Your account identifier on the blockchain");
println!("\n=== Account Management Example Complete ===");
Ok(())
}