Skip to content

Commit 1a1ad23

Browse files
committed
further invoice and interface state improvements
1 parent 0bfe36d commit 1a1ad23

File tree

8 files changed

+190
-296
lines changed

8 files changed

+190
-296
lines changed

Cargo.lock

+2-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

invoice/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ name = "rgbinvoice"
1717

1818
[dependencies]
1919
amplify = { workspace = true }
20-
base58 = "0.2.0"
2120
baid64 = { workspace = true }
2221
strict_encoding = { workspace = true }
2322
bp-core = { workspace = true }

invoice/src/builder.rs

+15-96
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121

2222
use std::str::FromStr;
2323

24-
use rgb::{AttachId, ContractId, State, StateData};
25-
use strict_encoding::{FieldName, StrictSerialize, TypeName};
24+
use rgb::{ContractId, StateData};
25+
use strict_encoding::{FieldName, SerializeError, StrictSerialize, TypeName};
2626

27-
use crate::invoice::{Beneficiary, InvoiceState, RgbInvoice, RgbTransport, XChainNet};
28-
use crate::TransportParseError;
27+
use crate::{Beneficiary, RgbInvoice, RgbTransport, TransportParseError, XChainNet};
2928

3029
#[derive(Clone, Eq, PartialEq, Debug)]
3130
pub struct RgbInvoiceBuilder(RgbInvoice);
@@ -40,7 +39,7 @@ impl RgbInvoiceBuilder {
4039
operation: None,
4140
assignment: None,
4241
beneficiary: beneficiary.into(),
43-
owned_state: InvoiceState::Any,
42+
state: None,
4443
expiry: None,
4544
unknown_query: none!(),
4645
})
@@ -72,106 +71,26 @@ impl RgbInvoiceBuilder {
7271
self
7372
}
7473

