dbc/
proof.rs

1// Deterministic bitcoin commitments 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
22use std::error::Error;
23use std::fmt::Debug;
24use std::str::FromStr;
25
26use bc::Tx;
27use commit_verify::mpc;
28use strict_encoding::{StrictDecode, StrictDeserialize, StrictDumb, StrictEncode, StrictSerialize};
29
30use crate::LIB_NAME_BPCORE;
31
32/// Trait defining DBC method - or enumberation of allowed DBC methods used by
33/// proofs, single-use-seals etc.
34pub trait DbcMethod:
35    Copy
36    + Eq
37    + Ord
38    + std::hash::Hash
39    + strict_encoding::StrictDumb
40    + strict_encoding::StrictEncode
41    + strict_encoding::StrictDecode
42{
43}
44
45/// wrong deterministic bitcoin commitment closing method id '{0}'.
46#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
47#[display(doc_comments)]
48pub struct MethodParseError(pub String);
49
50/// Method of DBC construction.
51#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
52#[cfg_attr(
53    feature = "serde",
54    derive(Serialize, Deserialize),
55    serde(crate = "serde_crate", rename_all = "camelCase")
56)]
57#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
58#[strict_type(lib = LIB_NAME_BPCORE, tags = repr, into_u8, try_from_u8)]
59#[repr(u8)]
60pub enum Method {
61    /// OP_RETURN commitment present in the first OP_RETURN-containing
62    /// transaction output.
63    #[display("opret1st")]
64    #[strict_type(dumb)]
65    OpretFirst = 0x00,
66
67    /// Taproot-based OP_RETURN commitment present in the first Taproot
68    /// transaction output.
69    #[display("tapret1st")]
70    TapretFirst = 0x01,
71}
72
73impl DbcMethod for Method {}
74
75impl FromStr for Method {
76    type Err = MethodParseError;
77
78    fn from_str(s: &str) -> Result<Self, Self::Err> {
79        Ok(match s.to_lowercase() {
80            s if s == Method::OpretFirst.to_string() => Method::OpretFirst,
81            s if s == Method::TapretFirst.to_string() => Method::TapretFirst,
82            _ => return Err(MethodParseError(s.to_owned())),
83        })
84    }
85}
86
87/// Deterministic bitcoin commitment proof types.
88pub trait Proof<M: DbcMethod = Method>:
89    Clone + Eq + Debug + StrictSerialize + StrictDeserialize + StrictDumb
90{
91    /// Verification error.
92    type Error: Error;
93
94    /// Returns a single-use seal closing method used by the DBC proof.
95    fn method(&self) -> M;
96
97    /// Verifies DBC proof against the provided transaction.
98    fn verify(&self, msg: &mpc::Commitment, tx: &Tx) -> Result<(), Self::Error>;
99}