web3_sha3

This commit is contained in:
NikVolf 2016-03-26 03:00:05 +03:00
parent 51d182eb5a
commit 3d94670f1f
3 changed files with 31 additions and 0 deletions

View File

@ -18,6 +18,8 @@
use jsonrpc_core::*; use jsonrpc_core::*;
use util::version; use util::version;
use v1::traits::Web3; use v1::traits::Web3;
use v1::types::Bytes;
use util::sha3::Hashable;
/// Web3 rpc implementation. /// Web3 rpc implementation.
pub struct Web3Client; pub struct Web3Client;
@ -34,4 +36,15 @@ impl Web3 for Web3Client {
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }
fn sha3(&self, params: Params) -> Result<Value, Error> {
from_params::<(Bytes,)>(params).and_then(
|(data,)| {
let sha3 = data.to_vec().sha3();
to_value(&sha3)
}
)
}
} }

View File

@ -31,3 +31,17 @@ fn rpc_web3_version() {
assert_eq!(io.handle_request(request), Some(response)); 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()));
}

View File

@ -23,10 +23,14 @@ pub trait Web3: Sized + Send + Sync + 'static {
/// Returns current client version. /// Returns current client version.
fn client_version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn client_version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns sha3 of the given data
fn sha3(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate. /// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> { fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self)); let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("web3_clientVersion", Web3::client_version); delegate.add_method("web3_clientVersion", Web3::client_version);
delegate.add_method("web3_sha3", Web3::sha3);
delegate delegate
} }
} }