75-
/// Set the invoiced state, which includes both state data and an optional attachment
76-
/// information.
77-
///
78-
/// # Panics
79-
///
80-
/// If any state information or attachment requirements are already present in the invoice.
81-
///
82-
/// # See also
83-
///
84-
/// - [`Self::add_state_data`], adding just state data information, not affecting attachment
85-
/// requirements;
86-
/// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
87-
/// object;
88-
/// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
89-
/// information.
90-
pub fn set_state(mut self, state: State) -> Self {
91-
if !self.0.owned_state.is_any() {
92-
panic!("invoice already has state information");
93-
}
94-
self.0.owned_state = InvoiceState::Specific(state);
95-
self
96-
}
97-
9874
/// Add state data to the invoice.
9975
///
100-
/// NB: This keeps existing attachment requirements.
101-
///
102-
/// # Panics
103-
///
104-
/// If the invoice already have any state information (excluding attachment requirements).
105-
///
106-
/// # See also
107-
///
108-
/// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
109-
/// requirements;
110-
/// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
111-
/// object;
112-
/// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
113-
/// information.
114-
pub fn add_state_data(mut self, data: StateData) -> Self {
115-
self.0.owned_state = match self.0.owned_state {
116-
InvoiceState::Any => InvoiceState::Specific(State::from(data)),
117-
InvoiceState::Specific(_) => panic!("invoice already has state information"),
118-
InvoiceState::Attach(attach_id) => InvoiceState::Specific(State::with(data, attach_id)),
119-
};
76+
/// See also [`Self::serialize_state_data`], which adds state data by serializing them from a
77+
/// state object.
78+
pub fn set_state(mut self, data: StateData) -> Self {
79+
self.0.state = Some(data);
12080
self
12181
}
12282

12383
/// Add state data to the invoice by strict-serializing the provided object.
12484
///
125-
/// NB: This keeps existing attachment requirements.
126-
///
12785
/// Use the function carefully, since the common pitfall here is to perform double serialization
12886
/// of an already serialized data type, like `SmallBlob`. This produces an invalid state object
129-
/// which can't be properly parsed later.
130-
///
131-
/// # Panics
132-
///
133-
/// If the invoice already has any state information (excluding attachment requirements).
134-
///
135-
/// # See also
136-
///
137-
/// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
138-
/// requirements;
139-
/// - [`Self::add_state_data`], adding just state data information, not affecting attachment
140-
/// requirements;
141-
/// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
142-
/// information.
143-
pub fn serialize_state_data(mut self, data: impl StrictSerialize) -> Self {
144-
self.0.owned_state = InvoiceState::Specific(State::from_serialized(data));
145-
self
146-
}
147-
148-
/// Add attachment requirements to an invoice, keeping the rest of the invoice state information
149-
/// unchanged.
150-
///
151-
/// # Panics
152-
///
153-
/// If the invoice already has attachment requirements defined.
154-
///
155-
/// # See also
156-
///
157-
/// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
158-
/// requirements;
159-
/// - [`Self::add_state_data`], adding just state data information, not affecting attachment
160-
/// requirements;
161-
/// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
162-
/// object;
163-
pub fn add_attachment(mut self, attach_id: AttachId) -> Result<Self, Self> {
164-
self.0.owned_state = match self.0.owned_state {
165-
InvoiceState::Any => InvoiceState::Attach(attach_id),
166-
InvoiceState::Attach(_)
167-
| InvoiceState::Specific(State {
168-
attach: Some(_), ..
169-
}) => panic!("invoice already has attachment requirements"),
170-
InvoiceState::Specific(mut state) => {
171-
state.attach = Some(attach_id);
172-
InvoiceState::Specific(state)
173-
}
174-
};
87+
/// which can't be properly parsed later. See also [`Self::set_state`], which sets state data
88+
/// directly with no serialization.
89+
pub fn serialize_state_data(
90+
mut self,
91+
data: &impl StrictSerialize,
92+
) -> Result<Self, SerializeError> {
93+
self.0.state = Some(StateData::from_serialized(data)?);
17594
Ok(self)
17695
}
17796

invoice/src/invoice.rs

+3-29
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bp::seals::txout::CloseMethod;
2424
use bp::{InvalidPubkey, OutputPk, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
2525
use indexmap::IndexMap;
2626
use invoice::{AddressNetwork, AddressPayload, Network};
27-
use rgb::{AttachId, ContractId, Layer1, SecretSeal, State};
27+
use rgb::{ContractId, Layer1, SecretSeal, StateData};
2828
use strict_encoding::{FieldName, TypeName};
2929

3030
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
@@ -37,33 +37,6 @@ pub enum RgbTransport {
3737
UnspecifiedMeans,
3838
}
3939

40-
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
41-
pub enum InvoiceState {
42-
Any,
43-
Specific(State),
44-
Attach(AttachId),
45-
}
46-
47-
impl InvoiceState {
48-
pub fn is_any(&self) -> bool { matches!(self, InvoiceState::Any) }
49-
50-
pub fn state(&self) -> Option<&State> {
51-
match self {
52-
InvoiceState::Any => None,
53-
InvoiceState::Specific(s) => Some(s),
54-
InvoiceState::Attach(_) => None,
55-
}
56-
}
57-
58-
pub fn attach_id(&self) -> Option<AttachId> {
59-
match self {
60-
InvoiceState::Any => None,
61-
InvoiceState::Specific(s) => s.attach,
62-
InvoiceState::Attach(id) => Some(*id),
63-
}
64-
}
65-
}
66-
6740
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
6841
#[non_exhaustive]
6942
pub enum ChainNet {
@@ -237,9 +210,10 @@ pub struct RgbInvoice {
237210
pub operation: Option<FieldName>,
238211
pub assignment: Option<FieldName>,
239212
pub beneficiary: XChainNet<Beneficiary>,
240-
pub owned_state: InvoiceState,
213+
pub state: Option<StateData>,
241214
/// UTC unix timestamp
242215
pub expiry: Option<i64>,
216+
// Attachment requirements should go here
243217
pub unknown_query: IndexMap<String, String>,
244218
}
245219

invoice/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,5 @@ pub use builder::RgbInvoiceBuilder;
3737
pub use parse::{InvoiceParseError, TransportParseError};
3838

3939
pub use crate::invoice::{
40-
Beneficiary, ChainNet, InvoiceState, Pay2Vout, Pay2VoutError, RgbInvoice, RgbTransport,
41-
XChainNet,
40+
Beneficiary, ChainNet, Pay2Vout, Pay2VoutError, RgbInvoice, RgbTransport, XChainNet,
4241
};

0 commit comments

Comments
 (0)