forked from MutinyWallet/mutiny-node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.rs
552 lines (508 loc) · 19.3 KB
/
error.rs
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use aes::cipher::block_padding::UnpadError;
use bdk_wallet::error::BuildFeeBumpError;
use bdk_wallet::signer::SignerError;
use bdk_wallet::tx_builder::AddUtxoError;
use bitcoin::psbt::ExtractTxError;
use hex_conservative::HexToArrayError;
use lightning::ln::channelmanager::RetryableSendFailure;
use lightning::ln::peer_handler::PeerHandleError;
use lightning_invoice::ParseOrSemanticError;
use lightning_rapid_gossip_sync::GraphSyncError;
use lightning_transaction_sync::TxSyncError;
use log::error;
use std::string::FromUtf8Error;
use thiserror::Error;
#[derive(Error, Debug)]
#[allow(dead_code)]
// copied from LDK lite
/// An error that possibly needs to be handled by the user.
pub enum MutinyError {
/// Returned when trying to start Mutiny while it is already running.
#[error("Mutiny is already running.")]
AlreadyRunning,
/// Returned when trying to stop Mutiny while it is not running.
#[error("Mutiny is not running.")]
NotRunning,
/// Returned when Mutiny tries to startup with a different network than the one it was
/// previously running on.
#[error("Incorrect expected network.")]
NetworkMismatch,
/// Returned on any resource that is not found.
#[error("Resource Not found.")]
NotFound,
/// The funding transaction could not be created.
#[error("Funding transaction could not be created.")]
FundingTxCreationFailed,
/// A network connection has been closed.
#[error("Network connection closed.")]
ConnectionFailed,
/// The invoice or address is on a different network
#[error("The invoice or address is on a different network.")]
IncorrectNetwork,
/// Payment of the given invoice has already been initiated.
#[error("An invoice must not get payed twice.")]
NonUniquePaymentHash,
/// Payment Timed out
#[error("Payment timed out.")]
PaymentTimeout,
/// The given invoice is invalid.
#[error("The given invoice is invalid.")]
InvoiceInvalid,
/// The given invoice is expired.
#[error("The given invoice is expired.")]
InvoiceExpired,
/// Invoice creation failed.
#[error("Failed to create invoice.")]
InvoiceCreationFailed,
/// We have enough balance to pay an invoice, but
/// the this would take from our reserve amount which is not allowed.
#[error("Channel reserve amount is too high.")]
ReserveAmountError,
/// We do not have enough balance to pay the given amount.
#[error("We do not have enough balance to pay the given amount.")]
InsufficientBalance,
/// Could not make a request to the LSP.
#[error("Failed to make a request to the LSP.")]
LspGenericError,
/// LSP indicated it could not fund the channel requested.
#[error("Failed to request channel from LSP due to funding error.")]
LspFundingError,
/// LSP indicated the amount is too high to fund.
#[error("Failed to request channel from LSP due to amount being too high.")]
LspAmountTooHighError,
/// LSP indicated it was not connected to the client node.
#[error("Failed to have a connection to the LSP node.")]
LspConnectionError,
/// LSP required an invoice and none was provided.
#[error("Failed to provide an invoice to the LSP.")]
LspInvoiceRequired,
/// Subscription Client Not Configured
#[error("Subscription Client Not Configured")]
SubscriptionClientNotConfigured,
/// Invalid Arguments were given
#[error("Invalid Arguments were given")]
InvalidArgumentsError,
/// No route for the given target could be found.
#[error("Failed to find route.")]
RoutingFailed,
/// A given peer info could not be parsed.
#[error("Failed to parse the given peer information.")]
PeerInfoParseFailed,
/// A channel could not be opened.
#[error("Failed to create channel.")]
ChannelCreationFailed,
/// A channel could not be opened.
#[error("Failed to create channel. {0}")]
ChannelCreationFailedWithReason(String),
/// A channel could not be closed.
#[error("Failed to close channel.")]
ChannelClosingFailed,
/// Persistence failed.
#[error("Failed to persist data.")]
PersistenceFailed {
#[from]
source: MutinyStorageError,
},
#[error("Failed to read data from storage.")]
ReadError { source: MutinyStorageError },
#[error("Failed to decode lightning data.")]
LnDecodeError,
/// A failure to generate a mnemonic seed.
#[error("Failed to generate seed")]
SeedGenerationFailed,
/// User provided invalid mnemonic.
#[error("Invalid mnemonic")]
InvalidMnemonic,
/// Invalid BTC transaction or hex string.
#[error("Invalid BTC transaction")]
InvalidTransaction,
/// A wallet operation failed.
#[error("Failed to conduct wallet operation.")]
WalletOperationFailed,
/// A signing operation failed.
#[error("Failed to sign given transaction.")]
WalletSigningFailed,
/// A chain access operation failed.
#[error("Failed to conduct chain access operation.")]
ChainAccessFailed,
/// A failure to sync the on-chain wallet
#[error("Failed to to sync on-chain wallet.")]
WalletSyncError,
/// An error with rapid gossip sync
#[error("Failed to execute a rapid gossip sync function")]
RapidGossipSyncError,
/// A error with DLCs
#[error("Failed to execute a dlc function")]
DLCManagerError,
/// Node pubkey given is invalid
#[error("The given node pubkey is invalid.")]
PubkeyInvalid,
/// Error converting JS f64 value to Amount
#[error("Satoshi amount is invalid")]
BadAmountError,
/// Error getting the bitcoin price
#[error("Failed to get the bitcoin price.")]
BitcoinPriceError,
/// Error getting nostr data
#[error("Failed to get nostr data.")]
NostrError,
/// Error with Nip07 Extension
#[error("Error with NIP-07 extension")]
Nip07Extension,
/// Incorrect password entered.
#[error("Incorrect password entered.")]
IncorrectPassword,
/// Cannot change password to the same password
#[error("Cannot change password to the same password.")]
SamePassword,
/// Error calling Cashu Mint
#[error("Error calling Cashu Mint.")]
CashuMintError,
/// Mint URL in token was empty
#[error("Mint URL in token is empty.")]
EmptyMintURLError,
/// Token already spent.
#[error("Token has been already spent.")]
TokenAlreadySpent,
#[error("Message Packet size exceeded")]
PacketSizeExceeded,
#[error("Invalid fee rate")]
InvalidFeerate,
#[error("Invalid psbt")]
InvalidPsbt,
#[error("Invalid hex")]
InvalidHex,
/// Failed to authenticate using JWT
#[error("Failed to authenticate using JWT.")]
JwtAuthFailure,
#[error("Failed to parse VSS value from getObject response.")]
FailedParsingVssValue,
#[error(transparent)]
Other(anyhow::Error),
}
#[derive(Error, Debug)]
pub enum MutinyStorageError {
#[error("Failed to serialize or deserialize")]
SerdeError {
#[from]
source: serde_json::Error,
},
#[error("Failed to get lock on memory storage")]
LockError,
#[error("Failed to use indexeddb storage")]
IndexedDBError,
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl PartialEq for MutinyStorageError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::SerdeError { .. }, Self::SerdeError { .. }) => true,
(Self::LockError, Self::LockError) => true,
(Self::IndexedDBError, Self::IndexedDBError) => true,
(Self::Other(e), Self::Other(e2)) => e.to_string() == e2.to_string(),
_ => false,
}
}
}
impl PartialEq for MutinyError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::AlreadyRunning, Self::AlreadyRunning) => true,
(Self::NotRunning, Self::NotRunning) => true,
(Self::NetworkMismatch, Self::NetworkMismatch) => true,
(Self::NotFound, Self::NotFound) => true,
(Self::FundingTxCreationFailed, Self::FundingTxCreationFailed) => true,
(Self::ConnectionFailed, Self::ConnectionFailed) => true,
(Self::IncorrectNetwork, Self::IncorrectNetwork) => true,
(Self::NonUniquePaymentHash, Self::NonUniquePaymentHash) => true,
(Self::PaymentTimeout, Self::PaymentTimeout) => true,
(Self::InvoiceInvalid, Self::InvoiceInvalid) => true,
(Self::InvoiceExpired, Self::InvoiceExpired) => true,
(Self::InvoiceCreationFailed, Self::InvoiceCreationFailed) => true,
(Self::ReserveAmountError, Self::ReserveAmountError) => true,
(Self::InsufficientBalance, Self::InsufficientBalance) => true,
(Self::LspGenericError, Self::LspGenericError) => true,
(Self::LspFundingError, Self::LspFundingError) => true,
(Self::LspAmountTooHighError, Self::LspAmountTooHighError) => true,
(Self::LspConnectionError, Self::LspConnectionError) => true,
(Self::SubscriptionClientNotConfigured, Self::SubscriptionClientNotConfigured) => true,
(Self::InvalidArgumentsError, Self::InvalidArgumentsError) => true,
(Self::RoutingFailed, Self::RoutingFailed) => true,
(Self::PeerInfoParseFailed, Self::PeerInfoParseFailed) => true,
(Self::ChannelCreationFailed, Self::ChannelCreationFailed) => true,
(
Self::ChannelCreationFailedWithReason(x),
Self::ChannelCreationFailedWithReason(y),
) => x == y,
(Self::ChannelClosingFailed, Self::ChannelClosingFailed) => true,
(Self::PersistenceFailed { source }, Self::PersistenceFailed { source: source2 }) => {
source == source2
}
(Self::ReadError { source }, Self::ReadError { source: source2 }) => source == source2,
(Self::LnDecodeError, Self::LnDecodeError) => true,
(Self::SeedGenerationFailed, Self::SeedGenerationFailed) => true,
(Self::InvalidMnemonic, Self::InvalidMnemonic) => true,
(Self::WalletOperationFailed, Self::WalletOperationFailed) => true,
(Self::WalletSigningFailed, Self::WalletSigningFailed) => true,
(Self::ChainAccessFailed, Self::ChainAccessFailed) => true,
(Self::WalletSyncError, Self::WalletSyncError) => true,
(Self::RapidGossipSyncError, Self::RapidGossipSyncError) => true,
(Self::PubkeyInvalid, Self::PubkeyInvalid) => true,
(Self::BadAmountError, Self::BadAmountError) => true,
(Self::BitcoinPriceError, Self::BitcoinPriceError) => true,
(Self::DLCManagerError, Self::DLCManagerError) => true,
(Self::NostrError, Self::NostrError) => true,
(Self::IncorrectPassword, Self::IncorrectPassword) => true,
(Self::SamePassword, Self::SamePassword) => true,
(Self::CashuMintError, Self::CashuMintError) => true,
(Self::EmptyMintURLError, Self::EmptyMintURLError) => true,
(Self::TokenAlreadySpent, Self::TokenAlreadySpent) => true,
(Self::Other(e), Self::Other(e2)) => e.to_string() == e2.to_string(),
_ => false,
}
}
}
impl MutinyError {
pub fn read_err(e: MutinyStorageError) -> Self {
MutinyError::ReadError { source: e }
}
pub fn write_err(e: MutinyStorageError) -> Self {
MutinyError::PersistenceFailed { source: e }
}
}
impl From<ExtractTxError> for MutinyError {
fn from(_e: ExtractTxError) -> Self {
Self::InvalidPsbt
}
}
impl From<UnpadError> for MutinyError {
fn from(_e: UnpadError) -> Self {
Self::IncorrectPassword
}
}
impl From<base64::DecodeError> for MutinyError {
fn from(_e: base64::DecodeError) -> Self {
Self::IncorrectPassword
}
}
impl From<FromUtf8Error> for MutinyError {
fn from(_e: FromUtf8Error) -> Self {
Self::IncorrectPassword
}
}
impl From<aes_gcm::Error> for MutinyError {
fn from(_: aes_gcm::Error) -> Self {
Self::IncorrectPassword
}
}
impl From<aes_gcm::aes::cipher::InvalidLength> for MutinyError {
fn from(_: aes_gcm::aes::cipher::InvalidLength) -> Self {
Self::IncorrectPassword
}
}
impl From<bdk_chain::local_chain::AlterCheckPointError> for MutinyError {
fn from(_e: bdk_chain::local_chain::AlterCheckPointError) -> Self {
Self::WalletOperationFailed
}
}
impl From<bdk_wallet::descriptor::error::Error> for MutinyError {
fn from(_: bdk_wallet::descriptor::error::Error) -> Self {
Self::WalletOperationFailed
}
}
// impl From<bdk_wallet::NewError<MutinyError>> for MutinyError {
// fn from(e: bdk_wallet::NewError<MutinyError>) -> Self {
// match e {
// bdk_wallet::NewError::Write(e) => e,
// bdk_wallet::NewError::Descriptor(e) => e.into(),
// bdk_wallet::NewError::NonEmptyDatabase => Self::WalletOperationFailed,
// }
// }
// }
impl From<bdk_wallet::LoadError> for MutinyError {
fn from(e: bdk_wallet::LoadError) -> Self {
match e {
bdk_wallet::LoadError::Descriptor(e) => e.into(),
bdk_wallet::LoadError::MissingGenesis => Self::WalletOperationFailed,
bdk_wallet::LoadError::MissingNetwork => Self::WalletOperationFailed,
bdk_wallet::LoadError::MissingDescriptor(_keychain_kind) => Self::WalletOperationFailed,
bdk_wallet::LoadError::Mismatch(_load_mismatch) => Self::WalletSyncError,
}
}
}
impl From<AddUtxoError> for MutinyError {
fn from(_: AddUtxoError) -> Self {
Self::WalletOperationFailed
}
}
impl From<bip39::Error> for MutinyError {
fn from(_e: bip39::Error) -> Self {
Self::InvalidMnemonic
}
}
impl From<bitcoin::bip32::Error> for MutinyError {
fn from(_e: bitcoin::bip32::Error) -> Self {
Self::InvalidMnemonic
}
}
impl From<bitcoin::address::ParseError> for MutinyError {
fn from(_e: bitcoin::address::ParseError) -> Self {
Self::PubkeyInvalid
}
}
impl From<bitcoin::hex::HexToBytesError> for MutinyError {
fn from(_e: bitcoin::hex::HexToBytesError) -> Self {
Self::InvalidHex
}
}
impl From<bitcoin::hex::HexToArrayError> for MutinyError {
fn from(_e: bitcoin::hex::HexToArrayError) -> Self {
Self::InvalidHex
}
}
impl From<TxSyncError> for MutinyError {
fn from(_e: TxSyncError) -> Self {
MutinyError::ChainAccessFailed
}
}
impl From<lightning::ln::msgs::DecodeError> for MutinyError {
fn from(_e: lightning::ln::msgs::DecodeError) -> Self {
MutinyError::LnDecodeError
}
}
impl From<lightning::ln::script::InvalidShutdownScript> for MutinyError {
fn from(_e: lightning::ln::script::InvalidShutdownScript) -> Self {
MutinyError::InvalidArgumentsError
}
}
impl From<ParseOrSemanticError> for MutinyError {
fn from(_e: ParseOrSemanticError) -> Self {
Self::InvoiceInvalid
}
}
impl From<PeerHandleError> for MutinyError {
fn from(_e: PeerHandleError) -> Self {
// TODO handle the case where `no_connection_possible`
Self::ConnectionFailed
}
}
impl From<RetryableSendFailure> for MutinyError {
fn from(s: RetryableSendFailure) -> Self {
match s {
RetryableSendFailure::PaymentExpired => Self::InvoiceExpired,
RetryableSendFailure::RouteNotFound => Self::RoutingFailed,
RetryableSendFailure::DuplicatePayment => Self::NonUniquePaymentHash,
RetryableSendFailure::OnionPacketSizeExceeded => Self::PacketSizeExceeded,
}
}
}
impl From<GraphSyncError> for MutinyError {
fn from(_e: GraphSyncError) -> Self {
MutinyError::RapidGossipSyncError
}
}
impl From<std::io::Error> for MutinyError {
fn from(e: std::io::Error) -> Self {
MutinyError::PersistenceFailed {
source: MutinyStorageError::Other(e.into()),
}
}
}
impl From<serde_json::Error> for MutinyError {
fn from(_: serde_json::Error) -> Self {
Self::ReadError {
source: MutinyStorageError::Other(anyhow::anyhow!("Failed to deserialize")),
}
}
}
impl<G> From<std::sync::PoisonError<G>> for MutinyStorageError {
fn from(_e: std::sync::PoisonError<G>) -> Self {
MutinyStorageError::LockError
}
}
impl<G> From<std::sync::TryLockError<G>> for MutinyError {
fn from(_e: std::sync::TryLockError<G>) -> Self {
MutinyStorageError::LockError.into()
}
}
impl<G> From<std::sync::TryLockError<G>> for MutinyStorageError {
fn from(_e: std::sync::TryLockError<G>) -> Self {
MutinyStorageError::LockError
}
}
// impl From<bitcoin::hashes::hex::Error> for MutinyError {
// fn from(_e: bitcoin::hashes::hex::Error) -> Self {
// MutinyError::ReadError {
// source: MutinyStorageError::Other(anyhow::anyhow!("Failed to decode hex")),
// }
// }
// }
impl From<HexToArrayError> for MutinyError {
fn from(value: HexToArrayError) -> Self {
MutinyError::ReadError {
source: MutinyStorageError::Other(anyhow::anyhow!(value)),
}
}
}
// impl From<bitcoin::address::Error> for MutinyError {
// fn from(e: bitcoin::address::Error) -> Self {
// match e {
// bitcoin::address::Error::NetworkValidation { .. } => MutinyError::IncorrectNetwork,
// bitcoin::address::Error::UnrecognizedScript => MutinyError::InvalidArgumentsError,
// bitcoin::address::Error::UnknownAddressType(_) => MutinyError::InvalidArgumentsError,
// _ => MutinyError::ReadError {
// source: MutinyStorageError::Other(anyhow::anyhow!("Failed to decode address")),
// },
// }
// }
// }
impl From<esplora_client::Error> for MutinyError {
fn from(_e: esplora_client::Error) -> Self {
// This is most likely a chain access failure
Self::ChainAccessFailed
}
}
impl From<Box<esplora_client::Error>> for MutinyError {
fn from(_e: Box<esplora_client::Error>) -> Self {
// This is most likely a chain access failure
Self::ChainAccessFailed
}
}
// impl From<bdk_wallet::InsertTxError> for MutinyError {
// fn from(_e: bdk_wallet::InsertTxError) -> Self {
// Self::WalletSyncError
// }
// }
impl From<bdk_wallet::error::CreateTxError> for MutinyError {
fn from(_e: bdk_wallet::error::CreateTxError) -> Self {
Self::WalletOperationFailed
}
}
impl From<BuildFeeBumpError> for MutinyError {
fn from(e: BuildFeeBumpError) -> Self {
match e {
BuildFeeBumpError::UnknownUtxo(_) => Self::NotFound,
BuildFeeBumpError::TransactionNotFound(_) => Self::NotFound,
BuildFeeBumpError::TransactionConfirmed(_) => Self::NotFound,
BuildFeeBumpError::IrreplaceableTransaction(_) => Self::InvalidArgumentsError,
BuildFeeBumpError::FeeRateUnavailable => Self::WalletOperationFailed,
}
}
}
impl From<SignerError> for MutinyError {
fn from(_: SignerError) -> Self {
Self::WalletOperationFailed
}
}
impl From<anyhow::Error> for MutinyError {
fn from(e: anyhow::Error) -> Self {
error!("Got unhandled error: {e}");
// handle fedimint anyhow errors
match e.to_string().as_str() {
"Insufficient balance" => Self::InsufficientBalance,
"MissingInvoiceAmount" => Self::BadAmountError,
_str => Self::Other(e),
}
}
}