Skip to content

Commit

Permalink
Fold registration server functions into the exit trust root
Browse files Browse the repository at this point in the history
When we first moved to the exit db smart contract the registration
server was created to provide a way to register bootstrapping clients
despite them having no balance and even no internet connection, by
passing that data to exits.

As this design has gotten more general and the requirements have
expanded to support out of the box appliance exits the root of trust
server was created, it's natural to fold the registration server info
the reference root of trust implementation in this repo.

As part of this process the trust root, and the api for the client
registration has been refactored, simplified, and cleaned up.
  • Loading branch information
jkilpatr committed Oct 29, 2024
1 parent d84dfbc commit 45907da
Show file tree
Hide file tree
Showing 34 changed files with 1,128 additions and 969 deletions.
61 changes: 56 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 29 additions & 77 deletions althea_types/src/contact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ impl From<ContactType> for ContactDetails {

impl ContactType {
pub fn convert(val: ContactDetails, seq: Option<u32>) -> Option<Self> {
let same = ExitRegistrationDetails {
phone: val.phone,
email: val.email,
phone_code: None,
email_code: None,
sequence_number: seq,
};
ContactStorage::convert(same).map(|val| val.into())
match (val.email, val.phone) {
(Some(email), Some(phone)) => Some(ContactType::Both {
email: email.parse().ok()?,
number: phone.parse().ok()?,
sequence_number: seq,
}),
(Some(email), None) => Some(ContactType::Email {
email: email.parse().ok()?,
sequence_number: seq,
}),
(None, Some(phone)) => Some(ContactType::Phone {
number: phone.parse().ok()?,
sequence_number: seq,
}),
(None, None) => None,
}
}
}

Expand Down Expand Up @@ -299,150 +307,94 @@ impl ContactStorage {
email: Some(email),
phone_code: _,
email_code: _,
sequence_number,
exit_database_contract: _,
} => match (phone.parse(), email.parse()) {
(Ok(validated_phone), Ok(validated_email)) => Some(ContactStorage {
number: Some(validated_phone),
email: Some(validated_email),
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
(Err(_e), Ok(validated_email)) => Some(ContactStorage {
email: Some(validated_email),
number: None,
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
(Ok(validated_phone), Err(_e)) => Some(ContactStorage {
number: Some(validated_phone),
email: None,
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
(Err(_ea), Err(_eb)) => Some(ContactStorage {
number: None,
email: None,
invalid_email: Some(email),
invalid_number: Some(phone),
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
},
ExitRegistrationDetails {
phone: Some(phone),
email: None,
phone_code: _,
email_code: _,
sequence_number,
exit_database_contract: _,
} => match phone.parse() {
Ok(validated_phone) => Some(ContactStorage {
number: Some(validated_phone),
email: None,
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
Err(_e) => Some(ContactStorage {
number: None,
email: None,
invalid_number: Some(phone),
invalid_email: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
},
ExitRegistrationDetails {
phone: None,
email: Some(email),
phone_code: _,
email_code: _,
sequence_number,
exit_database_contract: _,
} => match email.parse() {
Ok(validated_email) => Some(ContactStorage {
email: Some(validated_email),
number: None,
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
Err(_e) => Some(ContactStorage {
email: None,
number: None,
invalid_email: Some(email),
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
},
ExitRegistrationDetails {
phone: None,
email: None,
phone_code: _,
email_code: _,
sequence_number,
exit_database_contract: _,
} => Some(ContactStorage {
email: None,
number: None,
invalid_email: None,
invalid_number: None,
sequence_number: sequence_number.unwrap_or(0),
sequence_number: 0,
}),
}
}
}

impl From<ContactType> for ExitRegistrationDetails {
fn from(ct: ContactType) -> Self {
match ct {
ContactType::Both {
number,
email,
sequence_number,
} => ExitRegistrationDetails {
phone: Some(number.to_string()),
email: Some(email.to_string()),
email_code: None,
phone_code: None,
sequence_number,
},
ContactType::Email {
email,
sequence_number,
} => ExitRegistrationDetails {
phone: None,
email: Some(email.to_string()),
email_code: None,
phone_code: None,
sequence_number,
},
ContactType::Phone {
number,
sequence_number,
} => ExitRegistrationDetails {
phone: Some(number.to_string()),
email: None,
email_code: None,
phone_code: None,
sequence_number,
},
ContactType::Bad {
invalid_email,
invalid_number,
sequence_number,
} => ExitRegistrationDetails {
phone: invalid_number,
email: invalid_email,
email_code: None,
phone_code: None,
sequence_number,
},
}
}
}

impl From<ContactStorage> for ExitRegistrationDetails {
fn from(cs: ContactStorage) -> Self {
let ct: ContactType = cs.into();
ct.into()
}
}
9 changes: 7 additions & 2 deletions althea_types/src/exits/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod tests {
use crate::exits::identity::random_exit_identity;
use crate::exits::ExitRegistrationDetails;
use crate::ExitClientIdentity;
use clarity::Address;
use crypto_box::PublicKey;
use crypto_box::SecretKey;
use sodiumoxide::crypto::box_;
Expand All @@ -171,6 +172,10 @@ mod tests {
(public_key, secret_key)
}

pub fn random_address() -> Address {
Address::from_slice(&[1u8; 20]).unwrap()
}

/// Used to test cross compatibility with libsodium
pub fn encrypt_exit_client_id_libsodium(
our_publickey: WgKey,
Expand Down Expand Up @@ -247,7 +252,7 @@ mod tests {
email_code: None,
phone: None,
phone_code: None,
sequence_number: None,
exit_database_contract: random_address(),
},
};

Expand Down Expand Up @@ -292,7 +297,7 @@ mod tests {
email_code: None,
phone: None,
phone_code: None,
sequence_number: None,
exit_database_contract: random_address(),
},
};

Expand Down
6 changes: 4 additions & 2 deletions althea_types/src/exits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::default_system_chain;
use crate::wg_key::WgKey;
use crate::{exits::identity::ExitIdentity, Identity, SystemChain};
use clarity::Address;
use ipnetwork::IpNetwork;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -11,6 +12,7 @@ pub mod encryption;
pub mod identity;
pub mod server_list_signatures;

/// Struct for registration communication between the client and the exit
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Default)]
pub struct ExitRegistrationDetails {
#[serde(skip_serializing_if = "Option::is_none", default)]
Expand All @@ -21,8 +23,8 @@ pub struct ExitRegistrationDetails {
pub phone: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub phone_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub sequence_number: Option<u32>,
/// This is the exit database contract that the client wishes to register with
pub exit_database_contract: Address,
}

/// This is the state an exit can be in
Expand Down
Loading

0 comments on commit 45907da

Please sign in to comment.