use bitcoin::blockdata::opcodes::all::OP_RETURN;
use bitcoin::ScriptBuf;
use crate::commit_verify::mpc::Commitment;
use crate::commit_verify::{EmbedCommitProof, EmbedCommitVerify, EmbedVerifyError};
use crate::dbc::opret::{OpretError, OpretFirst, OpretProof};
impl EmbedCommitProof<Commitment, ScriptBuf, OpretFirst> for OpretProof {
fn restore_original_container(
&self,
commit_container: &ScriptBuf,
) -> Result<ScriptBuf, EmbedVerifyError<OpretError>> {
if !commit_container.is_op_return() {
return Err(OpretError::NoOpretOutput.into());
}
if commit_container.len() != 34 {
return Err(OpretError::InvalidOpretScript.into());
}
let mut script = ScriptBuf::new();
script.push_opcode(OP_RETURN);
Ok(script)
}
}
impl EmbedCommitVerify<Commitment, OpretFirst> for ScriptBuf {
type Proof = OpretProof;
type CommitError = OpretError;
fn embed_commit(&mut self, msg: &Commitment) -> Result<Self::Proof, Self::CommitError> {
if !self.is_op_return() {
return Err(OpretError::NoOpretOutput);
}
if self.len() != 1 {
return Err(OpretError::InvalidOpretScript);
}
*self = ScriptBuf::new_op_return(msg.to_byte_array());
Ok(OpretProof::default())
}
}