Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Oct 18, 2024
1 parent 474f67d commit 92579ed
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ log = "0.4.20"

# [profile.dev]
# debug = 0

[patch.'https://github.com/reown-com/erc6492.git']
erc6492 = { path = "../erc6492-rs" }
7 changes: 6 additions & 1 deletion crates/ffi/src/account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ impl FFIAccountClient {
});
}

Ok(self.account_client.do_sign_message(signatures2).await.to_string())
Ok(self
.account_client
.do_sign_message(signatures2)
.await
.map_err(|e| FFIError::Unknown(e.to_string()))?
.to_string())
}

pub async fn send_transactions(
Expand Down
2 changes: 1 addition & 1 deletion crates/yttrium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alloy = { version = "0.3.6", features = [
"signer-mnemonic",
"eip712",
] }
erc6492 = { git = "https://github.com/reown-com/erc6492.git", branch = "feat/create-6492" }
# foundry-block-explorers = "0.2.3"
getrandom = { version = "0.2", features = ["js"] }

Expand Down Expand Up @@ -48,7 +49,6 @@ reqwest.workspace = true
[dev-dependencies]
# mocking
wiremock = "0.6.0"
erc6492 = { git = "https://github.com/reown-com/erc6492.git", branch = "feat/verify-message-hash" }

# Networking
reqwest.workspace = true
Expand Down
21 changes: 16 additions & 5 deletions crates/yttrium/src/account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::bundler::{
use crate::config::Config;
use crate::private_key_service::PrivateKeyService;
use crate::sign_service::SignService;
use crate::smart_accounts::safe::{prepare_sign, sign, PreparedSignature};
use crate::smart_accounts::safe::{
prepare_sign, sign, Owners, PreparedSignature,
};
use crate::transaction::send::safe_test::{
self, DoSendTransactionParams, OwnerSignature, PreparedSendTransaction,
};
Expand Down Expand Up @@ -157,7 +159,7 @@ impl AccountClient {
pub async fn do_sign_message(
&self,
signatures: Vec<OwnerSignature>,
) -> Bytes {
) -> eyre::Result<Bytes> {
if !self.safe {
unimplemented!(
"sign_message is not supported for non-safe accounts"
Expand All @@ -170,12 +172,21 @@ impl AccountClient {
self.config.endpoints.rpc.base_url.parse().unwrap(),
);

sign(
self.owner.parse::<Address>().unwrap().into(),
Ok(sign(
Owners {
owners: vec![self.owner.parse::<Address>().unwrap()],
threshold: 1,
},
self.get_address()
.await
.unwrap()
.parse::<Address>()
.unwrap()
.into(),
signatures,
&provider,
)
.await
.await)
}

pub async fn send_transactions(
Expand Down
22 changes: 18 additions & 4 deletions crates/yttrium/src/smart_accounts/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use alloy::{
sol,
sol_types::{SolCall, SolValue},
};
use erc6492::create::create_erc6492_signature;
use serde::{Deserialize, Serialize};

sol! {
Expand Down Expand Up @@ -303,7 +304,11 @@ pub fn prepare_sign(
PreparedSignature { safe_message, domain }
}

// TODO refactor to make account_address optional, if not provided it will
// determine it based on Owners TODO refactor to make owners optional, in the
// case where it already being deployed is assumed
pub async fn sign<P, T, N>(
owners: Owners,
account_address: AccountAddress,
signatures: Vec<OwnerSignature>,
provider: &P,
Expand All @@ -319,20 +324,29 @@ where

let signature = Bytes::from(signatures[0].signature.as_bytes());

// Null validator address for regular Safe signature
let signature = (Address::ZERO, signature).abi_encode_packed().into();

println!("signature: {:?}", signature);

let signature = if provider
.get_code_at(account_address.into())
.await
.unwrap() // TODO handle error
.is_empty()
{
// TODO check if deployed, if so do ERC-6492
signature
create_erc6492_signature(
SAFE_PROXY_FACTORY_ADDRESS,
factory_data(owners).abi_encode().into(),
signature,
)
} else {
signature
};

// Null validator address for regular Safe signature
(Address::ZERO, signature).abi_encode_packed().into()
println!("signature (w/ 6492): {:?}", signature);

signature
}

#[cfg(test)]
Expand Down
61 changes: 50 additions & 11 deletions crates/yttrium/src/transaction/send/safe_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ mod tests {
providers::{ext::AnvilApi, PendingTransactionConfig, ProviderBuilder},
rpc::types::TransactionRequest,
sol,
sol_types::SolValue,
};
use erc6492::create::ERC6492_MAGIC_BYTES;

async fn use_faucet(
provider: ReqwestProvider,
Expand Down Expand Up @@ -831,12 +833,10 @@ mod tests {

let owner = LocalSigner::random();
let owner_address = owner.address();
let owners = Owners { owners: vec![owner.address()], threshold: 1 };

let sender_address = get_account_address(
provider.clone(),
Owners { owners: vec![owner.address()], threshold: 1 },
)
.await;
let sender_address =
get_account_address(provider.clone(), owners.clone()).await;

let receipt = send_transactions(
vec![],
Expand Down Expand Up @@ -865,6 +865,7 @@ mod tests {
owner.sign_typed_data_sync(&safe_message, &domain).unwrap();

let signature = sign(
owners,
sender_address,
vec![OwnerSignature { owner: owner_address, signature }],
&provider,
Expand Down Expand Up @@ -897,7 +898,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "not implemented yet"]
// #[ignore = "not implemented yet"]
async fn test_sign_message_not_deployed() {
let config = Config::local();
let provider = ReqwestProvider::<Ethereum>::new_http(
Expand All @@ -906,12 +907,10 @@ mod tests {

let owner = LocalSigner::random();
let owner_address = owner.address();
let owners = Owners { owners: vec![owner.address()], threshold: 1 };

let sender_address = get_account_address(
provider.clone(),
Owners { owners: vec![owner.address()], threshold: 1 },
)
.await;
let sender_address =
get_account_address(provider.clone(), owners.clone()).await;

assert!(provider
.get_code_at(sender_address.into())
Expand All @@ -930,6 +929,7 @@ mod tests {
owner.sign_typed_data_sync(&safe_message, &domain).unwrap();

let signature = sign(
owners,
sender_address,
vec![OwnerSignature { owner: owner_address, signature }],
&provider,
Expand All @@ -942,6 +942,45 @@ mod tests {
.unwrap()
.is_empty());

let working_signature = alloy::primitives::bytes!("0000000000000000000000004e1dcf7ad4e460cfd30791ccc4f9c8a4f820ec67000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000001241688f0b9000000000000000000000000ebe001b3d534b9b6e2500fb78e67a1a137f561ce0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000007a6733696a793000000000000000000000000000000000000000000000000000000000000000000000844fff40e16f8df5c33b2c7d965da59c4388f834ccb5d47bd34540071e8f1d9dc5f01562e8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000413166c6ce0ad1c80ff9451f750940773f745179462eed1c6be7f49fc3e213f59b2ff5dba38c2ca56b7ddef619a782837e7faf2a5aa50f1b0393444efd90a982eb20000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492");

let sig = &working_signature[..working_signature.len() - 32];
println!("sig working : {}", hex::encode(sig));
let decoded: (Address, Bytes, Bytes) =
alloy::sol_types::SolValue::abi_decode_params(sig, true).unwrap();
println!("decoded working: {:?}", decoded);
let working_signature: Bytes = (
(
decoded.0,
decoded.1,
(Address::ZERO, decoded.2).abi_encode_packed(),
)
.abi_encode_para(),
ERC6492_MAGIC_BYTES,
)
.abi_encode_packed()
.into();

assert!(erc6492::verify_signature(
working_signature.clone(),
alloy::primitives::address!(
"b9c5de50e15d52764eA43c858eD9F57C964960Cd"
),
eip191_hash_message("Hello AppKit!"),
&ReqwestProvider::<Ethereum>::new_http(
"https://rpc.sepolia.org".parse().unwrap(),
)
)
.await
.unwrap()
.is_valid());

// let sig = &signature[..signature.len() - 32];
// println!("sig not working: {}", hex::encode(sig));
// let decoded: (Address, Bytes, Bytes) =
// alloy::sol_types::SolValue::abi_decode_params(sig, true).unwrap();
// println!("decoded not working: {:?}", decoded);

assert!(erc6492::verify_signature(
signature,
sender_address.into(),
Expand Down

0 comments on commit 92579ed

Please sign in to comment.