rgb/
errors.rs

1// RGB API library for smart contracts on Bitcoin & Lightning network
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2023 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22#![allow(clippy::result_large_err)]
23
24use std::convert::Infallible;
25use std::io;
26
27use amplify::IoError;
28#[cfg(feature = "bp")]
29use bpwallet::psbt::{ConstructionError, DecodeError};
30use nonasync::persistence::PersistenceError;
31use psrgbt::{CommitError, EmbedError, TapretKeyError};
32use rgbstd::containers::LoadError;
33use rgbstd::contract::{BuilderError, ContractError};
34use rgbstd::persistence::{
35    ComposeError, ConsignError, FasciaError, Stock, StockError, StockErrorAll, StockErrorMem,
36};
37use rgbstd::validation::ValidationError;
38use rgbstd::{AssignmentType, ChainNet};
39use strict_types::encoding::Ident;
40
41use crate::validation;
42
43#[derive(Debug, Display, Error, From)]
44#[display(inner)]
45pub enum WalletError {
46    #[from]
47    #[from(io::Error)]
48    File(IoError),
49
50    #[from]
51    StockLoad(LoadError),
52
53    WalletPersist(PersistenceError),
54
55    StockPersist(PersistenceError),
56
57    #[cfg(feature = "cli")]
58    #[from]
59    WalletExec(bpwallet::cli::ExecError),
60
61    #[from]
62    Builder(BuilderError),
63
64    #[from]
65    Contract(ContractError),
66
67    Invoicing(String),
68
69    #[cfg(feature = "bp")]
70    #[from]
71    PsbtDecode(DecodeError),
72
73    /// wallet with id '{0}' is not known to the system.
74    #[display(doc_comments)]
75    WalletUnknown(Ident),
76
77    #[from]
78    InvalidConsignment(ValidationError),
79
80    /// invalid identifier.
81    #[from]
82    #[display(doc_comments)]
83    InvalidId(baid64::Baid64ParseError),
84
85    /// the contract source doesn't fit requirements imposed by the used schema.
86    ///
87    /// {0}
88    #[display(doc_comments)]
89    IncompleteContract(validation::Status),
90
91    /// cannot find the terminal to add the tapret tweak to.
92    NoTweakTerminal,
93
94    /// resolver error: {0}
95    #[display(doc_comments)]
96    Resolver(String),
97
98    #[from(StockError)]
99    #[from(StockErrorAll)]
100    #[display(inner)]
101    Stock(String),
102
103    #[cfg(feature = "cli")]
104    #[from]
105    Yaml(serde_yaml::Error),
106
107    #[from]
108    Custom(String),
109}
110
111impl From<Infallible> for WalletError {
112    fn from(_: Infallible) -> Self { unreachable!() }
113}
114
115impl From<(Stock, WalletError)> for WalletError {
116    fn from((_, e): (Stock, WalletError)) -> Self { e }
117}
118
119#[allow(clippy::large_enum_variant)]
120#[derive(Debug, Display, Error, From)]
121pub enum PayError {
122    #[from]
123    #[display(inner)]
124    Composition(CompositionError),
125
126    #[display("{0}")]
127    Completion(CompletionError),
128}
129
130#[derive(Debug, Display, Error, From)]
131#[display(doc_comments)]
132pub enum CompositionError {
133    /// invoice doesn't specify a contract.
134    NoContract,
135
136    /// invoice doesn't provide information about the assignment type and it's impossible to derive
137    /// which assignment type should be used from the schema.
138    NoAssignmentType,
139
140    /// invoice doesn't provide information about the assignment state
141    NoAssignmentState,
142
143    /// invoice specifies an unknown contract.
144    UnknownContract,
145
146    /// state provided via PSBT inputs is not sufficient to cover invoice state
147    /// requirements.
148    InsufficientState,
149
150    /// the invoice has expired.
151    InvoiceExpired,
152
153    /// invoice specifies a schema which is not valid for the specified contract.
154    InvalidSchema,
155
156    /// invoice requesting chain-network pair {0} but contract commits to a different one ({1})
157    InvoiceBeneficiaryWrongChainNet(ChainNet, ChainNet),
158
159    /// non-fungible state is not yet supported by the invoices.
160    Unsupported,
161
162    #[cfg(feature = "bp")]
163    #[from]
164    #[display(inner)]
165    Construction(ConstructionError),
166
167    #[from]
168    #[display(inner)]
169    Contract(ContractError),
170
171    #[from]
172    #[display(inner)]
173    Embed(EmbedError),
174
175    /// no outputs available to store state of type {0}
176    NoExtraOrChange(AssignmentType),
177
178    /// cannot find an output where to put the tapret commitment.
179    NoOutputForTapretCommitment,
180
181    /// the provided PSBT doesn't pay any sats to the RGB beneficiary address.
182    NoBeneficiaryOutput,
183
184    /// beneficiary output number is given when secret seal is used.
185    BeneficiaryVout,
186
187    /// the operation produces too many extra state transitions which can't fit
188    /// the container requirements.
189    TooManyExtras,
190
191    #[from]
192    #[display(inner)]
193    Builder(BuilderError),
194
195    #[from(String)]
196    #[from(StockError)]
197    #[from(StockErrorMem<ComposeError>)]
198    #[display(inner)]
199    Stock(String),
200
201    /// unsupported close method: {0}
202    #[display(doc_comments)]
203    UnsupportedCloseMethod(String),
204
205    /// unexpected error: {0}
206    Unexpected(String),
207}
208
209#[derive(Debug, Display, Error, From)]
210#[display(doc_comments)]
211pub enum CompletionError {
212    /// unspecified contract.
213    NoContract,
214
215    /// the provided PSBT doesn't pay any sats to the RGB beneficiary address.
216    NoBeneficiaryOutput,
217
218    /// the provided PSBT has conflicting descriptor in the taptweak output.
219    InconclusiveDerivation,
220
221    #[from]
222    #[display(inner)]
223    TapretKey(TapretKeyError),
224
225    #[from]
226    #[display(inner)]
227    Commit(CommitError),
228
229    #[from(String)]
230    #[from(StockErrorMem<ConsignError>)]
231    #[from(StockErrorMem<FasciaError>)]
232    #[display(inner)]
233    Stock(String),
234}
235
236impl From<Infallible> for CompletionError {
237    fn from(_: Infallible) -> Self { unreachable!() }
238}