use crate::structs::{Input, Output, UtxoEntry};
use crate::utxo_id::UtxoId;
use crate::{SafeBytesAccess, Transaction, WithMetadataHashable};
impl UtxoEntry {
pub fn to_utxo_id(&self) -> UtxoId {
UtxoId {
transaction_hash: self.transaction_hash.clone(),
output_index: self.output_index,
}
}
pub fn amount(&self) -> u64 {
return self.output.as_ref().unwrap().amount();
}
pub fn to_input(&self) -> Input {
return Input {
transaction_hash: Some(self.transaction_hash.clone().into()),
proof: vec![],
product_id: None,
output_index: self.output_index,
output: self.output.clone(),
};
}
pub fn from_output(
output: &Output,
transaction_hash: &Vec<u8>,
output_index: i64,
time: i64,
) -> UtxoEntry {
return UtxoEntry {
transaction_hash: transaction_hash.clone(),
output_index,
address: output.address.safe_bytes().expect("bytes"),
output: Some(output.clone()),
time,
};
}
pub fn from_transaction(transaction: &Transaction, time: i64) -> Vec<UtxoEntry> {
let map = transaction
.outputs
.iter()
.enumerate()
.map(|(i, output)| Self::from_output(output, &transaction.hash_vec(), i as i64, time))
.collect();
return map;
}
}