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

ERC-1271 support and testing a partner wallet app. #609

Merged
merged 12 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tls_codec = "0.4.0"
tokio = { version = "1.35.1", features = ["macros"] }
tonic = "^0.11"
tracing = "0.1"
anyhow = "1.0"

# Internal Crate Dependencies
xmtp_cryptography = { path = "xmtp_cryptography" }
Expand Down
6 changes: 0 additions & 6 deletions bindings_ffi/Cargo.lock

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

3 changes: 2 additions & 1 deletion xmtp_id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ chrono.workspace = true
serde.workspace = true
async-trait.workspace = true
futures.workspace = true

ethers.workspace = true
anyhow.workspace = true
26 changes: 26 additions & 0 deletions xmtp_id/abi/ERC1271.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"inputs": [
{
"internalType": "bytes32",
"name": "_hash",
"type": "bytes32"
},
{
"internalType": "bytes",
"name": "_signature",
"type": "bytes"
}
],
"name": "isValidSignature",
"outputs": [
{
"internalType": "bytes4",
"name": "magicValue",
"type": "bytes4"
}
],
"stateMutability": "view",
"type": "function"
}
]
45 changes: 45 additions & 0 deletions xmtp_id/src/erc1271_verifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use ethers::types::{Address, BlockNumber, Bytes};
use ethers::providers::{Http, Middleware, Provider};
use std::convert::TryFrom;
use std::sync::Arc;
use anyhow::Error;
use ethers::contract::abigen;

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

abigen!(
ERC1271,
"./abi/ERC1271.json",
derives(serde::Serialize, serde::Deserialize)
);

pub struct ERC1271Verifier {
pub provider: Arc<Provider<Http>>,
}

impl ERC1271Verifier {
pub fn new(url: &str) -> Self {
let provider = Arc::new(Provider::<Http>::try_from(url).unwrap());
Self { provider }
}

/// Verifies an ERC-1271(https://eips.ethereum.org/EIPS/eip-1271) signature.
///
/// # Arguments
///
/// * `wallet_address` - Address of the ERC1271 wallet.
37ng marked this conversation as resolved.
Show resolved Hide resolved
/// * `block_number` - Block number to verify the signature at.
/// * `hash`, `signature` - Inputs to ERC-1271, used for signer verification.
pub async fn is_valid_signature<M: Middleware>(&self, wallet_address: Address, block_number: BlockNumber, hash: [u8; 32], signature: Bytes) -> Result<bool, Error> {
let erc1271 = ERC1271::new(wallet_address, self.provider.clone());

let res: [u8; 4] = erc1271
.is_valid_signature(hash, signature)
.block(block_number)
.call()
.await?
.into();

Check warning on line 41 in xmtp_id/src/erc1271_verifier.rs

View workflow job for this annotation

GitHub Actions / workspace

useless conversion to the same type: `[u8; 4]`

warning: useless conversion to the same type: `[u8; 4]` --> xmtp_id/src/erc1271_verifier.rs:36:28 | 36 | let res: [u8; 4] = erc1271 | ____________________________^ 37 | | .is_valid_signature(hash, signature) 38 | | .block(block_number) 39 | | .call() 40 | | .await? 41 | | .into(); | |___________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default help: consider removing `.into()` | 36 ~ let res: [u8; 4] = erc1271 37 + .is_valid_signature(hash, signature) 38 + .block(block_number) 39 + .call() 40 ~ .await?; |
37ng marked this conversation as resolved.
Show resolved Hide resolved

Ok(res == EIP1271_MAGIC_VALUE)
}
}
2 changes: 1 addition & 1 deletion xmtp_id/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod credential_verifier;
pub mod verified_key_package;
pub mod erc1271_verifier;

use std::sync::RwLock;

Expand Down
Loading