From be1ec93271b5cefd9f4dd3c39d4b35c21da89b83 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Sun, 29 May 2016 15:46:57 +0200 Subject: [PATCH] implement eth_sign --- rpc/src/v1/impls/eth.rs | 16 ++++++---------- rpc/src/v1/tests/eth.rs | 2 +- rpc/src/v1/tests/mocked/eth.rs | 30 +++++++++++++++++++++++------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 026459d9f..100770ae1 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -27,6 +27,7 @@ use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; use util::rlp::{encode, decode, UntrustedRlp, View}; +use util::keys::store::AccountProvider; use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; use ethcore::views::*; @@ -39,7 +40,6 @@ use v1::traits::{Eth, EthFilter}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; use v1::helpers::{PollFilter, PollManager}; use v1::impls::{dispatch_transaction, sign_and_dispatch}; -use util::keys::store::AccountProvider; use serde; /// Eth rpc implementation. @@ -495,12 +495,8 @@ impl Eth for EthClient where } fn sign(&self, params: Params) -> Result { - from_params::<(Address, Bytes)>(params).and_then(|(addr, data)| { - let accounts = take_weak!(self.accounts); - match accounts.account_secret(&addr) { - Ok(secret) => rpc_unimplemented!(), - Err(_) => rpc_unimplemented!(), - } + from_params::<(Address, H256)>(params).and_then(|(addr, msg)| { + to_value(&take_weak!(self.accounts).sign(&addr, &msg).unwrap_or(H520::zero())) }) } @@ -553,15 +549,15 @@ impl Eth for EthClient where }) } - fn compile_lll(&self, _: params) -> Result { + fn compile_lll(&self, _: Params) -> Result { rpc_unimplemented!() } - fn compile_serpent(&self, _: params) -> Result { + fn compile_serpent(&self, _: Params) -> Result { rpc_unimplemented!() } - fn compile_solidity(&self, _: params) -> Result { + fn compile_solidity(&self, _: Params) -> Result { rpc_unimplemented!() } } diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index eac5bafcb..80a856aca 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -27,7 +27,7 @@ use ethcore::transaction::{Transaction, Action}; use ethminer::{MinerService, ExternalMiner}; use devtools::RandomTempPath; use util::io::IoChannel; -use util::hash::{Address, FixedHash}; +use util::hash::Address; use util::numbers::{Uint, U256}; use util::keys::{AccountProvider, TestAccount, TestAccountProvider}; use jsonrpc_core::IoHandler; diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index 32a2cd99a..9f6f9881f 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -20,7 +20,7 @@ use std::sync::{Arc, RwLock}; use jsonrpc_core::IoHandler; use util::hash::{Address, H256, FixedHash}; use util::numbers::{Uint, U256}; -use util::keys::{TestAccount, TestAccountProvider}; +use util::keys::{AccountProvider, TestAccount, TestAccountProvider}; use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed, TransactionID}; use ethcore::log_entry::{LocalizedLogEntry, LogEntry}; use ethcore::receipt::LocalizedReceipt; @@ -129,6 +129,28 @@ fn rpc_eth_submit_hashrate() { Some(U256::from(0x500_000))); } +#[test] +fn rpc_eth_sign() { + let tester = EthTester::default(); + + let account = tester.accounts_provider.new_account("abcd").unwrap(); + let message = H256::from("0x0cc175b9c0f1b6a831c399e26977266192eb5ffee6ae2fec3ad71c777531578f"); + let signed = tester.accounts_provider.sign(&account, &message).unwrap(); + + let req = r#"{ + "jsonrpc": "2.0", + "method": "eth_sign", + "params": [ + ""#.to_owned() + &format!("0x{:?}", account) + r#"", + "0x0cc175b9c0f1b6a831c399e26977266192eb5ffee6ae2fec3ad71c777531578f" + ], + "id": 1 + }"#; + let res = r#"{"jsonrpc":"2.0","result":""#.to_owned() + &format!("0x{:?}", signed) + r#"","id":1}"#; + + assert_eq!(tester.io.handle_request(&req), Some(res)); +} + #[test] #[ignore] fn rpc_eth_author() { @@ -527,12 +549,6 @@ fn rpc_eth_send_raw_transaction() { unimplemented!() } -#[test] -#[ignore] -fn rpc_eth_sign() { - unimplemented!() -} - #[test] fn rpc_eth_transaction_receipt() { let receipt = LocalizedReceipt {