Merge pull request #3830 from ethcore/cli-rpc
Add Ws Json rpc client and command line utils (take 2)
This commit is contained in:
commit
eb90d70be1
76
Cargo.lock
generated
76
Cargo.lock
generated
@ -33,9 +33,11 @@ dependencies = [
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-rpc-client 1.4.0",
|
||||
"regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rlp 0.1.0",
|
||||
"rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rpc-cli 1.4.0",
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -741,6 +743,14 @@ dependencies = [
|
||||
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gcc"
|
||||
version = "0.3.35"
|
||||
@ -870,6 +880,18 @@ name = "itoa"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "jsonrpc-core"
|
||||
version = "3.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_codegen 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jsonrpc-core"
|
||||
version = "4.0.0"
|
||||
@ -1302,6 +1324,26 @@ dependencies = [
|
||||
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parity-rpc-client"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"ethcore-rpc 1.5.0",
|
||||
"ethcore-signer 1.5.0",
|
||||
"ethcore-util 1.5.0",
|
||||
"futures 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"jsonrpc-core 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ws 0.5.3 (git+https://github.com/ethcore/ws-rs.git?branch=mio-upstream-stable)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parity-ui"
|
||||
version = "1.5.0"
|
||||
@ -1570,6 +1612,28 @@ dependencies = [
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rpassword"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rpc-cli"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"ethcore-bigint 0.1.2",
|
||||
"ethcore-rpc 1.5.0",
|
||||
"ethcore-util 1.5.0",
|
||||
"futures 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-rpc-client 1.4.0",
|
||||
"rpassword 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-crypto"
|
||||
version = "0.2.36"
|
||||
@ -1809,6 +1873,14 @@ name = "target_info"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "tempdir"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.2.14"
|
||||
@ -2084,6 +2156,7 @@ dependencies = [
|
||||
"checksum ethabi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0c53453517f620847be51943db329276ae52f2e210cfc659e81182864be2f"
|
||||
"checksum fdlimit 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b1ee15a7050e5580b3712877157068ea713b245b080ff302ae2ca973cfcd9baa"
|
||||
"checksum flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "3eeb481e957304178d2e782f2da1257f1434dfecbae883bafb61ada2a9fea3bb"
|
||||
"checksum futures 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bad0a2ac64b227fdc10c254051ae5af542cf19c9328704fd4092f7914196897"
|
||||
"checksum gcc 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)" = "91ecd03771effb0c968fd6950b37e89476a578aaf1c70297d8e92b6516ec3312"
|
||||
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
|
||||
"checksum hamming 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65043da274378d68241eb9a8f8f8aa54e349136f7b8e12f63e3ef44043cc30e1"
|
||||
@ -2097,6 +2170,7 @@ dependencies = [
|
||||
"checksum isatty 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7408a548dc0e406b7912d9f84c261cc533c1866e047644a811c133c56041ac0c"
|
||||
"checksum itertools 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "086e1fa5fe48840b1cfdef3a20c7e3115599f8d5c4c87ef32a794a7cdd184d76"
|
||||
"checksum itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ae3088ea4baeceb0284ee9eea42f591226e6beaecf65373e41b38d95a1b8e7a1"
|
||||
"checksum jsonrpc-core 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3c5094610b07f28f3edaf3947b732dadb31dbba4941d4d0c1c7a8350208f4414"
|
||||
"checksum jsonrpc-core 4.0.0 (git+https://github.com/ethcore/jsonrpc.git)" = "<none>"
|
||||
"checksum jsonrpc-http-server 6.1.1 (git+https://github.com/ethcore/jsonrpc.git)" = "<none>"
|
||||
"checksum jsonrpc-ipc-server 0.2.4 (git+https://github.com/ethcore/jsonrpc.git)" = "<none>"
|
||||
@ -2171,6 +2245,7 @@ dependencies = [
|
||||
"checksum rocksdb-sys 0.3.0 (git+https://github.com/ethcore/rust-rocksdb)" = "<none>"
|
||||
"checksum rotor 0.6.3 (git+https://github.com/ethcore/rotor)" = "<none>"
|
||||
"checksum rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d3a99497c5c544e629cc8b359ae5ede321eba5fa8e5a8078f3ced727a976c3f"
|
||||
"checksum rpassword 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab6e42be826e215f30ff830904f8f4a0933c6e2ae890e1af8b408f5bae60081e"
|
||||
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
|
||||
"checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b"
|
||||
"checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
|
||||
@ -2200,6 +2275,7 @@ dependencies = [
|
||||
"checksum syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44bded3cabafc65c90b663b1071bd2d198a9ab7515e6ce729e4570aaf53c407e"
|
||||
"checksum syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7628a0506e8f9666fdabb5f265d0059b059edac9a3f810bda077abb5d826bd8d"
|
||||
"checksum target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c63f48baada5c52e65a29eef93ab4f8982681b67f9e8d29c7b05abcfec2b9ffe"
|
||||
"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"
|
||||
"checksum term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "f2077e54d38055cf1ca0fd7933a2e00cd3ec8f6fed352b2a377f06dcdaaf3281"
|
||||
"checksum term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3deff8a2b3b6607d6d7cc32ac25c0b33709453ca9cceac006caac51e963cf94a"
|
||||
"checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"
|
||||
|
@ -48,6 +48,8 @@ rlp = { path = "util/rlp" }
|
||||
ethcore-stratum = { path = "stratum" }
|
||||
ethcore-dapps = { path = "dapps", optional = true }
|
||||
clippy = { version = "0.0.103", optional = true}
|
||||
rpc-cli = { path = "rpc_cli" }
|
||||
parity-rpc-client = { path = "rpc_client" }
|
||||
ethcore-light = { path = "ethcore/light" }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
|
@ -32,6 +32,8 @@ usage! {
|
||||
cmd_import: bool,
|
||||
cmd_signer: bool,
|
||||
cmd_new_token: bool,
|
||||
cmd_sign: bool,
|
||||
cmd_reject: bool,
|
||||
cmd_snapshot: bool,
|
||||
cmd_restore: bool,
|
||||
cmd_ui: bool,
|
||||
@ -44,6 +46,7 @@ usage! {
|
||||
arg_pid_file: String,
|
||||
arg_file: Option<String>,
|
||||
arg_path: Vec<String>,
|
||||
arg_id: Option<usize>,
|
||||
|
||||
// Flags
|
||||
// -- Legacy Options
|
||||
@ -509,6 +512,8 @@ mod tests {
|
||||
cmd_blocks: false,
|
||||
cmd_import: false,
|
||||
cmd_signer: false,
|
||||
cmd_sign: false,
|
||||
cmd_reject: false,
|
||||
cmd_new_token: false,
|
||||
cmd_snapshot: false,
|
||||
cmd_restore: false,
|
||||
@ -521,6 +526,7 @@ mod tests {
|
||||
// Arguments
|
||||
arg_pid_file: "".into(),
|
||||
arg_file: None,
|
||||
arg_id: None,
|
||||
arg_path: vec![],
|
||||
|
||||
// -- Operating Options
|
||||
|
@ -12,6 +12,9 @@ Usage:
|
||||
parity import [ <file> ] [options]
|
||||
parity export (blocks | state) [ <file> ] [options]
|
||||
parity signer new-token [options]
|
||||
parity signer list [options]
|
||||
parity signer sign [ <id> ] [ --password FILE ] [options]
|
||||
parity signer reject <id> [options]
|
||||
parity snapshot <file> [options]
|
||||
parity restore [ <file> ] [options]
|
||||
parity tools hash <file>
|
||||
|
@ -43,6 +43,8 @@ use presale::ImportWallet;
|
||||
use account::{AccountCmd, NewAccount, ListAccounts, ImportAccounts, ImportFromGethAccounts};
|
||||
use snapshot::{self, SnapshotCommand};
|
||||
|
||||
const AUTHCODE_FILENAME: &'static str = "authcodes";
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Cmd {
|
||||
Run(RunCmd),
|
||||
@ -51,6 +53,21 @@ pub enum Cmd {
|
||||
ImportPresaleWallet(ImportWallet),
|
||||
Blockchain(BlockchainCmd),
|
||||
SignerToken(SignerConfiguration),
|
||||
SignerSign {
|
||||
id: Option<usize>,
|
||||
pwfile: Option<PathBuf>,
|
||||
port: u16,
|
||||
authfile: PathBuf,
|
||||
},
|
||||
SignerList {
|
||||
port: u16,
|
||||
authfile: PathBuf
|
||||
},
|
||||
SignerReject {
|
||||
id: Option<usize>,
|
||||
port: u16,
|
||||
authfile: PathBuf
|
||||
},
|
||||
Snapshot(SnapshotCommand),
|
||||
Hash(Option<String>),
|
||||
}
|
||||
@ -103,8 +120,36 @@ impl Configuration {
|
||||
|
||||
let cmd = if self.args.flag_version {
|
||||
Cmd::Version
|
||||
} else if self.args.cmd_signer && self.args.cmd_new_token {
|
||||
Cmd::SignerToken(signer_conf)
|
||||
} else if self.args.cmd_signer {
|
||||
let mut authfile = PathBuf::from(signer_conf.signer_path.clone());
|
||||
authfile.push(AUTHCODE_FILENAME);
|
||||
|
||||
if self.args.cmd_new_token {
|
||||
Cmd::SignerToken(signer_conf)
|
||||
} else if self.args.cmd_sign {
|
||||
let pwfile = self.args.flag_password.get(0).map(|pwfile| {
|
||||
PathBuf::from(pwfile)
|
||||
});
|
||||
Cmd::SignerSign {
|
||||
id: self.args.arg_id,
|
||||
pwfile: pwfile,
|
||||
port: signer_conf.port,
|
||||
authfile: authfile,
|
||||
}
|
||||
} else if self.args.cmd_reject {
|
||||
Cmd::SignerReject {
|
||||
id: self.args.arg_id,
|
||||
port: signer_conf.port,
|
||||
authfile: authfile,
|
||||
}
|
||||
} else if self.args.cmd_list {
|
||||
Cmd::SignerList {
|
||||
port: signer_conf.port,
|
||||
authfile: authfile,
|
||||
}
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
} else if self.args.cmd_tools && self.args.cmd_hash {
|
||||
Cmd::Hash(self.args.arg_file)
|
||||
} else if self.args.cmd_db && self.args.cmd_kill {
|
||||
@ -1143,4 +1188,3 @@ mod tests {
|
||||
assert!(conf.init_reserved_nodes().is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,6 +71,8 @@ extern crate ethcore_stratum;
|
||||
#[cfg(feature = "dapps")]
|
||||
extern crate ethcore_dapps;
|
||||
|
||||
extern crate rpc_cli;
|
||||
|
||||
macro_rules! dependency {
|
||||
($dep_ty:ident, $url:expr) => {
|
||||
{
|
||||
@ -147,6 +149,12 @@ fn execute(command: Execute) -> Result<String, String> {
|
||||
Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd),
|
||||
Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd),
|
||||
Cmd::SignerToken(signer_cmd) => signer::execute(signer_cmd),
|
||||
Cmd::SignerSign { id, pwfile, port, authfile } =>
|
||||
rpc_cli::cmd_signer_sign(id, pwfile, port, authfile),
|
||||
Cmd::SignerList { port, authfile } =>
|
||||
rpc_cli::cmd_signer_list(port, authfile),
|
||||
Cmd::SignerReject { id, port, authfile } =>
|
||||
rpc_cli::cmd_signer_reject(id, port, authfile),
|
||||
Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd),
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,13 @@
|
||||
|
||||
use std::fmt;
|
||||
use serde::{Serialize, Serializer};
|
||||
use util::log::Colour;
|
||||
|
||||
use v1::types::{U256, TransactionRequest, RichRawTransaction, H160, H256, H520, Bytes};
|
||||
use v1::helpers;
|
||||
|
||||
/// Confirmation waiting in a queue
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct ConfirmationRequest {
|
||||
/// Id of this confirmation
|
||||
pub id: U256,
|
||||
@ -39,8 +41,25 @@ impl From<helpers::ConfirmationRequest> for ConfirmationRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ConfirmationRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "#{}: {}", self.id, self.payload)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ConfirmationPayload {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ConfirmationPayload::SendTransaction(ref transaction) => write!(f, "{}", transaction),
|
||||
ConfirmationPayload::SignTransaction(ref transaction) => write!(f, "(Sign only) {}", transaction),
|
||||
ConfirmationPayload::Signature(ref sign) => write!(f, "{}", sign),
|
||||
ConfirmationPayload::Decrypt(ref decrypt) => write!(f, "{}", decrypt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sign request
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct SignRequest {
|
||||
/// Address
|
||||
pub address: H160,
|
||||
@ -57,8 +76,19 @@ impl From<(H160, H256)> for SignRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SignRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"sign 0x{:?} with {}",
|
||||
self.hash,
|
||||
Colour::White.bold().paint(format!("0x{:?}", self.address)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypt request
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct DecryptRequest {
|
||||
/// Address
|
||||
pub address: H160,
|
||||
@ -75,6 +105,16 @@ impl From<(H160, Bytes)> for DecryptRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DecryptRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"decrypt data with {}",
|
||||
Colour::White.bold().paint(format!("0x{:?}", self.address)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Confirmation response for particular payload
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ConfirmationResponse {
|
||||
@ -111,7 +151,7 @@ pub struct ConfirmationResponseWithToken {
|
||||
}
|
||||
|
||||
/// Confirmation payload, i.e. the thing to be confirmed
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub enum ConfirmationPayload {
|
||||
/// Send Transaction
|
||||
#[serde(rename="sendTransaction")]
|
||||
@ -145,7 +185,7 @@ impl From<helpers::ConfirmationPayload> for ConfirmationPayload {
|
||||
}
|
||||
|
||||
/// Possible modifications to the confirmed transaction sent by `Trusted Signer`
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct TransactionModification {
|
||||
/// Modified gas price
|
||||
@ -325,4 +365,3 @@ mod tests {
|
||||
assert_eq!(res.unwrap(), expected.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,9 @@
|
||||
|
||||
use v1::types::{Bytes, H160, U256};
|
||||
use v1::helpers;
|
||||
use util::log::Colour;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Transaction request coming from RPC
|
||||
#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
@ -40,6 +43,43 @@ pub struct TransactionRequest {
|
||||
pub nonce: Option<U256>,
|
||||
}
|
||||
|
||||
pub fn format_ether(i: U256) -> String {
|
||||
let mut string = format!("{}", i);
|
||||
let idx = string.len() as isize - 18;
|
||||
if idx <= 0 {
|
||||
let mut prefix = String::from("0.");
|
||||
for _ in 0..idx.abs() {
|
||||
prefix.push('0');
|
||||
}
|
||||
string = prefix + &string;
|
||||
} else {
|
||||
string.insert(idx as usize, '.');
|
||||
}
|
||||
String::from(string.trim_right_matches('0')
|
||||
.trim_right_matches('.'))
|
||||
}
|
||||
|
||||
impl fmt::Display for TransactionRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let eth = self.value.unwrap_or(U256::from(0));
|
||||
match self.to {
|
||||
Some(ref to) => write!(
|
||||
f,
|
||||
"{} ETH from {} to 0x{:?}",
|
||||
Colour::White.bold().paint(format_ether(eth)),
|
||||
Colour::White.bold().paint(format!("0x{:?}", self.from)),
|
||||
to
|
||||
),
|
||||
None => write!(
|
||||
f,
|
||||
"{} ETH from {} for contract creation",
|
||||
Colour::White.bold().paint(format_ether(eth)),
|
||||
Colour::White.bold().paint(format!("0x{:?}", self.from)),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<helpers::TransactionRequest> for TransactionRequest {
|
||||
fn from(r: helpers::TransactionRequest) -> Self {
|
||||
TransactionRequest {
|
||||
@ -191,5 +231,15 @@ mod tests {
|
||||
|
||||
assert!(deserialized.is_err(), "Should be error because to is empty");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_ether() {
|
||||
assert_eq!(&format_ether(U256::from(1000000000000000000u64)), "1");
|
||||
assert_eq!(&format_ether(U256::from(500000000000000000u64)), "0.5");
|
||||
assert_eq!(&format_ether(U256::from(50000000000000000u64)), "0.05");
|
||||
assert_eq!(&format_ether(U256::from(5000000000000000u64)), "0.005");
|
||||
assert_eq!(&format_ether(U256::from(2000000000000000000u64)), "2");
|
||||
assert_eq!(&format_ether(U256::from(2500000000000000000u64)), "2.5");
|
||||
assert_eq!(&format_ether(U256::from(10000000000000000000u64)), "10");
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::fmt;
|
||||
use serde;
|
||||
use util::{U256 as EthU256, U128 as EthU128, Uint};
|
||||
|
||||
@ -46,6 +47,18 @@ macro_rules! impl_uint {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::LowerHex for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:#x}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer {
|
||||
serializer.serialize_str(&format!("0x{}", self.0.to_hex()))
|
||||
|
15
rpc_cli/Cargo.toml
Normal file
15
rpc_cli/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
||||
[package]
|
||||
authors = ["Ethcore <admin@ethcore.io>"]
|
||||
description = "Parity Cli Tool"
|
||||
homepage = "http://ethcore.io"
|
||||
license = "GPL-3.0"
|
||||
name = "rpc-cli"
|
||||
version = "1.4.0"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.1"
|
||||
rpassword = "0.3.0"
|
||||
ethcore-bigint = { path = "../util/bigint" }
|
||||
ethcore-rpc = { path = "../rpc" }
|
||||
parity-rpc-client = { path = "../rpc_client" }
|
||||
ethcore-util = { path = "../util" }
|
182
rpc_cli/src/lib.rs
Normal file
182
rpc_cli/src/lib.rs
Normal file
@ -0,0 +1,182 @@
|
||||
extern crate futures;
|
||||
|
||||
extern crate ethcore_util as util;
|
||||
extern crate ethcore_rpc as rpc;
|
||||
extern crate ethcore_bigint as bigint;
|
||||
extern crate rpassword;
|
||||
|
||||
extern crate parity_rpc_client as client;
|
||||
|
||||
use rpc::v1::types::{U256, ConfirmationRequest};
|
||||
use client::signer_client::SignerRpc;
|
||||
use std::io::{Write, BufRead, BufReader, stdout, stdin};
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
|
||||
use futures::Future;
|
||||
|
||||
fn sign_interactive(
|
||||
signer: &mut SignerRpc,
|
||||
password: &str,
|
||||
request: ConfirmationRequest
|
||||
) {
|
||||
print!("\n{}\nSign this transaction? (y)es/(N)o/(r)eject: ", request);
|
||||
let _ = stdout().flush();
|
||||
match BufReader::new(stdin()).lines().next() {
|
||||
Some(Ok(line)) => {
|
||||
match line.to_lowercase().chars().nth(0) {
|
||||
Some('y') => {
|
||||
match sign_transaction(signer, request.id, password) {
|
||||
Ok(s) | Err(s) => println!("{}", s),
|
||||
}
|
||||
}
|
||||
Some('r') => {
|
||||
match reject_transaction(signer, request.id) {
|
||||
Ok(s) | Err(s) => println!("{}", s),
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
_ => println!("Could not read from stdin")
|
||||
}
|
||||
}
|
||||
|
||||
fn sign_transactions(
|
||||
signer: &mut SignerRpc,
|
||||
password: String
|
||||
) -> Result<String, String> {
|
||||
try!(signer.requests_to_confirm().map(|reqs| {
|
||||
match reqs {
|
||||
Ok(ref reqs) if reqs.is_empty() => {
|
||||
Ok("No transactions in signing queue".to_owned())
|
||||
}
|
||||
Ok(reqs) => {
|
||||
for r in reqs {
|
||||
sign_interactive(signer, &password, r)
|
||||
}
|
||||
Ok("".to_owned())
|
||||
}
|
||||
Err(err) => {
|
||||
Err(format!("error: {:?}", err))
|
||||
}
|
||||
}
|
||||
}).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}).wait())
|
||||
}
|
||||
|
||||
fn list_transactions(signer: &mut SignerRpc) -> Result<String, String> {
|
||||
try!(signer.requests_to_confirm().map(|reqs| {
|
||||
match reqs {
|
||||
Ok(ref reqs) if reqs.is_empty() => {
|
||||
Ok("No transactions in signing queue".to_owned())
|
||||
}
|
||||
Ok(ref reqs) => {
|
||||
Ok(format!("Transaction queue:\n{}", reqs
|
||||
.iter()
|
||||
.map(|r| format!("{}", r))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")))
|
||||
}
|
||||
Err(err) => {
|
||||
Err(format!("error: {:?}", err))
|
||||
}
|
||||
}
|
||||
}).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}).wait())
|
||||
}
|
||||
|
||||
fn sign_transaction(
|
||||
signer: &mut SignerRpc, id: U256, password: &str
|
||||
) -> Result<String, String> {
|
||||
try!(signer.confirm_request(id, None, None, password).map(|res| {
|
||||
match res {
|
||||
Ok(u) => Ok(format!("Signed transaction id: {:#x}", u)),
|
||||
Err(e) => Err(format!("{:?}", e)),
|
||||
}
|
||||
}).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}).wait())
|
||||
}
|
||||
|
||||
fn reject_transaction(
|
||||
signer: &mut SignerRpc, id: U256) -> Result<String, String>
|
||||
{
|
||||
try!(signer.reject_request(id).map(|res| {
|
||||
match res {
|
||||
Ok(true) => Ok(format!("Rejected transaction id {:#x}", id)),
|
||||
Ok(false) => Err(format!("No such request")),
|
||||
Err(e) => Err(format!("{:?}", e)),
|
||||
}
|
||||
}).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}).wait())
|
||||
}
|
||||
|
||||
// cmds
|
||||
|
||||
pub fn cmd_signer_list(
|
||||
signerport: u16, authfile: PathBuf
|
||||
) -> Result<String, String> {
|
||||
let addr = &format!("ws://127.0.0.1:{}", signerport);
|
||||
let mut signer = try!(SignerRpc::new(addr, &authfile).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}));
|
||||
list_transactions(&mut signer)
|
||||
}
|
||||
|
||||
pub fn cmd_signer_reject(
|
||||
id: Option<usize>, signerport: u16, authfile: PathBuf
|
||||
) -> Result<String, String> {
|
||||
let id = try!(id.ok_or(format!("id required for signer reject")));
|
||||
let addr = &format!("ws://127.0.0.1:{}", signerport);
|
||||
let mut signer = try!(SignerRpc::new(addr, &authfile).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}));
|
||||
reject_transaction(&mut signer, U256::from(id))
|
||||
}
|
||||
|
||||
pub fn cmd_signer_sign(
|
||||
id: Option<usize>,
|
||||
pwfile: Option<PathBuf>,
|
||||
signerport: u16,
|
||||
authfile: PathBuf
|
||||
) -> Result<String, String> {
|
||||
let password;
|
||||
match pwfile {
|
||||
Some(pwfile) => {
|
||||
match File::open(pwfile) {
|
||||
Ok(fd) => {
|
||||
match BufReader::new(fd).lines().next() {
|
||||
Some(Ok(line)) => password = line,
|
||||
_ => return Err(format!("No password in file"))
|
||||
}
|
||||
},
|
||||
Err(e) =>
|
||||
return Err(format!("Could not open password file: {}", e))
|
||||
}
|
||||
}
|
||||
None => {
|
||||
password = match rpassword::prompt_password_stdout("Password: ") {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(format!("{}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let addr = &format!("ws://127.0.0.1:{}", signerport);
|
||||
let mut signer = try!(SignerRpc::new(addr, &authfile).map_err(|err| {
|
||||
format!("{:?}", err)
|
||||
}));
|
||||
|
||||
match id {
|
||||
Some(id) => {
|
||||
sign_transaction(&mut signer, U256::from(id), &password)
|
||||
},
|
||||
None => {
|
||||
sign_transactions(&mut signer, password)
|
||||
}
|
||||
}
|
||||
}
|
23
rpc_client/Cargo.toml
Normal file
23
rpc_client/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
||||
[package]
|
||||
authors = ["Ethcore <admin@ethcore.io>"]
|
||||
description = "Parity Rpc Client"
|
||||
homepage = "http://ethcore.io"
|
||||
license = "GPL-3.0"
|
||||
name = "parity-rpc-client"
|
||||
version = "1.4.0"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.1"
|
||||
jsonrpc-core = "3.0.2"
|
||||
lazy_static = "0.2.1"
|
||||
log = "0.3.6"
|
||||
matches = "0.1.2"
|
||||
rand = "0.3.14"
|
||||
serde = "0.8"
|
||||
serde_json = "0.8"
|
||||
tempdir = "0.3.5"
|
||||
url = "1.2.0"
|
||||
ws = { git = "https://github.com/ethcore/ws-rs.git", branch = "mio-upstream-stable" }
|
||||
ethcore-rpc = { path = "../rpc" }
|
||||
ethcore-signer = { path = "../signer" }
|
||||
ethcore-util = { path = "../util" }
|
322
rpc_client/src/client.rs
Normal file
322
rpc_client/src/client.rs
Normal file
@ -0,0 +1,322 @@
|
||||
extern crate jsonrpc_core;
|
||||
|
||||
use std::fmt::{Debug, Formatter, Error as FmtError};
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::collections::BTreeMap;
|
||||
use std::thread;
|
||||
use std::time;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use util::{Hashable, Mutex};
|
||||
use url::Url;
|
||||
use std::fs::File;
|
||||
|
||||
use ws::{
|
||||
self,
|
||||
Request,
|
||||
Handler,
|
||||
Sender,
|
||||
Handshake,
|
||||
Error as WsError,
|
||||
ErrorKind as WsErrorKind,
|
||||
Message,
|
||||
Result as WsResult,
|
||||
};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::{
|
||||
self as json,
|
||||
Value as JsonValue,
|
||||
Error as JsonError,
|
||||
};
|
||||
|
||||
use futures::{BoxFuture, Canceled, Complete, Future, oneshot, done};
|
||||
|
||||
use jsonrpc_core::{Id, Version, Params, Error as JsonRpcError};
|
||||
use jsonrpc_core::request::MethodCall;
|
||||
use jsonrpc_core::response::{SyncOutput, Success, Failure};
|
||||
|
||||
/// The actual websocket connection handler, passed into the
|
||||
/// event loop of ws-rs
|
||||
struct RpcHandler {
|
||||
pending: Pending,
|
||||
// Option is used here as temporary storage until connection
|
||||
// is setup and the values are moved into the new `Rpc`
|
||||
complete: Option<Complete<Result<Rpc, RpcError>>>,
|
||||
auth_code: String,
|
||||
out: Option<Sender>,
|
||||
}
|
||||
|
||||
impl RpcHandler {
|
||||
fn new(
|
||||
out: Sender,
|
||||
auth_code: String,
|
||||
complete: Complete<Result<Rpc, RpcError>>
|
||||
) -> Self {
|
||||
RpcHandler {
|
||||
out: Some(out),
|
||||
auth_code: auth_code,
|
||||
pending: Pending::new(),
|
||||
complete: Some(complete),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler for RpcHandler {
|
||||
fn build_request(&mut self, url: &Url) -> WsResult<Request> {
|
||||
match Request::from_url(url) {
|
||||
Ok(mut r) => {
|
||||
let timestamp = try!(time::UNIX_EPOCH.elapsed().map_err(|err| {
|
||||
WsError::new(WsErrorKind::Internal, format!("{}", err))
|
||||
}));
|
||||
let secs = timestamp.as_secs();
|
||||
let hashed = format!("{}:{}", self.auth_code, secs).sha3();
|
||||
let proto = format!("{:?}_{}", hashed, secs);
|
||||
r.add_protocol(&proto);
|
||||
Ok(r)
|
||||
},
|
||||
Err(e) =>
|
||||
Err(WsError::new(WsErrorKind::Internal, format!("{}", e))),
|
||||
}
|
||||
}
|
||||
fn on_error(&mut self, err: WsError) {
|
||||
match self.complete.take() {
|
||||
Some(c) => c.complete(Err(RpcError::WsError(err))),
|
||||
None => println!("unexpected error: {}", err),
|
||||
}
|
||||
}
|
||||
fn on_open(&mut self, _: Handshake) -> WsResult<()> {
|
||||
match (self.complete.take(), self.out.take()) {
|
||||
(Some(c), Some(out)) => {
|
||||
c.complete(Ok(Rpc {
|
||||
out: out,
|
||||
counter: AtomicUsize::new(0),
|
||||
pending: self.pending.clone(),
|
||||
}));
|
||||
Ok(())
|
||||
},
|
||||
_ => {
|
||||
let msg = format!("on_open called twice");
|
||||
Err(WsError::new(WsErrorKind::Internal, msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
fn on_message(&mut self, msg: Message) -> WsResult<()> {
|
||||
let ret: Result<JsonValue, JsonRpcError>;
|
||||
let response_id;
|
||||
let string = &msg.to_string();
|
||||
match json::from_str::<SyncOutput>(&string) {
|
||||
Ok(SyncOutput::Success(Success { result, id: Id::Num(id), .. })) =>
|
||||
{
|
||||
ret = Ok(result);
|
||||
response_id = id as usize;
|
||||
}
|
||||
Ok(SyncOutput::Failure(Failure { error, id: Id::Num(id), .. })) => {
|
||||
ret = Err(error);
|
||||
response_id = id as usize;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(
|
||||
target: "rpc-client",
|
||||
"recieved invalid message: {}\n {:?}",
|
||||
string,
|
||||
e
|
||||
);
|
||||
return Ok(())
|
||||
},
|
||||
_ => {
|
||||
warn!(
|
||||
target: "rpc-client",
|
||||
"recieved invalid message: {}",
|
||||
string
|
||||
);
|
||||
return Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
match self.pending.remove(response_id) {
|
||||
Some(c) => c.complete(ret.map_err(|err| {
|
||||
RpcError::JsonRpc(err)
|
||||
})),
|
||||
None => warn!(
|
||||
target: "rpc-client",
|
||||
"warning: unexpected id: {}",
|
||||
response_id
|
||||
),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Keeping track of issued requests to be matched up with responses
|
||||
#[derive(Clone)]
|
||||
struct Pending(
|
||||
Arc<Mutex<BTreeMap<usize, Complete<Result<JsonValue, RpcError>>>>>
|
||||
);
|
||||
|
||||
impl Pending {
|
||||
fn new() -> Self {
|
||||
Pending(Arc::new(Mutex::new(BTreeMap::new())))
|
||||
}
|
||||
fn insert(&mut self, k: usize, v: Complete<Result<JsonValue, RpcError>>) {
|
||||
self.0.lock().insert(k, v);
|
||||
}
|
||||
fn remove(
|
||||
&mut self,
|
||||
k: usize
|
||||
) -> Option<Complete<Result<JsonValue, RpcError>>> {
|
||||
self.0.lock().remove(&k)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_authcode(path: &PathBuf) -> Result<String, RpcError> {
|
||||
if let Ok(fd) = File::open(path) {
|
||||
if let Some(Ok(line)) = BufReader::new(fd).lines().next() {
|
||||
let mut parts = line.split(';');
|
||||
let token = parts.next();
|
||||
|
||||
if let Some(code) = token {
|
||||
return Ok(code.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(RpcError::NoAuthCode)
|
||||
}
|
||||
|
||||
/// The handle to the connection
|
||||
pub struct Rpc {
|
||||
out: Sender,
|
||||
counter: AtomicUsize,
|
||||
pending: Pending,
|
||||
}
|
||||
|
||||
impl Rpc {
|
||||
/// Blocking, returns a new initialized connection or RpcError
|
||||
pub fn new(url: &str, authpath: &PathBuf) -> Result<Self, RpcError> {
|
||||
let rpc = try!(Self::connect(url, authpath).map(|rpc| rpc).wait());
|
||||
rpc
|
||||
}
|
||||
/// Non-blocking, returns a future
|
||||
pub fn connect(
|
||||
url: &str, authpath: &PathBuf
|
||||
) -> BoxFuture<Result<Self, RpcError>, Canceled> {
|
||||
let (c, p) = oneshot::<Result<Self, RpcError>>();
|
||||
match get_authcode(authpath) {
|
||||
Err(e) => return done(Ok(Err(e))).boxed(),
|
||||
Ok(code) => {
|
||||
let url = String::from(url);
|
||||
// The ws::connect takes a FnMut closure, which means c cannot
|
||||
// be moved into it, since it's consumed on complete.
|
||||
// Therefore we wrap it in an option and pick it out once.
|
||||
let mut once = Some(c);
|
||||
thread::spawn(move || {
|
||||
let conn = ws::connect(url, |out| {
|
||||
// this will panic if the closure is called twice,
|
||||
// which it should never be.
|
||||
let c = once.take()
|
||||
.expect("connection closure called only once");
|
||||
RpcHandler::new(out, code.clone(), c)
|
||||
});
|
||||
match conn {
|
||||
Err(err) => {
|
||||
// since ws::connect is only called once, it cannot
|
||||
// both fail and succeed.
|
||||
let c = once.take()
|
||||
.expect("connection closure called only once");
|
||||
c.complete(Err(RpcError::WsError(err)));
|
||||
},
|
||||
// c will complete on the `on_open` event in the Handler
|
||||
_ => ()
|
||||
}
|
||||
});
|
||||
p.boxed()
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Non-blocking, returns a future of the request response
|
||||
pub fn request<T>(
|
||||
&mut self, method: &'static str, params: Vec<JsonValue>
|
||||
) -> BoxFuture<Result<T, RpcError>, Canceled>
|
||||
where T: Deserialize + Send + Sized {
|
||||
|
||||
let (c, p) = oneshot::<Result<JsonValue, RpcError>>();
|
||||
|
||||
let id = self.counter.fetch_add(1, Ordering::Relaxed);
|
||||
self.pending.insert(id, c);
|
||||
|
||||
let request = MethodCall {
|
||||
jsonrpc: Version::V2,
|
||||
method: method.to_owned(),
|
||||
params: Some(Params::Array(params)),
|
||||
id: Id::Num(id as u64),
|
||||
};
|
||||
|
||||
let serialized = json::to_string(&request)
|
||||
.expect("request is serializable");
|
||||
let _ = self.out.send(serialized);
|
||||
|
||||
p.map(|result| {
|
||||
match result {
|
||||
Ok(json) => {
|
||||
let t: T = try!(json::from_value(json));
|
||||
Ok(t)
|
||||
},
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
}).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
pub enum RpcError {
|
||||
WrongVersion(String),
|
||||
ParseError(JsonError),
|
||||
MalformedResponse(String),
|
||||
JsonRpc(JsonRpcError),
|
||||
WsError(WsError),
|
||||
Canceled(Canceled),
|
||||
UnexpectedId,
|
||||
NoAuthCode,
|
||||
}
|
||||
|
||||
impl Debug for RpcError {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
||||
match *self {
|
||||
RpcError::WrongVersion(ref s)
|
||||
=> write!(f, "Expected version 2.0, got {}", s),
|
||||
RpcError::ParseError(ref err)
|
||||
=> write!(f, "ParseError: {}", err),
|
||||
RpcError::MalformedResponse(ref s)
|
||||
=> write!(f, "Malformed response: {}", s),
|
||||
RpcError::JsonRpc(ref json)
|
||||
=> write!(f, "JsonRpc error: {:?}", json),
|
||||
RpcError::WsError(ref s)
|
||||
=> write!(f, "Websocket error: {}", s),
|
||||
RpcError::Canceled(ref s)
|
||||
=> write!(f, "Futures error: {:?}", s),
|
||||
RpcError::UnexpectedId
|
||||
=> write!(f, "Unexpected response id"),
|
||||
RpcError::NoAuthCode
|
||||
=> write!(f, "No authcodes available"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<JsonError> for RpcError {
|
||||
fn from(err: JsonError) -> RpcError {
|
||||
RpcError::ParseError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WsError> for RpcError {
|
||||
fn from(err: WsError) -> RpcError {
|
||||
RpcError::WsError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Canceled> for RpcError {
|
||||
fn from(err: Canceled) -> RpcError {
|
||||
RpcError::Canceled(err)
|
||||
}
|
||||
}
|
73
rpc_client/src/lib.rs
Normal file
73
rpc_client/src/lib.rs
Normal file
@ -0,0 +1,73 @@
|
||||
pub mod client;
|
||||
pub mod signer_client;
|
||||
|
||||
extern crate ws;
|
||||
extern crate ethcore_signer;
|
||||
extern crate url;
|
||||
extern crate futures;
|
||||
extern crate ethcore_util as util;
|
||||
extern crate ethcore_rpc as rpc;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate rand;
|
||||
extern crate tempdir;
|
||||
extern crate jsonrpc_core;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate matches;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use futures::Future;
|
||||
use std::path::PathBuf;
|
||||
use client::{Rpc, RpcError};
|
||||
use ethcore_signer;
|
||||
|
||||
#[test]
|
||||
fn test_connection_refused() {
|
||||
let (_srv, port, mut authcodes) = ethcore_signer::tests::serve();
|
||||
|
||||
let _ = authcodes.generate_new();
|
||||
authcodes.to_file(&authcodes.path).unwrap();
|
||||
|
||||
let connect = Rpc::connect(&format!("ws://127.0.0.1:{}", port - 1),
|
||||
authcodes.path.as_path());
|
||||
|
||||
let _ = connect.map(|conn| {
|
||||
assert!(matches!(&conn, &Err(RpcError::WsError(_))));
|
||||
}).wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_authcode_fail() {
|
||||
let (_srv, port, _) = ethcore_signer::tests::serve();
|
||||
let path = PathBuf::from("nonexist");
|
||||
|
||||
let connect = Rpc::connect(&format!("ws://127.0.0.1:{}", port), &path);
|
||||
|
||||
let _ = connect.map(|conn| {
|
||||
assert!(matches!(&conn, &Err(RpcError::NoAuthCode)));
|
||||
}).wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_authcode_correct() {
|
||||
let (_srv, port, mut authcodes) = ethcore_signer::tests::serve();
|
||||
|
||||
let _ = authcodes.generate_new();
|
||||
authcodes.to_file(&authcodes.path).unwrap();
|
||||
|
||||
let connect = Rpc::connect(&format!("ws://127.0.0.1:{}", port),
|
||||
authcodes.path.as_path());
|
||||
|
||||
let _ = connect.map(|conn| {
|
||||
assert!(conn.is_ok())
|
||||
}).wait();
|
||||
}
|
||||
|
||||
}
|
43
rpc_client/src/signer_client.rs
Normal file
43
rpc_client/src/signer_client.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use client::{Rpc, RpcError};
|
||||
use rpc::v1::types::{ConfirmationRequest,
|
||||
TransactionModification,
|
||||
U256};
|
||||
use serde_json::{Value as JsonValue, to_value};
|
||||
use std::path::PathBuf;
|
||||
use futures::{BoxFuture, Canceled};
|
||||
|
||||
pub struct SignerRpc {
|
||||
rpc: Rpc,
|
||||
}
|
||||
|
||||
impl SignerRpc {
|
||||
pub fn new(url: &str, authfile: &PathBuf) -> Result<Self, RpcError> {
|
||||
Ok(SignerRpc { rpc: try!(Rpc::new(&url, authfile)) })
|
||||
}
|
||||
pub fn requests_to_confirm(&mut self) ->
|
||||
BoxFuture<Result<Vec<ConfirmationRequest>, RpcError>, Canceled>
|
||||
{
|
||||
self.rpc.request("signer_requestsToConfirm", vec![])
|
||||
}
|
||||
pub fn confirm_request(
|
||||
&mut self,
|
||||
id: U256,
|
||||
new_gas: Option<U256>,
|
||||
new_gas_price: Option<U256>,
|
||||
pwd: &str
|
||||
) -> BoxFuture<Result<U256, RpcError>, Canceled>
|
||||
{
|
||||
self.rpc.request("signer_confirmRequest", vec![
|
||||
to_value(&format!("{:#x}", id)),
|
||||
to_value(&TransactionModification { gas_price: new_gas_price, gas: new_gas }),
|
||||
to_value(&pwd),
|
||||
])
|
||||
}
|
||||
pub fn reject_request(&mut self, id: U256) ->
|
||||
BoxFuture<Result<bool, RpcError>, Canceled>
|
||||
{
|
||||
self.rpc.request("signer_rejectRequest", vec![
|
||||
JsonValue::String(format!("{:#x}", id))
|
||||
])
|
||||
}
|
||||
}
|
@ -52,13 +52,13 @@ extern crate ethcore_io as io;
|
||||
extern crate ethcore_rpc as rpc;
|
||||
extern crate jsonrpc_core;
|
||||
extern crate ws;
|
||||
#[cfg(test)]
|
||||
|
||||
extern crate ethcore_devtools as devtools;
|
||||
|
||||
mod authcode_store;
|
||||
mod ws_server;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Exported tests for use in signer RPC client testing
|
||||
pub mod tests;
|
||||
pub use authcode_store::*;
|
||||
pub use ws_server::*;
|
||||
|
@ -15,20 +15,23 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::time;
|
||||
use std::sync::Arc;
|
||||
use devtools::{http_client, RandomTempPath};
|
||||
|
||||
use devtools::http_client;
|
||||
use devtools::RandomTempPath;
|
||||
|
||||
use rpc::ConfirmationsQueue;
|
||||
use util::Hashable;
|
||||
use rand;
|
||||
|
||||
use ServerBuilder;
|
||||
use Server;
|
||||
use AuthCodes;
|
||||
|
||||
/// Struct representing authcodes
|
||||
pub struct GuardedAuthCodes {
|
||||
authcodes: AuthCodes,
|
||||
path: RandomTempPath,
|
||||
/// The path to the mock authcodes
|
||||
pub path: RandomTempPath,
|
||||
}
|
||||
impl Deref for GuardedAuthCodes {
|
||||
type Target = AuthCodes;
|
||||
@ -42,6 +45,7 @@ impl DerefMut for GuardedAuthCodes {
|
||||
}
|
||||
}
|
||||
|
||||
/// Setup a mock signer for testsp
|
||||
pub fn serve() -> (Server, usize, GuardedAuthCodes) {
|
||||
let mut path = RandomTempPath::new();
|
||||
path.panic_on_drop_failure = false;
|
||||
@ -56,194 +60,202 @@ pub fn serve() -> (Server, usize, GuardedAuthCodes) {
|
||||
})
|
||||
}
|
||||
|
||||
/// Test a single request to running server
|
||||
pub fn request(server: Server, request: &str) -> http_client::Response {
|
||||
http_client::request(server.addr(), request)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_reject_invalid_host() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
#[cfg(test)]
|
||||
mod testing {
|
||||
use std::time;
|
||||
use util::Hashable;
|
||||
use devtools::http_client;
|
||||
use super::{serve, request};
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: test:8180\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
#[test]
|
||||
fn should_reject_invalid_host() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
assert!(response.body.contains("URL Blocked"));
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: test:8180\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
assert!(response.body.contains("URL Blocked"));
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_home_parity_host() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET http://home.parity/ HTTP/1.1\r\n\
|
||||
Host: home.parity\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_serve_styles_even_on_disallowed_domain() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET /styles.css HTTP/1.1\r\n\
|
||||
Host: test:8180\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_200_ok_for_connect_requests() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
CONNECT home.parity:8080 HTTP/1.1\r\n\
|
||||
Host: home.parity\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_block_if_authorization_is_incorrect() {
|
||||
// given
|
||||
let (server, port, _) = serve();
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Upgrade\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol: wrong\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
", port)
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_if_authorization_is_correct() {
|
||||
// given
|
||||
let (server, port, mut authcodes) = serve();
|
||||
let code = authcodes.generate_new().unwrap().replace("-", "");
|
||||
authcodes.to_file(&authcodes.path).unwrap();
|
||||
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol: {:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 101 Switching Protocols".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_initial_connection_but_only_once() {
|
||||
// given
|
||||
let (server, port, authcodes) = serve();
|
||||
let code = "initial";
|
||||
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
assert!(authcodes.is_empty());
|
||||
|
||||
// when
|
||||
let response1 = http_client::request(server.addr(),
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol:{:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
let response2 = http_client::request(server.addr(),
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol:{:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// then
|
||||
assert_eq!(response1.status, "HTTP/1.1 101 Switching Protocols".to_owned());
|
||||
assert_eq!(response2.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
http_client::assert_security_headers_present(&response2.headers, None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_home_parity_host() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET http://home.parity/ HTTP/1.1\r\n\
|
||||
Host: home.parity\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_serve_styles_even_on_disallowed_domain() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
GET /styles.css HTTP/1.1\r\n\
|
||||
Host: test:8180\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_200_ok_for_connect_requests() {
|
||||
// given
|
||||
let server = serve().0;
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
"\
|
||||
CONNECT home.parity:8080 HTTP/1.1\r\n\
|
||||
Host: home.parity\r\n\
|
||||
Connection: close\r\n\
|
||||
\r\n\
|
||||
{}
|
||||
"
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 200 OK".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_block_if_authorization_is_incorrect() {
|
||||
// given
|
||||
let (server, port, _) = serve();
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Upgrade\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol: wrong\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
", port)
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
http_client::assert_security_headers_present(&response.headers, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_if_authorization_is_correct() {
|
||||
// given
|
||||
let (server, port, mut authcodes) = serve();
|
||||
let code = authcodes.generate_new().unwrap().replace("-", "");
|
||||
authcodes.to_file(&authcodes.path).unwrap();
|
||||
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
|
||||
// when
|
||||
let response = request(server,
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol: {:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
|
||||
// then
|
||||
assert_eq!(response.status, "HTTP/1.1 101 Switching Protocols".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_allow_initial_connection_but_only_once() {
|
||||
// given
|
||||
let (server, port, authcodes) = serve();
|
||||
let code = "initial";
|
||||
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
assert!(authcodes.is_empty());
|
||||
|
||||
// when
|
||||
let response1 = http_client::request(server.addr(),
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol:{:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
let response2 = http_client::request(server.addr(),
|
||||
&format!("\
|
||||
GET / HTTP/1.1\r\n\
|
||||
Host: 127.0.0.1:{}\r\n\
|
||||
Connection: Close\r\n\
|
||||
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
||||
Sec-WebSocket-Protocol:{:?}_{}\r\n\
|
||||
Sec-WebSocket-Version: 13\r\n\
|
||||
\r\n\
|
||||
{{}}
|
||||
",
|
||||
port,
|
||||
format!("{}:{}", code, timestamp).sha3(),
|
||||
timestamp,
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// then
|
||||
assert_eq!(response1.status, "HTTP/1.1 101 Switching Protocols".to_owned());
|
||||
assert_eq!(response2.status, "HTTP/1.1 403 FORBIDDEN".to_owned());
|
||||
http_client::assert_security_headers_present(&response2.headers, None);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user