From a1664c6bbc99aa2df7f1d3f261b6fd82ff90c504 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 11 Oct 2018 23:16:48 +0100 Subject: [PATCH] impl Display for BlockHash and TxId --- Cargo.lock | 7 +++++++ zcash_primitives/Cargo.toml | 1 + zcash_primitives/src/block.rs | 10 ++++++++++ zcash_primitives/src/lib.rs | 1 + zcash_primitives/src/transaction/mod.rs | 10 ++++++++++ 5 files changed, 29 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 33d7675..baffb57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,6 +190,11 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hex-literal" version = "0.1.1" @@ -435,6 +440,7 @@ dependencies = [ "blake2-rfc 0.2.18 (git+https://github.com/gtank/blake2-rfc?rev=7a5b5fc99ae483a0043db7547fb79a6fa44b88a9)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ff 0.4.0", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "pairing 0.14.2", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -493,6 +499,7 @@ dependencies = [ "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4da5f0e01bd8a71a224a4eedecaacfcabda388dbb7a80faf04d3514287572d95" "checksum hex-literal-impl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d340b6514f232f6db1bd16db65302a5278a04fef9ce867cb932e7e5fa21130a" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index 01e5243..ab0d87b 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -8,6 +8,7 @@ authors = [ [dependencies] byteorder = "1" ff = { path = "../ff" } +hex = "0.3" lazy_static = "1" pairing = { path = "../pairing" } rand = "0.4" diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs index 816975d..d0c47b3 100644 --- a/zcash_primitives/src/block.rs +++ b/zcash_primitives/src/block.rs @@ -1,8 +1,18 @@ +use hex; +use std::fmt; use std::ops::Deref; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct BlockHash(pub [u8; 32]); +impl fmt::Display for BlockHash { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let mut data = self.0.to_vec(); + data.reverse(); + formatter.write_str(&hex::encode(data)) + } +} + /// A Zcash block header. pub struct BlockHeader(BlockHeaderData); diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index 97b826f..ba76dc7 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -4,6 +4,7 @@ extern crate lazy_static; extern crate blake2_rfc; extern crate byteorder; extern crate ff; +extern crate hex; extern crate pairing; extern crate rand; extern crate sapling_crypto; diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 2993dfd..6aea0cf 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -1,5 +1,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use hex; use sapling_crypto::redjubjub::Signature; +use std::fmt; use std::io::{self, Read, Write}; use std::ops::Deref; @@ -23,6 +25,14 @@ const SAPLING_TX_VERSION: u32 = 4; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct TxId(pub [u8; 32]); +impl fmt::Display for TxId { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let mut data = self.0.to_vec(); + data.reverse(); + formatter.write_str(&hex::encode(data)) + } +} + /// A Zcash transaction. #[derive(Debug)] pub struct Transaction(TransactionData);