From 3d94670f1f1c69122c16c051bb76fa4ab5e5fa3b Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 26 Mar 2016 03:00:05 +0300 Subject: [PATCH 1/4] web3_sha3 --- rpc/src/v1/impls/web3.rs | 13 +++++++++++++ rpc/src/v1/tests/web3.rs | 14 ++++++++++++++ rpc/src/v1/traits/web3.rs | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/rpc/src/v1/impls/web3.rs b/rpc/src/v1/impls/web3.rs index 64a82adb9..72651dd4e 100644 --- a/rpc/src/v1/impls/web3.rs +++ b/rpc/src/v1/impls/web3.rs @@ -18,6 +18,8 @@ use jsonrpc_core::*; use util::version; use v1::traits::Web3; +use v1::types::Bytes; +use util::sha3::Hashable; /// Web3 rpc implementation. pub struct Web3Client; @@ -34,4 +36,15 @@ impl Web3 for Web3Client { _ => Err(Error::invalid_params()) } } + + fn sha3(&self, params: Params) -> Result { + from_params::<(Bytes,)>(params).and_then( + |(data,)| { + let sha3 = data.to_vec().sha3(); + to_value(&sha3) + } + ) + + } + } diff --git a/rpc/src/v1/tests/web3.rs b/rpc/src/v1/tests/web3.rs index c717d361a..af21a932f 100644 --- a/rpc/src/v1/tests/web3.rs +++ b/rpc/src/v1/tests/web3.rs @@ -31,3 +31,17 @@ fn rpc_web3_version() { assert_eq!(io.handle_request(request), Some(response)); } + +#[test] +fn rpc_web3_sha3() { + let web3 = Web3Client::new().to_delegate(); + let io = IoHandler::new(); + io.add_delegate(web3); + + let v = version().to_owned().replace("Parity/", "Parity//"); + + let request = r#"{"jsonrpc": "2.0", "method": "web3_sha3", "params": ["0x00"], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a","id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} diff --git a/rpc/src/v1/traits/web3.rs b/rpc/src/v1/traits/web3.rs index bb8653504..c7b21a1a1 100644 --- a/rpc/src/v1/traits/web3.rs +++ b/rpc/src/v1/traits/web3.rs @@ -23,10 +23,14 @@ pub trait Web3: Sized + Send + Sync + 'static { /// Returns current client version. fn client_version(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Returns sha3 of the given data + fn sha3(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Should be used to convert object to io delegate. fn to_delegate(self) -> IoDelegate { let mut delegate = IoDelegate::new(Arc::new(self)); delegate.add_method("web3_clientVersion", Web3::client_version); + delegate.add_method("web3_sha3", Web3::sha3); delegate } } From dc4654fa4cb46dcb95e38b08fa7b40a5b997a102 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 26 Mar 2016 03:15:27 +0300 Subject: [PATCH 2/4] avoid copy --- rpc/src/v1/impls/web3.rs | 3 ++- rpc/src/v1/types/bytes.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rpc/src/v1/impls/web3.rs b/rpc/src/v1/impls/web3.rs index 72651dd4e..dcea09ec6 100644 --- a/rpc/src/v1/impls/web3.rs +++ b/rpc/src/v1/impls/web3.rs @@ -40,7 +40,8 @@ impl Web3 for Web3Client { fn sha3(&self, params: Params) -> Result { from_params::<(Bytes,)>(params).and_then( |(data,)| { - let sha3 = data.to_vec().sha3(); + let Bytes(ref v) = data; + let sha3 = v.sha3(); to_value(&sha3) } ) diff --git a/rpc/src/v1/types/bytes.rs b/rpc/src/v1/types/bytes.rs index 8c47806f8..efae15ef6 100644 --- a/rpc/src/v1/types/bytes.rs +++ b/rpc/src/v1/types/bytes.rs @@ -21,7 +21,7 @@ use util::common::FromHex; /// Wrapper structure around vector of bytes. #[derive(Debug, PartialEq, Default)] -pub struct Bytes(Vec); +pub struct Bytes(pub Vec); impl Bytes { /// Simple constructor. From 079a39a5f1b17c656fd9397b636374cc038e5a36 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 26 Mar 2016 03:19:55 +0300 Subject: [PATCH 3/4] wiki test --- rpc/src/v1/tests/web3.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rpc/src/v1/tests/web3.rs b/rpc/src/v1/tests/web3.rs index af21a932f..178dddfd7 100644 --- a/rpc/src/v1/tests/web3.rs +++ b/rpc/src/v1/tests/web3.rs @@ -45,3 +45,17 @@ fn rpc_web3_sha3() { assert_eq!(io.handle_request(request), Some(response.to_owned())); } + +#[test] +fn rpc_web3_sha3_wiki() { + let web3 = Web3Client::new().to_delegate(); + let io = IoHandler::new(); + io.add_delegate(web3); + + let v = version().to_owned().replace("Parity/", "Parity//"); + + let request = r#"{"jsonrpc": "2.0", "method": "web3_sha3", "params": ["0x68656c6c6f20776f726c64"], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad","id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} From 44d9ccf2c56b7fe01ab93880546bb98aba292dfd Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 26 Mar 2016 11:19:51 +0100 Subject: [PATCH 4/4] Update web3.rs [ci-skip] --- rpc/src/v1/impls/web3.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/rpc/src/v1/impls/web3.rs b/rpc/src/v1/impls/web3.rs index dcea09ec6..87343a18a 100644 --- a/rpc/src/v1/impls/web3.rs +++ b/rpc/src/v1/impls/web3.rs @@ -45,7 +45,5 @@ impl Web3 for Web3Client { to_value(&sha3) } ) - } - }