dbc/opret/
spk.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 bc::opcodes::OP_RETURN;
23use bc::ScriptPubkey;
24use commit_verify::mpc::Commitment;
25use commit_verify::{EmbedCommitProof, EmbedCommitVerify, EmbedVerifyError};
26
27use crate::opret::{OpretError, OpretFirst, OpretProof};
28
29impl EmbedCommitProof<Commitment, ScriptPubkey, OpretFirst> for OpretProof {
30    fn restore_original_container(
31        &self,
32        commit_container: &ScriptPubkey,
33    ) -> Result<ScriptPubkey, EmbedVerifyError<OpretError>> {
34        if !commit_container.is_op_return() {
35            return Err(OpretError::NoOpretOutput.into());
36        }
37        if commit_container.len() != 34 {
38            return Err(OpretError::InvalidOpretScript.into());
39        }
40        Ok(ScriptPubkey::from_checked(vec![OP_RETURN]))
41    }
42}
43
44impl EmbedCommitVerify<Commitment, OpretFirst> for ScriptPubkey {
45    type Proof = OpretProof;
46    type CommitError = OpretError;
47
48    fn embed_commit(&mut self, msg: &Commitment) -> Result<Self::Proof, Self::CommitError> {
49        if !self.is_op_return() {
50            return Err(OpretError::NoOpretOutput);
51        }
52        if self.len() != 1 {
53            return Err(OpretError::InvalidOpretScript);
54        }
55        *self = ScriptPubkey::op_return(msg.as_slice());
56        Ok(OpretProof::default())
57    }
58}