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
use crate::{invalid_input, permanent_failure, runtime_error};

use breez_sdk_core::error::SendPaymentError;
use std::fmt::{Display, Formatter};

/// A code that specifies the RuntimeError that occurred
#[derive(Debug, PartialEq, Eq)]
pub enum RuntimeErrorCode {
    // 3L runtime errors
    /// The backend auth service is unavailable.
    AuthServiceUnavailable,
    OfferServiceUnavailable,
    /// The lsp service is unavailable. Could there be a loss of internet connection?
    LspServiceUnavailable,
    /// The backup service is unavailable. Could there be a loss of internet connection?
    BackupServiceUnavailable,
    /// No backup was found for the provided mnemonic.
    BackupNotFound,

    // Breez runtime errors
    /// Information about the remote node isn't cached and couldn't be accessed. Could be a network error.
    NodeUnavailable,
}

impl Display for RuntimeErrorCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

pub type Error = perro::Error<RuntimeErrorCode>;
pub type Result<T> = std::result::Result<T, Error>;

/// A code that specifies the PayError that occurred.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum PayErrorCode {
    /// An already recognized invoice tried to be paid.
    /// Either a payment attempt is in progress or the invoice has already been paid.
    /// There's no point in retrying this payment.
    AlreadyUsedInvoice,

    /// The invoice has already expired.
    /// There's no point in retrying this payment.
    InvoiceExpired,

    /// Not a single route was found.
    /// There's no point in retrying this payment.
    NoRouteFound,

    /// A locally issued invoice tried to be paid. Self-payments are not supported.
    /// There's no point in retrying this payment.
    PayingToSelf,

    /// The payment failed for another reason. Might be an issue with the receiver.
    PaymentFailed,

    /// Payment timed out.
    /// It might make sense to retry the payment.
    PaymentTimeout,

    /// Route too expensive. The route's fee exceeds the settings.
    RouteTooExpensive,

    /// The remote lightning node is not available. Could be a network error.
    NodeUnavailable,

    /// An unexpected error occurred.
    /// This likely is a result of a bug within 3L/Breez SDK and should be reported to lipa.
    UnexpectedError,
}

impl Display for PayErrorCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

pub type PayError = perro::Error<PayErrorCode>;
pub type PayResult<T> = std::result::Result<T, PayError>;

/// A code that specifies the LnUrlPayError that occurred.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum LnUrlPayErrorCode {
    /// LNURL server returned an error.
    LnUrlServerError,

    /// Not a single route was found.
    NoRouteFound,

    /// The payment failed for another reason. Might be an issue with the receiver.
    PaymentFailed,

    /// Payment timed out.
    /// It might make sense to retry the payment.
    PaymentTimeout,

    /// Route too expensive. The route's fee exceeds the settings.
    RouteTooExpensive,

    /// The remote lightning node or LNURL server is not available. Could be a network error.
    ServiceConnectivity,

    /// An unexpected error occurred.
    /// This likely is a result of a bug within 3L/Breez SDK and should be reported to lipa.
    UnexpectedError,

    /// The invoice is issued for another bitcoin network (e.g. testnet).
    InvalidNetwork,
}

impl Display for LnUrlPayErrorCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

pub type LnUrlPayError = perro::Error<LnUrlPayErrorCode>;
pub type LnUrlPayResult<T> = std::result::Result<T, LnUrlPayError>;

/// A code that specifies the LnUrlWithdrawError that occurred.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum LnUrlWithdrawErrorCode {
    /// LNURL server returned an error.
    LnUrlServerError,

    /// The remote lightning node or LNURL server is not available. Could be a network error.
    ServiceConnectivity,

    /// An unexpected error occurred.
    /// This likely is a result of a bug within 3L/Breez SDK and should be reported to lipa.
    UnexpectedError,
}

impl Display for LnUrlWithdrawErrorCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

pub type LnUrlWithdrawError = perro::Error<LnUrlWithdrawErrorCode>;
pub type LnUrlWithdrawResult<T> = std::result::Result<T, LnUrlWithdrawError>;

#[derive(Debug, thiserror::Error)]
pub enum UnsupportedDataType {
    #[error("Bitcoin on-chain address")]
    BitcoinAddress,
    #[error("LNURL Auth")]
    LnUrlAuth,
    #[error("Lightning node id")]
    NodeId,
    #[error("URL")]
    Url,
    #[error("Network: {network}")]
    Network { network: String },
}

