seals/
wtxout.rs

1// Bitcoin protocol single-use-seals library.
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//
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
22//! Witness-output enabled TxO-seals allow constructing graphs of seals, useful in protocols like
23//! RGB.
24
25use bc::{Outpoint, Vout};
26use commit_verify::{Sha256, StrictHash};
27
28use crate::{Noise, TxoSealExt};
29
30/// A single-use seal definition type allowing seals to point to the output of the same transaction
31/// (witness transaction) which commits to the message defining the seals.
32#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
33#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
34#[strict_type(lib = dbc::LIB_NAME_BPCORE, tags = custom, dumb = Self::Wout(strict_dumb!()))]
35#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
36pub enum WOutpoint {
37    /// A seal definition pointing to an output of a not-yet-existing witness transaction closing
38    /// some other seals, which will contain a commitment to this seal definition (witness
39    /// transaction).
40    #[display("~:{0}")]
41    #[strict_type(tag = 0)]
42    Wout(Vout),
43
44    /// A seal definition pointing to an output of an already existing transaction.
45    #[display(inner)]
46    #[strict_type(tag = 1)]
47    Extern(Outpoint),
48}
49
50/// A composed single-use seal definition type, which includes a primary and a fallback seal.
51///
52/// The type allows creation of seals pointing to the output of the same transaction (witness
53/// transaction) which commits to the message defining the seals.
54#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
55#[display("{primary}/{secondary}")]
56#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
57#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
58#[derive(CommitEncode)]
59#[commit_encode(strategy = strict, id = StrictHash)]
60#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
61pub struct WTxoSeal {
62    /// A primary seal definition.
63    pub primary: WOutpoint,
64    /// A fallback seal definition.
65    pub secondary: TxoSealExt,
66}
67
68impl WTxoSeal {
69    /// Creates a new witness-output-based seal definition without a fallback.
70    ///
71    /// # Arguments
72    ///
73    /// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
74    /// same output is used.
75    pub fn vout_no_fallback(vout: Vout, noise_engine: Sha256, nonce: u64) -> Self {
76        Self::with(WOutpoint::Wout(vout), noise_engine, nonce)
77    }
78
79    /// Creates a new external outpoint-based seal definition without a fallback.
80    ///
81    /// # Arguments
82    ///
83    /// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
84    /// same output is used.
85    pub fn no_fallback(outpoint: Outpoint, noise_engine: Sha256, nonce: u64) -> Self {
86        Self::with(WOutpoint::Extern(outpoint), noise_engine, nonce)
87    }
88
89    /// Creates a new seal definition without a fallback.
90    ///
91    /// # Arguments
92    ///
93    /// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
94    /// same output is used.
95    pub fn with(outpoint: WOutpoint, noise_engine: Sha256, nonce: u64) -> Self {
96        Self {
97            primary: outpoint,
98            secondary: TxoSealExt::Noise(Noise::with(outpoint, noise_engine, nonce)),
99        }
100    }
101}