rgbstd/accessors/bundle.rs
1// RGB standard library for working with smart contracts on Bitcoin & Lightning
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
22use rgb::{GraphSeal, OpId, Operation, Transition, TransitionBundle};
23
24use crate::accessors::TypedAssignsExt;
25
26#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
27#[display(doc_comments)]
28pub enum RevealError {
29 /// the provided state transition is not a part of the bundle
30 UnrelatedTransition(OpId),
31}
32
33pub trait BundleExt {
34 /// Ensures that the seal is revealed inside the bundle.
35 fn reveal_seal(&mut self, seal: GraphSeal);
36
37 /// Ensures that the transition is revealed inside the bundle.
38 ///
39 /// # Returns
40 ///
41 /// `true` if the transition was previously concealed; `false` if it was
42 /// already revealed; error if the transition is unrelated to the bundle.
43 fn reveal_transition(&mut self, transition: &Transition) -> Result<bool, RevealError>;
44}
45
46impl BundleExt for TransitionBundle {
47 fn reveal_seal(&mut self, seal: GraphSeal) {
48 for (_, item) in self.keyed_values_mut() {
49 if let Some(transition) = &mut item.transition {
50 for (_, assign) in transition.assignments.keyed_values_mut() {
51 assign.reveal_seal(seal)
52 }
53 }
54 }
55 }
56
57 fn reveal_transition(&mut self, transition: &Transition) -> Result<bool, RevealError> {
58 let id = transition.id();
59 let item = self
60 .get_mut(&id)
61 .ok_or(RevealError::UnrelatedTransition(id))?;
62 match item.transition {
63 None => {
64 item.transition = Some(transition.clone());
65 Ok(true)
66 }
67 Some(_) => Ok(false),
68 }
69 }
70}