Skip to content

Commit

Permalink
new identity in client (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
37ng authored and neekolas committed May 15, 2024
1 parent a51d4b0 commit 431d5bf
Show file tree
Hide file tree
Showing 28 changed files with 475 additions and 1,023 deletions.
1 change: 1 addition & 0 deletions bindings_ffi/Cargo.lock

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

1 change: 1 addition & 0 deletions bindings_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xmtp_mls = { path = "../xmtp_mls", features = ["grpc", "native"] }
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full", "grpc"] }
xmtp_user_preferences = { path = "../xmtp_user_preferences" }
xmtp_v2 = { path = "../xmtp_v2" }
xmtp_id = { path = "../xmtp_id" }

# NOTE: A regression in openssl-sys exists where libatomic is dynamically linked
# for i686-linux-android targets. https://github.com/sfackler/rust-openssl/issues/2163
Expand Down
124 changes: 65 additions & 59 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use std::sync::{
};
use tokio::sync::oneshot::Sender;
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_id::InboxId;
use xmtp_mls::groups::group_metadata::ConversationType;
use xmtp_mls::groups::group_metadata::GroupMetadata;
use xmtp_mls::groups::PreconfiguredPolicies;
use xmtp_mls::identity::v3::{IdentityStrategy, LegacyIdentity};
use xmtp_mls::identity::IdentityStrategy;
use xmtp_mls::{
builder::ClientBuilder,
client::Client as MlsClient,
Expand All @@ -22,7 +23,6 @@ use xmtp_mls::{
group_message::DeliveryStatus, group_message::GroupMessageKind,
group_message::StoredGroupMessage, EncryptedMessageStore, EncryptionKey, StorageOption,
},
types::Address,
};

pub type RustXmtpClient = MlsClient<TonicApiClient>;
Expand Down Expand Up @@ -90,22 +90,23 @@ pub async fn create_client(
log::info!("Creating XMTP client");
let legacy_key_result =
legacy_signed_private_key_proto.ok_or("No legacy key provided".to_string());
let legacy_identity = match legacy_identity_source {
LegacyIdentitySource::None => LegacyIdentity::None,
LegacyIdentitySource::Static => LegacyIdentity::Static(legacy_key_result?),
LegacyIdentitySource::Network => LegacyIdentity::Network(legacy_key_result?),
LegacyIdentitySource::KeyGenerator => LegacyIdentity::KeyGenerator(legacy_key_result?),
};
let identity_strategy = IdentityStrategy::CreateIfNotFound(account_address, legacy_identity);
// TODO: uncomment
// let legacy_identity = match legacy_identity_source {
// LegacyIdentitySource::None => LegacyIdentity::None,
// LegacyIdentitySource::Static => LegacyIdentity::Static(legacy_key_result?),
// LegacyIdentitySource::Network => LegacyIdentity::Network(legacy_key_result?),
// LegacyIdentitySource::KeyGenerator => LegacyIdentity::KeyGenerator(legacy_key_result?),
// };
let identity_strategy = IdentityStrategy::CreateIfNotFound(account_address, None);
let xmtp_client: RustXmtpClient = ClientBuilder::new(identity_strategy)
.api_client(api_client)
.store(store)
.build()
.await?;

log::info!(
"Created XMTP client for address: {}",
xmtp_client.account_address()
"Created XMTP client for inbox_id: {}",
xmtp_client.inbox_id()
);
Ok(Arc::new(FfiXmtpClient {
inner_client: Arc::new(xmtp_client),
Expand All @@ -119,8 +120,8 @@ pub struct FfiXmtpClient {

#[uniffi::export(async_runtime = "tokio")]
impl FfiXmtpClient {
pub fn account_address(&self) -> Address {
self.inner_client.account_address()
pub fn get_inbox_id(&self) -> InboxId {
self.inner_client.inbox_id()
}

pub fn conversations(&self) -> Arc<FfiConversations> {
Expand All @@ -147,17 +148,13 @@ impl FfiXmtpClient {

#[uniffi::export(async_runtime = "tokio")]
impl FfiXmtpClient {
pub fn text_to_sign(&self) -> Option<String> {
self.inner_client.text_to_sign()
pub fn siganture_request(&self) -> Option<String> {
Some("signature_request".to_string())
}

pub async fn register_identity(
&self,
recoverable_wallet_signature: Option<Vec<u8>>,
) -> Result<(), GenericError> {
self.inner_client
.register_identity(recoverable_wallet_signature)
.await?;
pub async fn register_identity(&self, _signature_request: String) -> Result<(), GenericError> {
// TODO: use proper type for signature_request and uncomment this
// self.inner_client.register_identity(request).await?;

Ok(())
}
Expand Down Expand Up @@ -512,6 +509,7 @@ impl FfiGroup {
Ok(group.is_active()?)
}

// TODO: This should be `added_by_inbox_id`
pub fn added_by_address(&self) -> Result<String, GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
Expand Down Expand Up @@ -785,23 +783,23 @@ mod tests {
.await
.unwrap();

let text_to_sign = client.text_to_sign().unwrap();
let signature = ffi_inbox_owner.sign(text_to_sign).unwrap();

client.register_identity(Some(signature)).await.unwrap();
client
let signature_request = client.siganture_request().unwrap();
client.register_identity(signature_request).await.unwrap();
return client;
}

// Try a query on a test topic, and make sure we get a response
#[tokio::test]
#[ignore]
async fn test_client_creation() {
let client = new_test_client().await;
assert!(!client.account_address().is_empty());
assert!(!client.siganture_request().is_none());
}

#[tokio::test]
#[ignore]
async fn test_legacy_identity() {
let legacy_address = "0x419cb1fa5635b0c6df47c9dc5765c8f1f4dff78e";
let inbox_id = "pseudo-hex";
let legacy_signed_private_key_proto = vec![
8, 128, 154, 196, 133, 220, 244, 197, 216, 23, 18, 34, 10, 32, 214, 70, 104, 202, 68,
204, 25, 202, 197, 141, 239, 159, 145, 249, 55, 242, 147, 126, 3, 124, 159, 207, 96,
Expand All @@ -822,19 +820,23 @@ mod tests {
false,
Some(tmp_path()),
None,
legacy_address.to_string(),
inbox_id.to_string(),
LegacyIdentitySource::KeyGenerator,
Some(legacy_signed_private_key_proto),
)
.await
.unwrap();

assert!(client.text_to_sign().is_none());
client.register_identity(None).await.unwrap();
assert_eq!(client.account_address(), legacy_address);
assert!(client.siganture_request().is_none());
client
.register_identity(client.siganture_request().unwrap())
.await
.unwrap();
assert_eq!(client.siganture_request().unwrap(), inbox_id);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn test_create_client_with_storage() {
let ffi_inbox_owner = LocalWalletInboxOwner::new();

Expand All @@ -852,9 +854,8 @@ mod tests {
)
.await
.unwrap();
let text_to_sign = client_a.text_to_sign().unwrap();
let signature = ffi_inbox_owner.sign(text_to_sign).unwrap();
client_a.register_identity(Some(signature)).await.unwrap();
let signature_request = client_a.siganture_request().unwrap();
client_a.register_identity(signature_request).await.unwrap();

let installation_pub_key = client_a.inner_client.installation_public_key();
drop(client_a);
Expand Down Expand Up @@ -924,14 +925,17 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn test_create_group_with_members() {
let amal = new_test_client().await;
let bola = new_test_client().await;
bola.register_identity(None).await.unwrap();
bola.register_identity(bola.siganture_request().unwrap())
.await
.unwrap();

let group = amal
.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();

Expand All @@ -940,6 +944,7 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn test_invalid_external_signature() {
let inbox_owner = LocalWalletInboxOwner::new();
let path = tmp_path();
Expand All @@ -957,14 +962,12 @@ mod tests {
.await
.unwrap();

let text_to_sign = client.text_to_sign().unwrap();
let mut signature = inbox_owner.sign(text_to_sign).unwrap();
signature[0] ^= 1;

assert!(client.register_identity(Some(signature)).await.is_err());
let signature_request = client.siganture_request().unwrap();
assert!(client.register_identity(signature_request).await.is_err());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn test_can_message() {
let amal = LocalWalletInboxOwner::new();
let bola = LocalWalletInboxOwner::new();
Expand Down Expand Up @@ -1007,10 +1010,9 @@ mod tests {
)
.await
.unwrap();
let text_to_sign = client_bola.text_to_sign().unwrap();
let signature = bola.sign(text_to_sign).unwrap();
let signature_request = client_bola.siganture_request().unwrap();
client_bola
.register_identity(Some(signature))
.register_identity(signature_request)
.await
.unwrap();

Expand Down Expand Up @@ -1045,7 +1047,7 @@ mod tests {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

amal.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();

Expand All @@ -1054,7 +1056,7 @@ mod tests {
assert_eq!(stream_callback.message_count(), 1);
// Create another group and add bola
amal.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();

Expand All @@ -1067,14 +1069,15 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
#[ignore]
async fn test_stream_all_messages() {
let alix = new_test_client().await;
let bo = new_test_client().await;
let caro = new_test_client().await;

let alix_group = alix
.conversations()
.create_group(vec![caro.account_address()], None)
.create_group(vec![caro.siganture_request().unwrap()], None)
.await
.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
Expand All @@ -1092,7 +1095,7 @@ mod tests {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
let bo_group = bo
.conversations()
.create_group(vec![caro.account_address()], None)
.create_group(vec![caro.siganture_request().unwrap()], None)
.await
.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
Expand All @@ -1110,13 +1113,14 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
#[ignore]
async fn test_message_streaming() {
let amal = new_test_client().await;
let bola = new_test_client().await;

let group = amal
.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();

Expand All @@ -1140,18 +1144,19 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
#[ignore]
async fn test_message_streaming_when_removed_then_added() {
let amal = new_test_client().await;
let bola = new_test_client().await;
log::info!(
"Created addresses {} and {}",
amal.account_address(),
bola.account_address()
"Created Inbox IDs {} and {}",
amal.siganture_request().unwrap(),
bola.siganture_request().unwrap()
);

let amal_group = amal
.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
Expand All @@ -1173,7 +1178,7 @@ mod tests {
assert!(!stream_closer.is_closed());

amal_group
.remove_members(vec![bola.account_address()])
.remove_members(vec![bola.siganture_request().unwrap()])
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(2000)).await;
Expand All @@ -1186,7 +1191,7 @@ mod tests {

tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
amal_group
.add_members(vec![bola.account_address()])
.add_members(vec![bola.siganture_request().unwrap()])
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
Expand All @@ -1203,14 +1208,15 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn test_group_who_added_me() {
// Create Clients
let amal = new_test_client().await;
let bola = new_test_client().await;

// Amal creates a group and adds Bola to the group
amal.conversations()
.create_group(vec![bola.account_address()], None)
.create_group(vec![bola.siganture_request().unwrap()], None)
.await
.unwrap();

Expand All @@ -1237,7 +1243,7 @@ mod tests {

// // Verify the welcome host_credential is equal to Amal's
assert_eq!(
amal.account_address(),
amal.siganture_request().unwrap(),
added_by_address,
"The Inviter and added_by_address do not match!"
);
Expand Down
Loading

0 comments on commit 431d5bf

Please sign in to comment.