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

interface for determining scw or eoa #615

Merged
merged 10 commits into from
Apr 10, 2024
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
1,379 changes: 132 additions & 1,247 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tracing = "0.1"
anyhow = "1.0"
jsonrpsee = { version = "0.22", features = ["macros", "server", "client-core"] }
tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] }
ctor = "0.2"

# Internal Crate Dependencies
xmtp_cryptography = { path = "xmtp_cryptography" }
Expand Down
4 changes: 2 additions & 2 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern crate xmtp_mls;
use std::{fs, path::PathBuf, time::Duration};

use clap::{Parser, Subcommand, ValueEnum};
use ethers::signers::{coins_bip39::English, MnemonicBuilder};
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder};
use kv_log_macro::{error, info};
use prost::Message;

Expand All @@ -25,7 +25,7 @@ use thiserror::Error;
use xmtp_api_grpc::grpc_api_helper::Client as ApiClient;
use xmtp_cryptography::{
signature::{RecoverableSignature, SignatureError},
utils::{rng, LocalWallet},
utils::rng,
};
use xmtp_mls::{
builder::{ClientBuilderError, IdentityStrategy, LegacyIdentity},
Expand Down
10 changes: 8 additions & 2 deletions xmtp_cryptography/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub enum SignatureError {
},
#[error("Error creating signature")]
SigningError(#[from] ecdsa::Error),
#[error("Error thrown from thirdParty")]
ThirdPartyError(String),
#[error("Error creating signature with ethers wallet")]
EthersWalletError(#[from] ethers::signers::WalletError),
#[error("unknown data store error")]
Unknown,
}
Expand Down Expand Up @@ -74,6 +74,12 @@ impl RecoverableSignature {
}
}

impl From<Vec<u8>> for RecoverableSignature {
fn from(value: Vec<u8>) -> Self {
RecoverableSignature::Eip191Signature(value)
}
}

impl From<RecoverableSignature> for Vec<u8> {
fn from(value: RecoverableSignature) -> Self {
match value {
Expand Down
10 changes: 0 additions & 10 deletions xmtp_id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,15 @@ openmls.workspace = true
openmls_basic_credential.workspace = true
openmls_rust_crypto.workspace = true
prost.workspace = true
chrono.workspace = true
serde.workspace = true
async-trait.workspace = true
futures.workspace = true
sha2 = "0.10.8"
rand.workspace = true
hex.workspace = true
ethers.workspace = true
anyhow.workspace = true

[dev-dependencies]
tracing-subscriber.workspace = true
serde_json.workspace = true
anyhow.workspace = true
tokio = { workspace = true, features = ["time"] }
jsonrpsee = { workspace = true, features = ["macros", "ws-client"] }
ethers = { workspace = true, features = ["ws"] }
tokio-test = "0.4"
futures = "0.3"
ctor = "0.2.5"
surf = "2.3"
regex = "1.10"
17 changes: 11 additions & 6 deletions xmtp_id/src/erc1271_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use anyhow::Error;
use ethers::contract::abigen;
use ethers::providers::{Http, Provider};
use ethers::types::{Address, BlockNumber, Bytes};
use std::convert::TryFrom;
use std::sync::Arc;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum VerifierError {
#[error("calling smart contract {0}")]
Contract(#[from] ethers::contract::ContractError<Provider<Http>>),
}

const EIP1271_MAGIC_VALUE: [u8; 4] = [0x16, 0x26, 0xba, 0x7e];

Expand All @@ -15,6 +20,7 @@ abigen!(
derives(serde::Serialize, serde::Deserialize)
);

#[derive(Debug)]
pub struct ERC1271Verifier {
pub provider: Arc<Provider<Http>>,
}
Expand All @@ -38,7 +44,7 @@ impl ERC1271Verifier {
block_number: Option<BlockNumber>,
hash: [u8; 32],
signature: Bytes,
) -> Result<bool, Error> {
) -> Result<bool, VerifierError> {
let erc1271 = ERC1271::new(contract_address, self.provider.clone());

let res: [u8; 4] = erc1271
Expand All @@ -56,16 +62,15 @@ mod tests {
use super::*;
use ethers::{
abi::{self, Token},
providers::{Http, Middleware, Provider},
types::{Bytes, H256, U256},
providers::Middleware,
types::{H256, U256},
};

use ethers::{
core::utils::Anvil,
middleware::SignerMiddleware,
signers::{LocalWallet, Signer as _},
};
use std::{convert::TryFrom, sync::Arc};

abigen!(
CoinbaseSmartWallet,
Expand Down
52 changes: 50 additions & 2 deletions xmtp_id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use openmls_traits::types::CryptoError;
use thiserror::Error;
use xmtp_mls::{
configuration::CIPHERSUITE,
credential::Credential,
credential::{AssociationError, UnsignedGrantMessagingAccessData},
credential::{AssociationError, Credential, UnsignedGrantMessagingAccessData},
types::Address,
utils::time::now_ns,
};
Expand All @@ -32,6 +31,11 @@ pub enum IdentityError {
VerificationError(#[from] VerificationError),
}

#[async_trait::async_trait]
pub trait WalletIdentity {
async fn is_smart_wallet(&self, block: Option<u64>) -> Result<bool, IdentityError>;
}

pub struct Identity {
#[allow(dead_code)]
pub(crate) account_address: Address,
Expand Down Expand Up @@ -89,3 +93,47 @@ impl Identity {
Ok(credential.account_address().to_string())
}
}

#[cfg(test)]
mod tests {
use super::*;
use ethers::{
middleware::Middleware,
providers::{Http, Provider},
types::Address,
};
use std::str::FromStr;
use xmtp_cryptography::utils::generate_local_wallet;
use xmtp_mls::InboxOwner;

struct EthereumWallet {
provider: Provider<Http>,
address: String,
}

impl EthereumWallet {
pub fn new(address: String) -> Self {
let provider = Provider::<Http>::try_from("https://eth.llamarpc.com").unwrap();
Self { provider, address }
}
}

#[async_trait::async_trait]
impl WalletIdentity for EthereumWallet {
async fn is_smart_wallet(&self, block: Option<u64>) -> Result<bool, IdentityError> {
let address = Address::from_str(&self.address).unwrap();
let res = self.provider.get_code(address, block.map(Into::into)).await;
Ok(!res.unwrap().to_vec().is_empty())
}
}

#[tokio::test]
async fn test_is_smart_wallet() {
let wallet = generate_local_wallet();
let eth = EthereumWallet::new(wallet.get_address());
let scw = EthereumWallet::new("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".into());

assert!(!eth.is_smart_wallet(None).await.unwrap());
assert!(scw.is_smart_wallet(None).await.unwrap());
}
}
2 changes: 1 addition & 1 deletion xmtp_mls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ xmtp_cryptography = { workspace = true }
xmtp_v2 = { path = "../xmtp_v2" }

[dev-dependencies]
ctor = "0.2"
ctor.workspace = true
flume = "0.11"
mockall = "0.11.4"
tempfile = "3.5.0"
Expand Down
6 changes: 5 additions & 1 deletion xmtp_mls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ use storage::StorageError;
use xmtp_cryptography::signature::{RecoverableSignature, SignatureError};

pub trait InboxOwner {
/// Get address of the wallet.
fn get_address(&self) -> String;
/// Sign text with the wallet.
fn sign(&self, text: &str) -> Result<RecoverableSignature, SignatureError>;
}

// Inserts a model to the underlying data store
/// Inserts a model to the underlying data store
pub trait Store<StorageConnection> {
fn store(&self, into: &StorageConnection) -> Result<(), StorageError>;
}

/// Fetches a model from the underlying data store
pub trait Fetch<Model> {
type Key;
fn fetch(&self, key: &Self::Key) -> Result<Option<Model>, StorageError>;
}

/// Deletes a model from the underlying data store
pub trait Delete<Model> {
type Key;
fn delete(&self, key: Self::Key) -> Result<usize, StorageError>;
Expand Down
5 changes: 1 addition & 4 deletions xmtp_mls/src/owner/evm_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ impl InboxOwner for LocalWallet {
}

fn sign(&self, text: &str) -> Result<RecoverableSignature, SignatureError> {
let signature = executor::block_on(self.sign_message(text))
.map_err(|e| SignatureError::ThirdPartyError(e.to_string()))?;

Ok(RecoverableSignature::Eip191Signature(signature.to_vec()))
Ok(executor::block_on(self.sign_message(text))?.to_vec().into())
}
}
1 change: 0 additions & 1 deletion xmtp_mls/src/owner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// #[cfg(feature = "ethers")]
mod evm_owner;
2 changes: 1 addition & 1 deletion xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ where

#[cfg(test)]
mod tests {
use std::{boxed::Box, fs};
use std::fs;

use super::{
db_connection::DbConnection, identity::StoredIdentity, EncryptedMessageStore, StorageError,
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/storage/encrypted_store/refresh_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl DbConnection<'_> {
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::{storage::encrypted_store::tests::with_connection, Store};
use crate::storage::encrypted_store::tests::with_connection;

#[test]
fn get_cursor_with_no_existing_state() {
Expand Down
Loading