Skip to content

Commit 5845726

Browse files
author
Mauve Signweaver
committed
Update to new veilid version
1 parent f5bfd52 commit 5845726

File tree

8 files changed

+99
-73
lines changed

8 files changed

+99
-73
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ edition = "2021"
66
[dependencies]
77
iroh = "0.24.0"
88
iroh-blobs = "0.24.0"
9-
veilid-core = { git = "https://gitlab.com/veilid/veilid.git", branch = "main" }
10-
veilid-iroh-blobs = { git = "https://github.com/RangerMauve/veilid-iroh-blobs.git", branch = "default" }
9+
veilid-core = { git = "https://gitlab.com/veilid/veilid.git", version = "0.4.3" }
10+
veilid-iroh-blobs = { git = "https://github.com/RangerMauve/veilid-iroh-blobs.git", version = "0.1.1" }
1111
tracing = "0.1"
1212
xdg = "2.4"
1313
tmpdir = "1"
1414
serde = "1.0.204"
1515
serde_cbor = "0.11.2"
1616
clap = { version = "4.5.9", features = ["derive"] }
1717
anyhow = "1.0.86"
18-
tokio = {version ="1.39.3", features=["full"] }
19-
tokio-stream = "0.1.16"
18+
tokio = {version ="1.38.1", features=["full"] }
19+
tokio-stream = "0.1.15"
2020
async-stream = "0.3.5"
2121
futures = "0.3.31"
2222
futures-core = "0.3.31"

src/backend.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use iroh_blobs::Hash;
1212
use serde::{Deserialize, Serialize};
1313
use std::collections::HashMap;
1414
use std::mem;
15+
use std::ops::Deref;
1516
use std::path::{Path, PathBuf};
1617
use std::sync::Arc;
1718
use tokio::fs;
@@ -56,9 +57,9 @@ impl BackendInner {
5657
println!("Saving group IDs {:?}", info);
5758
let data =
5859
serde_cbor::to_vec(&info).map_err(|e| anyhow!("Failed to serialize keypair: {}", e))?;
59-
self.get_protected_store()?
60+
self.veilid()?
61+
.protected_store()?
6062
.save_user_secret(KNOWN_GROUP_LIST, &data)
61-
.await
6263
.map_err(|e| anyhow!("Unable to store known group IDs: {}", e))?;
6364
Ok(())
6465
}
@@ -78,12 +79,6 @@ impl BackendInner {
7879
.ok_or_else(|| anyhow!("Veilid Iroh Blobs API not initialized"))?
7980
.clone())
8081
}
81-
82-
fn get_protected_store(&self) -> Result<ProtectedStore> {
83-
let veilid_api = self.veilid()?;
84-
let store = veilid_api.protected_store()?;
85-
Ok(store)
86-
}
8782
}
8883

