Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
EIP-712 implementation (#9631)
Browse files Browse the repository at this point in the history
* EIP-712 impl

* added more tests

* removed size parsing unwrap

* corrected TYPE_REGEX to disallow zero sized fixed length arrays, replaced LinkedHashSet with IndexSet, added API spec to docs, fixed Type::Byte encoding branch

* use Option<u64> instead of u64 for Type::Array::Length

* replace `.iter()` with  `.values()`

Co-Authored-By: seunlanlege <[email protected]>

* tabify eip712.rs

* use proper comments for docs

* Cargo.lock: revert unrelated changes

* tabify encode.rs
  • Loading branch information
seunlanlege committed Nov 14, 2018
1 parent c922135 commit 0955316
Show file tree
Hide file tree
Showing 12 changed files with 1,296 additions and 2 deletions.
153 changes: 152 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ parity-updater = { path = "../updater" }
parity-version = { path = "../util/version" }
patricia-trie = "0.2"
rlp = { version = "0.2.4", features = ["ethereum"] }
eip712 = { path = "../util/EIP-712" }
stats = { path = "../util/stats" }
vm = { path = "../ethcore/vm" }

Expand Down
1 change: 1 addition & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extern crate parity_reactor;
extern crate parity_updater as updater;
extern crate parity_version as version;
extern crate patricia_trie as trie;
extern crate eip712;
extern crate rlp;
extern crate stats;
extern crate vm;
Expand Down
8 changes: 8 additions & 0 deletions rpc/src/v1/helpers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ pub fn signing(error: AccountError) -> Error {
}
}

pub fn invalid_call_data<T: fmt::Display>(error: T) -> Error {
Error {
code: ErrorCode::ServerError(codes::ENCODING_ERROR),
message: format!("{}", error),
data: None
}
}

pub fn password(error: AccountError) -> Error {
Error {
code: ErrorCode::ServerError(codes::PASSWORD_INVALID),
Expand Down
24 changes: 24 additions & 0 deletions rpc/src/v1/impls/personal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use v1::types::{
RichRawTransaction as RpcRichRawTransaction,
};
use v1::metadata::Metadata;
use eip712::{EIP712, hash_structured_data};

/// Account management (personal) rpc implementation.
pub struct PersonalClient<D: Dispatcher> {
Expand Down Expand Up @@ -150,6 +151,29 @@ impl<D: Dispatcher + 'static> Personal for PersonalClient<D> {
}))
}

fn sign_typed_data(&self, typed_data: EIP712, account: RpcH160, password: String) -> BoxFuture<RpcH520> {
let data = match hash_structured_data(typed_data) {
Ok(d) => d,
Err(err) => return Box::new(future::done(Err(errors::invalid_call_data(err.kind())))),
};
let dispatcher = self.dispatcher.clone();
let accounts = self.accounts.clone();

let payload = RpcConfirmationPayload::EthSignMessage((account.clone(), RpcBytes(data)).into());

Box::new(dispatch::from_rpc(payload, account.into(), &dispatcher)
.and_then(|payload| {
dispatch::execute(dispatcher, accounts, payload, dispatch::SignWith::Password(password.into()))
})
.map(|v| v.into_value())
.then(|res| match res {
Ok(RpcConfirmationResponse::Signature(signature)) => Ok(signature),
Err(e) => Err(e),
e => Err(errors::internal("Unexpected result", e)),
})
)
}

