rgbstd/accessors/
assignments.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 amplify::confinement::SmallVec;
23use rgb::{
24    Assign, AssignAttach, AssignData, AssignFungible, AssignRights, ExposedSeal, ExposedState,
25    TypedAssigns,
26};
27
28pub trait TypedAssignsExt<Seal: ExposedSeal> {
29    fn reveal_seal(&mut self, seal: Seal);
30
31    fn filter_revealed_seals(&self) -> Vec<Seal>;
32}
33
34impl<Seal: ExposedSeal> TypedAssignsExt<Seal> for TypedAssigns<Seal> {
35    fn reveal_seal(&mut self, seal: Seal) {
36        fn reveal<State: ExposedState, Seal: ExposedSeal>(
37            vec: &mut SmallVec<Assign<State, Seal>>,
38            revealed: Seal,
39        ) {
40            for assign in vec.iter_mut() {
41                match assign {
42                    Assign::ConfidentialSeal { seal, state } if *seal == revealed.conceal() => {
43                        *assign = Assign::Revealed {
44                            seal: revealed,
45                            state: state.clone(),
46                        }
47                    }
48                    Assign::Confidential { seal, state } if *seal == revealed.conceal() => {
49                        *assign = Assign::ConfidentialState {
50                            seal: revealed,
51                            state: *state,
52                        }
53                    }
54                    _ => {}
55                }
56            }
57        }
58
59        match self {
60            TypedAssigns::Declarative(v) => reveal(v, seal),
61            TypedAssigns::Fungible(v) => reveal(v, seal),
62            TypedAssigns::Structured(v) => reveal(v, seal),
63            TypedAssigns::Attachment(v) => reveal(v, seal),
64        }
65    }
66
67    fn filter_revealed_seals(&self) -> Vec<Seal> {
68        match self {
69            TypedAssigns::Declarative(s) => {
70                s.iter().filter_map(AssignRights::revealed_seal).collect()
71            }
72            TypedAssigns::Fungible(s) => {
73                s.iter().filter_map(AssignFungible::revealed_seal).collect()
74            }
75            TypedAssigns::Structured(s) => s.iter().filter_map(AssignData::revealed_seal).collect(),
76            TypedAssigns::Attachment(s) => {
77                s.iter().filter_map(AssignAttach::revealed_seal).collect()
78            }
79        }
80    }
81}