Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Web3sha3 beta #826

Merged
merged 4 commits into from
Mar 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions rpc/src/v1/impls/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,4 +36,14 @@ impl Web3 for Web3Client {
_ => 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)
}
)
}
}
28 changes: 28 additions & 0 deletions rpc/src/v1/tests/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,31 @@ 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()));
}

#[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()));
}
4 changes: 4 additions & 0 deletions rpc/src/v1/traits/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ pub trait Web3: Sized + Send + Sync + 'static {
/// Returns current client version.
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.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("web3_clientVersion", Web3::client_version);
delegate.add_method("web3_sha3", Web3::sha3);
delegate
}
}
2 changes: 1 addition & 1 deletion rpc/src/v1/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use util::common::FromHex;

/// Wrapper structure around vector of bytes.
#[derive(Debug, PartialEq, Default)]
pub struct Bytes(Vec<u8>);
pub struct Bytes(pub Vec<u8>);

impl Bytes {
/// Simple constructor.
Expand Down