This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08344e0
commit 05526ca
Showing
7 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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/>. | ||
|
||
//! EIP-191 compliant decoding | ||
use v1::types::{EIP191Version, Bytes, WithValidator}; | ||
use eip712::{hash_structured_data, EIP712}; | ||
use serde_json::{Value, from_value}; | ||
use v1::helpers::errors; | ||
use jsonrpc_core::Error; | ||
use v1::helpers::dispatch::eth_data_hash; | ||
use hash::keccak; | ||
use std::fmt::Display; | ||
use ethereum_types::H256; | ||
|
||
pub fn decode_message(version: EIP191Version, data: Value) -> Result<H256, Error> { | ||
let data = match version { | ||
EIP191Version::StructuredData => { | ||
let typed_data = from_value::<EIP712>(data) | ||
.map_err(map_serde_err("StructuredData"))?; | ||
|
||
hash_structured_data(typed_data) | ||
.map_err(|err| errors::invalid_call_data(err.kind()))? | ||
} | ||
|
||
EIP191Version::WithValidator => { | ||
let typed_data = from_value::<WithValidator>(data) | ||
.map_err(map_serde_err("WithValidator"))?; | ||
let prefix = b"\x19\x00"; | ||
let data = [&prefix[..], &typed_data.address.0[..], &typed_data.application_data.0[..]].concat(); | ||
keccak(data) | ||
} | ||
|
||
EIP191Version::PersonalMessage => { | ||
let bytes = from_value::<Bytes>(data) | ||
.map_err(map_serde_err("Bytes"))?; | ||
eth_data_hash(bytes.0) | ||
} | ||
}; | ||
|
||
Ok(data) | ||
} | ||
|
||
fn map_serde_err<T: Display>(struct_name: &'static str) -> impl Fn(T) -> Error { | ||
move |error: T| { | ||
errors::invalid_call_data(format!("Error deserializing '{}': {}", struct_name, error)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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/>. | ||
|
||
//! EIP-191 specific types | ||
use serde::{Deserialize, Deserializer}; | ||
use serde::de; | ||
use v1::types::{H160, Bytes}; | ||
|
||
pub enum EIP191Version { | ||
StructuredData, | ||
PersonalMessage, | ||
WithValidator | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct WithValidator { | ||
// address of intended validator | ||
pub address: H160, | ||
// application specific data | ||
pub application_data: Bytes | ||
} | ||
|
||
impl<'de> Deserialize<'de> for EIP191Version { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
let byte_version = match s.as_str() { | ||
"0x00" => EIP191Version::WithValidator, | ||
"0x01" => EIP191Version::StructuredData, | ||
"0x45" => EIP191Version::PersonalMessage, | ||
other => return Err(de::Error::custom(format!("Invalid byte version '{}'", other))), | ||
}; | ||
Ok(byte_version) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters