Skip to content

Commit d069771

Browse files
committed
docs: add API docs for Transaction type
1 parent 1727502 commit d069771

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

bdk-ffi/src/bitcoin.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,62 +245,124 @@ impl Display for Address {
245245
impl_from_core_type!(BdkAddress, Address);
246246
impl_into_core_type!(Address, BdkAddress);
247247

248+
/// Bitcoin transaction.
249+
/// An authenticated movement of coins.
248250
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
249251
pub struct Transaction(BdkTransaction);
250252

251253
#[uniffi::export]
252254
impl Transaction {
255+
/// Creates a new `Transaction` instance from serialized transaction bytes.
253256
#[uniffi::constructor]
254257
pub fn new(transaction_bytes: Vec<u8>) -> Result<Self, TransactionError> {
255258
let mut decoder = Cursor::new(transaction_bytes);
256259
let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder)?;
257260
Ok(Transaction(tx))
258261
}
259262

263+
/// Computes the Txid.
264+
/// Hashes the transaction excluding the segwit data (i.e. the marker, flag bytes, and the witness fields themselves).
260265
pub fn compute_txid(&self) -> String {
261266
self.0.compute_txid().to_string()
262267
}
263268

269+
/// Returns the weight of this transaction, as defined by BIP-141.
270+
///
271+
/// > Transaction weight is defined as Base transaction size * 3 + Total transaction size (ie.
272+
/// > the same method as calculating Block weight from Base size and Total size).
273+
///
274+
/// For transactions with an empty witness, this is simply the consensus-serialized size times
275+
/// four. For transactions with a witness, this is the non-witness consensus-serialized size
276+
/// multiplied by three plus the with-witness consensus-serialized size.
277+
///
278+
/// For transactions with no inputs, this function will return a value 2 less than the actual
279+
/// weight of the serialized transaction. The reason is that zero-input transactions, post-segwit,
280+
/// cannot be unambiguously serialized; we make a choice that adds two extra bytes. For more
281+
/// details see [BIP 141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki)
282+
/// which uses a "input count" of `0x00` as a `marker` for a Segwit-encoded transaction.
283+
///
284+
/// If you need to use 0-input transactions, we strongly recommend you do so using the PSBT
285+
/// API. The unsigned transaction encoded within PSBT is always a non-segwit transaction
286+
/// and can therefore avoid this ambiguity.
287+
#[inline]
264288
pub fn weight(&self) -> u64 {
265289
self.0.weight().to_wu()
266290
}
267291

292+
/// Returns the total transaction size
293+
///
294+
/// Total transaction size is the transaction size in bytes serialized as described in BIP144,
295+
/// including base data and witness data.
268296
pub fn total_size(&self) -> u64 {
269297
self.0.total_size() as u64
270298
}
271299

300+
/// Returns the "virtual size" (vsize) of this transaction.
301+
///
302+
/// Will be `ceil(weight / 4.0)`. Note this implements the virtual size as per [`BIP141`], which
303+
/// is different to what is implemented in Bitcoin Core.
304+
/// > Virtual transaction size is defined as Transaction weight / 4 (rounded up to the next integer).
305+
///
306+
/// [`BIP141`]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
307+
#[inline]
272308
pub fn vsize(&self) -> u64 {
273309
self.0.vsize() as u64
274310
}
275311

312+
/// Checks if this is a coinbase transaction.
313+
/// The first transaction in the block distributes the mining reward and is called the coinbase transaction.
314+
/// It is impossible to check if the transaction is first in the block, so this function checks the structure
315+
/// of the transaction instead - the previous output must be all-zeros (creates satoshis “out of thin air”).
276316
pub fn is_coinbase(&self) -> bool {
277317
self.0.is_coinbase()
278318
}
279319

320+
/// Returns `true` if the transaction itself opted in to be BIP-125-replaceable (RBF).
321+
///
322+
/// # Warning
323+
///
324+
/// **Incorrectly relying on RBF may lead to monetary loss!**
325+
///
326+
/// This **does not** cover the case where a transaction becomes replaceable due to ancestors
327+
/// being RBF. Please note that transactions **may be replaced** even if they **do not** include
328+
/// the RBF signal: <https://bitcoinops.org/en/newsletters/2022/10/19/#transaction-replacement-option>.
280329
pub fn is_explicitly_rbf(&self) -> bool {
281330
self.0.is_explicitly_rbf()
282331
}
283332

333+
/// Returns `true` if this transactions nLockTime is enabled ([BIP-65]).
334+
///
335+
/// [BIP-65]: https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
284336
pub fn is_lock_time_enabled(&self) -> bool {
285337
self.0.is_lock_time_enabled()
286338
}
287339

340+
/// The protocol version, is currently expected to be 1 or 2 (BIP 68).
288341
pub fn version(&self) -> i32 {
289342
self.0.version.0
290343
}
291344

345+
/// Serialize transaction into consensus-valid format. See https://docs.rs/bitcoin/latest/bitcoin/struct.Transaction.html#serialization-notes for more notes on transaction serialization.
292346
pub fn serialize(&self) -> Vec<u8> {
293347
serialize(&self.0)
294348
}
295349

350+
/// List of transaction inputs.
296351
pub fn input(&self) -> Vec<TxIn> {
297352
self.0.input.iter().map(|tx_in| tx_in.into()).collect()
298353
}
299354

355+
/// List of transaction outputs.
300356
pub fn output(&self) -> Vec<TxOut> {
301357
self.0.output.iter().map(|tx_out| tx_out.into()).collect()
302358
}
303359

360+
/// Block height or timestamp. Transaction cannot be included in a block until this height/time.
361+
///
362+
/// /// ### Relevant BIPs
363+
///
364+
/// * [BIP-65 OP_CHECKLOCKTIMEVERIFY](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki)
365+
/// * [BIP-113 Median time-past as endpoint for lock-time calculations](https://github.com/bitcoin/bips/blob/master/bip-0113.mediawiki)
304366
pub fn lock_time(&self) -> u32 {
305367
self.0.lock_time.to_consensus_u32()
306368
}

0 commit comments

Comments
 (0)