Skip to content

Commit

Permalink
primitives: fix Txid and BlockHash types serde serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 19, 2023
1 parent 9f636cf commit 272faaf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 89 deletions.
18 changes: 12 additions & 6 deletions primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use amplify::{Bytes32, Wrapper};
use amplify::hex::{self, FromHex};
use amplify::{Bytes32, Bytes32StrRev, Wrapper};

use crate::LIB_NAME_BITCOIN;

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Display, From)]
#[display(LowerHex)]
#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[wrapper(BorrowSlice, Index, RangeOps)]
#[wrapper(BorrowSlice, Index, RangeOps, Debug, LowerHex, UpperHex, Display, FromStr)]
pub struct BlockHash(
#[from]
#[from([u8; 32])]
Bytes32,
Bytes32StrRev,
);
impl_sha256d_hashtype!(BlockHash, "BlockHash");

impl FromHex for BlockHash {
fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error>
where I: Iterator<Item = Result<u8, hex::Error>> + ExactSizeIterator + DoubleEndedIterator {
Bytes32StrRev::from_byte_iter(iter).map(Self)
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(
Expand Down
2 changes: 0 additions & 2 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ extern crate serde_crate as serde;
/// Re-export of `secp256k1` crate.
pub extern crate secp256k1;

#[macro_use]
mod macros;
mod block;
pub mod opcodes;
mod script;
Expand Down
74 changes: 0 additions & 74 deletions primitives/src/macros.rs

This file was deleted.

23 changes: 16 additions & 7 deletions primitives/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use std::iter::Sum;
use std::num::ParseIntError;
use std::str::FromStr;

use amplify::{hex, Bytes32, Wrapper};
use amplify::hex::FromHex;
use amplify::{hex, Bytes32StrRev, Wrapper};

use super::{VarIntArray, LIB_NAME_BITCOIN};
use crate::{NonStandardValue, ScriptPubkey, SigScript};

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Display, From)]
#[display(LowerHex)]
#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[derive(CommitEncode)]
Expand All @@ -40,19 +40,25 @@ use crate::{NonStandardValue, ScriptPubkey, SigScript};
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[wrapper(BorrowSlice, Index, RangeOps)]
#[wrapper(BorrowSlice, Index, RangeOps, Debug, LowerHex, UpperHex, Display, FromStr)]
// all-zeros used in coinbase
pub struct Txid(
#[from]
#[from([u8; 32])]
Bytes32,
Bytes32StrRev,
);
impl_sha256d_hashtype!(Txid, "Txid");

impl Txid {
pub fn coinbase() -> Self { Self(zero!()) }
}

impl FromHex for Txid {
fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error>
where I: Iterator<Item = Result<u8, hex::Error>> + ExactSizeIterator + DoubleEndedIterator {
Bytes32StrRev::from_byte_iter(iter).map(Self)
}
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
Expand Down Expand Up @@ -367,11 +373,14 @@ mod test {

#[test]
fn txid_byteorder() {
let hex = "c9a86c99127f1b2d1ff495c238f13069ac881ec9527905016122d11d85b19b61";
let hex = "ed9f6388c0360c1861d331a0388d5a54815dd720cc67fa783c348217a0e943ca";
let from_str = Txid::from_str(hex).unwrap();
let from_hex = Txid::from_hex(hex).unwrap();
assert_eq!(from_str, from_hex);
assert_eq!(from_str.to_string(), from_str.to_hex());
assert_eq!(from_str.to_string(), hex);
assert_eq!(format!("{from_str:x}"), hex);
assert_eq!(from_str[0], 0xca);
}

#[test]
Expand Down

0 comments on commit 272faaf

Please sign in to comment.