|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +mod errors; |
| 4 | +mod events; |
| 5 | +mod storage; |
| 6 | +#[cfg(test)] |
| 7 | +mod test; |
| 8 | + |
| 9 | +use soroban_sdk::{contract, contractimpl, xdr::ToXdr, Address, Bytes, BytesN, Env}; |
| 10 | + |
| 11 | +pub use errors::Error; |
| 12 | +pub use events::AccountDeployed; |
| 13 | +pub use storage::DataKey; |
| 14 | + |
| 15 | +#[contract] |
| 16 | +pub struct AccountFactoryContract; |
| 17 | + |
| 18 | +#[contractimpl] |
| 19 | +impl AccountFactoryContract { |
| 20 | + /// Initialize the factory with admin and ephemeral account wasm hash. |
| 21 | + pub fn initialize( |
| 22 | + env: Env, |
| 23 | + admin: Address, |
| 24 | + ephemeral_wasm_hash: BytesN<32>, |
| 25 | + ) -> Result<(), Error> { |
| 26 | + if storage::has_admin(&env) { |
| 27 | + return Err(Error::AlreadyInitialized); |
| 28 | + } |
| 29 | + |
| 30 | + admin.require_auth(); |
| 31 | + |
| 32 | + storage::set_admin(&env, &admin); |
| 33 | + storage::set_ephemeral_wasm_hash(&env, &ephemeral_wasm_hash); |
| 34 | + |
| 35 | + Ok(()) |
| 36 | + } |
| 37 | + |
| 38 | + /// Deploy and initialize a new ephemeral account contract instance. |
| 39 | + pub fn create_account( |
| 40 | + env: Env, |
| 41 | + creator: Address, |
| 42 | + expiry_ledger: u32, |
| 43 | + recovery_address: Address, |
| 44 | + native_transfer_address: Address, |
| 45 | + claim_verifier_address: Address, |
| 46 | + ) -> Result<Address, Error> { |
| 47 | + let wasm_hash = storage::get_ephemeral_wasm_hash(&env).ok_or(Error::NotInitialized)?; |
| 48 | + |
| 49 | + creator.require_auth(); |
| 50 | + |
| 51 | + let salt = Self::account_salt(&env, &creator, expiry_ledger); |
| 52 | + let deployed_address = env |
| 53 | + .deployer() |
| 54 | + .with_current_contract(salt) |
| 55 | + .deploy_v2(wasm_hash, ()); |
| 56 | + |
| 57 | + let account_client = |
| 58 | + ephemeral_account::EphemeralAccountContractClient::new(&env, &deployed_address); |
| 59 | + account_client.initialize( |
| 60 | + &creator, |
| 61 | + &expiry_ledger, |
| 62 | + &recovery_address, |
| 63 | + &native_transfer_address, |
| 64 | + &claim_verifier_address, |
| 65 | + ); |
| 66 | + |
| 67 | + storage::add_deployed_account(&env, &creator, &deployed_address); |
| 68 | + events::emit_account_deployed(&env, deployed_address.clone(), creator, expiry_ledger); |
| 69 | + |
| 70 | + Ok(deployed_address) |
| 71 | + } |
| 72 | + |
| 73 | + /// Get all accounts deployed by a specific creator. |
| 74 | + pub fn get_accounts_by_creator(env: Env, creator: Address) -> soroban_sdk::Vec<Address> { |
| 75 | + storage::get_accounts_by_creator(&env, &creator) |
| 76 | + } |
| 77 | + |
| 78 | + /// Get all deployed accounts. |
| 79 | + pub fn get_all_accounts(env: Env) -> soroban_sdk::Vec<Address> { |
| 80 | + storage::get_all_accounts(&env) |
| 81 | + } |
| 82 | + |
| 83 | + fn account_salt(env: &Env, creator: &Address, expiry_ledger: u32) -> BytesN<32> { |
| 84 | + let mut salt_preimage = Bytes::new(env); |
| 85 | + salt_preimage.append(&creator.to_xdr(env)); |
| 86 | + salt_preimage.append(&expiry_ledger.to_xdr(env)); |
| 87 | + salt_preimage.append(&env.ledger().sequence().to_xdr(env)); |
| 88 | + |
| 89 | + env.crypto().sha256(&salt_preimage).into() |
| 90 | + } |
| 91 | +} |
0 commit comments