1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#[allow(unused_macros)]
macro_rules! hex_psbt {
($s:expr) => { $crate::consensus::deserialize(&<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()) };
}
macro_rules! merge {
($thing:ident, $slf:ident, $other:ident) => {
if let (&None, Some($thing)) = (&$slf.$thing, $other.$thing) {
$slf.$thing = Some($thing);
}
};
}
macro_rules! impl_psbt_de_serialize {
($thing:ty) => {
impl_psbt_serialize!($thing);
impl_psbt_deserialize!($thing);
};
}
macro_rules! impl_psbt_deserialize {
($thing:ty) => {
impl $crate::util::psbt::serialize::Deserialize for $thing {
fn deserialize(bytes: &[u8]) -> Result<Self, $crate::consensus::encode::Error> {
$crate::consensus::deserialize(&bytes[..])
}
}
};
}
macro_rules! impl_psbt_serialize {
($thing:ty) => {
impl $crate::util::psbt::serialize::Serialize for $thing {
fn serialize(&self) -> Vec<u8> {
$crate::consensus::serialize(self)
}
}
};
}
macro_rules! impl_psbtmap_consensus_encoding {
($thing:ty) => {
impl $crate::consensus::Encodable for $thing {
fn consensus_encode<S: ::std::io::Write>(
&self,
mut s: S,
) -> Result<usize, $crate::consensus::encode::Error> {
let mut len = 0;
for pair in $crate::util::psbt::Map::get_pairs(self)? {
len += $crate::consensus::Encodable::consensus_encode(
&pair,
&mut s,
)?;
}
Ok(len + $crate::consensus::Encodable::consensus_encode(&0x00_u8, s)?)
}
}
};
}
macro_rules! impl_psbtmap_consensus_decoding {
($thing:ty) => {
impl $crate::consensus::Decodable for $thing {
fn consensus_decode<D: ::std::io::Read>(
mut d: D,
) -> Result<Self, $crate::consensus::encode::Error> {
let mut rv: Self = ::std::default::Default::default();
loop {
match $crate::consensus::Decodable::consensus_decode(&mut d) {
Ok(pair) => $crate::util::psbt::Map::insert_pair(&mut rv, pair)?,
Err($crate::consensus::encode::Error::Psbt($crate::util::psbt::Error::NoMorePairs)) => return Ok(rv),
Err(e) => return Err(e),
}
}
}
}
};
}
macro_rules! impl_psbtmap_consensus_enc_dec_oding {
($thing:ty) => {
impl_psbtmap_consensus_decoding!($thing);
impl_psbtmap_consensus_encoding!($thing);
};
}
#[cfg_attr(rustfmt, rustfmt_skip)]
macro_rules! impl_psbt_insert_pair {
($slf:ident.$unkeyed_name:ident <= <$raw_key:ident: _>|<$raw_value:ident: $unkeyed_value_type:ty>) => {
if $raw_key.key.is_empty() {
if $slf.$unkeyed_name.is_none() {
let val: $unkeyed_value_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
$slf.$unkeyed_name = Some(val)
} else {
return Err($crate::util::psbt::Error::DuplicateKey($raw_key).into());
}
} else {
return Err($crate::util::psbt::Error::InvalidKey($raw_key).into());
}
};
($slf:ident.$keyed_name:ident <= <$raw_key:ident: $keyed_key_type:ty>|<$raw_value:ident: $keyed_value_type:ty>) => {
if !$raw_key.key.is_empty() {
let key_val: $keyed_key_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?;
match $slf.$keyed_name.entry(key_val) {
::std::collections::btree_map::Entry::Vacant(empty_key) => {
let val: $keyed_value_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
empty_key.insert(val);
}
::std::collections::btree_map::Entry::Occupied(_) => return Err($crate::util::psbt::Error::DuplicateKey($raw_key).into()),
}
} else {
return Err($crate::util::psbt::Error::InvalidKey($raw_key).into());
}
};
}
#[cfg_attr(rustfmt, rustfmt_skip)]
macro_rules! impl_psbt_get_pair {
($rv:ident.push($slf:ident.$unkeyed_name:ident as <$unkeyed_typeval:expr, _>|<$unkeyed_value_type:ty>)) => {
if let Some(ref $unkeyed_name) = $slf.$unkeyed_name {
$rv.push($crate::util::psbt::raw::Pair {
key: $crate::util::psbt::raw::Key {
type_value: $unkeyed_typeval,
key: vec![],
},
value: $crate::util::psbt::serialize::Serialize::serialize($unkeyed_name),
});
}
};
($rv:ident.push($slf:ident.$keyed_name:ident as <$keyed_typeval:expr, $keyed_key_type:ty>|<$keyed_value_type:ty>)) => {
for (key, val) in &$slf.$keyed_name {
$rv.push($crate::util::psbt::raw::Pair {
key: $crate::util::psbt::raw::Key {
type_value: $keyed_typeval,
key: $crate::util::psbt::serialize::Serialize::serialize(key),
},
value: $crate::util::psbt::serialize::Serialize::serialize(val),
});
}
};
}