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
+1 -1
View File
@@ -363,7 +363,7 @@ impl BlockHeader {
proof: Proof,
) -> Result<Self, Error> {
// Convert hex pre pow string
let mut header_bytes = from_hex(pre_pow)
let mut header_bytes = from_hex(&pre_pow)
.map_err(|e| Error::Serialization(ser::Error::HexError(e.to_string())))?;
// Serialize and append serialized nonce and proof
serialize_default(&mut header_bytes, &nonce)?;
+1 -1
View File
@@ -80,7 +80,7 @@ impl Hash {
/// Convert hex string back to hash.
pub fn from_hex(hex: &str) -> Result<Hash, Error> {
let bytes = util::from_hex(hex.to_string())
let bytes = util::from_hex(hex)
.map_err(|_| Error::HexError(format!("failed to decode {}", hex)))?;
Ok(Hash::from_vec(&bytes))
}
+1 -1
View File
@@ -115,7 +115,7 @@ impl ShortId {
/// Reconstructs a switch commit hash from a hex string.
pub fn from_hex(hex: &str) -> Result<ShortId, ser::Error> {
let bytes = util::from_hex(hex.to_string())
let bytes = util::from_hex(hex)
.map_err(|_| ser::Error::HexError("short_id from_hex error".to_string()))?;
Ok(ShortId::from_bytes(&bytes))
}
+1 -1
View File
@@ -84,7 +84,7 @@ impl MerkleProof {
/// Convert hex string representation back to a Merkle proof instance
pub fn from_hex(hex: &str) -> Result<MerkleProof, String> {
let bytes = util::from_hex(hex.to_string()).unwrap();
let bytes = util::from_hex(hex).unwrap();
let res = ser::deserialize_default(&mut &bytes[..])
.map_err(|_| "failed to deserialize a Merkle Proof".to_string())?;
Ok(res)
+8 -16
View File
@@ -92,10 +92,8 @@ pub fn genesis_floo() -> core::Block {
let kernel = core::TxKernel {
features: core::KernelFeatures::Coinbase,
excess: Commitment::from_vec(
util::from_hex(
"08df2f1d996cee37715d9ac0a0f3b13aae508d1101945acb8044954aee30960be9".to_string(),
)
.unwrap(),
util::from_hex("08df2f1d996cee37715d9ac0a0f3b13aae508d1101945acb8044954aee30960be9")
.unwrap(),
),
excess_sig: Signature::from_raw_data(&[
25, 176, 52, 246, 172, 1, 12, 220, 247, 111, 73, 101, 13, 16, 157, 130, 110, 196, 123,
@@ -108,10 +106,8 @@ pub fn genesis_floo() -> core::Block {
let output = core::Output {
features: core::OutputFeatures::Coinbase,
commit: Commitment::from_vec(
util::from_hex(
"08c12007af16d1ee55fffe92cef808c77e318dae70c3bc70cb6361f49d517f1b68".to_string(),
)
.unwrap(),
util::from_hex("08c12007af16d1ee55fffe92cef808c77e318dae70c3bc70cb6361f49d517f1b68")
.unwrap(),
),
proof: RangeProof {
plen: SINGLE_BULLET_PROOF_SIZE,
@@ -208,10 +204,8 @@ pub fn genesis_main() -> core::Block {
let kernel = core::TxKernel {
features: core::KernelFeatures::Coinbase,
excess: Commitment::from_vec(
util::from_hex(
"096385d86c5cfda718aa0b7295be0adf7e5ac051edfe130593a2a257f09f78a3b1".to_string(),
)
.unwrap(),
util::from_hex("096385d86c5cfda718aa0b7295be0adf7e5ac051edfe130593a2a257f09f78a3b1")
.unwrap(),
),
excess_sig: Signature::from_raw_data(&[
80, 208, 41, 171, 28, 224, 250, 121, 60, 192, 213, 232, 111, 199, 111, 105, 18, 22, 54,
@@ -224,10 +218,8 @@ pub fn genesis_main() -> core::Block {
let output = core::Output {
features: core::OutputFeatures::Coinbase,
commit: Commitment::from_vec(
util::from_hex(
"08b7e57c448db5ef25aa119dde2312c64d7ff1b890c416c6dda5ec73cbfed2edea".to_string(),
)
.unwrap(),
util::from_hex("08b7e57c448db5ef25aa119dde2312c64d7ff1b890c416c6dda5ec73cbfed2edea")
.unwrap(),
),
proof: RangeProof {
plen: SINGLE_BULLET_PROOF_SIZE,
+7 -7
View File
@@ -44,7 +44,7 @@ pub mod pubkey_serde {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
String::deserialize(deserializer)
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
.and_then(|string| from_hex(&string).map_err(|err| Error::custom(err.to_string())))
.and_then(|bytes: Vec<u8>| {
PublicKey::from_slice(&static_secp, &bytes)
.map_err(|err| Error::custom(err.to_string()))
@@ -81,7 +81,7 @@ pub mod option_sig_serde {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
Option::<String>::deserialize(deserializer).and_then(|res| match res {
Some(string) => from_hex(string)
Some(string) => from_hex(&string)
.map_err(|err| Error::custom(err.to_string()))
.and_then(|bytes: Vec<u8>| {
let mut b = [0u8; 64];
@@ -123,7 +123,7 @@ pub mod option_seckey_serde {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
Option::<String>::deserialize(deserializer).and_then(|res| match res {
Some(string) => from_hex(string)
Some(string) => from_hex(&string)
.map_err(|err| Error::custom(err.to_string()))
.and_then(|bytes: Vec<u8>| {
let mut b = [0u8; 32];
@@ -161,7 +161,7 @@ pub mod sig_serde {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
String::deserialize(deserializer)
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
.and_then(|string| from_hex(&string).map_err(|err| Error::custom(err.to_string())))
.and_then(|bytes: Vec<u8>| {
let mut b = [0u8; 64];
b.copy_from_slice(&bytes[0..64]);
@@ -195,7 +195,7 @@ pub mod option_commitment_serde {
D: Deserializer<'de>,
{
Option::<String>::deserialize(deserializer).and_then(|res| match res {
Some(string) => from_hex(string)
Some(string) => from_hex(&string)
.map_err(|err| Error::custom(err.to_string()))
.and_then(|bytes: Vec<u8>| Ok(Some(Commitment::from_vec(bytes.to_vec())))),
None => Ok(None),
@@ -221,7 +221,7 @@ where
use serde::de::{Error, IntoDeserializer};
let val = String::deserialize(deserializer)
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))?;
.and_then(|string| from_hex(&string).map_err(|err| Error::custom(err.to_string())))?;
RangeProof::deserialize(val.into_deserializer())
}
@@ -232,7 +232,7 @@ where
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| from_hex(string).map_err(|err| Error::custom(err.to_string())))
.and_then(|string| from_hex(&string).map_err(|err| Error::custom(err.to_string())))
.and_then(|bytes: Vec<u8>| Ok(Commitment::from_vec(bytes.to_vec())))
}