Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radmap#3 #7

Merged
merged 38 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
56c1fc3
Refactor constants to its own module
tripledoublev Jul 30, 2024
aeee213
Move test in lib.rs
tripledoublev Jul 30, 2024
a775d33
Create common.rs for CommonKeypair struct and DHTEntity trait
tripledoublev Jul 30, 2024
15bc35b
Refactor main.rs to use CommonKeypair and DHTEntity
tripledoublev Jul 30, 2024
dd6bf43
Refactor Repo struct to include necessary fields and implement DHTEnt…
tripledoublev Jul 30, 2024
c8c73d8
Replace CryptoKey with SharedSecret for encryption_key
tripledoublev Jul 30, 2024
a652269
Add anyhow to dependencies
tripledoublev Jul 30, 2024
50338a1
Refactor Group struct to include repos and implement DHTEntity trait
tripledoublev Jul 30, 2024
14f84f3
encryption_key uses random_shared_secret
tripledoublev Jul 30, 2024
ce611c7
Refactor code into backend, group, and repo.
tripledoublev Jul 30, 2024
a794934
Retrieves the existing repository and constructs a Repo object from t…
tripledoublev Aug 1, 2024
2dce9f8
Ensure veilid is running and fully connected.
Aug 22, 2024
a8b961d
Refactor Group struct to include record_key
tripledoublev Sep 4, 2024
7e3b171
Print the created Record Key
tripledoublev Sep 4, 2024
23fa77b
Include record_key in create_group and get_group
tripledoublev Sep 4, 2024
12285d7
Remove unnecessary parentheses
tripledoublev Sep 4, 2024
51e6cae
Refactor Group struct to use record_key instead of id
tripledoublev Sep 4, 2024
d9a4b5c
Use record_key instead of group id
tripledoublev Sep 4, 2024
58c61c8
open dht record with record key as first argument
tripledoublev Sep 4, 2024
f506946
Use Load_user_secret with record_key
tripledoublev Sep 4, 2024
f1fb2c4
Clean-up; remove duplication
tripledoublev Sep 4, 2024
3c0960c
Insert the group into the in-memory map
tripledoublev Sep 4, 2024
3836524
use record_key instead of id in basic_test
tripledoublev Sep 4, 2024
eb57b5d
get id returns group id
tripledoublev Sep 4, 2024
a765c6f
Store keypair with using record_key
tripledoublev Sep 5, 2024
a7654f0
Refactor open_dht_record in get_group
tripledoublev Sep 5, 2024
bbe33b4
load_user_key with record_key
tripledoublev Sep 5, 2024
2763306
remove duplicate (retrieved_keypair)
tripledoublev Sep 5, 2024
3dba8a2
Refactor redundant return statements, pattern matching, and option ha…
tripledoublev Sep 5, 2024
28c56a6
Set RUST_MIN_STACK to prevent stack overflow during tests
tripledoublev Sep 5, 2024
79fa7ff
Refactor Backend to use group.id() instead of record_key for group ma…
tripledoublev Sep 5, 2024
b42eddb
Refactor Group struct to leverage DHTRecordDescriptor for key management
tripledoublev Sep 5, 2024
413ac51
Update tests to align with Group struct refactor and DHTRecordDescrip…
tripledoublev Sep 5, 2024
7d7d646
Refactor `main.rs` to use `group.id()` and remove `record_key`
tripledoublev Sep 5, 2024
f6e703a
Include the 'id' field to the CommonKeypair struct
tripledoublev Sep 10, 2024
d715d62
Add new id field and public_key is owner key
tripledoublev Sep 10, 2024
eaf4f75
Remove id param, use self.id
tripledoublev Sep 10, 2024
bde340e
Update test to align with change to CommonKeypair struct
tripledoublev Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,29 @@ impl Backend {
let kind = Some(CRYPTO_KIND_VLD0);

let dht_record = routing_context.create_dht_record(schema, kind).await?;
let record_key = dht_record.key().clone();
let keypair = vld0_generate_keypair();
let crypto_system = CryptoSystemVLD0::new(veilid.crypto()?);

let encryption_key = crypto_system.random_shared_secret();

let group = Group::new(
keypair.key.clone(),
record_key,
dht_record,
encryption_key,
Some(CryptoTyped::new(CRYPTO_KIND_VLD0, keypair.secret)),
Arc::new(routing_context),
crypto_system,
);

let protected_store = veilid.protected_store().unwrap();
CommonKeypair {
RangerMauve marked this conversation as resolved.
Show resolved Hide resolved
public_key: group.get_id(),
public_key: group.id(),
secret_key: group.get_secret_key(),
encryption_key: group.get_encryption_key(),
}
.store_keypair(&protected_store, &record_key.value)
.store_keypair(&protected_store, &group.id())
.await
.map_err(|e| anyhow!(e))?;

self.groups.insert(record_key.value, Box::new(group.clone()));
self.groups.insert(group.id(), Box::new(group.clone()));

Ok(group)
}
Expand Down Expand Up @@ -200,18 +196,13 @@ impl Backend {


let group = Group {
id: retrieved_keypair.public_key.clone(),
record_key,
dht_record,
dht_record: dht_record.clone(),
encryption_key: retrieved_keypair.encryption_key.clone(),
secret_key: retrieved_keypair
.secret_key
.map(|sk| CryptoTyped::new(CRYPTO_KIND_VLD0, sk)),
routing_context: Arc::new(routing_context),
crypto_system,
repos: Vec::new(),
};
self.groups.insert(record_key.value, Box::new(group.clone()));
self.groups.insert(group.id(), Box::new(group.clone()));

Ok(Box::new(group))
}
Expand Down
24 changes: 13 additions & 11 deletions src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,40 @@ use crate::repo::Repo;

