tidecoin 0.33.0-beta

General purpose library for using and interoperating with Tidecoin.
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Error code for the address module.

use core::convert::Infallible;
use core::fmt;

use internals::write_err;

use crate::address::{Address, NetworkUnchecked};
use crate::prelude::String;
use crate::script::{witness_program, witness_version};
use crate::Network;

/// Error while generating address from script.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FromScriptError {
    /// Script is not a p2pkh, p2sh or witness program.
    UnrecognizedScript,
    /// A witness program error.
    WitnessProgram(witness_program::Error),
    /// A witness version construction error.
    WitnessVersion(witness_version::TryFromError),
}

impl From<Infallible> for FromScriptError {
    fn from(never: Infallible) -> Self {
        match never {}
    }
}

impl fmt::Display for FromScriptError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::WitnessVersion(ref e) => write_err!(f, "witness version construction error"; e),
            Self::WitnessProgram(ref e) => write_err!(f, "witness program error"; e),
            Self::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for FromScriptError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::UnrecognizedScript => None,
            Self::WitnessVersion(ref e) => Some(e),
            Self::WitnessProgram(ref e) => Some(e),
        }
    }
}

impl From<witness_program::Error> for FromScriptError {
    fn from(e: witness_program::Error) -> Self {
        Self::WitnessProgram(e)
    }
}

impl From<witness_version::TryFromError> for FromScriptError {
    fn from(e: witness_version::TryFromError) -> Self {
        Self::WitnessVersion(e)
    }
}

/// Address type is either invalid or not supported in rust-tidecoin.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnknownAddressTypeError(pub String);

impl fmt::Display for UnknownAddressTypeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write_err!(f, "failed to parse {} as address type", self.0; self)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UnknownAddressTypeError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

/// Address parsing error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
    /// Base58 legacy decoding error.
    Base58(Base58Error),
    /// Bech32 SegWit decoding error.
    Bech32(Bech32Error),
    /// Address's network differs from required one.
    NetworkValidation(NetworkValidationError),
}

impl From<Infallible> for ParseError {
    fn from(never: Infallible) -> Self {
        match never {}
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Base58(ref e) => write_err!(f, "base58 error"; e),
            Self::Bech32(ref e) => write_err!(f, "bech32 error"; e),
            Self::NetworkValidation(ref e) => write_err!(f, "validation error"; e),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Base58(ref e) => Some(e),
            Self::Bech32(ref e) => Some(e),
            Self::NetworkValidation(ref e) => Some(e),
        }
    }
}

impl From<Base58Error> for ParseError {
    fn from(e: Base58Error) -> Self {
        Self::Base58(e)
    }
}

impl From<Bech32Error> for ParseError {
    fn from(e: Bech32Error) -> Self {
        Self::Bech32(e)
    }
}

impl From<UnknownHrpError> for ParseError {
    fn from(e: UnknownHrpError) -> Self {
        Self::Bech32(e.into())
    }
}

impl From<NetworkValidationError> for ParseError {
    fn from(e: NetworkValidationError) -> Self {
        Self::NetworkValidation(e)
    }
}

/// Unknown HRP error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnknownHrpError(pub String);

impl fmt::Display for UnknownHrpError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "unknown hrp: {}", self.0)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UnknownHrpError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

/// Witness namespace does not match the parsed HRP.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InvalidSegwitHrpError {
    /// The offending bech32 human-readable part.
    pub(crate) hrp: String,
    /// The namespace this HRP is reserved for.
    pub(crate) expected: &'static str,
}

impl fmt::Display for InvalidSegwitHrpError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "hrp {} is only valid for {}", self.hrp, self.expected)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidSegwitHrpError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

/// Address's network differs from required one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NetworkValidationError {
    /// Network that was required.
    pub(crate) required: Network,
    /// The address itself.
    pub(crate) address: Address<NetworkUnchecked>,
}

impl fmt::Display for NetworkValidationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "address ")?;
        fmt::Display::fmt(&self.address.inner(), f)?;
        write!(f, " is not valid on {}", self.required)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for NetworkValidationError {}

/// Bech32 related error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Bech32Error {
    /// Parse SegWit Bech32 error.
    ParseBech32(ParseBech32Error),
    /// A witness version conversion/parsing error.
    WitnessVersion(witness_version::TryFromError),
    /// A witness program error.
    WitnessProgram(witness_program::Error),
    /// Tried to parse an unknown HRP.
    UnknownHrp(UnknownHrpError),
    /// The parsed witness program does not belong to this HRP namespace.
    InvalidSegwitHrp(InvalidSegwitHrpError),
}

impl From<Infallible> for Bech32Error {
    fn from(never: Infallible) -> Self {
        match never {}
    }
}

impl fmt::Display for Bech32Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ParseBech32(ref e) => write_err!(f, "SegWit parsing error"; e),
            Self::WitnessVersion(ref e) => {
                write_err!(f, "witness version conversion/parsing error"; e)
            }
            Self::WitnessProgram(ref e) => write_err!(f, "witness program error"; e),
            Self::UnknownHrp(ref e) => write_err!(f, "unknown hrp error"; e),
            Self::InvalidSegwitHrp(ref e) => write_err!(f, "invalid segwit hrp"; e),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Bech32Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::ParseBech32(ref e) => Some(e),
            Self::WitnessVersion(ref e) => Some(e),
            Self::WitnessProgram(ref e) => Some(e),
            Self::UnknownHrp(ref e) => Some(e),
            Self::InvalidSegwitHrp(ref e) => Some(e),
        }
    }
}

impl From<witness_version::TryFromError> for Bech32Error {
    fn from(e: witness_version::TryFromError) -> Self {
        Self::WitnessVersion(e)
    }
}

impl From<witness_program::Error> for Bech32Error {
    fn from(e: witness_program::Error) -> Self {
        Self::WitnessProgram(e)
    }
}

impl From<UnknownHrpError> for Bech32Error {
    fn from(e: UnknownHrpError) -> Self {
        Self::UnknownHrp(e)
    }
}

impl From<InvalidSegwitHrpError> for Bech32Error {
    fn from(e: InvalidSegwitHrpError) -> Self {
        Self::InvalidSegwitHrp(e)
    }
}

/// Bech32 parsing related error.
// This wrapper exists because we do not want to expose the `bech32` crate in our public API.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseBech32Error(pub(crate) bech32::segwit::DecodeError);

impl From<Infallible> for ParseBech32Error {
    fn from(never: Infallible) -> Self {
        match never {}
    }
}

impl fmt::Display for ParseBech32Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write_err!(f, "bech32 parsing error"; self.0)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseBech32Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.0)
    }
}

/// Base58 related error.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Base58Error {
    /// Parse legacy Base58 error.
    ParseBase58(base58::Error),
    /// Legacy address is too long.
    LegacyAddressTooLong(LegacyAddressTooLongError),
    /// Invalid base58 payload data length for legacy address.
    InvalidBase58PayloadLength(InvalidBase58PayloadLengthError),
    /// Invalid legacy address prefix in base58 data payload.
    InvalidLegacyPrefix(InvalidLegacyPrefixError),
}

impl From<Infallible> for Base58Error {
    fn from(never: Infallible) -> Self {
        match never {}
    }
}

impl fmt::Display for Base58Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ParseBase58(ref e) => write_err!(f, "legacy parsing error"; e),
            Self::LegacyAddressTooLong(ref e) => write_err!(f, "legacy address length error"; e),
            Self::InvalidBase58PayloadLength(ref e) => {
                write_err!(f, "legacy payload length error"; e)
            }
            Self::InvalidLegacyPrefix(ref e) => write_err!(f, "legacy prefix error"; e),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Base58Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::ParseBase58(ref e) => Some(e),
            Self::LegacyAddressTooLong(ref e) => Some(e),
            Self::InvalidBase58PayloadLength(ref e) => Some(e),
            Self::InvalidLegacyPrefix(ref e) => Some(e),
        }
    }
}

impl From<base58::Error> for Base58Error {
    fn from(e: base58::Error) -> Self {
        Self::ParseBase58(e)
    }
}

impl From<LegacyAddressTooLongError> for Base58Error {
    fn from(e: LegacyAddressTooLongError) -> Self {
        Self::LegacyAddressTooLong(e)
    }
}

impl From<InvalidBase58PayloadLengthError> for Base58Error {
    fn from(e: InvalidBase58PayloadLengthError) -> Self {
        Self::InvalidBase58PayloadLength(e)
    }
}

impl From<InvalidLegacyPrefixError> for Base58Error {
    fn from(e: InvalidLegacyPrefixError) -> Self {
        Self::InvalidLegacyPrefix(e)
    }
}

/// Decoded base58 data was an invalid length.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidBase58PayloadLengthError {
    /// The base58 payload length we got after decoding address string.
    pub(crate) length: usize,
}

impl InvalidBase58PayloadLengthError {
    /// Returns the invalid payload length.
    pub fn invalid_base58_payload_length(&self) -> usize {
        self.length
    }
}

impl fmt::Display for InvalidBase58PayloadLengthError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "decoded base58 data was an invalid length: {} (expected 21)", self.length)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidBase58PayloadLengthError {}

/// Legacy base58 address was too long, max 50 characters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LegacyAddressTooLongError {
    /// The length of the legacy address.
    pub(crate) length: usize,
}

impl LegacyAddressTooLongError {
    /// Returns the invalid legacy address length.
    pub fn invalid_legacy_address_length(&self) -> usize {
        self.length
    }
}

impl fmt::Display for LegacyAddressTooLongError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "legacy address is too long: {} (max 50 characters)", self.length)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for LegacyAddressTooLongError {}

/// Invalid legacy address prefix in decoded base58 data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidLegacyPrefixError {
    /// The invalid prefix byte.
    pub(crate) invalid: u8,
}

impl InvalidLegacyPrefixError {
    /// Returns the invalid prefix.
    pub fn invalid_legacy_address_prefix(&self) -> u8 {
        self.invalid
    }
}

impl fmt::Display for InvalidLegacyPrefixError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "invalid legacy address prefix in decoded base58 data {}", self.invalid)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidLegacyPrefixError {}