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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// LNP/BP client-side-validation foundation libraries implementing LNPBP
// specifications & standards (LNPBP-4, 7, 8, 9, 42, 81)
//
// Written in 2019-2022 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the Apache 2.0 License along with this
// software. If not, see <https://opensource.org/licenses/Apache-2.0>.

//! Strategies simplifying implementation of encoding traits.
//!
//! Implemented after concept by Martin Habovštiak <martin.habovstiak@gmail.com>

use std::io;

use amplify::Wrapper;

use super::{net, Error, StrictDecode, StrictEncode};

/// Encodes/decodes data as a [`bitcoin_hashes::Hash`]-based (wrapper) type,
/// i.e. as a fixed-size byte string of [`bitcoin_hashes::Hash::LEN`] length.
pub struct HashFixedBytes;

/// Encodes/decodes data in the same way as they are encoded/decoded according
/// to bitcoin consensus rules from Bitcoin Core
#[cfg(feature = "bitcoin")]
pub struct BitcoinConsensus;

/// Encodes/decodes data in the same way as they are encoded/decoded according
/// to monero consensus rules from monero-project/monero
#[cfg(feature = "monero")]
pub struct MoneroConsensus;

/// Encodes/decodes data as a wrapped type, i.e. according to the rules of
/// encoding for its inner representation. Applicable only for types
/// implementing [`amplify::Wrapper`]
pub struct Wrapped;

/// Encodes/decodes internet address according to LNPBP-42 "Uniform address
/// encoding" rules. Applicable only for types implementing [`net::Uniform`].
pub struct UsingUniformAddr;

/// Marker trait defining specific encoding strategy which should be used for
/// automatic implementation of both [`StrictEncode`] and [`StrictDecode`].
pub trait Strategy {
    /// Specific strategy. List of supported strategies:
    /// - [`HashFixedBytes`]
    /// - [`BitcoinConsensus`]
    /// - [`Wrapped`]
    /// - [`UsingUniformAddr`]
    /// - [`MoneroConsensus`]
    type Strategy;
}

impl<T> StrictEncode for T
where
    T: Strategy + Clone,
    amplify::Holder<T, <T as Strategy>::Strategy>: StrictEncode,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, e: E) -> Result<usize, Error> {
        amplify::Holder::new(self.clone()).strict_encode(e)
    }
}

impl<T> StrictDecode for T
where
    T: Strategy,
    amplify::Holder<T, <T as Strategy>::Strategy>: StrictDecode,
{
    #[inline]
    fn strict_decode<D: io::Read>(d: D) -> Result<Self, Error> {
        Ok(amplify::Holder::strict_decode(d)?.into_inner())
    }
}

impl<W> StrictEncode for amplify::Holder<W, Wrapped>
where
    W: Wrapper,
    W::Inner: StrictEncode,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, e: E) -> Result<usize, Error> {
        self.as_inner().as_inner().strict_encode(e)
    }
}

impl<W> StrictDecode for amplify::Holder<W, Wrapped>
where
    W: Wrapper,
    W::Inner: StrictDecode,
{
    #[inline]
    fn strict_decode<D: io::Read>(d: D) -> Result<Self, Error> {
        Ok(Self::new(W::from_inner(W::Inner::strict_decode(d)?)))
    }
}

impl<H> StrictEncode for amplify::Holder<H, HashFixedBytes>
where
    H: bitcoin_hashes::Hash,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, mut e: E) -> Result<usize, Error> {
        e.write_all(&self.as_inner()[..])?;
        Ok(H::LEN)
    }
}

impl<H> StrictDecode for amplify::Holder<H, HashFixedBytes>
where
    H: bitcoin_hashes::Hash,
{
    #[inline]
    fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
        let mut buf = vec![0u8; H::LEN];
        d.read_exact(&mut buf)?;
        Ok(Self::new(H::from_slice(&buf).expect(
            "internal hash data representation length mismatch between \
             `from_slice` requirements and `LEN` constant balue",
        )))
    }
}

#[cfg(feature = "bitcoin")]
impl<B> StrictEncode for amplify::Holder<B, BitcoinConsensus>
where
    B: bitcoin::consensus::Encodable,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, mut e: E) -> Result<usize, Error> {
        self.as_inner()
            .consensus_encode(&mut e)
            .map_err(Error::from)
    }
}

#[cfg(feature = "bitcoin")]
impl<B> StrictDecode for amplify::Holder<B, BitcoinConsensus>
where
    B: bitcoin::consensus::Decodable,
{
    #[inline]
    fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
        Ok(Self::new(B::consensus_decode(&mut d).map_err(Error::from)?))
    }
}

#[cfg(feature = "monero")]
impl<B> StrictEncode for amplify::Holder<B, MoneroConsensus>
where
    B: monero::consensus::Encodable,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, mut e: E) -> Result<usize, Error> {
        self.as_inner()
            .consensus_encode(&mut e)
            .map_err(Error::from)
    }
}

#[cfg(feature = "monero")]
impl<B> StrictDecode for amplify::Holder<B, MoneroConsensus>
where
    B: monero::consensus::Decodable,
{
    #[inline]
    fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
        Ok(Self::new(B::consensus_decode(&mut d).map_err(Error::from)?))
    }
}

impl<A> StrictEncode for amplify::Holder<A, UsingUniformAddr>
where
    A: net::Uniform,
{
    #[inline]
    fn strict_encode<E: io::Write>(&self, e: E) -> Result<usize, Error> {
        self.as_inner().to_raw_uniform().strict_encode(e)
    }
}

impl<A> StrictDecode for amplify::Holder<A, UsingUniformAddr>
where
    A: net::Uniform,
{
    #[inline]
    fn strict_decode<D: io::Read>(d: D) -> Result<Self, Error> {
        Ok(Self::new(
            A::from_raw_uniform_addr(net::RawUniformAddr::strict_decode(d)?)
                .map_err(|err| Error::DataIntegrityError(err.to_string()))?,
        ))
    }
}

#[cfg(feature = "bitcoin")]
impl From<bitcoin::hashes::Error> for Error {
    #[inline]
    fn from(_: bitcoin::hashes::Error) -> Self {
        Error::DataIntegrityError("Incorrect hash length".to_string())
    }
}

#[cfg(feature = "bitcoin")]
impl From<bitcoin::consensus::encode::Error> for Error {
    #[inline]
    fn from(e: bitcoin::consensus::encode::Error) -> Self {
        if let bitcoin::consensus::encode::Error::Io(err) = e {
            err.into()
        } else {
            Error::DataIntegrityError(e.to_string())
        }
    }
}

#[cfg(feature = "monero")]
impl From<monero::consensus::encode::Error> for Error {
    #[inline]
    fn from(e: monero::consensus::encode::Error) -> Self {
        if let monero::consensus::encode::Error::Io(err) = e {
            err.into()
        } else {
            Error::DataIntegrityError(e.to_string())
        }
    }
}