8984
#[derive(Clone)]
@@ -300,7 +295,10 @@ impl Backend {
300295
let veilid = inner.veilid()?;
301296

302297
let routing_context = veilid.routing_context()?;
303-
let crypto_system = CryptoSystemVLD0::new(veilid.crypto()?);
298+
let crypto_system = veilid
299+
.crypto()?
300+
.get(CRYPTO_KIND_VLD0)
301+
.ok_or_else(|| anyhow!("Unable to init crypto system"));
304302

305303
let record_key = TypedKey::new(CRYPTO_KIND_VLD0, keys.id);
306304
// First open the DHT record
@@ -346,12 +344,18 @@ impl Backend {
346344
let veilid = inner.veilid()?;
347345

348346
let routing_context = veilid.routing_context()?;
347+
let crypto = veilid.crypto()?;
348+
let crypto_system = crypto
349+
.get(CRYPTO_KIND_VLD0)
350+
.ok_or_else(|| anyhow!("Unable to init crypto system"))?;
351+
349352
let schema = DHTSchema::dflt(65)?; // 64 members + a title
350353
let kind = Some(CRYPTO_KIND_VLD0);
354+
let owner_keypair = crypto_system.generate_keypair();
351355

352-
let dht_record = routing_context.create_dht_record(schema, kind).await?;
353-
let keypair = vld0_generate_keypair();
354-
let crypto_system = CryptoSystemVLD0::new(veilid.crypto()?);
356+
let dht_record = routing_context
357+
.create_dht_record(schema, Some(owner_keypair), kind)
358+
.await?;
355359

356360
let encryption_key = crypto_system.random_shared_secret();
357361

@@ -397,7 +401,10 @@ impl Backend {
397401
.await
398402
.map_err(|_| anyhow!("Failed to load keypair"))?;
399403

400-
let crypto_system = CryptoSystemVLD0::new(veilid.crypto()?);
404+
let crypto = veilid.crypto()?;
405+
let crypto_system = crypto
406+
.get(CRYPTO_KIND_VLD0)
407+
.ok_or_else(|| anyhow!("Unable to init crypto system"))?;
401408

402409
// Use the owner key from the DHT record as the default writer
403410
let owner_key = retrieved_keypair.public_key; // Call the owner() method to get the owner key
@@ -442,11 +449,11 @@ impl Backend {
442449
}
443450

444451
pub async fn list_known_group_ids(&self) -> Result<Vec<CryptoKey>> {
445-
let data = self
446-
.get_protected_store()
447-
.await?
452+
let mut inner = self.inner.lock().await;
453+
let veilid = inner.veilid()?;
454+
let data = veilid
455+
.protected_store()?
448456
.load_user_secret(KNOWN_GROUP_LIST)
449-
.await
450457
.map_err(|_| anyhow!("Failed to load keypair"))?
451458
.ok_or_else(|| anyhow!("Keypair not found"))?;
452459
let info: KnownGroupList =
@@ -464,13 +471,6 @@ impl Backend {
464471
Ok(())
465472
}
466473

467-
pub async fn get_protected_store(&self) -> Result<Arc<ProtectedStore>> {
468-
let mut inner = self.inner.lock().await;
469-
inner
470-
.veilid()
471-
.map(|api| Arc::new(api.protected_store().unwrap()))
472-
}
473-
474474
pub async fn create_collection(&self) -> Result<Hash> {
475475
// Initialize a new Iroh Node in memory
476476
let node = Node::memory().spawn().await?;

src/common.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,13 @@ impl CommonKeypair {
113113
serde_cbor::to_vec(&self).map_err(|e| anyhow!("Failed to serialize keypair: {}", e))?;
114114
protected_store
115115
.save_user_secret(self.id.to_string(), &keypair_data)
116-
.await
117116
.map_err(|e| anyhow!("Unable to store keypair: {}", e))?;
118117
Ok(())
119118
}
120119

121120
pub async fn load_keypair(protected_store: &ProtectedStore, id: &CryptoKey) -> Result<Self> {
122121
let keypair_data = protected_store
123122
.load_user_secret(id.to_string())
124-
.await
125123
.map_err(|_| anyhow!("Failed to load keypair"))?
126124
.ok_or_else(|| anyhow!("Keypair not found"))?;
127125
let retrieved_keypair: CommonKeypair = serde_cbor::from_slice(&keypair_data)
@@ -134,7 +132,7 @@ pub trait DHTEntity {
134132
fn get_id(&self) -> CryptoKey;
135133
fn get_encryption_key(&self) -> SharedSecret;
136134
fn get_routing_context(&self) -> RoutingContext;
137-
fn get_crypto_system(&self) -> CryptoSystemVLD0;
135+
fn get_veilid_api(&self) -> VeilidAPI;
138136
fn get_dht_record(&self) -> DHTRecordDescriptor;
139137
fn get_secret_key(&self) -> Option<CryptoKey>;
140138

@@ -149,25 +147,34 @@ pub trait DHTEntity {
149147
}
150148

151149
fn encrypt_aead(&self, data: &[u8], associated_data: Option<&[u8]>) -> Result<Vec<u8>> {
152-
let nonce = self.get_crypto_system().random_nonce();
150+
let veilid = self.get_veilid_api();
151+
let crypto = veilid.crypto()?;
152+
let crypto_system = crypto
153+
.get(CRYPTO_KIND_VLD0)
154+
.ok_or_else(|| anyhow!("Unable to init crypto system"))?;
155+
let nonce = crypto_system.random_nonce();
153156
let mut buffer = Vec::with_capacity(nonce.as_slice().len() + data.len());
154157
buffer.extend_from_slice(nonce.as_slice());
155-
buffer.extend_from_slice(
156-
&self
157-
.get_crypto_system()
158-
.encrypt_aead(data, &nonce, &self.get_encryption_key(), associated_data)
159-
.map_err(|e| anyhow!("Failed to encrypt data: {}", e))?,
160-
);
158+
let encrypted_chunk = crypto_system
159+
.encrypt_aead(data, &nonce, &self.get_encryption_key(), associated_data)
160+
.map_err(|e| anyhow!("Failed to encrypt data: {}", e))?;
161+
buffer.extend_from_slice(&encrypted_chunk);
161162
Ok(buffer)
162163
}
163164

164165
fn decrypt_aead(&self, data: &[u8], associated_data: Option<&[u8]>) -> Result<Vec<u8>> {
166+
let veilid = self.get_veilid_api();
167+
let crypto = veilid.crypto()?;
168+
let crypto_system = crypto
169+
.get(CRYPTO_KIND_VLD0)
170+
.ok_or_else(|| anyhow!("Unable to init crypto system"))?;
171+
165172
let nonce: [u8; 24] = data[..24]
166173
.try_into()
167174
.map_err(|_| anyhow!("Failed to convert nonce slice to array"))?;
168175
let nonce = Nonce::new(nonce);
169176
let encrypted_data = &data[24..];
170-
self.get_crypto_system()
177+
crypto_system
171178
.decrypt_aead(
172179
encrypted_data,
173180
&nonce,

src/group.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ impl Group {
279279
// ))
280280
let keypair = None;
281281

282+
let veilid = self.get_veilid_api();
283+
282284
let dht_record = self
283285
.routing_context
284286
.open_dht_record(repo_id.clone(), keypair)
@@ -289,7 +291,7 @@ impl Group {
289291
encryption_key: self.encryption_key.clone(),
290292
secret_key: None,
291293
routing_context: self.routing_context.clone(),
292-
crypto_system: self.get_crypto_system(),
294+
veilid: veilid.clone(),
293295
iroh_blobs: self.iroh_blobs.clone(),
294296
};
295297

@@ -358,7 +360,6 @@ impl Group {
358360

359361
let key_bytes = protected_store
360362
.load_user_secret(group_repo_key)
361-
.await
362363
.map_err(|err| anyhow!("Unable to load repo from disk"))?
363364
.ok_or_else(|| anyhow!("No repo exists on disk for this group"))?;
364365

@@ -383,14 +384,14 @@ impl Group {
383384
.secret_key
384385
.map(|key| TypedKey::new(CRYPTO_KIND_VLD0, key));
385386

386-
let repo = Repo {
387+
let repo = Repo::new(
387388
dht_record,
388-
encryption_key: self.encryption_key.clone(),
389+
self.encryption_key.clone(),
389390
secret_key,
390-
routing_context: self.routing_context.clone(),
391-
crypto_system: self.get_crypto_system(),
392-
iroh_blobs: self.iroh_blobs.clone(),
393-
};
391+
self.routing_context.clone(),
392+
self.veilid.clone(),
393+
self.iroh_blobs.clone(),
394+
);
394395
repo.update_route_on_dht().await?;
395396

396397
self.add_repo(repo.clone()).await?;
@@ -406,7 +407,10 @@ impl Group {
406407
// Create a new DHT record for the repo
407408
let schema = DHTSchema::dflt(3)?;
408409
let kind = Some(CRYPTO_KIND_VLD0);
409-
let repo_dht_record = self.routing_context.create_dht_record(schema, kind).await?;
410+
let repo_dht_record = self
411+
.routing_context
412+
.create_dht_record(schema, None, kind)
413+
.await?;
410414

411415
// Identify the repo with the DHT record's key
412416
let repo_id = repo_dht_record.key().clone();
@@ -423,7 +427,7 @@ impl Group {
423427
encryption_key,
424428
Some(secret_key_typed),
425429
self.routing_context.clone(),
426-
self.get_crypto_system(),
430+
self.veilid.clone(),
427431
self.iroh_blobs.clone(),
428432
);
429433

@@ -449,7 +453,6 @@ impl Group {
449453
let key_bytes = *repo.id();
450454
protected_store
451455
.save_user_secret(group_repo_key, key_bytes.as_slice())
452-
.await
453456
.map_err(|e| anyhow!("Unable to store repo id for group: {}", e))?;
454457

455458
self.add_repo(repo).await?;
@@ -547,9 +550,8 @@ impl DHTEntity for Group {
547550
self.routing_context.clone()
548551
}
549552

550-
fn get_crypto_system(&self) -> CryptoSystemVLD0 {
551-
// TODO handle the error?
552-
CryptoSystemVLD0::new(self.veilid.crypto().unwrap())
553+
fn get_veilid_api(&self) -> VeilidAPI {
554+
self.veilid.clone()
553555
}
554556

555557
fn get_dht_record(&self) -> DHTRecordDescriptor {

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![recursion_limit = "256"]
12
pub mod backend;
23
pub mod common;
34
pub mod constants;
@@ -158,10 +159,10 @@ mod tests {
158159

159160
let mut loaded_group = backend.get_group(&group.id()).await.expect(GROUP_NOT_FOUND);
160161

161-
let protected_store = backend.get_protected_store().await.unwrap();
162+
let veilid = backend.get_veilid_api().await.unwrap();
163+
let protected_store = veilid.protected_store().unwrap();
162164
let keypair_data = protected_store
163165
.load_user_secret(group.id().to_string())
164-
.await
165166
.expect(FAILED_TO_LOAD_KEYPAIR)
166167
.expect(KEYPAIR_NOT_FOUND);
167168

@@ -639,7 +640,12 @@ mod tests {
639640
.await
640641
.expect("Failed to write to temp file");
641642

642-
let protected_store = backend.get_protected_store().await.unwrap();
643+
let protected_store = backend
644+
.get_veilid_api()
645+
.await
646+
.unwrap()
647+
.protected_store()
648+
.unwrap();
643649

644650
let repo = group.create_repo().await?;
645651

@@ -879,6 +885,7 @@ mod tests {
879885
let mut retries = 5;
880886
while retries > 0 {
881887
if group2.download_hash_from_peers(&file_hash).await.is_ok() {
888+
println!("Download success!");
882889
break;
883890
}
884891
retries -= 1;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![recursion_limit = "256"]
12
use crate::backend::Backend;
23
use crate::common::{init_veilid, CommonKeypair, DHTEntity};
34
use crate::constants::{UNABLE_TO_GET_GROUP_NAME, UNABLE_TO_SET_GROUP_NAME};

src/repo.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Repo {
2828
pub encryption_key: SharedSecret,
2929
pub secret_key: Option<CryptoTyped<CryptoKey>>,
3030
pub routing_context: RoutingContext,
31-
pub crypto_system: CryptoSystemVLD0,
31+
pub veilid: VeilidAPI,
3232
pub iroh_blobs: VeilidIrohBlobs,
3333
}
3434

@@ -38,15 +38,15 @@ impl Repo {
3838
encryption_key: SharedSecret,
3939
secret_key: Option<CryptoTyped<CryptoKey>>,
4040
routing_context: RoutingContext,
41-
crypto_system: CryptoSystemVLD0,
41+
veilid: VeilidAPI,
4242
iroh_blobs: VeilidIrohBlobs,
4343
) -> Self {
4444
Self {
4545
dht_record,
4646
encryption_key,
4747
secret_key,
4848
routing_context,
49-
crypto_system,
49+
veilid,
5050
iroh_blobs,
5151
}
5252
}
@@ -377,8 +377,8 @@ impl DHTEntity for Repo {
377377
self.routing_context.clone()
378378
}
379379

380-
fn get_crypto_system(&self) -> CryptoSystemVLD0 {
381-
self.crypto_system.clone()
380+
fn get_veilid_api(&self) -> VeilidAPI {
381+
self.veilid.clone()
382382
}
383383

384384
fn get_dht_record(&self) -> DHTRecordDescriptor {

0 commit comments

Comments
 (0)