#[derive(Debug, thiserror::Error)]
pub enum DecodeDataError {
    #[error("LNURL error: {msg}")]
    LnUrlError { msg: String },
    #[error("Unsupported data type: {typ}")]
    Unsupported { typ: UnsupportedDataType },
    #[error("Unrecognized data type: {msg}")]
    Unrecognized { msg: String },
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum MnemonicError {
    /// Mnemonic has a word count that is not a multiple of 6.
    #[error("BadWordCount with count: {count}")]
    BadWordCount { count: u64 },
    /// Mnemonic contains an unknown word at the pointed index.
    #[error("UnknownWord at index: {index}")]
    UnknownWord { index: u64 },
    /// Entropy was not a multiple of 32 bits or between 128-256n bits in length.
    #[error("BadEntropyBitCount")]
    BadEntropyBitCount,
    /// The mnemonic has an invalid checksum.
    #[error("InvalidChecksum")]
    InvalidChecksum,
    /// The mnemonic can be interpreted as multiple languages.
    #[error("AmbiguousLanguages")]
    AmbiguousLanguages,
}

pub fn to_mnemonic_error(e: bip39::Error) -> MnemonicError {
    match e {
        bip39::Error::BadWordCount(count) => MnemonicError::BadWordCount {
            count: count as u64,
        },
        bip39::Error::UnknownWord(index) => MnemonicError::UnknownWord {
            index: index as u64,
        },
        bip39::Error::BadEntropyBitCount(_) => MnemonicError::BadEntropyBitCount,
        bip39::Error::InvalidChecksum => MnemonicError::InvalidChecksum,
        bip39::Error::AmbiguousLanguages(_) => MnemonicError::AmbiguousLanguages,
    }
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum SimpleError {
    #[error("SimpleError: {msg}")]
    Simple { msg: String },
}

pub(crate) fn map_send_payment_error(err: SendPaymentError) -> PayError {
    match err {
        SendPaymentError::AlreadyPaid => {
            runtime_error(PayErrorCode::AlreadyUsedInvoice, String::new())
        }
        SendPaymentError::Generic { err } => runtime_error(PayErrorCode::UnexpectedError, err),
        SendPaymentError::InvalidAmount { err } => invalid_input(format!("Invalid amount: {err}")),
        SendPaymentError::InvalidInvoice { err } => {
            invalid_input(format!("Invalid invoice: {err}"))
        }
        SendPaymentError::InvoiceExpired { err } => {
            runtime_error(PayErrorCode::InvoiceExpired, err)
        }
        SendPaymentError::PaymentFailed { err } => runtime_error(PayErrorCode::PaymentFailed, err),
        SendPaymentError::PaymentTimeout { err } => {
            runtime_error(PayErrorCode::PaymentTimeout, err)
        }
        SendPaymentError::RouteNotFound { err } => runtime_error(PayErrorCode::NoRouteFound, err),
        SendPaymentError::RouteTooExpensive { err } => {
            runtime_error(PayErrorCode::RouteTooExpensive, err)
        }
        SendPaymentError::ServiceConnectivity { err } => {
            runtime_error(PayErrorCode::NodeUnavailable, err)
        }
        SendPaymentError::InvalidNetwork { err } => {
            invalid_input(format!("Invalid network: {err}"))
        }
    }
}

pub(crate) fn map_lnurl_pay_error(error: breez_sdk_core::LnUrlPayError) -> LnUrlPayError {
    use breez_sdk_core::LnUrlPayError;
    match error {
        LnUrlPayError::InvalidUri { err } => invalid_input(format!("InvalidUri: {err}")),
        LnUrlPayError::AlreadyPaid => permanent_failure("LNURL pay invoice has been already paid"),
        LnUrlPayError::Generic { err } => runtime_error(LnUrlPayErrorCode::UnexpectedError, err),
        LnUrlPayError::InvalidAmount { err } => runtime_error(
            LnUrlPayErrorCode::LnUrlServerError,
            format!("Invalid amount in the invoice from LNURL pay server: {err}"),
        ),
        LnUrlPayError::InvalidInvoice { err } => runtime_error(
            LnUrlPayErrorCode::LnUrlServerError,
            format!("Invalid invoice from LNURL pay server: {err}"),
        ),
        LnUrlPayError::InvoiceExpired { err } => {
            permanent_failure(format!("Invoice for LNURL pay has already expired: {err}"))
        }
        LnUrlPayError::PaymentFailed { err } => {
            runtime_error(LnUrlPayErrorCode::PaymentFailed, err)
        }
        LnUrlPayError::PaymentTimeout { err } => {
            runtime_error(LnUrlPayErrorCode::PaymentTimeout, err)
        }
        LnUrlPayError::RouteNotFound { err } => runtime_error(LnUrlPayErrorCode::NoRouteFound, err),
        LnUrlPayError::RouteTooExpensive { err } => {
            runtime_error(LnUrlPayErrorCode::RouteTooExpensive, err)
        }
        LnUrlPayError::ServiceConnectivity { err } => {
            runtime_error(LnUrlPayErrorCode::ServiceConnectivity, err)
        }
        LnUrlPayError::InvalidNetwork { err } => {
            runtime_error(LnUrlPayErrorCode::InvalidNetwork, err)
        }
    }
}

pub(crate) fn map_lnurl_withdraw_error(
    error: breez_sdk_core::LnUrlWithdrawError,
) -> LnUrlWithdrawError {
    use breez_sdk_core::LnUrlWithdrawError;
    match error {
        LnUrlWithdrawError::Generic { err } => {
            runtime_error(LnUrlWithdrawErrorCode::UnexpectedError, err)
        }
        LnUrlWithdrawError::InvalidAmount { err } => {
            invalid_input(format!("Invalid withdraw amount: {err}"))
        }
        LnUrlWithdrawError::InvalidInvoice { err } => {
            permanent_failure(format!("Invalid invoice was created locally: {err}"))
        }
        LnUrlWithdrawError::InvalidUri { err } => invalid_input(format!("InvalidUri: {err}")),
        LnUrlWithdrawError::ServiceConnectivity { err } => {
            runtime_error(LnUrlWithdrawErrorCode::ServiceConnectivity, err)
        }
        LnUrlWithdrawError::InvoiceNoRoutingHints { err } => permanent_failure(format!(
            "A locally created invoice doesn't have any routing hints: {err}"
        )),
    }
}

/// A code that specifies the NotificationHandlingError that occurred.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum NotificationHandlingErrorCode {
    /// Information about the remote node isn't cached and couldn't be accessed.
    /// Could be a network error.
    NodeUnavailable,
    /// The notification payload implied the existence of an in-progress swap, but it couldn't be
    /// found. Maybe another instance of the wallet completed the swap.
    InProgressSwapNotFound,
    /// The notification payload implied the existence of an incoming payment, but it was not
    /// received in time. Starting the app might help complete the payment.
    ExpectedPaymentNotReceived,
    /// An inbound payment was rejected as it required opening a new channel.
    InsufficientInboundLiquidity,
    /// A request to one of lipa's services failed.
    LipaServiceUnavailable,
    /// The notification payload is disabled in the provided
    /// [`NotificationToggles`](crate::notification_handling::NotificationToggles).
    NotificationDisabledInNotificationToggles,
}

impl Display for NotificationHandlingErrorCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

pub type NotificationHandlingError = perro::Error<NotificationHandlingErrorCode>;
pub type NotificationHandlingResult<T> = std::result::Result<T, NotificationHandlingError>;

impl NotificationHandlingErrorCode {
    pub(crate) fn from_runtime_error(_error: RuntimeErrorCode) -> Self {
        Self::NodeUnavailable
    }
}

/// Enum representing possible errors why parsing could fail.
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    /// Parsing failed because parsed string was not complete.
    /// Additional characters are needed to make the string valid.
    /// It makes parsed string a valid prefix of a valid string.
    #[error("Incomplete")]
    Incomplete,

    /// Parsing failed because an unexpected character at position `at` was met.
    /// The character **has to be removed**.
    #[error("InvalidCharacter at {at}")]
    InvalidCharacter { at: u32 },
}

impl From<parser::ParseError> for ParseError {
    fn from(error: parser::ParseError) -> Self {
        match error {
            parser::ParseError::Incomplete => ParseError::Incomplete,
            parser::ParseError::UnexpectedCharacter(at) | parser::ParseError::ExcessSuffix(at) => {
                ParseError::InvalidCharacter { at: at as u32 }
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum ParsePhoneNumberPrefixError {
    #[error("Incomplete")]
    Incomplete,
    #[error("InvalidCharacter at {at}")]
    InvalidCharacter { at: u32 },
    #[error("UnsupportedCountry")]
    UnsupportedCountry,
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum ParsePhoneNumberError {
    #[error("ParsingError")]
    ParsingError,
    #[error("MissingCountryCode")]
    MissingCountryCode,
    #[error("InvalidCountryCode")]
    InvalidCountryCode,
    #[error("InvalidPhoneNumber")]
    InvalidPhoneNumber,
    #[error("UnsupportedCountry")]
    UnsupportedCountry,
}