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

Add Rust-Toolchain, refactor generics, typos/cleanup #271

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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"
Copy link
Contributor Author

@insipx insipx Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also pin to a specific version, not sure what the preference is, it seems like 'stable' has been used in the past for this project?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stable works for me. We have enough crazy build-time stuff happening in the bindings, last thing we need is to be doing that on a nightly build

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
Loading