Skip to content

Commit

Permalink
modified creation of EIP712 message (WIP: not recovering yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHongos committed Dec 6, 2023
1 parent d3a12c5 commit 3dab165
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ yew = { version="0.20", features=["csr"] }
alloy-providers = { git="https://github.com/alloy-rs/alloy" }
alloy-json-rpc = { git="https://github.com/alloy-rs/alloy" }
alloy-rpc-types = { git="https://github.com/alloy-rs/alloy" }
alloy-web = { path = "../../alloy-web" }
#git="https://github.com/DrHongos/alloy-web"
alloy-web = { git="https://github.com/DrHongos/alloy-web" }
#path = "../../alloy-web"
alloy-primitives = "0.5.2"
serde_json = "1.0.108"
alloy-dyn-abi = { version = "0.5.2", features = ["eip712"] }
Expand Down
146 changes: 27 additions & 119 deletions src/components/button_sign.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_dyn_abi::eip712::TypedData;
use alloy_sol_types::Eip712Domain;
use alloy_sol_types::{Eip712Domain, SolStruct};
use alloy_sol_macro::sol;
use alloy_primitives::{U256, FixedBytes, Address, hex};
use alloy_dyn_abi::Resolver;
Expand All @@ -14,124 +14,30 @@ use ethers::core::types::Signature;
use std::str::FromStr;

/*
take from the ecrecover method
https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/util/src/signature.ts
https://github.com/MetaMask/eth-sig-util/blob/main/src/utils.ts
using 'ethers' only to recover signatures => find alloy's prospect into integrating this
*/

const DOCUMENT_SIGNATURE_NAME: &str = "DocumentSignature";
const VERIFIER_NAME: &str = "Test App";

fn typed_data_for_document(name: &str, chain_id_v: u64) -> TypedData {
fn typed_data_for_document(name: String, chain_id_v: u64) -> TypedData {

sol! {
struct EIP712Domain {
string name;
string version;
uint256 chainId;
address verifyingContract;
bytes32 salt;
}
sol! {
#[derive(Serialize)]
struct DocumentSignature {
string name;
string content;
}
};
// create resolver
let mut graph = Resolver::default();
graph.ingest_sol_struct::<EIP712Domain>();
graph.ingest_sol_struct::<DocumentSignature>();

TypedData {
domain: Eip712Domain {
name: Some(Cow::Borrowed(VERIFIER_NAME)),
version: Some(Cow::Borrowed("1")),
chain_id: Some(U256::from(chain_id_v)),
verifying_contract: Some(Address::ZERO),
salt: Some(FixedBytes::ZERO),
},
resolver: graph,
primary_type: DOCUMENT_SIGNATURE_NAME.to_string(),
message: DocumentDescription::new(name).into_value(),
}

/*
// example test
let s: FixedBytes<32> = FixedBytes::ZERO;
let json = json!({
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
},
{
"name": "salt",
"type": "bytes32"
}
]
},
"primaryType": "EIP712Domain",
"domain": {
"name": "example.metamask.io",
"version": "1",
"chainId": 1,
"verifyingContract": "0x0000000000000000000000000000000000000000",
"salt": s
},
"message": {}
});
let typed_data: TypedData = serde_json::from_value(json).unwrap();
// test? FAILED!
let hash = typed_data.eip712_signing_hash().unwrap();
log(format!(
"signature hash is {:#?} and should be 122d1c8ef94b76dad44dcb03fa772361e20855c63311a15d5afe02d1b38f6077",
hex::encode(hash)
).as_str());
//
typed_data
// end of example
*/
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentDescription {
pub name: String,
pub content: String,
}

impl DocumentDescription {

pub fn into_value(&self) -> serde_json::Value {
let mut m = Map::new();
m.insert("name".to_string(), serde_json::Value::String(self.name.clone()));
m.insert("content".to_string(), serde_json::Value::String(self.content.clone()));
serde_json::Value::Object(m)
}

pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
content: format!("By signing this message you comply with {}. This request will not trigger a blockchain transaction or cost any gas fees.", name),
}
}

let domain = alloy_sol_types::eip712_domain!(
name: name.clone(),
version: "1",
chain_id: chain_id_v,
);
let doc = DocumentSignature {
name: (name.into()),
content: "content of the doc".into(),
};

TypedData::from_struct(&doc, Some(domain))
}

#[function_component(SignatureButton)]
Expand All @@ -144,9 +50,9 @@ pub fn signature_button() -> Html {
let ethereum = ethereum.clone();
Callback::from(move |_: MouseEvent| {
if ethereum.is_connected() {
let data = typed_data_for_document("Content of this Document", chain_id);
let data = typed_data_for_document("Rust Dapp".to_string(), chain_id);
log(format!("TypedData {:#?}", data).as_str());

let ethereum = ethereum.clone();
spawn_local(async move {
let jason = json!(data).to_string();
Expand All @@ -155,19 +61,21 @@ pub fn signature_button() -> Html {
.sign_typed_data(jason, &ethereum.account())
.await
.expect("Could not sign message");

log(format!("Signed message..{:#?}", &signature_res).as_str());
let signature = Signature::from_str(&signature_res).expect("Could not parse signature");

let signature_p = signature_res.strip_prefix("0x").unwrap();
let signature_p = hex::decode(signature_p).expect("Hex error");
let signature = Signature::try_from(signature_p.as_slice()).expect("Could not parse Signature");
log(format!("Signature..{:#?}", signature).as_str());

// recover
// recover
let eip712_encoded_data = data.encode_data().expect("Could not encode eip712 data");
log(format!("encoded eip712 data..{:#?}", eip712_encoded_data).as_str());

//log(format!("encoded eip712 data..{:#?}", eip712_encoded_data.to_string()).as_str());
let eip712_signing_hash = data.eip712_signing_hash().expect("Could not encode eip 712 signing hash");
log(format!("encoded signing hash..{:#?}", eip712_signing_hash).as_str());
// BUG: error on some part of the process as the recovered address isn't the signer
let rec = signature.recover(eip712_encoded_data);
let rec = signature.recover(eip712_encoded_data.as_slice());
log(format!("Signing with {:?} recovered {:?}", ethereum.account(), rec).as_str());

});
} else {
log("Are we disconnected?");
Expand Down

0 comments on commit 3dab165

Please sign in to comment.