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

Commit 74fadbb

Browse files
committed
EIP-712 implementation (#9631)
* 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
1 parent 2919cfc commit 74fadbb

File tree

12 files changed

+1277
-4
lines changed

12 files changed

+1277
-4
lines changed

Cargo.lock

Lines changed: 133 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ parity-updater = { path = "../updater" }
5959
parity-version = { path = "../util/version" }
6060
patricia-trie = "0.3.0"
6161
rlp = { version = "0.3.0", features = ["ethereum"] }
62+
eip712 = { path = "../util/EIP-712" }
6263
stats = { path = "../util/stats" }
6364
vm = { path = "../ethcore/vm" }
6465

rpc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern crate parity_runtime;
6363
extern crate parity_updater as updater;
6464
extern crate parity_version as version;
6565
extern crate patricia_trie as trie;
66+
extern crate eip712;
6667
extern crate rlp;
6768
extern crate stats;
6869
extern crate vm;

rpc/src/v1/helpers/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ pub fn signing(error: AccountError) -> Error {
286286
}
287287
}
288288

289+
pub fn invalid_call_data<T: fmt::Display>(error: T) -> Error {
290+
Error {
291+
code: ErrorCode::ServerError(codes::ENCODING_ERROR),
292+
message: format!("{}", error),
293+
data: None
294+
}
295+
}
296+
289297
pub fn password(error: AccountError) -> Error {
290298
Error {
291299
code: ErrorCode::ServerError(codes::PASSWORD_INVALID),

rpc/src/v1/impls/personal.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use v1::types::{
3838
RichRawTransaction as RpcRichRawTransaction,
3939
};
4040
use v1::metadata::Metadata;
41+
use eip712::{EIP712, hash_structured_data};
4142

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

154+
fn sign_typed_data(&self, typed_data: EIP712, account: RpcH160, password: String) -> BoxFuture<RpcH520> {
155+
let data = match hash_structured_data(typed_data) {
156+
Ok(d) => d,
157+
Err(err) => return Box::new(future::done(Err(errors::invalid_call_data(err.kind())))),
158+
};
159+
let dispatcher = self.dispatcher.clone();
160+
let accounts = self.accounts.clone();
161+
162+
let payload = RpcConfirmationPayload::EthSignMessage((account.clone(), RpcBytes(data)).into());
163+
164+
Box::new(dispatch::from_rpc(payload, account.into(), &dispatcher)
165+
.and_then(|payload| {
166+
dispatch::execute(dispatcher, accounts, payload, dispatch::SignWith::Password(password.into()))
167+
})
168+
.map(|v| v.into_value())
169+
.then(|res| match res {
170+
Ok(RpcConfirmationResponse::Signature(signature)) => Ok(signature),
171+
Err(e) => Err(e),
172+
e => Err(errors::internal("Unexpected result", e)),
173+
})
174+
)
175+
}
176+
153177
fn ec_recover(&self, data: RpcBytes, signature: RpcH520) -> BoxFuture<RpcH160> {
154178
let signature: H520 = signature.into();
155179
let signature = Signature::from_electrum(&signature);

rpc/src/v1/traits/personal.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
//! Personal rpc interface.
1818
use jsonrpc_core::{BoxFuture, Result};
19-
19+
use eip712::EIP712;
2020
use v1::types::{Bytes, U128, H160, H256, H520, TransactionRequest, RichRawTransaction as RpcRichRawTransaction};
2121

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

45+
/// Produces an EIP-712 compliant signature with given account using the given password to unlock the
46+
/// account during the request.
47+
#[rpc(name = "personal_signTypedData")]
48+
fn sign_typed_data(&self, EIP712, H160, String) -> BoxFuture<H520>;
49+
4550
/// Returns the account associated with the private key that was used to calculate the signature in
4651
/// `personal_sign`.
4752
#[rpc(name = "personal_ecRecover")]

util/EIP-712/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "eip712"
3+
version = "0.1.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
6+
[dependencies]
7+
serde_derive = "1.0"
8+
serde = "1.0"
9+
serde_json = "1.0"
10+
ethabi = "6.0"
11+
keccak-hash = "0.1"
12+
ethereum-types = "0.4"
13+
failure = "0.1"
14+
itertools = "0.7"
15+
failure_derive = "0.1"
16+
lazy_static = "1.1"
17+
toolshed = "0.4"
18+
regex = "1.0"
19+
validator = "0.8"
20+
validator_derive = "0.8"
21+
lunarity-lexer = "0.1"
22+
rustc-hex = "2.0"
23+
indexmap = "1.0.2"

util/EIP-712/src/eip712.rs

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! EIP712 structs
18+
use serde_json::{Value};
19+
use std::collections::HashMap;
20+
use ethereum_types::{U256, H256, Address};
21+
use regex::Regex;
22+
use validator::Validate;
23+
use validator::ValidationErrors;
24+
25+
pub(crate) type MessageTypes = HashMap<String, Vec<FieldType>>;
26+
27+
lazy_static! {
28+
// match solidity identifier with the addition of '[(\d)*]*'
29+
static ref TYPE_REGEX: Regex = Regex::new(r"^[a-zA-Z_$][a-zA-Z_$0-9]*(\[([1-9]\d*)*\])*$").unwrap();
30+
static ref IDENT_REGEX: Regex = Regex::new(r"^[a-zA-Z_$][a-zA-Z_$0-9]*$").unwrap();
31+
}
32+
33+
#[serde(rename_all = "camelCase")]
34+
#[serde(deny_unknown_fields)]
35+
#[derive(Deserialize, Serialize, Validate, Debug, Clone)]
36+
pub(crate) struct EIP712Domain {
37+
pub(crate) name: String,
38+
pub(crate) version: String,
39+
pub(crate) chain_id: U256,
40+
pub(crate) verifying_contract: Address,
41+
#[serde(skip_serializing_if="Option::is_none")]
42+
pub(crate) salt: Option<H256>,
43+
}
44+
/// EIP-712 struct
45+
#[serde(rename_all = "camelCase")]
46+
#[serde(deny_unknown_fields)]
47+
#[derive(Deserialize, Debug, Clone)]
48+
pub struct EIP712 {
49+
pub(crate) types: MessageTypes,
50+
pub(crate) primary_type: String,
51+
pub(crate) message: Value,
52+
pub(crate) domain: EIP712Domain,
53+
}
54+
55+
impl Validate for EIP712 {
56+
fn validate(&self) -> Result<(), ValidationErrors> {
57+
for field_types in self.types.values() {
58+
for field_type in field_types {
59+
field_type.validate()?;
60+
}
61+
}
62+
Ok(())
63+
}
64+
}
65+
66+
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
67+
pub(crate) struct FieldType {
68+
#[validate(regex = "IDENT_REGEX")]
69+
pub name: String,
70+
#[serde(rename = "type")]
71+
#[validate(regex = "TYPE_REGEX")]
72+
pub type_: String,
73+
}
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
use serde_json::from_str;
79+
80+
#[test]
81+
fn test_regex() {
82+
let test_cases = vec!["unint bytes32", "Seun\\[]", "byte[]uint", "byte[7[]uint][]", "Person[0]"];
83+
for case in test_cases {
84+
assert_eq!(TYPE_REGEX.is_match(case), false)
85+
}
86+
87+
let test_cases = vec!["bytes32", "Foo[]", "bytes1", "bytes32[][]", "byte[9]", "contents"];
88+
for case in test_cases {
89+
assert_eq!(TYPE_REGEX.is_match(case), true)
90+
}
91+
}
92+
93+
#[test]
94+
fn test_deserialization() {
95+
let string = r#"{
96+
"primaryType": "Mail",
97+
"domain": {
98+
"name": "Ether Mail",
99+
"version": "1",
100+
"chainId": "0x1",
101+
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
102+
},
103+
"message": {
104+
"from": {
105+
"name": "Cow",
106+
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
107+
},
108+
"to": {
109+
"name": "Bob",
110+
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
111+
},
112+
"contents": "Hello, Bob!"
113+
},
114+
"types": {
115+
"EIP712Domain": [
116+
{ "name": "name", "type": "string" },
117+
{ "name": "version", "type": "string" },
118+
{ "name": "chainId", "type": "uint256" },
119+
{ "name": "verifyingContract", "type": "address" }
120+
],
121+
"Person": [
122+
{ "name": "name", "type": "string" },
123+
{ "name": "wallet", "type": "address" }
124+
],
125+
"Mail": [
126+
{ "name": "from", "type": "Person" },
127+
{ "name": "to", "type": "Person" },
128+
{ "name": "contents", "type": "string" }
129+
]
130+
}
131+
}"#;
132+
let _ = from_str::<EIP712>(string).unwrap();
133+
}
134+
135+
#[test]
136+
fn test_failing_deserialization() {
137+
let string = r#"{
138+
"primaryType": "Mail",
139+
"domain": {
140+
"name": "Ether Mail",
141+
"version": "1",
142+
"chainId": "0x1",
143+
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
144+
},
145+
"message": {
146+
"from": {
147+
"name": "Cow",
148+
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
149+
},
150+
"to": {
151+
"name": "Bob",
152+
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
153+
},
154+
"contents": "Hello, Bob!"
155+
},
156+
"types": {
157+
"EIP712Domain": [
158+
{ "name": "name", "type": "string" },
159+
{ "name": "version", "type": "string" },
160+
{ "name": "chainId", "type": "7uint256[x] Seun" },
161+
{ "name": "verifyingContract", "type": "address" }
162+
],
163+
"Person": [
164+
{ "name": "name", "type": "string" },
165+
{ "name": "wallet amen", "type": "address" }
166+
],
167+
"Mail": [
168+
{ "name": "from", "type": "Person" },
169+
{ "name": "to", "type": "Person" },
170+
{ "name": "contents", "type": "string" }
171+
]
172+
}
173+
}"#;
174+
let data = from_str::<EIP712>(string).unwrap();
175+
assert_eq!(data.validate().is_err(), true);
176+
}
177+
}

0 commit comments

Comments
 (0)