ndb_bridge.rs (2608B)
1 use crate::error::RadrootsNostrAccountsError; 2 use crate::manager::RadrootsNostrAccountsManager; 3 use radroots_nostr_ndb::prelude::RadrootsNostrNdb; 4 5 pub fn radroots_nostr_accounts_register_default_secret_with_ndb( 6 manager: &RadrootsNostrAccountsManager, 7 ndb: &RadrootsNostrNdb, 8 ) -> Result<bool, RadrootsNostrAccountsError> { 9 let Some(signer) = manager.default_signer_capability()? else { 10 return Ok(false); 11 }; 12 let Some(identity) = manager.resolve_signing_identity_for_signer(&signer)? else { 13 return Ok(false); 14 }; 15 Ok(ndb.add_giftwrap_secret_key(identity.secret_key_bytes())) 16 } 17 18 #[cfg(test)] 19 mod tests { 20 use super::*; 21 use crate::store::RadrootsNostrFileAccountStore; 22 use crate::vault::RadrootsNostrSecretVaultMemory; 23 use radroots_nostr_ndb::prelude::RadrootsNostrNdbConfig; 24 use std::sync::Arc; 25 26 #[test] 27 fn register_default_secret_returns_true_for_signing_account() { 28 let temp = tempfile::tempdir().expect("tempdir"); 29 let store = Arc::new(RadrootsNostrFileAccountStore::new( 30 temp.path().join("accounts.json"), 31 )); 32 let vault = Arc::new(RadrootsNostrSecretVaultMemory::new()); 33 let manager = RadrootsNostrAccountsManager::new(store, vault).expect("manager"); 34 manager 35 .generate_identity(Some("primary".into()), true) 36 .expect("generate"); 37 38 let ndb = RadrootsNostrNdb::open(RadrootsNostrNdbConfig::new(temp.path().join("ndb"))) 39 .expect("ndb"); 40 let added = radroots_nostr_accounts_register_default_secret_with_ndb(&manager, &ndb) 41 .expect("register"); 42 assert!(added); 43 } 44 45 #[test] 46 fn register_default_secret_returns_false_for_watch_only_account() { 47 let temp = tempfile::tempdir().expect("tempdir"); 48 let store = Arc::new(RadrootsNostrFileAccountStore::new( 49 temp.path().join("accounts.json"), 50 )); 51 let vault = Arc::new(RadrootsNostrSecretVaultMemory::new()); 52 let manager = RadrootsNostrAccountsManager::new(store, vault).expect("manager"); 53 manager 54 .upsert_public_identity( 55 radroots_identity::RadrootsIdentity::generate().to_public(), 56 Some("watch".into()), 57 true, 58 ) 59 .expect("watch"); 60 61 let ndb = RadrootsNostrNdb::open(RadrootsNostrNdbConfig::new(temp.path().join("ndb"))) 62 .expect("ndb"); 63 let added = radroots_nostr_accounts_register_default_secret_with_ndb(&manager, &ndb) 64 .expect("register"); 65 assert!(!added); 66 } 67 }