rgbcore/operation/
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-2024 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
9// Copyright (C) 2019-2024 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::{ChainBlindSeal, ParseError, SingleBlindSeal};
27use bp::seals::txout::ExplicitSeal;
28pub use bp::seals::txout::TxoSeal;
29pub use bp::seals::SecretSeal;
30use bp::Txid;
31use commit_verify::Conceal;
32use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};
33
34pub type GenesisSeal = SingleBlindSeal;
35pub type GraphSeal = ChainBlindSeal;
36
37pub type OutputSeal = ExplicitSeal<Txid>;
38
39pub trait ExposedSeal:
40    Debug
41    + StrictDumb
42    + StrictEncode
43    + StrictDecode
44    + Eq
45    + Ord
46    + Copy
47    + Hash
48    + TxoSeal
49    + Conceal<Concealed = SecretSeal>
50{
51    #[inline]
52    fn to_output_seal(self) -> Option<OutputSeal> {
53        let outpoint = self.outpoint()?;
54        Some(ExplicitSeal::new(outpoint))
55    }
56
57    fn to_output_seal_or_default(self, witness_id: Txid) -> OutputSeal {
58        self.to_output_seal()
59            .unwrap_or(ExplicitSeal::new(self.outpoint_or(witness_id)))
60    }
61}
62
63impl ExposedSeal for GraphSeal {}
64
65impl ExposedSeal for GenesisSeal {}
66
67#[cfg(test)]
68mod test {
69    use amplify::hex::FromHex;
70    use bp::seals::txout::{BlindSeal, TxPtr};
71    use bp::Vout;
72
73    use super::*;
74
75    #[test]
76    fn secret_seal_is_sha256d() {
77        let reveal = BlindSeal {
78            blinding: 54683213134637,
79            txid: TxPtr::Txid(
80                Txid::from_hex("646ca5c1062619e2a2d60771c9dfd820551fb773e4dc8c4ed67965a8d1fae839")
81                    .unwrap(),
82            ),
83            vout: Vout::from(2),
84        };
85        let secret = reveal.to_secret_seal();
86        assert_eq!(
87            secret.to_string(),
88            "utxob:nBRVm39A-ioJydHE-ug2d90m-aZyfPI0-MCc0ZNM-oMXMs2O-opKQ7"
89        );
90        assert_eq!(reveal.to_secret_seal(), reveal.conceal())
91    }
92}