Skip to content

Commit

Permalink
Create DHTRecordInfo struct for storage in protected store
Browse files Browse the repository at this point in the history
  • Loading branch information
tripledoublev committed Oct 2, 2024
1 parent 631f2fa commit 0953fc6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ impl CommonKeypair {
Ok(retrieved_keypair)
}
}
#[derive(Debug)]
#[derive(Serialize, Deserialize)]
pub struct DHTRecordInfo {
pub id: CryptoKey,
pub dht_key: CryptoKey,
pub cid: Option<String>, // CID of the collection
}

impl DHTRecordInfo {
pub async fn store(&self, protected_store: &ProtectedStore) -> Result<()> {
let record_data = serde_cbor::to_vec(&self).map_err(|e| anyhow!("Failed to serialize DHT record info: {}", e))?;
protected_store.save_user_secret(self.id.to_string(), &record_data).await.map_err(|e| anyhow!("Unable to store DHT record info: {}", e))?;
Ok(())
}

pub async fn load(protected_store: &ProtectedStore, id: &CryptoKey) -> Result<Self> {
let record_data = protected_store.load_user_secret(id.to_string()).await.map_err(|_| anyhow!("Failed to load DHT record info"))?.ok_or_else(|| anyhow!("DHT record info not found"))?;
let retrieved_info: DHTRecordInfo = serde_cbor::from_slice(&record_data).map_err(|_| anyhow!("Failed to deserialize DHT record info"))?;
Ok(retrieved_info)
}
}


pub trait DHTEntity {
Expand Down

0 comments on commit 0953fc6

Please sign in to comment.