fn ec_recover(&self, data: RpcBytes, signature: RpcH520) -> BoxFuture<RpcH160> {
let signature: H520 = signature.into();
let signature = Signature::from_electrum(&signature);
Expand Down
7 changes: 6 additions & 1 deletion rpc/src/v1/traits/personal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Personal rpc interface.
use jsonrpc_core::{BoxFuture, Result};

use eip712::EIP712;
use v1::types::{Bytes, U128, H160, H256, H520, TransactionRequest, RichRawTransaction as RpcRichRawTransaction};

build_rpc_trait! {
Expand All @@ -42,6 +42,11 @@ build_rpc_trait! {
#[rpc(name = "personal_sign")]
fn sign(&self, Bytes, H160, String) -> BoxFuture<H520>;

/// Produces an EIP-712 compliant signature with given account using the given password to unlock the
/// account during the request.
#[rpc(name = "personal_signTypedData")]
fn sign_typed_data(&self, EIP712, H160, String) -> BoxFuture<H520>;

/// Returns the account associated with the private key that was used to calculate the signature in
/// `personal_sign`.
#[rpc(name = "personal_ecRecover")]
Expand Down
23 changes: 23 additions & 0 deletions util/EIP-712/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "eip712"
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
serde_derive = "1.0"
serde = "1.0"
serde_json = "1.0"
ethabi = "6.0"
keccak-hash = "0.1"
ethereum-types = "0.4"
failure = "0.1"
itertools = "0.7"
failure_derive = "0.1"
lazy_static = "1.1"
toolshed = "0.4"
regex = "1.0"
validator = "0.8"
validator_derive = "0.8"
lunarity-lexer = "0.1"
rustc-hex = "2.0"
indexmap = "1.0.2"
177 changes: 177 additions & 0 deletions util/EIP-712/src/eip712.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! EIP712 structs
use serde_json::{Value};
use std::collections::HashMap;
use ethereum_types::{U256, H256, Address};
use regex::Regex;
use validator::Validate;
use validator::ValidationErrors;

pub(crate) type MessageTypes = HashMap<String, Vec<FieldType>>;

lazy_static! {
// match solidity identifier with the addition of '[(\d)*]*'
static ref TYPE_REGEX: Regex = Regex::new(r"^[a-zA-Z_$][a-zA-Z_$0-9]*(\[([1-9]\d*)*\])*$").unwrap();
static ref IDENT_REGEX: Regex = Regex::new(r"^[a-zA-Z_$][a-zA-Z_$0-9]*$").unwrap();
}

#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[derive(Deserialize, Serialize, Validate, Debug, Clone)]
pub(crate) struct EIP712Domain {
pub(crate) name: String,
pub(crate) version: String,
pub(crate) chain_id: U256,
pub(crate) verifying_contract: Address,
#[serde(skip_serializing_if="Option::is_none")]
pub(crate) salt: Option<H256>,
}
/// EIP-712 struct
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[derive(Deserialize, Debug, Clone)]
pub struct EIP712 {
pub(crate) types: MessageTypes,
pub(crate) primary_type: String,
pub(crate) message: Value,
pub(crate) domain: EIP712Domain,
}

impl Validate for EIP712 {
fn validate(&self) -> Result<(), ValidationErrors> {
for field_types in self.types.values() {
for field_type in field_types {
field_type.validate()?;
}
}
Ok(())
}
}

#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub(crate) struct FieldType {
#[validate(regex = "IDENT_REGEX")]
pub name: String,
#[serde(rename = "type")]
#[validate(regex = "TYPE_REGEX")]
pub type_: String,
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::from_str;

#[test]
fn test_regex() {
let test_cases = vec!["unint bytes32", "Seun\\[]", "byte[]uint", "byte[7[]uint][]", "Person[0]"];
for case in test_cases {
assert_eq!(TYPE_REGEX.is_match(case), false)
}

let test_cases = vec!["bytes32", "Foo[]", "bytes1", "bytes32[][]", "byte[9]", "contents"];
for case in test_cases {
assert_eq!(TYPE_REGEX.is_match(case), true)
}
}

#[test]
fn test_deserialization() {
let string = r#"{
"primaryType": "Mail",
"domain": {
"name": "Ether Mail",
"version": "1",
"chainId": "0x1",
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
},
"types": {
"EIP712Domain": [
{ "name": "name", "type": "string" },
{ "name": "version", "type": "string" },
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"Person": [
{ "name": "name", "type": "string" },
{ "name": "wallet", "type": "address" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person" },
{ "name": "contents", "type": "string" }
]
}
}"#;
let _ = from_str::<EIP712>(string).unwrap();
}

#[test]
fn test_failing_deserialization() {
let string = r#"{
"primaryType": "Mail",
"domain": {
"name": "Ether Mail",
"version": "1",
"chainId": "0x1",
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
},
"types": {
"EIP712Domain": [
{ "name": "name", "type": "string" },
{ "name": "version", "type": "string" },
{ "name": "chainId", "type": "7uint256[x] Seun" },
{ "name": "verifyingContract", "type": "address" }
],
"Person": [
{ "name": "name", "type": "string" },
{ "name": "wallet amen", "type": "address" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person" },
{ "name": "contents", "type": "string" }
]
}
}"#;
let data = from_str::<EIP712>(string).unwrap();
assert_eq!(data.validate().is_err(), true);
}
}
Loading

0 comments on commit 0955316

Please sign in to comment.