1#[cfg(feature = "compat_v32")]
4pub mod compat_v32;
5pub mod input;
6pub mod receive;
15pub mod send;
16
17pub use bitcoin_network_kind;
18pub use bitcoin_primitives;
19pub use secp256k1;
20
21use bitcoin_primitives::OutPoint;
22
23#[rustfmt::skip]
26pub const NUMS_H: [u8; 32] = [
27 0x50, 0x92, 0x9b, 0x74, 0xc1, 0xa0, 0x49, 0x54,
28 0xb7, 0x8b, 0x4b, 0x60, 0x35, 0xe9, 0x7a, 0x5e,
29 0x07, 0x8a, 0x5a, 0x0f, 0x28, 0xec, 0x96, 0xd5,
30 0x47, 0xbf, 0xee, 0x9a, 0xce, 0x80, 0x3a, 0xc0,
31];
32
33pub fn is_nums(x: &[u8; 32]) -> bool {
38 *x == NUMS_H
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct NoMinOutpoint;
48
49impl core::error::Error for NoMinOutpoint {}
50
51impl core::fmt::Display for NoMinOutpoint {
52 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53 write!(f, "No minimal outpoint, update at least once")
54 }
55}
56
57#[derive(Default)]
88pub struct LexMin {
89 current_min: Option<OutPoint>,
91}
92
93impl LexMin {
94 pub fn update(&mut self, outpoint: &OutPoint) {
108 if let Some(ref min) = self.current_min {
109 let new_min = core::cmp::min_by(outpoint, min, |a, b| {
110 let a_txid = a.txid.to_byte_array();
111 let b_txid = b.txid.to_byte_array();
112
113 match a_txid.cmp(&b_txid) {
114 core::cmp::Ordering::Equal => {
115 let a_vout_bytes = a.vout.to_le_bytes();
116 let b_vout_bytes = b.vout.to_le_bytes();
117 a_vout_bytes.cmp(&b_vout_bytes)
118 }
119 other => other,
120 }
121 });
122 self.current_min = Some(*new_min);
123 } else {
124 self.current_min = Some(*outpoint);
125 }
126 }
127
128 pub fn bytes(&self) -> Result<[u8; 36], NoMinOutpoint> {
140 if let Some(min) = self.current_min {
141 let mut result = [0u8; 36];
142 result[..32].copy_from_slice(&min.txid.to_byte_array());
143 result[32..36].copy_from_slice(&min.vout.to_le_bytes());
144
145 Ok(result)
146 } else {
147 Err(NoMinOutpoint)
148 }
149 }
150}
151
152#[cfg(test)]
153#[cfg_attr(coverage_nightly, coverage(off))]
154mod test {
155 mod lex_min {
156 use crate::LexMin;
157 use bitcoin_primitives::{OutPoint, Txid};
158
159 #[test]
160 fn different_txids_and_vouts() {
161 let mut lex_min = LexMin::default();
162 let outpoints = [
163 OutPoint {
164 txid: Txid::from_byte_array([3u8; 32]),
165 vout: 2,
166 },
167 OutPoint {
168 txid: Txid::from_byte_array([2u8; 32]),
169 vout: 1,
170 },
171 OutPoint {
172 txid: Txid::from_byte_array([5u8; 32]),
173 vout: 3,
174 },
175 ];
176
177 for outpoint in outpoints.iter() {
178 lex_min.update(outpoint);
179 }
180
181 let result = lex_min.bytes().expect("should succeed");
182
183 let mut expected_bytes = [2u8; 36];
184 expected_bytes[32..36].copy_from_slice(&1u32.to_le_bytes());
185
186 assert_eq!(result, expected_bytes);
187 }
188
189 #[test]
190 fn fail_if_not_updated() {
191 let e = LexMin::default().bytes().expect_err("should fail");
192 assert_eq!("No minimal outpoint, update at least once", e.to_string());
193 }
194
195 #[test]
196 fn identical_txid_different_vouts() {
197 let mut lex_min = LexMin::default();
198 let txid = Txid::from_byte_array([0u8; 32]);
199 let outpoints = [
200 OutPoint { txid, vout: 10 },
201 OutPoint { txid, vout: 2 },
202 OutPoint { txid, vout: 5 },
203 ];
204
205 for outpoint in outpoints.iter() {
206 lex_min.update(outpoint);
207 }
208
209 let result = lex_min.bytes().expect("should succeed");
210
211 let mut expected_bytes = [0u8; 36];
212 expected_bytes[32..36].copy_from_slice(&2u32.to_le_bytes());
213 assert_eq!(result, expected_bytes);
214 }
215
216 #[test]
217 fn same_vout_different_txid() {
218 let mut lex_min = LexMin::default();
219 let outpoints = [
220 OutPoint {
221 txid: Txid::from_byte_array([2u8; 32]),
222 vout: 7,
223 },
224 OutPoint {
225 txid: Txid::from_byte_array([1u8; 32]),
226 vout: 7,
227 },
228 OutPoint {
229 txid: Txid::from_byte_array([3u8; 32]),
230 vout: 7,
231 },
232 ];
233
234 for outpoint in outpoints.iter() {
235 lex_min.update(outpoint);
236 }
237
238 let result = lex_min.bytes().expect("should succeed");
239
240 let mut expected_bytes = [1u8; 36];
241 expected_bytes[32..36].copy_from_slice(&7u32.to_le_bytes());
242 assert_eq!(result, expected_bytes);
243 }
244
245 #[test]
246 fn edge_case_vout_is_u32_max() {
247 let mut lex_min = LexMin::default();
248 let outpoints = [
249 OutPoint {
250 txid: Txid::from_byte_array([1u8; 32]),
251 vout: u32::MAX,
252 },
253 OutPoint {
254 txid: Txid::from_byte_array([1u8; 32]),
255 vout: u32::MIN,
256 },
257 ];
258
259 for outpoint in outpoints.iter() {
260 lex_min.update(outpoint);
261 }
262
263 let result = lex_min.bytes().expect("should succeed");
264
265 let mut expected_bytes = [1u8; 36];
266 expected_bytes[..32].copy_from_slice(&[1u8; 32]);
267 expected_bytes[32..36].copy_from_slice(&u32::MIN.to_le_bytes());
268 assert_eq!(result, expected_bytes);
269 }
270
271 #[test]
272 fn txid_takes_precedence() {
273 let mut lex_min = LexMin::default();
274 let outpoints = [
275 OutPoint {
276 txid: Txid::from_byte_array([8u8; 32]),
277 vout: 0,
278 },
279 OutPoint {
280 txid: Txid::from_byte_array([5u8; 32]),
281 vout: 100,
282 },
283 ];
284
285 for outpoint in outpoints.iter() {
286 lex_min.update(outpoint);
287 }
288
289 let result = lex_min.bytes().expect("should succeed");
290
291 let mut expected_bytes = [5u8; 36];
292 expected_bytes[32..36].copy_from_slice(&100u32.to_le_bytes());
293 assert_eq!(result, expected_bytes);
294 }
295
296 #[test]
297 fn txid_endianness_matters() {
298 let mut lex_min = LexMin::default();
299 let mut txid_bytes_be = [0u8; 32];
302 txid_bytes_be[0] = 1;
303
304 let mut txid_bytes_le = [0u8; 32];
307 txid_bytes_le[31] = 1;
308
309 let outpoints = [
310 OutPoint {
311 txid: Txid::from_byte_array(txid_bytes_be),
312 vout: 1,
313 },
314 OutPoint {
315 txid: Txid::from_byte_array(txid_bytes_le),
316 vout: 1,
317 },
318 ];
319
320 for outpoint in outpoints.iter() {
321 lex_min.update(outpoint);
322 }
323
324 let result = lex_min.bytes().expect("should succeed");
327
328 let mut expected_bytes = [0u8; 36];
329 expected_bytes[31] = 1;
331 expected_bytes[32..36].copy_from_slice(&1u32.to_le_bytes());
332
333 assert_eq!(result, expected_bytes);
334 }
335 }
336}