Skip to content

Commit 7c438b2

Browse files
committed
transaction: Implement Coinbase type with explicit API design
We provide explicit convenience methods (compute_txid, compute_wtxid) rather than implementing Deref to only expose methods that make sense for coinbase transactions. This prevents inheritance of the entire Transaction API. inner() serves as an escape hatch when full Transaction access is needed. This forces the user to be explicit about when they need the underlying Transaction functionalities. Following this approach, we avoid implicit inheritance through Deref. We also added assume_* methods to make it clear that the caller is responsible for ensuring the transaction is actually a coinbase transaction in the first position of a block
1 parent 6571307 commit 7c438b2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

bitcoin/src/blockdata/transaction.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,42 @@ internals::transparent_newtype! {
11691169
pub fn assume_coinbase_ref(inner: &_) -> &Self;
11701170
}
11711171
}
1172+
1173+
impl Coinbase {
1174+
/// Creates a `Coinbase` wrapper assuming this transaction is a coinbase transaction.
1175+
///
1176+
/// This method does not validate that the transaction is actually a coinbase transaction.
1177+
/// The caller must ensure that this transaction is indeed a valid coinbase transaction.
1178+
pub fn assume_coinbase(tx: Transaction) -> Self {
1179+
Self(tx)
1180+
}
1181+
1182+
/// Returns a reference to the underlying transaction.
1183+
///
1184+
/// Warning: The coinbase input contains dummy prevouts that should not be treated as real prevouts.
1185+
#[doc(alias = "as_inner")]
1186+
pub fn as_transaction(&self) -> &Transaction { &self.0 }
1187+
1188+
/// Returns the underlying transaction.
1189+
///
1190+
/// Warning: The coinbase input contains dummy prevouts that should not be treated as real prevouts.
1191+
#[doc(alias = "into_inner")]
1192+
pub fn into_transaction(self) -> Transaction { self.0 }
1193+
1194+
/// Computes the [`Txid`] of this coinbase transaction.
1195+
pub fn compute_txid(&self) -> Txid {
1196+
self.0.compute_txid()
1197+
}
1198+
1199+
/// Returns the wtxid of this coinbase transaction.
1200+
///
1201+
/// For coinbase transactions, this is always `Wtxid::COINBASE`.
1202+
#[doc(alias = "compute_wtxid")]
1203+
pub const fn wtxid(&self) -> Wtxid {
1204+
Wtxid::COINBASE
1205+
}
1206+
}
1207+
11721208
mod sealed {
11731209
pub trait Sealed {}
11741210
impl Sealed for super::Transaction {}

0 commit comments

Comments
 (0)