Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/core/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{ffi::c_void, marker::PhantomData};
use std::{
ffi::c_void,
fmt::{self, Debug, Display, Formatter},
marker::PhantomData,
};

use libbitcoinkernel_sys::{
btck_Transaction, btck_TransactionInput, btck_TransactionOutPoint, btck_TransactionOutput,
Expand Down Expand Up @@ -653,7 +657,6 @@ pub trait TxidExt: AsPtr<btck_Txid> {
}
}

#[derive(Debug)]
pub struct Txid {
inner: *mut btck_Txid,
}
Expand Down Expand Up @@ -709,7 +712,22 @@ impl PartialEq<TxidRef<'_>> for Txid {

impl Eq for Txid {}

#[derive(Debug)]
impl Debug for Txid {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Txid({:?})", self.to_bytes())
}
}

impl Display for Txid {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();
for byte in bytes.iter().rev() {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}

pub struct TxidRef<'a> {
inner: *const btck_Txid,
marker: PhantomData<&'a ()>,
Expand Down Expand Up @@ -765,6 +783,22 @@ impl PartialEq<Txid> for TxidRef<'_> {
}
}

impl<'a> Debug for TxidRef<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Txid({:?})", self.to_bytes())
}
}

impl<'a> Display for TxidRef<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let bytes = self.to_bytes();
for byte in bytes.iter().rev() {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1322,4 +1356,30 @@ mod tests {

assert_eq!(iter_count, count);
}

#[test]
fn test_txid_display() {
let (tx, _) = get_test_transactions();
let txid = tx.txid().to_owned();

let display = format!("{}", txid);

assert_eq!(
display,
"f3ac0618ad042336fbec1f88a4e965481b46cd3381a807591c78c75fdbae7d67"
);
}

#[test]
fn test_txid_ref_display() {
let (tx, _) = get_test_transactions();
let txid = tx.txid();

let display = format!("{}", txid);

assert_eq!(
display,
"f3ac0618ad042336fbec1f88a4e965481b46cd3381a807591c78c75fdbae7d67"
);
}
}
Loading