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
248impl Default for Counterparty {
249 fn default() -> Self {
254 Self {
255 counterparty_type: CounterpartyType::Uninitialized,
256 public_key: None,
257 }
258 }
259}
260
261#[cfg(feature = "network")]
262impl serde::Serialize for Counterparty {
263 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
264 match self.counterparty_type {
265 CounterpartyType::Anyone => serializer.serialize_str("anyone"),
266 CounterpartyType::Self_ => serializer.serialize_str("self"),
267 CounterpartyType::Other => {
268 if let Some(ref pk) = self.public_key {
269 serializer.serialize_str(&pk.to_der_hex())
270 } else {
271 serializer.serialize_none()
272 }
273 }
274 CounterpartyType::Uninitialized => serializer.serialize_str(""),
275 }
276 }
277}
278
279#[cfg(feature = "network")]
280impl<'de> serde::Deserialize<'de> for Counterparty {
281 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
282 let s: Option<String> = Option::<String>::deserialize(deserializer)?;
289 let s = match s {
290 None => {
291 return Ok(Counterparty {
292 counterparty_type: CounterpartyType::Uninitialized,
293 public_key: None,
294 })
295 }
296 Some(s) => s,
297 };
298 match s.as_str() {
299 "anyone" => Ok(Counterparty {
300 counterparty_type: CounterpartyType::Anyone,
301 public_key: None,
302 }),
303 "self" => Ok(Counterparty {
304 counterparty_type: CounterpartyType::Self_,
305 public_key: None,
306 }),
307 "" => Ok(Counterparty {
308 counterparty_type: CounterpartyType::Uninitialized,
309 public_key: None,
310 }),
311 hex_str => {
312 let pk = PublicKey::from_string(hex_str).map_err(serde::de::Error::custom)?;
313 Ok(Counterparty {
314 counterparty_type: CounterpartyType::Other,
315 public_key: Some(pk),
316 })
317 }
318 }
319 }
320}
321
322pub fn anyone_pubkey() -> PublicKey {
332 let priv_key = PrivateKey::from_bytes(&{
333 let mut buf = [0u8; 32];
334 buf[31] = 1;
335 buf
336 })
337 .expect("PrivateKey(1) is always valid");
339 priv_key.to_public_key()
340}
341
342pub fn anyone_private_key() -> PrivateKey {
344 PrivateKey::from_bytes(&{
345 let mut buf = [0u8; 32];
346 buf[31] = 1;
347 buf
348 })
349 .expect("PrivateKey(1) is always valid")
351}
352
353#[cfg(all(test, feature = "network"))]
354mod serde_tests {
355 use super::*;
359 use crate::wallet::interfaces::{CreateSignatureArgs, EncryptArgs};
360
361 #[test]
362 fn counterparty_default_is_uninitialized() {
363 let cp = Counterparty::default();
368 assert_eq!(cp.counterparty_type, CounterpartyType::Uninitialized);
369 assert!(cp.public_key.is_none());
370 }
371
372 #[test]
373 fn create_signature_args_omit_counterparty_is_uninitialized() {
374 let json = serde_json::json!({
380 "protocolID": [0, "cross-sdk test"],
381 "keyID": "x",
382 "data": [1, 2, 3],
383 });
384 let args: CreateSignatureArgs = serde_json::from_value(json).unwrap();
385 assert_eq!(
386 args.counterparty.counterparty_type,
387 CounterpartyType::Uninitialized,
388 "omitted counterparty must deserialize to Uninitialized, not Self_ — \
389 otherwise createSignature will derive against the wrong key"
390 );
391 }
392
393 #[test]
394 fn encrypt_args_omit_counterparty_is_uninitialized() {
395 let json = serde_json::json!({
398 "protocolID": [0, "cross-sdk test"],
399 "keyID": "x",
400 "plaintext": [1, 2, 3],
401 });
402 let args: EncryptArgs = serde_json::from_value(json).unwrap();
403 assert_eq!(
404 args.counterparty.counterparty_type,
405 CounterpartyType::Uninitialized
406 );
407 }
408
409 #[test]
410 fn counterparty_explicit_null_is_uninitialized() {
411 let json = serde_json::json!({
416 "protocolID": [0, "cross-sdk test"],
417 "keyID": "x",
418 "data": [1, 2, 3],
419 "counterparty": serde_json::Value::Null,
420 });
421 let args: CreateSignatureArgs = serde_json::from_value(json).unwrap();
422 assert_eq!(
423 args.counterparty.counterparty_type,
424 CounterpartyType::Uninitialized
425 );
426 }
427
428 #[test]
429 fn counterparty_explicit_self_and_anyone_parse() {
430 let self_cp: Counterparty = serde_json::from_str("\"self\"").unwrap();
433 assert_eq!(self_cp.counterparty_type, CounterpartyType::Self_);
434
435 let anyone_cp: Counterparty = serde_json::from_str("\"anyone\"").unwrap();
436 assert_eq!(anyone_cp.counterparty_type, CounterpartyType::Anyone);
437
438 let empty_cp: Counterparty = serde_json::from_str("\"\"").unwrap();
439 assert_eq!(empty_cp.counterparty_type, CounterpartyType::Uninitialized);
440 }
441}