EIP 191 (#9701)
* added sign_191 rpc method * fixed hash_structured_data return type * added ConfirmationPayload::SignMessage for non-prefixed signatures, added tests for sign191 * renamed WithValidator -> PresignedTransaction * rename applicationData to data in test * adds docs for EIP191Version, renamed SignRequest to EIP191SignRequest
This commit is contained in:
@@ -49,7 +49,8 @@ use v1::types::{
|
||||
RichRawTransaction as RpcRichRawTransaction,
|
||||
ConfirmationPayload as RpcConfirmationPayload,
|
||||
ConfirmationResponse,
|
||||
SignRequest as RpcSignRequest,
|
||||
EthSignRequest as RpcEthSignRequest,
|
||||
EIP191SignRequest as RpcSignRequest,
|
||||
DecryptRequest as RpcDecryptRequest,
|
||||
};
|
||||
use rlp;
|
||||
@@ -693,6 +694,19 @@ pub fn execute<D: Dispatcher + 'static>(
|
||||
);
|
||||
Box::new(future::done(res))
|
||||
},
|
||||
ConfirmationPayload::SignMessage(address, data) => {
|
||||
if accounts.is_hardware_address(&address) {
|
||||
return Box::new(future::err(errors::account("Error signing message with hardware_wallet",
|
||||
"Message signing is unsupported")));
|
||||
}
|
||||
let res = signature(&accounts, address, data, pass)
|
||||
.map(|result| result
|
||||
.map(|rsv| H520(rsv.into_electrum()))
|
||||
.map(RpcH520::from)
|
||||
.map(ConfirmationResponse::Signature)
|
||||
);
|
||||
Box::new(future::done(res))
|
||||
},
|
||||
ConfirmationPayload::Decrypt(address, data) => {
|
||||
if accounts.is_hardware_address(&address) {
|
||||
return Box::new(future::err(errors::unsupported("Decrypting via hardware wallets is not supported.", None)));
|
||||
@@ -775,8 +789,11 @@ pub fn from_rpc<D>(payload: RpcConfirmationPayload, default_account: Address, di
|
||||
RpcConfirmationPayload::Decrypt(RpcDecryptRequest { address, msg }) => {
|
||||
Box::new(future::ok(ConfirmationPayload::Decrypt(address.into(), msg.into())))
|
||||
},
|
||||
RpcConfirmationPayload::EthSignMessage(RpcSignRequest { address, data }) => {
|
||||
RpcConfirmationPayload::EthSignMessage(RpcEthSignRequest { address, data }) => {
|
||||
Box::new(future::ok(ConfirmationPayload::EthSignMessage(address.into(), data.into())))
|
||||
},
|
||||
RpcConfirmationPayload::EIP191SignMessage(RpcSignRequest { address, data }) => {
|
||||
Box::new(future::ok(ConfirmationPayload::SignMessage(address.into(), data.into())))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
61
rpc/src/v1/helpers/eip191.rs
Normal file
61
rpc/src/v1/helpers/eip191.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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 + hashing
|
||||
use v1::types::{EIP191Version, Bytes, PresignedTransaction};
|
||||
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;
|
||||
|
||||
/// deserializes and hashes the message depending on the version specifier
|
||||
pub fn hash_message(version: EIP191Version, message: Value) -> Result<H256, Error> {
|
||||
let data = match version {
|
||||
EIP191Version::StructuredData => {
|
||||
let typed_data = from_value::<EIP712>(message)
|
||||
.map_err(map_serde_err("StructuredData"))?;
|
||||
|
||||
hash_structured_data(typed_data)
|
||||
.map_err(|err| errors::invalid_call_data(err.kind()))?
|
||||
}
|
||||
|
||||
EIP191Version::PresignedTransaction => {
|
||||
let data = from_value::<PresignedTransaction>(message)
|
||||
.map_err(map_serde_err("WithValidator"))?;
|
||||
let prefix = b"\x19\x00";
|
||||
let data = [&prefix[..], &data.validator.0[..], &data.data.0[..]].concat();
|
||||
keccak(data)
|
||||
}
|
||||
|
||||
EIP191Version::PersonalMessage => {
|
||||
let bytes = from_value::<Bytes>(message)
|
||||
.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))
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ pub mod light_fetch;
|
||||
pub mod nonce;
|
||||
pub mod oneshot;
|
||||
pub mod secretstore;
|
||||
pub mod eip191;
|
||||
|
||||
mod network_settings;
|
||||
mod poll_filter;
|
||||
|
||||
@@ -18,6 +18,7 @@ use ethereum_types::{U256, Address};
|
||||
use bytes::Bytes;
|
||||
|
||||
use v1::types::{Origin, TransactionCondition};
|
||||
use ethereum_types::H256;
|
||||
|
||||
/// Transaction request coming from RPC
|
||||
#[derive(Debug, Clone, Default, Eq, PartialEq, Hash)]
|
||||
@@ -117,6 +118,8 @@ pub enum ConfirmationPayload {
|
||||
SignTransaction(FilledTransactionRequest),
|
||||
/// Sign a message with an Ethereum specific security prefix.
|
||||
EthSignMessage(Address, Bytes),
|
||||
/// Sign a message
|
||||
SignMessage(Address, H256),
|
||||
/// Decrypt request
|
||||
Decrypt(Address, Bytes),
|
||||
}
|
||||
@@ -127,6 +130,7 @@ impl ConfirmationPayload {
|
||||
ConfirmationPayload::SendTransaction(ref request) => request.from,
|
||||
ConfirmationPayload::SignTransaction(ref request) => request.from,
|
||||
ConfirmationPayload::EthSignMessage(ref address, _) => *address,
|
||||
ConfirmationPayload::SignMessage(ref address, _) => *address,
|
||||
ConfirmationPayload::Decrypt(ref address, _) => *address,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user