rust-web3 icon indicating copy to clipboard operation
rust-web3 copied to clipboard

EIP712

Open BlinkyStitt opened this issue 7 years ago • 5 comments
trafficstars

I'm wanting to hash 0x orders and they use EIP712. I'm trying to write this in rust and not javascript. Is this currently possible with rust-web3?

BlinkyStitt avatar Nov 06 '18 01:11 BlinkyStitt

@WyseNynja We don't have that method exposed yet (PRs welcome!). You can freely extend rust-web3 though, you will just need to define all the input and output types and their deserialization.

tomusdrw avatar Nov 09 '18 11:11 tomusdrw

There is a separate crate which may fit the bill here: eip-712

That3Percent avatar Jun 16 '20 22:06 That3Percent

Doesn't that conflict with this library's MIT license?

gakonst avatar Jun 18 '20 06:06 gakonst

@gakonst Correct, the crate mentioned here can't really be included in web3, but you are fine to build a GPL-3.0 project that uses both web3 and eip-712 to achieve what you need.

tomusdrw avatar Jun 18 '20 09:06 tomusdrw

Dismayed by the licensing, I went ahead to build a replacement for the GPL licensed eip-712 crate that is MIT licensed and is based on structs instead of JSON. It's not yet batteries included, but you can see examples of how to use it in the tests section.

https://github.com/graphprotocol/eip-712-derive

Example usage:

struct Mail {
    from: Person,
    to: Person,
    contents: String,
}
// This is all that is necessary to implement any EIP-712 signable struct
impl StructType for Mail {
    const TYPE_NAME: &'static str = "Mail";
    fn visit_members<T: MemberVisitor>(&self, visitor: &mut T) {
        visitor.visit("from", &self.from);
        visitor.visit("to", &self.to);
        visitor.visit("contents", &self.contents);
    }
}

/* Elided Person struct, etc */

fn test() -> (Signature, RecoveryId) {
 let domain = DomainStruct {
        name: "Ether Mail".to_owned(),
        version: "1".to_owned(),
        chain_id: chain_id::MAIN_NET,
        verifying_contract: Address(
            (&(hex::decode("CcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC").unwrap())[..])
                .try_into()
                .unwrap(),
        ),
    };
let domain_separator = DomainSeparator::new(&domain);
let message = Mail {
        from: Person { /* elided */ },
        to: Person { /* elided */ },
        contents: "Hello, Bob!".to_owned(),
    };
 sign_typed(&domain_separator, &message, &pk)
}

That3Percent avatar Jul 02 '20 16:07 That3Percent