-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add xmtp_id crate
- Loading branch information
Showing
11 changed files
with
153 additions
and
389 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "xmtp_id" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
log.workspace = true | ||
tracing.workspace = true | ||
thiserror.workspace = true | ||
xmtp_cryptography.workspace = true | ||
xmtp_mls.workspace = true | ||
xmtp_proto.workspace = true | ||
openmls_traits.workspace = true | ||
openmls.workspace = true | ||
openmls_basic_credential.workspace = true | ||
prost.workspace = true | ||
tls_codec.workspace = true | ||
chrono.workspace = true | ||
serde.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use openmls_traits::types::CryptoError; | ||
use thiserror::Error; | ||
use xmtp_mls::credential::AssociationError; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum IdentityError { | ||
#[error("bad association: {0}")] | ||
BadAssocation(#[from] AssociationError), | ||
#[error("generating key-pairs: {0}")] | ||
KeyGenerationError(#[from] CryptoError), | ||
#[error("uninitialized identity")] | ||
UninitializedIdentity, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
pub mod error; | ||
|
||
use std::sync::RwLock; | ||
|
||
use openmls::prelude::Credential as OpenMlsCredential; | ||
use openmls_basic_credential::SignatureKeyPair; | ||
use xmtp_mls::{ | ||
configuration::CIPHERSUITE, credential::UnsignedGrantMessagingAccessData, types::Address, | ||
utils::time::now_ns, | ||
}; | ||
|
||
use crate::error::IdentityError; | ||
|
||
pub struct Identity { | ||
#[allow(dead_code)] | ||
pub(crate) account_address: Address, | ||
#[allow(dead_code)] | ||
pub(crate) installation_keys: SignatureKeyPair, | ||
pub(crate) credential: RwLock<Option<OpenMlsCredential>>, | ||
pub(crate) unsigned_association_data: Option<UnsignedGrantMessagingAccessData>, | ||
} | ||
|
||
impl Identity { | ||
// Creates a credential that is not yet wallet signed. Implementors should sign the payload returned by 'text_to_sign' | ||
// and call 'register' with the signature. | ||
#[allow(dead_code)] | ||
pub(crate) fn create_to_be_signed(account_address: String) -> Result<Self, IdentityError> { | ||
let signature_keys = SignatureKeyPair::new(CIPHERSUITE.signature_algorithm())?; | ||
let unsigned_association_data = UnsignedGrantMessagingAccessData::new( | ||
account_address.clone(), | ||
signature_keys.to_public_vec(), | ||
now_ns() as u64, | ||
)?; | ||
let identity = Self { | ||
account_address, | ||
installation_keys: signature_keys, | ||
credential: RwLock::new(None), | ||
unsigned_association_data: Some(unsigned_association_data), | ||
}; | ||
|
||
Ok(identity) | ||
} | ||
|
||
pub fn text_to_sign(&self) -> Option<String> { | ||
if self.credential().is_ok() { | ||
return None; | ||
} | ||
self.unsigned_association_data | ||
.clone() | ||
.map(|data| data.text()) | ||
} | ||
|
||
fn credential(&self) -> Result<OpenMlsCredential, IdentityError> { | ||
self.credential | ||
.read() | ||
.unwrap_or_else(|err| err.into_inner()) | ||
.clone() | ||
.ok_or(IdentityError::UninitializedIdentity) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters