1pub extern crate bitcoin;
11
12extern crate alloc;
13
14mod error;
17mod psbt;
18
19pub mod v17;
21pub mod v18;
22pub mod v19;
23pub mod v20;
24pub mod v21;
25pub mod v22;
26pub mod v23;
27pub mod v24;
28pub mod v25;
29pub mod v26;
30pub mod v27;
31pub mod v28;
32pub mod v29;
33
34pub mod model;
36
37use core::fmt;
38
39use bitcoin::address::{self, Address, NetworkUnchecked};
40use bitcoin::amount::ParseAmountError;
41use bitcoin::hex::{self, FromHex as _};
42use bitcoin::{Amount, FeeRate, ScriptBuf, Witness};
43use serde::{Deserialize, Serialize};
44
45use crate::error::write_err;
46
47pub fn to_u32(value: i64, field: &str) -> Result<u32, NumericError> {
59 if value.is_negative() {
60 return Err(NumericError::Negative { value, field: field.to_owned() });
61 }
62 u32::try_from(value).map_err(|_| NumericError::Overflow { value, field: field.to_owned() })
63}
64
65#[derive(Debug)]
71pub enum NumericError {
72 Negative { field: String, value: i64 },
74 Overflow { field: String, value: i64 },
76}
77
78impl fmt::Display for NumericError {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 use NumericError::*;
81
82 match *self {
83 Negative{ ref field, value } => write!(f, "expected an unsigned numeric value however the value was negative (field name: {} value: {})", field, value),
84 Overflow { ref field, value } => write!(f, "a value larger than `u32::MAX` was unexpectedly encountered (field name: {} Value: {})", field, value),
85 }
86 }
87}
88
89#[cfg(feature = "std")]
90impl std::error::Error for NumericError {}
91
92fn btc_per_kb(btc_per_kb: f64) -> Result<Option<FeeRate>, ParseAmountError> {
94 let sats_per_kb = Amount::from_btc(btc_per_kb)?;
95 let sats_per_byte = sats_per_kb.to_sat() / 1000;
96
97 let rate = FeeRate::from_sat_per_vb(sats_per_byte);
99
100 Ok(rate)
101}
102
103fn witness_from_hex_slice<T: AsRef<str>>(witness: &[T]) -> Result<Witness, hex::HexToBytesError> {
106 let bytes: Vec<Vec<u8>> =
107 witness.iter().map(|hex| Vec::from_hex(hex.as_ref())).collect::<Result<_, _>>()?;
108 Ok(Witness::from_slice(&bytes))
109}
110
111pub fn compact_size_decode(slice: &mut &[u8]) -> u64 {
124 if slice.is_empty() {
125 panic!("tried to decode an empty slice");
126 }
127
128 match slice[0] {
129 0xFF => {
130 const SIZE: usize = 9;
131 if slice.len() < SIZE {
132 panic!("slice too short, expected at least 9 bytes");
133 };
134
135 let mut bytes = [0_u8; SIZE - 1];
136 bytes.copy_from_slice(&slice[1..SIZE]);
137
138 let v = u64::from_le_bytes(bytes);
139 debug_assert!(v > u32::MAX.into(), "non-minimal encoding of a u64");
140 *slice = &slice[SIZE..];
141 v
142 }
143 0xFE => {
144 const SIZE: usize = 5;
145 if slice.len() < SIZE {
146 panic!("slice too short, expected at least 5 bytes");
147 };
148
149 let mut bytes = [0_u8; SIZE - 1];
150 bytes.copy_from_slice(&slice[1..SIZE]);
151
152 let v = u32::from_le_bytes(bytes);
153 debug_assert!(v > u16::MAX.into(), "non-minimal encoding of a u32");
154 *slice = &slice[SIZE..];
155 u64::from(v)
156 }
157 0xFD => {
158 const SIZE: usize = 3;
159 if slice.len() < SIZE {
160 panic!("slice too short, expected at least 3 bytes");
161 };
162
163 let mut bytes = [0_u8; SIZE - 1];
164 bytes.copy_from_slice(&slice[1..SIZE]);
165
166 let v = u16::from_le_bytes(bytes);
167 debug_assert!(v >= 0xFD, "non-minimal encoding of a u16");
168 *slice = &slice[SIZE..];
169 u64::from(v)
170 }
171 n => {
172 *slice = &slice[1..];
173 u64::from(n)
174 }
175 }
176}
177
178#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
185#[cfg_attr(feature = "serde-deny-unknown-fields", serde(deny_unknown_fields))]
186pub struct ScriptPubkey {
187 pub asm: String,
189 #[serde(rename = "desc")]
191 pub descriptor: Option<String>,
192 pub hex: String,
194 #[serde(rename = "reqSigs")]
199 pub required_signatures: Option<i64>,
200 #[serde(rename = "type")]
202 pub type_: String,
203 pub address: Option<String>,
205 pub addresses: Option<Vec<String>>,
210}
211
212#[derive(Debug)]
214pub enum ScriptPubkeyError {
215 Hex(hex::HexToBytesError),
217 Address(address::ParseError),
219 Addresses(address::ParseError),
221}
222
223impl fmt::Display for ScriptPubkeyError {
224 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
225 use ScriptPubkeyError::*;
226 match *self {
227 Hex(ref e) => write_err!(f, "conversion of the `hex` field failed"; e),
228 Address(ref e) => write_err!(f, "conversion of the `address` field failed"; e),
229 Addresses(ref e) => write_err!(f, "conversion of the `addresses` field failed"; e),
230 }
231 }
232}
233
234#[cfg(feature = "std")]
235impl std::error::Error for ScriptPubkeyError {
236 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
237 use ScriptPubkeyError::*;
238 match *self {
239 Hex(ref e) => Some(e),
240 Address(ref e) => Some(e),
241 Addresses(ref e) => Some(e),
242 }
243 }
244}
245
246impl ScriptPubkey {
247 fn script_buf(&self) -> Result<ScriptBuf, hex::HexToBytesError> {
248 ScriptBuf::from_hex(&self.hex)
249 }
250
251 fn address(&self) -> Option<Result<Address<NetworkUnchecked>, address::ParseError>> {
252 self.address.as_ref().map(|addr| addr.parse::<Address<_>>())
253 }
254
255 pub fn into_model(self) -> Result<model::ScriptPubkey, ScriptPubkeyError> {
257 use ScriptPubkeyError as E;
258
259 let script_pubkey = ScriptBuf::from_hex(&self.hex).map_err(E::Hex)?;
260
261 let address =
262 self.address.map(|s| s.parse::<Address<_>>().map_err(E::Address)).transpose()?;
263
264 let addresses = self
265 .addresses
266 .map(|v| {
267 v.into_iter()
268 .map(|s| s.parse::<Address<_>>().map_err(E::Addresses))
269 .collect::<Result<Vec<_>, _>>()
270 })
271 .transpose()?;
272
273 Ok(model::ScriptPubkey {
274 script_pubkey,
275 required_signatures: self.required_signatures,
276 address,
277 addresses,
278 })
279 }
280}
281
282#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
284#[cfg_attr(feature = "serde-deny-unknown-fields", serde(deny_unknown_fields))]
285pub struct ScriptSig {
286 pub asm: String,
288 pub hex: String,
290}
291
292impl ScriptSig {
293 pub fn script_buf(&self) -> Result<ScriptBuf, hex::HexToBytesError> {
294 ScriptBuf::from_hex(&self.hex)
295 }
296}