Merge pull request #824 from ethcore/web3-sha3

json-rpc web3_sha3
This commit is contained in:
Gav Wood 2016-03-26 11:20:52 +01:00
commit 8ea53b69eb
4 changed files with 45 additions and 1 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,14 @@ 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 Bytes(ref v) = data;
let sha3 = v.sha3();
to_value(&sha3)
}
)
}
} }

View File

@ -31,3 +31,31 @@ 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()));
}
#[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()));
}

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
} }
} }

View File

@ -21,7 +21,7 @@ use util::common::FromHex;
/// Wrapper structure around vector of bytes. /// Wrapper structure around vector of bytes.
#[derive(Debug, PartialEq, Default)] #[derive(Debug, PartialEq, Default)]
pub struct Bytes(Vec<u8>); pub struct Bytes(pub Vec<u8>);
impl Bytes { impl Bytes {
/// Simple constructor. /// Simple constructor.