|
| 1 | +//! Account Creation Example |
| 2 | +//! |
| 3 | +//! This example demonstrates creating accounts from seeds. |
| 4 | +//! This is a basic building block for certificate creation. |
| 5 | +
|
| 6 | +use accounts::{Account, Accountable, KeyPair, Keyable}; |
| 7 | +use crypto::prelude::IntoSecret; |
| 8 | + |
| 9 | +const TEST_SEED: &str = "D6986115BE7334E50DA8D73B1A4670A510E8BF47E8C5C9960B8F5248EC7D6E3D"; |
| 10 | + |
| 11 | +/// Main function to demonstrate account creation |
| 12 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 13 | + // Step 1: Create an issuer account from a seed |
| 14 | + let issuer_account = create_account_from_seed(TEST_SEED, 0)?; |
| 15 | + let public_key = issuer_account.keypair.to_public_key_string(); |
| 16 | + |
| 17 | + println!("Public key: {public_key}"); |
| 18 | + |
| 19 | + // Step 2: Create a subject account (different index) |
| 20 | + let subject_account = create_account_from_seed(TEST_SEED, 1)?; |
| 21 | + let subject_public_key = subject_account.keypair.to_public_key_string(); |
| 22 | + println!("Public key: {subject_public_key}"); |
| 23 | + |
| 24 | + Ok(()) |
| 25 | +} |
| 26 | + |
| 27 | +/// Helper function to create an account from a hex seed string |
| 28 | +fn create_account_from_seed( |
| 29 | + seed_hex: &str, |
| 30 | + index: u32, |
| 31 | +) -> Result<Account<accounts::KeyECDSASECP256K1>, Box<dyn std::error::Error>> { |
| 32 | + // Use the HexSeed variant directly - much cleaner! |
| 33 | + let keyable = Keyable::HexSeed((seed_hex.to_string().into_secret(), index)); |
| 34 | + let accountable = Accountable::KeyAndType(keyable, accounts::KeyECDSASECP256K1::KEY_PAIR_TYPE); |
| 35 | + let account = Account::<accounts::KeyECDSASECP256K1>::try_from(accountable)?; |
| 36 | + |
| 37 | + Ok(account) |
| 38 | +} |
0 commit comments