redgold_schema/utxo_entry.rs
1//
2// pub struct FixedIdConvert {
3// id: [u8; 36],
4// }
5//
6// fn u32_to_vec(value: u32) -> Vec<u8> {
7// return value.to_le_bytes().to_vec();
8// }
9//
10// impl FixedIdConvert {
11// pub(crate) fn from_values(hash: &[u8; 32], value: u32) -> FixedIdConvert {
12// let bytes = value.to_le_bytes();
13// let mut merged = [0u8; 36];
14// merged[0..32].clone_from_slice(&*hash);
15// merged[32..36].clone_from_slice(&bytes);
16// return FixedIdConvert { id: merged };
17// }
18//
19// fn to_values(&self) -> ([u8; 32], u32) {
20// let mut hash = [0u8; 32];
21// let mut output_index = [0u8; 4];
22// hash.clone_from_slice(&self.id[0..32]);
23// output_index.clone_from_slice(&self.id[32..36]);
24// return (hash, u32::from_le_bytes(output_index));
25// }
26// }
27
28use crate::helpers::with_metadata_hashable::WithMetadataHashable;
29use crate::structs::{Address, CurrencyAmount, Hash, Input, Output, UtxoEntry, UtxoId};
30use crate::{RgResult, SafeOption, Transaction};
31
32impl UtxoEntry {
33
34 pub fn address(&self) -> RgResult<&Address> {
35 let o = self.output.safe_get_msg("Missing address from utxo entry")?;
36 o.address.safe_get_msg("Missing address from utxo entry")
37 }
38 pub fn utxo_id(&self) -> RgResult<&UtxoId> {
39 self.utxo_id.safe_get_msg("Missing id from utxo entry")
40 }
41
42 pub fn amount(&self) -> u64 {
43 return self.output.as_ref().unwrap().amount();
44 }
45
46 pub fn opt_amount(&self) -> Option<CurrencyAmount> {
47 self.output.as_ref().and_then(|o| o.opt_amount_typed())
48 }
49
50 pub fn height(&self) -> RgResult<i64> {
51 let h = self.output.as_ref()
52 .and_then(|o| o.data.as_ref())
53 .and_then(|o| o.height);
54 h.safe_get_msg("Missing height on utxo output").cloned()
55 }
56
57 // pub fn id_from_values(hash: &Vec<u8>, value: &Vec<u8>) -> Vec<u8> {
58 // let mut merged: Vec<u8> = Vec::new();
59 // merged.extend(hash);
60 // merged.extend(value);
61 // return merged;
62 // }
63
64 // pub fn id_from_fixed_values(hash: &[u8; 32], value: u32) -> Vec<u8> {
65 // return UtxoEntry::id_from_fixed(&FixedIdConvert::from_values(hash, value));
66 // }
67 //
68 // pub(crate) fn id_from_fixed(fixed: &FixedIdConvert) -> Vec<u8> {
69 // let (hash, index) = fixed.to_values();
70 // return UtxoEntry::id_from_values(&hash.to_vec(), &index.to_le_bytes().to_vec());
71 // }
72 //
73 // pub fn id_to_values(id: &Vec<u8>) -> (Vec<u8>, u32) {
74 // let vec = id[32..36].to_owned();
75 // let mut output_index = [0u8; 4];
76 // output_index.clone_from_slice(&vec);
77 // let value = u32::from_le_bytes(output_index);
78 // return (id[0..32].to_owned(), value);
79 // }
80
81 // // deprecate
82 // fn to_values(&self) -> (Vec<u8>, u32) {
83 // (self.transaction_hash.clone(), self.output_index)
84 // }
85 //
86 pub fn to_input(&self) -> Input {
87 let mut input = Input::default();
88 input.utxo_id = self.utxo_id.clone();
89 input.output = self.output.clone();
90 input
91 }
92
93 // pub fn address_index(&self) -> u32 {
94 // return self.to_values().1;
95 // }
96 //
97 // pub fn transaction_hash(&self) -> Vec<u8> {
98 // return self.to_values().0;
99 // }
100 //
101 // fn weights_to_bytes(weights: &Vec<f64>) -> Vec<u8> {
102 // let mut bytes: Vec<u8> = Vec::new();
103 // for weights in weights.iter() {
104 // bytes.extend_from_slice(&weights.to_le_bytes());
105 // }
106 // return bytes;
107 // }
108
109 pub fn from_output_new(
110 output: &Output,
111 transaction_hash: &Hash,
112 output_index: i64,
113 time: i64,
114 ) -> UtxoEntry {
115 return UtxoEntry {
116 utxo_id: Some(UtxoId::new(transaction_hash, output_index)),
117 output: Some(output.clone()),
118 time,
119 };
120 }
121
122 pub fn from_transaction(transaction: &Transaction, time: i64) -> Vec<UtxoEntry> {
123 let map = transaction
124 .outputs
125 .iter()
126 .enumerate()
127 .map(|(i, output)| Self::from_output_new(output, &transaction.hash_or(), i as i64, time))
128 .collect();
129 return map;
130 }
131
132 // pub fn ids_from_transaction_outputs(transaction: &Transaction) -> Vec<Vec<u8>> {
133 // return transaction
134 // .outputs
135 // .iter()
136 // .enumerate()
137 // .map(|(i, _output)| {
138 // UtxoEntry::id_from_fixed(&FixedIdConvert::from_values(
139 // &transaction.hash(),
140 // i as u32,
141 // ))
142 // })
143 // .collect();
144 // }
145
146 // pub fn ids_from_transaction_inputs(transaction: &Transaction) -> Vec<Vec<u8>> {
147 // return transaction
148 // .inputs
149 // .iter()
150 // .map(|input| {
151 // UtxoEntry::id_from_values(&input.transaction_hash, &u32_to_vec(input.output_index))
152 // })
153 // .collect();
154 // }
155}