Pass byte slice to to_hex (#3307)
Currently we pass a Vec. This requires an extra allocation and copy of all elements if a caller doesn't have a Vec already, which is at least 95% of cases. Another, a smaller issue, we have a function util::to_hex and some structs implement to_hex() on top of it, so we have a mix of it in the code. This PR introduces a trait and a blanket impl for AsRef<[u8]> which brings a uniform API (obj.to_hex()). One unfortunate case is arrays of size bigger than 32 - Rust doesn't implement AsRef for them so it requires an ugly hack (&array[..]).to_hex().
This commit is contained in:
@@ -20,10 +20,8 @@
|
||||
use crate::ser::{self, Error, ProtocolVersion, Readable, Reader, Writeable, Writer};
|
||||
use blake2::blake2b::Blake2b;
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use std::cmp::min;
|
||||
use std::convert::AsRef;
|
||||
use std::{fmt, ops};
|
||||
use util;
|
||||
use std::{cmp::min, convert::AsRef, fmt, ops};
|
||||
use util::ToHex;
|
||||
|
||||
/// A hash consisting of all zeroes, used as a sentinel. No known preimage.
|
||||
pub const ZERO_HASH: Hash = Hash([0; 32]);
|
||||
@@ -73,11 +71,6 @@ impl Hash {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Convert a hash to hex string format.
|
||||
pub fn to_hex(&self) -> String {
|
||||
util::to_hex(self.to_vec())
|
||||
}
|
||||
|
||||
/// Convert hex string back to hash.
|
||||
pub fn from_hex(hex: &str) -> Result<Hash, Error> {
|
||||
let bytes = util::from_hex(hex)
|
||||
|
||||
+7
-6
@@ -19,7 +19,7 @@ use crate::ser::{self, Readable, Reader, Writeable, Writer};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use siphasher::sip::SipHasher24;
|
||||
use std::cmp::{min, Ordering};
|
||||
use util;
|
||||
use util::ToHex;
|
||||
|
||||
/// The size of a short id used to identify inputs|outputs|kernels (6 bytes)
|
||||
pub const SHORT_ID_SIZE: usize = 6;
|
||||
@@ -84,6 +84,12 @@ impl ::std::fmt::Debug for ShortId {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for ShortId {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Readable for ShortId {
|
||||
fn read(reader: &mut dyn Reader) -> Result<ShortId, ser::Error> {
|
||||
let v = reader.read_fixed_bytes(SHORT_ID_SIZE)?;
|
||||
@@ -108,11 +114,6 @@ impl ShortId {
|
||||
ShortId(hash)
|
||||
}
|
||||
|
||||
/// Hex string representation of a short_id
|
||||
pub fn to_hex(&self) -> String {
|
||||
util::to_hex(self.0.to_vec())
|
||||
}
|
||||
|
||||
/// 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)
|
||||
|
||||
@@ -18,7 +18,7 @@ use crate::core::hash::Hash;
|
||||
use crate::core::pmmr;
|
||||
use crate::ser;
|
||||
use crate::ser::{PMMRIndexHashable, Readable, Reader, Writeable, Writer};
|
||||
use util;
|
||||
use util::ToHex;
|
||||
|
||||
/// Merkle proof errors.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -79,7 +79,7 @@ impl MerkleProof {
|
||||
pub fn to_hex(&self) -> String {
|
||||
let mut vec = Vec::new();
|
||||
ser::serialize_default(&mut vec, &self).expect("serialization failed");
|
||||
util::to_hex(vec)
|
||||
vec.to_hex()
|
||||
}
|
||||
|
||||
/// Convert hex string representation back to a Merkle proof instance
|
||||
|
||||
@@ -30,11 +30,11 @@ use std::cmp::{max, min};
|
||||
use std::convert::TryInto;
|
||||
use std::sync::Arc;
|
||||
use std::{error, fmt};
|
||||
use util;
|
||||
use util::secp;
|
||||
use util::secp::pedersen::{Commitment, RangeProof};
|
||||
use util::static_secp_instance;
|
||||
use util::RwLock;
|
||||
use util::ToHex;
|
||||
|
||||
/// Various tx kernel variants.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
|
||||
@@ -1469,6 +1469,11 @@ impl Output {
|
||||
self.proof
|
||||
}
|
||||
|
||||
/// Get range proof as byte slice
|
||||
pub fn proof_bytes(&self) -> &[u8] {
|
||||
&self.proof.proof[..]
|
||||
}
|
||||
|
||||
/// Validates the range proof using the commitment
|
||||
pub fn verify_proof(&self) -> Result<(), Error> {
|
||||
let secp = static_secp_instance();
|
||||
@@ -1523,14 +1528,11 @@ impl OutputIdentifier {
|
||||
commit: self.commit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// convert an output_identifier to hex string format.
|
||||
pub fn to_hex(&self) -> String {
|
||||
format!(
|
||||
"{:b}{}",
|
||||
self.features as u8,
|
||||
util::to_hex(self.commit.0.to_vec()),
|
||||
)
|
||||
impl ToHex for OutputIdentifier {
|
||||
fn to_hex(&self) -> String {
|
||||
format!("{:b}{}", self.features as u8, self.commit.to_hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -273,6 +273,7 @@ mod test {
|
||||
use super::*;
|
||||
use crate::core::hash::Hashed;
|
||||
use crate::ser::{self, ProtocolVersion};
|
||||
use util::ToHex;
|
||||
|
||||
#[test]
|
||||
fn floonet_genesis_hash() {
|
||||
|
||||
+12
-12
@@ -17,13 +17,13 @@
|
||||
use keychain::BlindingFactor;
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
use util::secp::pedersen::{Commitment, RangeProof};
|
||||
use util::{from_hex, to_hex};
|
||||
use util::{from_hex, ToHex};
|
||||
|
||||
/// Serializes a secp PublicKey to and from hex
|
||||
pub mod pubkey_serde {
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
use util::secp::key::PublicKey;
|
||||
use util::{from_hex, static_secp_instance, to_hex};
|
||||
use util::{from_hex, static_secp_instance, ToHex};
|
||||
|
||||
///
|
||||
pub fn serialize<S>(key: &PublicKey, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -32,7 +32,7 @@ pub mod pubkey_serde {
|
||||
{
|
||||
let static_secp = static_secp_instance();
|
||||
let static_secp = static_secp.lock();
|
||||
serializer.serialize_str(&to_hex(key.serialize_vec(&static_secp, true).to_vec()))
|
||||
serializer.serialize_str(&key.serialize_vec(&static_secp, true).to_hex())
|
||||
}
|
||||
|
||||
///
|
||||
@@ -56,7 +56,7 @@ pub mod pubkey_serde {
|
||||
pub mod option_sig_serde {
|
||||
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||
use serde::de::Error;
|
||||
use util::{from_hex, secp, static_secp_instance, to_hex};
|
||||
use util::{from_hex, secp, static_secp_instance, ToHex};
|
||||
|
||||
///
|
||||
pub fn serialize<S>(sig: &Option<secp::Signature>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -67,7 +67,7 @@ pub mod option_sig_serde {
|
||||
let static_secp = static_secp.lock();
|
||||
match sig {
|
||||
Some(sig) => {
|
||||
serializer.serialize_str(&to_hex(sig.serialize_compact(&static_secp).to_vec()))
|
||||
serializer.serialize_str(&(&sig.serialize_compact(&static_secp)[..]).to_hex())
|
||||
}
|
||||
None => serializer.serialize_none(),
|
||||
}
|
||||
@@ -99,7 +99,7 @@ pub mod option_sig_serde {
|
||||
pub mod option_seckey_serde {
|
||||
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||
use serde::de::Error;
|
||||
use util::{from_hex, secp, static_secp_instance, to_hex};
|
||||
use util::{from_hex, secp, static_secp_instance, ToHex};
|
||||
|
||||
///
|
||||
pub fn serialize<S>(
|
||||
@@ -110,7 +110,7 @@ pub mod option_seckey_serde {
|
||||
S: Serializer,
|
||||
{
|
||||
match key {
|
||||
Some(key) => serializer.serialize_str(&to_hex(key.0.to_vec())),
|
||||
Some(key) => serializer.serialize_str(&key.0.to_hex()),
|
||||
None => serializer.serialize_none(),
|
||||
}
|
||||
}
|
||||
@@ -141,7 +141,7 @@ pub mod option_seckey_serde {
|
||||
pub mod sig_serde {
|
||||
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||
use serde::de::Error;
|
||||
use util::{from_hex, secp, static_secp_instance, to_hex};
|
||||
use util::{from_hex, secp, static_secp_instance, ToHex};
|
||||
|
||||
///
|
||||
pub fn serialize<S>(sig: &secp::Signature, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -150,7 +150,7 @@ pub mod sig_serde {
|
||||
{
|
||||
let static_secp = static_secp_instance();
|
||||
let static_secp = static_secp.lock();
|
||||
serializer.serialize_str(&to_hex(sig.serialize_compact(&static_secp).to_vec()))
|
||||
serializer.serialize_str(&(&sig.serialize_compact(&static_secp)[..]).to_hex())
|
||||
}
|
||||
|
||||
///
|
||||
@@ -176,7 +176,7 @@ pub mod option_commitment_serde {
|
||||
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||
use serde::de::Error;
|
||||
use util::secp::pedersen::Commitment;
|
||||
use util::{from_hex, to_hex};
|
||||
use util::{from_hex, ToHex};
|
||||
|
||||
///
|
||||
pub fn serialize<S>(commit: &Option<Commitment>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -184,7 +184,7 @@ pub mod option_commitment_serde {
|
||||
S: Serializer,
|
||||
{
|
||||
match commit {
|
||||
Some(c) => serializer.serialize_str(&to_hex(c.0.to_vec())),
|
||||
Some(c) => serializer.serialize_str(&c.to_hex()),
|
||||
None => serializer.serialize_none(),
|
||||
}
|
||||
}
|
||||
@@ -242,7 +242,7 @@ where
|
||||
T: AsRef<[u8]>,
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&to_hex(bytes.as_ref().to_vec()))
|
||||
serializer.serialize_str(&bytes.to_hex())
|
||||
}
|
||||
|
||||
/// Used to ensure u64s are serialised in json
|
||||
|
||||
@@ -19,7 +19,7 @@ use crate::pow::{PoWContext, Proof};
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use croaring::Bitmap;
|
||||
use std::mem;
|
||||
use util;
|
||||
use util::ToHex;
|
||||
|
||||
struct Graph<T>
|
||||
where
|
||||
@@ -224,7 +224,7 @@ where
|
||||
pub fn sipkey_hex(&self, index: usize) -> Result<String, Error> {
|
||||
let mut rdr = vec![];
|
||||
rdr.write_u64::<BigEndian>(self.params.siphash_keys[index])?;
|
||||
Ok(util::to_hex(rdr))
|
||||
Ok(rdr.to_hex())
|
||||
}
|
||||
|
||||
/// Return number of bytes used by the graph
|
||||
|
||||
Reference in New Issue
Block a user