#[derive(Clone)]
pub struct Group {
pub id: CryptoKey,
pub record_key: TypedKey,
pub dht_record: DHTRecordDescriptor,
pub encryption_key: SharedSecret,
pub secret_key: Option<CryptoTyped<CryptoKey>>,
pub routing_context: Arc<RoutingContext>,
pub crypto_system: CryptoSystemVLD0,
pub repos: Vec<Repo>,
}

impl Group {
pub fn new(
id: CryptoKey,
record_key: TypedKey,
dht_record: DHTRecordDescriptor,
encryption_key: SharedSecret,
secret_key: Option<CryptoTyped<CryptoKey>>,
routing_context: Arc<RoutingContext>,
crypto_system: CryptoSystemVLD0,
) -> Self {
Self {
id,
record_key,
dht_record,
encryption_key,
secret_key,
routing_context,
crypto_system,
repos: Vec::new(),
}
}

pub fn id(&self) -> CryptoKey {
self.dht_record.key().value.clone()
}

pub fn owner_key(&self) -> CryptoKey {
self.dht_record.owner().clone()
}

pub fn owner_secret(&self) -> Option<CryptoKey> {
self.dht_record.owner_secret().cloned()
}

pub async fn add_repo(&mut self, repo: Repo) -> Result<()> {
self.repos.push(repo);
Expand All @@ -63,7 +65,7 @@ impl Group {

impl DHTEntity for Group {
fn get_id(&self) -> CryptoKey {
self.id.clone()
self.id().clone()
}

fn get_encryption_key(&self) -> SharedSecret {
Expand All @@ -83,6 +85,6 @@ impl DHTEntity for Group {
}

fn get_secret_key(&self) -> Option<CryptoKey> {
self.secret_key.clone().map(|key| key.value)
self.owner_secret()
}
}
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::constants::{GROUP_NOT_FOUND, UNABLE_TO_SET_GROUP_NAME, UNABLE_TO_GET_

use crate::backend::Backend;
use crate::common::{CommonKeypair, DHTEntity};
use veilid_core::vld0_generate_keypair;
use veilid_core::{
vld0_generate_keypair, TypedKey, CRYPTO_KIND_VLD0
};

#[cfg(test)]
mod tests {
Expand All @@ -28,36 +30,33 @@ mod tests {
backend.start().await.expect("Unable to start");
let group = backend.create_group().await.expect("Unable to create group");

let group_key = group.get_id();
let record_key = group.record_key.clone();

group.set_name(TEST_GROUP_NAME).await.expect(UNABLE_TO_SET_GROUP_NAME);
let name = group.get_name().await.expect(UNABLE_TO_GET_GROUP_NAME);
assert_eq!(name, TEST_GROUP_NAME);

backend.stop().await.expect("Unable to stop");

backend.start().await.expect("Unable to restart");
let loaded_group = backend.get_group(record_key.clone()).await.expect(GROUP_NOT_FOUND);
let loaded_group = backend.get_group(TypedKey::new(CRYPTO_KIND_VLD0, group.id())).await.expect(GROUP_NOT_FOUND);

let protected_store = backend.get_protected_store().unwrap();
let keypair_data = protected_store.load_user_secret(record_key.value.to_string())
let keypair_data = protected_store.load_user_secret(group.id().to_string())
.await
.expect(FAILED_TO_LOAD_KEYPAIR)
.expect(KEYPAIR_NOT_FOUND);
let retrieved_keypair: CommonKeypair = serde_cbor::from_slice(&keypair_data).expect(FAILED_TO_DESERIALIZE_KEYPAIR);

assert_eq!(retrieved_keypair.public_key, group.get_id());
assert_eq!(retrieved_keypair.public_key, group.id());
assert_eq!(retrieved_keypair.secret_key, group.get_secret_key());
assert_eq!(retrieved_keypair.encryption_key, group.get_encryption_key());

let mut loaded_group = backend.get_group(record_key.clone()).await.expect(GROUP_NOT_FOUND);
let mut loaded_group = backend.get_group(TypedKey::new(CRYPTO_KIND_VLD0, group.id())).await.expect(GROUP_NOT_FOUND);

// Check if we can get group name
let group_name = loaded_group.get_name().await.expect(UNABLE_TO_GET_GROUP_NAME);
assert_eq!(group_name, TEST_GROUP_NAME);

assert_eq!(loaded_group.get_id(), retrieved_keypair.public_key);
assert_eq!(loaded_group.id(), retrieved_keypair.public_key);

// Create a repo
let repo = backend.create_repo().await.expect("Unable to create repo");
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ async fn main() -> anyhow::Result<()> {
println!("Provided Encryption Key: {:?}", enckey);
} else {
let group = backend.create_group().await?;
println!("Group created with Public Key: {:?}", group.get_id());
println!("Group created with Record Key: {:?}", group.record_key);
println!("Group created with Record Key: {:?}", group.id());
println!("Group created with Secret Key: {:?}", group.get_secret_key().unwrap());
println!("Group created with Encryption Key: {:?}", group.get_encryption_key());
}
Expand Down