dbc/opret/
mod.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
22//! ScriptPubkey-based OP_RETURN commitments.
23
24mod tx;
25mod txout;
26mod spk;
27
28use bc::Tx;
29use commit_verify::mpc::Commitment;
30use commit_verify::{CommitmentProtocol, EmbedCommitVerify, EmbedVerifyError};
31use strict_encoding::{StrictDeserialize, StrictSerialize};
32
33use crate::proof::Method;
34use crate::{Proof, LIB_NAME_BPCORE};
35
36/// Marker non-instantiable enum defining LNPBP-12 taproot OP_RETURN (`tapret`)
37/// protocol.
38pub enum OpretFirst {}
39
40impl CommitmentProtocol for OpretFirst {}
41
42/// Errors during tapret commitment.
43#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
44#[cfg_attr(
45    feature = "serde",
46    derive(Serialize, Deserialize),
47    serde(crate = "serde_crate", rename_all = "camelCase")
48)]
49#[display(doc_comments)]
50pub enum OpretError {
51    /// transaction doesn't contain OP_RETURN output.
52    NoOpretOutput,
53
54    /// first OP_RETURN output inside the transaction already contains some
55    /// data.
56    InvalidOpretScript,
57}
58
59/// Empty type for use inside [`crate::Anchor`] for opret commitment scheme.
60#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
61#[derive(StrictType, StrictEncode, StrictDecode)]
62#[strict_type(lib = LIB_NAME_BPCORE)]
63#[cfg_attr(
64    feature = "serde",
65    derive(Serialize, Deserialize),
66    serde(crate = "serde_crate", rename_all = "camelCase")
67)]
68pub struct OpretProof(());
69
70impl StrictSerialize for OpretProof {}
71impl StrictDeserialize for OpretProof {}
72
73impl Proof<Method> for OpretProof {
74    type Error = EmbedVerifyError<OpretError>;
75
76    fn method(&self) -> Method { Method::OpretFirst }
77
78    fn verify(&self, msg: &Commitment, tx: &Tx) -> Result<(), EmbedVerifyError<OpretError>> {
79        tx.verify(msg, self)
80    }
81}