|
1 |
| -use dig_wallet::{Wallet, WalletError}; |
2 |
| -use datalayer_driver::NetworkType; |
3 |
| - |
4 |
| -#[tokio::main] |
5 |
| -async fn main() -> Result<(), WalletError> { |
6 |
| - println!("🚀 Dig Wallet Rust Example"); |
7 |
| - println!("==========================\n"); |
8 |
| - |
9 |
| - // 1. Create or load a wallet |
10 |
| - println!("📝 Loading/Creating wallet..."); |
11 |
| - let wallet = Wallet::load(Some("example_wallet".to_string()), true).await?; |
12 |
| - println!("✅ Wallet loaded successfully!\n"); |
13 |
| - |
14 |
| - // 2. Get wallet information |
15 |
| - println!("🔑 Wallet Information:"); |
16 |
| - let mnemonic = wallet.get_mnemonic()?; |
17 |
| - println!(" Mnemonic: {} words", mnemonic.split_whitespace().count()); |
18 |
| - |
19 |
| - let address = wallet.get_owner_public_key().await?; |
20 |
| - println!(" Address: {}", address); |
21 |
| - |
22 |
| - let puzzle_hash = wallet.get_owner_puzzle_hash().await?; |
23 |
| - println!(" Puzzle Hash: {}", hex::encode(puzzle_hash.as_ref())); |
24 |
| - println!(); |
25 |
| - |
26 |
| - // 3. Demonstrate address conversion |
27 |
| - println!("🔄 Address Conversion:"); |
28 |
| - let converted_puzzle_hash = Wallet::address_to_puzzle_hash(&address)?; |
29 |
| - let converted_address = Wallet::puzzle_hash_to_address(converted_puzzle_hash, "xch")?; |
30 |
| - println!(" Original Address: {}", address); |
31 |
| - println!(" Converted Back: {}", converted_address); |
32 |
| - println!(" Conversion Match: {}", address == converted_address); |
33 |
| - println!(); |
34 |
| - |
35 |
| - // 4. Create a signature |
36 |
| - println!("✍️ Digital Signature:"); |
37 |
| - let nonce = "example_nonce_12345"; |
38 |
| - let signature = wallet.create_key_ownership_signature(nonce).await?; |
39 |
| - println!(" Nonce: {}", nonce); |
40 |
| - println!(" Signature: {}...", &signature[..32]); |
41 |
| - |
42 |
| - // Verify the signature |
43 |
| - let public_key = wallet.get_public_synthetic_key().await?; |
44 |
| - let public_key_hex = hex::encode(public_key.to_bytes()); |
45 |
| - let is_valid = Wallet::verify_key_ownership_signature(nonce, &signature, &public_key_hex).await?; |
46 |
| - println!(" Signature Valid: {}", is_valid); |
47 |
| - println!(); |
48 |
| - |
49 |
| - // 5. List all wallets |
50 |
| - println!("📋 Available Wallets:"); |
51 |
| - let wallets = Wallet::list_wallets().await?; |
52 |
| - for wallet_name in wallets { |
53 |
| - println!(" - {}", wallet_name); |
54 |
| - } |
55 |
| - println!(); |
56 |
| - |
57 |
| - // 6. Demonstrate peer connection (commented out as it requires SSL certs) |
58 |
| - println!("🌐 Peer Connection:"); |
59 |
| - println!(" Note: Peer connection requires Chia SSL certificates."); |
60 |
| - println!(" Example methods available:"); |
61 |
| - println!(" - Wallet::connect_mainnet_peer()"); |
62 |
| - println!(" - Wallet::connect_testnet_peer()"); |
63 |
| - println!(" - Wallet::connect_random_peer(network, cert_path, key_path)"); |
64 |
| - |
65 |
| - /* |
66 |
| - // Uncomment this section if you have Chia SSL certificates set up |
67 |
| - println!(" Attempting to connect to mainnet..."); |
68 |
| - match Wallet::connect_mainnet_peer().await { |
69 |
| - Ok(_peer) => { |
70 |
| - println!(" ✅ Successfully connected to mainnet peer!"); |
71 |
| - |
72 |
| - // Example of using the peer for coin operations |
73 |
| - // let coins = wallet.select_unspent_coins(&peer, 1000000, 1000, vec![]).await?; |
74 |
| - // println!(" Found {} unspent coins", coins.len()); |
75 |
| - } |
76 |
| - Err(e) => { |
77 |
| - println!(" ⚠️ Failed to connect: {}", e); |
78 |
| - println!(" This is expected if Chia SSL certificates are not set up."); |
79 |
| - } |
80 |
| - } |
81 |
| - */ |
82 |
| - |
83 |
| - println!("\n🎉 Example completed successfully!"); |
84 |
| - println!(" The Rust wallet now has full feature parity with the TypeScript version!"); |
85 |
| - |
86 |
| - Ok(()) |
87 |
| -} |
| 1 | +use dig_wallet::{Wallet, WalletError}; |
| 2 | + |
| 3 | +#[tokio::main] |
| 4 | +async fn main() -> Result<(), WalletError> { |
| 5 | + println!("🚀 Dig Wallet Rust Example"); |
| 6 | + println!("==========================\n"); |
| 7 | + |
| 8 | + // 1. Create or load a wallet |
| 9 | + println!("📝 Loading/Creating wallet..."); |
| 10 | + let wallet = Wallet::load(Some("example_wallet".to_string()), true).await?; |
| 11 | + println!("✅ Wallet loaded successfully!\n"); |
| 12 | + |
| 13 | + // 2. Get wallet information |
| 14 | + println!("🔑 Wallet Information:"); |
| 15 | + let mnemonic = wallet.get_mnemonic()?; |
| 16 | + println!(" Mnemonic: {} words", mnemonic.split_whitespace().count()); |
| 17 | + |
| 18 | + let address = wallet.get_owner_public_key().await?; |
| 19 | + println!(" Address: {}", address); |
| 20 | + |
| 21 | + let puzzle_hash = wallet.get_owner_puzzle_hash().await?; |
| 22 | + println!(" Puzzle Hash: {}", hex::encode(puzzle_hash.as_ref())); |
| 23 | + println!(); |
| 24 | + |
| 25 | + // 3. Demonstrate address conversion |
| 26 | + println!("🔄 Address Conversion:"); |
| 27 | + let converted_puzzle_hash = Wallet::address_to_puzzle_hash(&address)?; |
| 28 | + let converted_address = Wallet::puzzle_hash_to_address(converted_puzzle_hash, "xch")?; |
| 29 | + println!(" Original Address: {}", address); |
| 30 | + println!(" Converted Back: {}", converted_address); |
| 31 | + println!(" Conversion Match: {}", address == converted_address); |
| 32 | + println!(); |
| 33 | + |
| 34 | + // 4. Create a signature |
| 35 | + println!("✍️ Digital Signature:"); |
| 36 | + let nonce = "example_nonce_12345"; |
| 37 | + let signature = wallet.create_key_ownership_signature(nonce).await?; |
| 38 | + println!(" Nonce: {}", nonce); |
| 39 | + println!(" Signature: {}...", &signature[..32]); |
| 40 | + |
| 41 | + // Verify the signature |
| 42 | + let public_key = wallet.get_public_synthetic_key().await?; |
| 43 | + let public_key_hex = hex::encode(public_key.to_bytes()); |
| 44 | + let is_valid = |
| 45 | + Wallet::verify_key_ownership_signature(nonce, &signature, &public_key_hex).await?; |
| 46 | + println!(" Signature Valid: {}", is_valid); |
| 47 | + println!(); |
| 48 | + |
| 49 | + // 5. List all wallets |
| 50 | + println!("📋 Available Wallets:"); |
| 51 | + let wallets = Wallet::list_wallets().await?; |
| 52 | + for wallet_name in wallets { |
| 53 | + println!(" - {}", wallet_name); |
| 54 | + } |
| 55 | + println!(); |
| 56 | + |
| 57 | + // 6. Demonstrate peer connection (commented out as it requires SSL certs) |
| 58 | + println!("🌐 Peer Connection:"); |
| 59 | + println!(" Note: Peer connection requires Chia SSL certificates."); |
| 60 | + println!(" Example methods available:"); |
| 61 | + println!(" - Wallet::connect_mainnet_peer()"); |
| 62 | + println!(" - Wallet::connect_testnet_peer()"); |
| 63 | + println!(" - Wallet::connect_random_peer(network, cert_path, key_path)"); |
| 64 | + |
| 65 | + /* |
| 66 | + // Uncomment this section if you have Chia SSL certificates set up |
| 67 | + println!(" Attempting to connect to mainnet..."); |
| 68 | + match Wallet::connect_mainnet_peer().await { |
| 69 | + Ok(_peer) => { |
| 70 | + println!(" ✅ Successfully connected to mainnet peer!"); |
| 71 | +
|
| 72 | + // Example of using the peer for coin operations |
| 73 | + // let coins = wallet.select_unspent_coins(&peer, 1000000, 1000, vec![]).await?; |
| 74 | + // println!(" Found {} unspent coins", coins.len()); |
| 75 | + } |
| 76 | + Err(e) => { |
| 77 | + println!(" ⚠️ Failed to connect: {}", e); |
| 78 | + println!(" This is expected if Chia SSL certificates are not set up."); |
| 79 | + } |
| 80 | + } |
| 81 | + */ |
| 82 | + |
| 83 | + println!("\n🎉 Example completed successfully!"); |
| 84 | + println!(" The Rust wallet now has full feature parity with the TypeScript version!"); |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
0 commit comments