Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into rich/rename-account
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhuaaa committed Oct 23, 2023
2 parents 869dcae + 1ca49e7 commit c71072c
Show file tree
Hide file tree
Showing 17 changed files with 326 additions and 263 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pkg/
wasm-pack.log

!xmtp/src/bin/
!xmtp_mls/src/bin/

# generated
bindings_swift/Sources
Expand Down Expand Up @@ -141,4 +142,4 @@ dist
*.jar

# Generated Uniffi definitions
xmtpv3.kt
xmtpv3.kt
4 changes: 2 additions & 2 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "stable"
components = [ "rustc", "cargo", "clippy", "rustfmt", "rust-analyzer" ]
targets = [ "wasm32-unknown-unknown" ]
profile = "default"
6 changes: 2 additions & 4 deletions xmtp/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ pub enum AccountError {
BadAssocation(#[from] AssociationError),
#[error("mutex poisoned error")]
MutexPoisoned,
#[error("unknown error")]
Unknown,
}

/// Holds an account and adds some serialization methods on top
pub struct VmacAccount {
account: OlmAccount,
}

// Struct that holds an account and adds some serialization methods on top
impl VmacAccount {
// Create a new instance
/// Create a new instance
pub fn new(account: OlmAccount) -> Self {
Self { account }
}
Expand Down
50 changes: 24 additions & 26 deletions xmtp/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use xmtp_proto::api_client::XmtpApiClient;
#[derive(Error, Debug)]
pub enum ClientBuilderError {
#[error("Missing parameter: {parameter}")]
MissingParameterError { parameter: &'static str },
MissingParameter { parameter: &'static str },

#[error("Failed to serialize/deserialize state for persistence: {source}")]
SerializationError { source: serde_json::Error },
Serialization { source: serde_json::Error },

#[error("Required account was not found in cache.")]
RequiredAccountNotFound,
Expand All @@ -27,17 +27,16 @@ pub enum ClientBuilderError {

#[error("Associating an address to account failed")]
AssociationFailed(#[from] AssociationError),
// #[error("Error Initalizing Store")]
// StoreInitialization(#[from] SE),
#[error("Error Initalizing Account")]

#[error("Error Initializing Account")]
AccountInitialization(#[from] AccountError),

#[error("Storage Error")]
StorageError(#[from] StorageError),
}

pub enum AccountStrategy<O: InboxOwner> {
CreateIfNotFound(O),
pub enum AccountStrategy<InboxOwner> {
CreateIfNotFound(InboxOwner),
CachedOnly(Address),
#[cfg(test)]
ExternalAccount(Account),
Expand All @@ -61,24 +60,20 @@ where
}
}

pub struct ClientBuilder<A, O>
where
A: XmtpApiClient + Default,
O: InboxOwner,
{
api_client: Option<A>,
pub struct ClientBuilder<ApiClient, Owner> {
api_client: Option<ApiClient>,
network: Network,
account: Option<Account>,
store: Option<EncryptedMessageStore>,
account_strategy: AccountStrategy<O>,
account_strategy: AccountStrategy<Owner>,
}

impl<A, O> ClientBuilder<A, O>
impl<ApiClient, Owner> ClientBuilder<ApiClient, Owner>
where
A: XmtpApiClient + Default,
O: InboxOwner,
ApiClient: XmtpApiClient,
Owner: InboxOwner,
{
pub fn new(strat: AccountStrategy<O>) -> Self {
pub fn new(strat: AccountStrategy<Owner>) -> Self {
Self {
api_client: None,
network: Network::Dev,
Expand All @@ -88,7 +83,7 @@ where
}
}

pub fn api_client(mut self, api_client: A) -> Self {
pub fn api_client(mut self, api_client: ApiClient) -> Self {
self.api_client = Some(api_client);
self
}
Expand All @@ -110,7 +105,7 @@ where

/// Fetch account from peristence or generate and sign a new one
fn find_or_create_account(
owner: &O,
owner: &Owner,
store: &mut EncryptedMessageStore,
) -> Result<Account, ClientBuilderError> {
let account = Self::retrieve_persisted_account(store)?;
Expand Down Expand Up @@ -141,7 +136,7 @@ where
Ok(accounts.pop())
}

fn sign_new_account(owner: &O) -> Result<Account, ClientBuilderError> {
fn sign_new_account(owner: &Owner) -> Result<Account, ClientBuilderError> {
let sign = |public_key_bytes: Vec<u8>| -> Result<Eip191Association, AssociationError> {
let assoc_text = AssociationText::Static {
blockchain_address: owner.get_address(),
Expand All @@ -155,8 +150,9 @@ where

Account::generate(sign).map_err(ClientBuilderError::AccountInitialization)
}
pub fn build(mut self) -> Result<Client<A>, ClientBuilderError> {
let api_client = self.api_client.take().unwrap_or_default();

pub fn build(mut self) -> Result<Client<ApiClient>, ClientBuilderError> {
let api_client = self.api_client.take().ok_or(ClientBuilderError::MissingParameter { parameter: "api_client"})?;
let mut store = self.store.take().unwrap_or_default();
// Fetch the Account based upon the account strategy.
let account = match self.account_strategy {
Expand Down Expand Up @@ -193,7 +189,6 @@ mod tests {
use crate::{
mock_xmtp_api_client::MockXmtpApiClient,
storage::{EncryptedMessageStore, StorageOption},
Client,
};

use super::ClientBuilder;
Expand All @@ -203,6 +198,7 @@ mod tests {
let wallet = generate_local_wallet();

Self::new(wallet.into())
.api_client(MockXmtpApiClient::default())
}
}

Expand Down Expand Up @@ -231,8 +227,9 @@ mod tests {
))
.unwrap();

let client_a: Client<MockXmtpApiClient> = ClientBuilder::new(wallet.clone().into())
let client_a = ClientBuilder::new(wallet.clone().into())
.store(store_a)
.api_client(MockXmtpApiClient::default())
.build()
.unwrap();
let keybytes_a = client_a
Expand All @@ -251,8 +248,9 @@ mod tests {
))
.unwrap();

let client_b: Client<MockXmtpApiClient> = ClientBuilder::new(wallet.into())
let client_b = ClientBuilder::new(wallet.into())
.store(store_b)
.api_client(MockXmtpApiClient::default())
.build()
.unwrap();
let keybytes_b = client_b
Expand Down
Loading

0 comments on commit c71072c

Please sign in to comment.