cleanup util::from_hex() (#3265)

* cleanup our from_hex()

* fix keychain tests

* add test coverage for empty hex string
This commit is contained in:
Antioch Peverell
2020-03-10 10:36:18 +00:00
committed by GitHub
parent ee5fe1ac63
commit 8ca381a9c2
18 changed files with 62 additions and 87 deletions
+2 -2
View File
@@ -747,7 +747,7 @@ impl ForeignRpc for Foreign {
) -> Result<BlockHeaderPrintable, ErrorKind> {
let mut parsed_hash: Option<Hash> = None;
if let Some(hash) = hash {
let vec = util::from_hex(hash)
let vec = util::from_hex(&hash)
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
parsed_hash = Some(Hash::from_vec(&vec));
}
@@ -761,7 +761,7 @@ impl ForeignRpc for Foreign {
) -> Result<BlockPrintable, ErrorKind> {
let mut parsed_hash: Option<Hash> = None;
if let Some(hash) = hash {
let vec = util::from_hex(hash)
let vec = util::from_hex(&hash)
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
parsed_hash = Some(Hash::from_vec(&vec));
}
+2 -2
View File
@@ -47,7 +47,7 @@ impl HeaderHandler {
}
}
check_block_param(&input)?;
let vec = util::from_hex(input)
let vec = util::from_hex(&input)
.map_err(|e| ErrorKind::Argument(format!("invalid input: {}", e)))?;
let h = Hash::from_vec(&vec);
let header = w(&self.chain)?
@@ -153,7 +153,7 @@ impl BlockHandler {
}
}
check_block_param(&input)?;
let vec = util::from_hex(input)
let vec = util::from_hex(&input)
.map_err(|e| ErrorKind::Argument(format!("invalid input: {}", e)))?;
Ok(Hash::from_vec(&vec))
}
+3 -3
View File
@@ -308,7 +308,7 @@ impl OutputHandler {
let query = must_get_query!(req);
let params = QueryParams::from(query);
params.process_multival_param("id", |id| {
if let Ok(x) = util::from_hex(String::from(id)) {
if let Ok(x) = util::from_hex(id) {
commitments.push(Commitment::from_vec(x));
}
});
@@ -392,7 +392,7 @@ impl KernelHandler {
.rsplit('/')
.next()
.ok_or_else(|| ErrorKind::RequestError("missing excess".into()))?;
let excess = util::from_hex(excess.to_owned())
let excess = util::from_hex(excess)
.map_err(|_| ErrorKind::RequestError("invalid excess hex".into()))?;
if excess.len() != 33 {
return Err(ErrorKind::RequestError("invalid excess length".into()).into());
@@ -444,7 +444,7 @@ impl KernelHandler {
min_height: Option<u64>,
max_height: Option<u64>,
) -> Result<LocatedTxKernel, Error> {
let excess = util::from_hex(excess)
let excess = util::from_hex(&excess)
.map_err(|_| ErrorKind::RequestError("invalid excess hex".into()))?;
if excess.len() != 33 {
return Err(ErrorKind::RequestError("invalid excess length".into()).into());
+1 -1
View File
@@ -109,7 +109,7 @@ async fn update_pool(
let fluff = params.get("fluff").is_some();
let wrapper: TxWrapper = parse_body(req).await?;
let tx_bin = util::from_hex(wrapper.tx_hex)
let tx_bin = util::from_hex(&wrapper.tx_hex)
.map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)))?;
// All wallet api interaction explicitly uses protocol version 1 for now.
+2 -4
View File
@@ -120,10 +120,8 @@ impl TxHashSetHandler {
// return a dummy output with merkle proof for position filled out
// (to avoid having to create a new type to pass around)
fn get_merkle_proof_for_output(&self, id: &str) -> Result<OutputPrintable, Error> {
let c = util::from_hex(String::from(id)).context(ErrorKind::Argument(format!(
"Not a valid commitment: {}",
id
)))?;
let c = util::from_hex(id)
.map_err(|_| ErrorKind::Argument(format!("Not a valid commitment: {}", id)))?;
let commit = Commitment::from_vec(c);
let chain = w(&self.chain)?;
let output_pos = chain.get_output_pos(&commit).context(ErrorKind::NotFound)?;
+2 -5
View File
@@ -19,7 +19,6 @@ use crate::rest::*;
use crate::types::*;
use crate::util;
use crate::util::secp::pedersen::Commitment;
use failure::ResultExt;
use std::sync::{Arc, Weak};
// All handlers use `Weak` references instead of `Arc` to avoid cycles that
@@ -35,10 +34,8 @@ fn get_unspent(
chain: &Arc<chain::Chain>,
id: &str,
) -> Result<Option<(CommitPos, OutputIdentifier)>, Error> {
let c = util::from_hex(String::from(id)).context(ErrorKind::Argument(format!(
"Not a valid commitment: {}",
id
)))?;
let c = util::from_hex(id)
.map_err(|_| ErrorKind::Argument(format!("Not a valid commitment: {}", id)))?;
let commit = Commitment::from_vec(c);
// We need the features here to be able to generate the necessary hash
+3 -4
View File
@@ -246,7 +246,7 @@ impl<'de> serde::de::Visitor<'de> for PrintableCommitmentVisitor {
{
Ok(PrintableCommitment {
commit: pedersen::Commitment::from_vec(
util::from_hex(String::from(v)).map_err(serde::de::Error::custom)?,
util::from_hex(v).map_err(serde::de::Error::custom)?,
),
})
}
@@ -337,7 +337,7 @@ impl OutputPrintable {
.clone()
.ok_or_else(|| ser::Error::HexError("output range_proof missing".to_string()))?;
let p_vec = util::from_hex(proof_str)
let p_vec = util::from_hex(&proof_str)
.map_err(|_| ser::Error::HexError("invalid output range_proof".to_string()))?;
let mut p_bytes = [0; util::secp::constants::MAX_PROOF_SIZE];
for i in 0..p_bytes.len() {
@@ -421,8 +421,7 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
no_dup!(commit);
let val: String = map.next_value()?;
let vec =
util::from_hex(val.clone()).map_err(serde::de::Error::custom)?;
let vec = util::from_hex(&val).map_err(serde::de::Error::custom)?;
commit = Some(pedersen::Commitment::from_vec(vec));
}
Field::Spent => {