Add convenience methods to Transaction.
Because we represent each transaction version as a different variant of the Transaction enum, we end up in a situation where fields that are common to different transaction versions are awkward to access, requiring a match statement with identical match arms. To fix this, this commit adds the following convenience methods: * `Transaction::inputs() -> impl Iterator<Item=&TransparentInput>`; * `Transaction::outputs() -> impl Iterator<Item=&TransparentOutput>`; * `Transaction::lock_time() -> LockTime`; * `Transaction::expiry_height() -> Option<ExpiryHeight>`; The last returns an `Option` because the field is only present in V3 and V4 transactions. There are some remaining fields that do not get common accessors, because it probably doesn't make sense to access independently of knowing the transaction version: `joinsplit_data`, `shielded_data`, `value_balance`.
This commit is contained in:
parent
7049f9d891
commit
56d7391f6d
|
|
@ -90,3 +90,45 @@ pub enum Transaction {
|
|||
joinsplit_data: Option<JoinSplitData<Groth16Proof>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
/// Iterate over the transparent inputs of this transaction, if any.
|
||||
pub fn inputs(&self) -> impl Iterator<Item=&TransparentInput> {
|
||||
match self {
|
||||
Transaction::V1 { ref inputs, .. } => inputs.iter(),
|
||||
Transaction::V2 { ref inputs, .. } => inputs.iter(),
|
||||
Transaction::V3 { ref inputs, .. } => inputs.iter(),
|
||||
Transaction::V4 { ref inputs, .. } => inputs.iter(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over the transparent outputs of this transaction, if any.
|
||||
pub fn outputs(&self) -> impl Iterator<Item=&TransparentOutput> {
|
||||
match self {
|
||||
Transaction::V1 { ref outputs, .. } => outputs.iter(),
|
||||
Transaction::V2 { ref outputs, .. } => outputs.iter(),
|
||||
Transaction::V3 { ref outputs, .. } => outputs.iter(),
|
||||
Transaction::V4 { ref outputs, .. } => outputs.iter(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get this transaction's lock time.
|
||||
pub fn lock_time(&self) -> LockTime {
|
||||
match self {
|
||||
Transaction::V1 { lock_time, .. } => *lock_time,
|
||||
Transaction::V2 { lock_time, .. } => *lock_time,
|
||||
Transaction::V3 { lock_time, .. } => *lock_time,
|
||||
Transaction::V4 { lock_time, .. } => *lock_time,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get this transaction's expiry height, if any.
|
||||
pub fn expiry_height(&self) -> Option<BlockHeight> {
|
||||
match self {
|
||||
Transaction::V1 { .. } => None,
|
||||
Transaction::V2 { .. } => None,
|
||||
Transaction::V3 { expiry_height, .. } => Some(*expiry_height),
|
||||
Transaction::V4 { expiry_height, .. } => Some(*expiry_height),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue