rgb/contract/
seal.rs

1// RGB Core Library: consensus layer for RGB smart contracts.
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// Copyright (C) 2019-2023 Dr Maxim Orlovsky. All rights reserved.
10//
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14//
15//     http://www.apache.org/licenses/LICENSE-2.0
16//
17// Unless required by applicable law or agreed to in writing, software
18// distributed under the License is distributed on an "AS IS" BASIS,
19// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20// See the License for the specific language governing permissions and
21// limitations under the License.
22
23use core::fmt::Debug;
24use std::hash::Hash;
25
26pub use bp::seals::txout::blind::{
27    ChainBlindSeal as GraphSeal, ParseError, SecretSeal, SingleBlindSeal as GenesisSeal,
28};
29pub use bp::seals::txout::TxoSeal;
30use bp::Txid;
31use commit_verify::{CommitEncode, Conceal};
32use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};
33
34use crate::LIB_NAME_RGB;
35
36pub trait ExposedSeal:
37    Debug
38    + StrictDumb
39    + StrictEncode
40    + StrictDecode
41    + CommitEncode
42    + Conceal<Concealed = SecretSeal>
43    + Eq
44    + Ord
45    + Copy
46    + Hash
47    + TxoSeal
48{
49}
50
51impl ExposedSeal for GraphSeal {}
52
53impl ExposedSeal for GenesisSeal {}
54
55#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
56#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
57#[strict_type(lib = LIB_NAME_RGB, tags = custom, dumb = SealWitness::Genesis)]
58#[cfg_attr(
59    feature = "serde",
60    derive(Serialize, Deserialize),
61    serde(crate = "serde_crate", rename_all = "camelCase")
62)]
63pub enum SealWitness {
64    #[strict_type(tag = 0)]
65    #[display("~")]
66    Genesis,
67
68    #[strict_type(tag = 1)]
69    #[display(inner)]
70    Present(Txid),
71
72    #[strict_type(tag = 2)]
73    #[display("~")]
74    Extension,
75}
76
77impl SealWitness {
78    pub fn txid(&self) -> Option<Txid> {
79        match self {
80            SealWitness::Genesis | SealWitness::Extension => None,
81            SealWitness::Present(txid) => Some(*txid),
82        }
83    }
84    pub fn map_txid<U>(&self, f: impl FnOnce(Txid) -> U) -> Option<U> { self.txid().map(f) }
85}