1use crate::primitives::private_key::PrivateKey;
7use crate::primitives::public_key::PublicKey;
8
9pub type PubKeyHex = String;
15
16pub type SatoshiValue = u64;
18
19pub type OutpointString = String;
21
22pub type TXIDHexString = String;
24
25pub type DescriptionString5to50Bytes = String;
27
28pub type BasketStringUnder300Bytes = String;
30
31pub type OutputTagStringUnder300Bytes = String;
33
34pub type LabelStringUnder300Bytes = String;
36
37pub type KeyIDStringUnder800Bytes = String;
39
40pub type OriginatorDomainNameStringUnder250Bytes = String;
42
43pub type CertificateFieldNameUnder50Bytes = String;
45
46pub type Base64String = String;
48
49pub type HexString = String;
51
52#[derive(Clone, Debug, PartialEq)]
58#[cfg_attr(feature = "network", derive(serde::Serialize, serde::Deserialize))]
59#[cfg_attr(feature = "network", serde(transparent))]
60pub struct BooleanDefaultTrue(pub Option<bool>);
61
62impl Default for BooleanDefaultTrue {
63 fn default() -> Self {
64 BooleanDefaultTrue(Some(true))
65 }
66}
67
68impl std::ops::Deref for BooleanDefaultTrue {
69 type Target = bool;
70 fn deref(&self) -> &bool {
71 static TRUE: bool = true;
72 static FALSE: bool = false;
73 match self.0 {
74 Some(true) | None => &TRUE,
75 Some(false) => &FALSE,
76 }
77 }
78}
79
80impl From<BooleanDefaultTrue> for Option<bool> {
81 fn from(v: BooleanDefaultTrue) -> Self {
82 v.0
83 }
84}
85
86impl From<Option<bool>> for BooleanDefaultTrue {
87 fn from(v: Option<bool>) -> Self {
88 BooleanDefaultTrue(v)
89 }
90}
91
92impl BooleanDefaultTrue {
93 pub fn is_none(&self) -> bool {
96 self.0.is_none()
97 }
98}
99
100#[derive(Clone, Debug, PartialEq)]
106#[cfg_attr(feature = "network", derive(serde::Serialize, serde::Deserialize))]
107#[cfg_attr(feature = "network", serde(transparent))]
108pub struct BooleanDefaultFalse(pub Option<bool>);
109
110impl Default for BooleanDefaultFalse {
111 fn default() -> Self {
112 BooleanDefaultFalse(Some(false))
113 }
114}
115
116impl std::ops::Deref for BooleanDefaultFalse {
117 type Target = bool;
118 fn deref(&self) -> &bool {
119 static TRUE: bool = true;
120 static FALSE: bool = false;
121 match self.0 {
122 Some(true) => &TRUE,
123 Some(false) | None => &FALSE,
124 }
125 }
126}
127
128impl From<BooleanDefaultFalse> for Option<bool> {
129 fn from(v: BooleanDefaultFalse) -> Self {
130 v.0
131 }
132}
133
134impl From<Option<bool>> for BooleanDefaultFalse {
135 fn from(v: Option<bool>) -> Self {
136 BooleanDefaultFalse(v)
137 }
138}
139
140impl BooleanDefaultFalse {
141 pub fn is_none(&self) -> bool {
144 self.0.is_none()
145 }
146}
147
148pub type PositiveIntegerDefault10Max10000 = Option<u32>;
150
151pub type PositiveIntegerOrZero = u32;
153
154#[derive(Clone, Debug)]
169pub struct Protocol {
170 pub security_level: u8,
171 pub protocol: String,
172}
173
174#[cfg(feature = "network")]
175impl serde::Serialize for Protocol {
176 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
177 use serde::ser::SerializeSeq;
178 let mut seq = serializer.serialize_seq(Some(2))?;
179 seq.serialize_element(&self.security_level)?;
180 seq.serialize_element(&self.protocol)?;
181 seq.end()
182 }
183}
184
185#[cfg(feature = "network")]
186impl<'de> serde::Deserialize<'de> for Protocol {
187 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
188 use serde::de::{self, SeqAccess, Visitor};
189 use std::fmt;
190
191 struct ProtocolVisitor;
192
193 impl<'de> Visitor<'de> for ProtocolVisitor {
194 type Value = Protocol;
195
196 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
197 formatter.write_str("an array [securityLevel, protocolName]")
198 }
199
200 fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Protocol, A::Error> {
201 let security_level: u8 = seq
202 .next_element()?
203 .ok_or_else(|| de::Error::invalid_length(0, &self))?;
204 let protocol: String = seq
205 .next_element()?
206 .ok_or_else(|| de::Error::invalid_length(1, &self))?;
207 Ok(Protocol {
208 security_level,
209 protocol,
210 })
211 }
212 }
213
214 deserializer.deserialize_seq(ProtocolVisitor)
215 }
216}
217
218#[derive(Clone, Debug, PartialEq)]
224pub enum CounterpartyType {
225 Uninitialized,
227 Self_,
229 Anyone,
231 Other,
233}
234
235#[derive(Clone, Debug)]
243pub struct Counterparty {
244 pub counterparty_type: CounterpartyType,
245 pub public_key: Option<PublicKey>,
246}
247
248#[cfg(feature = "network")]
249impl serde::Serialize for Counterparty {
250 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
251 match self.counterparty_type {
252 CounterpartyType::Anyone => serializer.serialize_str("anyone"),
253 CounterpartyType::Self_ => serializer.serialize_str("self"),
254 CounterpartyType::Other => {
255 if let Some(ref pk) = self.public_key {
256 serializer.serialize_str(&pk.to_der_hex())
257 } else {
258 serializer.serialize_none()
259 }
260 }
261 CounterpartyType::Uninitialized => serializer.serialize_str(""),
262 }
263 }
264}
265
266#[cfg(feature = "network")]
267impl<'de> serde::Deserialize<'de> for Counterparty {
268 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
269 let s = String::deserialize(deserializer)?;
270 match s.as_str() {
271 "anyone" => Ok(Counterparty {
272 counterparty_type: CounterpartyType::Anyone,
273 public_key: None,
274 }),
275 "self" => Ok(Counterparty {
276 counterparty_type: CounterpartyType::Self_,
277 public_key: None,
278 }),
279 "" => Ok(Counterparty {
280 counterparty_type: CounterpartyType::Uninitialized,
281 public_key: None,
282 }),
283 hex_str => {
284 let pk = PublicKey::from_string(hex_str).map_err(serde::de::Error::custom)?;
285 Ok(Counterparty {
286 counterparty_type: CounterpartyType::Other,
287 public_key: Some(pk),
288 })
289 }
290 }
291 }
292}
293
294pub fn anyone_pubkey() -> PublicKey {
304 let priv_key = PrivateKey::from_bytes(&{
305 let mut buf = [0u8; 32];
306 buf[31] = 1;
307 buf
308 })
309 .expect("PrivateKey(1) is always valid");
311 priv_key.to_public_key()
312}
313
314pub fn anyone_private_key() -> PrivateKey {
316 PrivateKey::from_bytes(&{
317 let mut buf = [0u8; 32];
318 buf[31] = 1;
319 buf
320 })
321 .expect("PrivateKey(1) is always valid")
323}