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

Commit

Permalink
private-tx: replace error_chain (#10510)
Browse files Browse the repository at this point in the history
* Update to vanilla tx pool error

* private-tx: remove error-chain, implement Error, derive Display

* private-tx: replace ErrorKind and bail!

* private-tx: add missing From impls and other compiler errors

* private-tx: use original tx-pool error

* Don't be silly cargo
  • Loading branch information
ascjones authored and soc1c committed Mar 27, 2019
1 parent 3b23817 commit 7d26a82
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 196 deletions.
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ethcore/private-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
common-types = { path = "../types" }
error-chain = { version = "0.12", default-features = false }
derive_more = "0.14.0"
ethabi = "6.0"
ethabi-contract = "6.0"
ethabi-derive = "6.0"
Expand Down
26 changes: 13 additions & 13 deletions ethcore/private-tx/src/encryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crypto;
use futures::Future;
use fetch::{Fetch, Client as FetchClient, Method, BodyReader, Request};
use bytes::{Bytes, ToPretty};
use error::{Error, ErrorKind};
use error::Error;
use url::Url;
use super::Signer;
use super::key_server_keys::address_to_key;
Expand Down Expand Up @@ -111,11 +111,11 @@ impl SecretStoreEncryptor {
return Ok(key);
}
let contract_address_signature = self.sign_contract_address(contract_address)?;
let requester = self.config.key_server_account.ok_or_else(|| ErrorKind::KeyServerAccountNotSet)?;
let requester = self.config.key_server_account.ok_or_else(|| Error::KeyServerAccountNotSet)?;

// key id in SS is H256 && we have H160 here => expand with assitional zeros
let contract_address_extended: H256 = contract_address.into();
let base_url = self.config.base_url.clone().ok_or_else(|| ErrorKind::KeyServerNotSet)?;
let base_url = self.config.base_url.clone().ok_or_else(|| Error::KeyServerNotSet)?;

// prepare request url
let url = format!("{}/{}/{}{}",
Expand All @@ -132,24 +132,24 @@ impl SecretStoreEncryptor {
Method::GET
};

let url = Url::from_str(&url).map_err(|e| ErrorKind::Encrypt(e.to_string()))?;
let url = Url::from_str(&url).map_err(|e| Error::Encrypt(e.to_string()))?;
let response = self.client.fetch(Request::new(url, method), Default::default()).wait()
.map_err(|e| ErrorKind::Encrypt(e.to_string()))?;
.map_err(|e| Error::Encrypt(e.to_string()))?;

if response.is_not_found() {
bail!(ErrorKind::EncryptionKeyNotFound(*contract_address));
return Err(Error::EncryptionKeyNotFound(*contract_address));
}

if !response.is_success() {
bail!(ErrorKind::Encrypt(response.status().canonical_reason().unwrap_or("unknown").into()));
return Err(Error::Encrypt(response.status().canonical_reason().unwrap_or("unknown").into()));
}

// read HTTP response
let mut result = String::new();
BodyReader::new(response).read_to_string(&mut result)?;

// response is JSON string (which is, in turn, hex-encoded, encrypted Public)
let encrypted_bytes: ethjson::bytes::Bytes = result.trim_matches('\"').parse().map_err(|e| ErrorKind::Encrypt(e))?;
let encrypted_bytes: ethjson::bytes::Bytes = result.trim_matches('\"').parse().map_err(|e| Error::Encrypt(e))?;

// decrypt Public
let decrypted_bytes = self.signer.decrypt(requester, &crypto::DEFAULT_MAC, &encrypted_bytes)?;
Expand Down Expand Up @@ -189,7 +189,7 @@ impl SecretStoreEncryptor {
}

fn sign_contract_address(&self, contract_address: &Address) -> Result<Signature, Error> {
let key_server_account = self.config.key_server_account.ok_or_else(|| ErrorKind::KeyServerAccountNotSet)?;
let key_server_account = self.config.key_server_account.ok_or_else(|| Error::KeyServerAccountNotSet)?;
Ok(self.signer.sign(key_server_account, address_to_key(contract_address))?)
}
}
Expand All @@ -204,7 +204,7 @@ impl Encryptor for SecretStoreEncryptor {
// retrieve the key, try to generate it if it doesn't exist yet
let key = match self.retrieve_key("", false, contract_address) {
Ok(key) => Ok(key),
Err(Error(ErrorKind::EncryptionKeyNotFound(_), _)) => {
Err(Error::EncryptionKeyNotFound(_)) => {
trace!(target: "privatetx", "Key for account wasnt found in sstore. Creating. Address: {:?}", contract_address);
self.retrieve_key(&format!("/{}", self.config.threshold), true, contract_address)
}
Expand All @@ -215,7 +215,7 @@ impl Encryptor for SecretStoreEncryptor {
let mut cypher = Vec::with_capacity(plain_data.len() + initialisation_vector.len());
cypher.extend(repeat(0).take(plain_data.len()));
crypto::aes::encrypt_128_ctr(&key, initialisation_vector, plain_data, &mut cypher)
.map_err(|e| ErrorKind::Encrypt(e.to_string()))?;
.map_err(|e| Error::Encrypt(e.to_string()))?;
cypher.extend_from_slice(&initialisation_vector);

Ok(cypher)
Expand All @@ -230,7 +230,7 @@ impl Encryptor for SecretStoreEncryptor {
// initialization vector takes INIT_VEC_LEN bytes
let cypher_len = cypher.len();
if cypher_len < INIT_VEC_LEN {
bail!(ErrorKind::Decrypt("Invalid cypher".into()));
return Err(Error::Decrypt("Invalid cypher".into()));
}

// retrieve existing key
Expand All @@ -241,7 +241,7 @@ impl Encryptor for SecretStoreEncryptor {
let mut plain_data = Vec::with_capacity(cypher_len - INIT_VEC_LEN);
plain_data.extend(repeat(0).take(cypher_len - INIT_VEC_LEN));
crypto::aes::decrypt_128_ctr(&key, &iv, cypher, &mut plain_data)
.map_err(|e| ErrorKind::Decrypt(e.to_string()))?;
.map_err(|e| Error::Decrypt(e.to_string()))?;
Ok(plain_data)
}
}
Expand Down
Loading

0 comments on commit 7d26a82

Please sign in to comment.