Skip to content

Commit

Permalink
fix validation APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 29, 2023
1 parent 29173ab commit 13b1a42
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 43 deletions.
13 changes: 5 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ license = "Apache-2.0"
amplify = "4.5.0"
strict_encoding = "2.6.1"
strict_types = "1.6.3"
commit_verify = "0.10.6"
single_use_seals = "0.10.1"
commit_verify = "0.11.0-beta.1"
single_use_seals = "0.11.0-beta.1"
bp-consensus = { version = "0.10.11", path = "consensus" }
bp-dbc = { version = "0.10.11", path = "./dbc" }
bp-seals = { version = "0.10.11", path = "./seals" }
Expand Down Expand Up @@ -81,3 +81,7 @@ stl = ["strict_types", "strict_types/base64", "bp-consensus/stl", "commit_verify

[package.metadata.docs.rs]
features = [ "all" ]

[patch.crates-io]
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.11" }
single_use_seals = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.11" }
58 changes: 49 additions & 9 deletions dbc/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use std::cmp::Ordering;
use amplify::{Bytes32, Wrapper};
use bc::{ScriptPubkey, Tx, Txid};
use commit_verify::mpc::{self, Message, ProtocolId};
use commit_verify::{CommitmentId, ConvolveCommitProof};
use commit_verify::{CommitmentId, ConvolveCommitProof, ConvolveVerifyError};
use strict_encoding::{StrictDumb, StrictEncode};

use crate::tapret::{TapretError, TapretProof};
use crate::tapret::TapretProof;
use crate::LIB_NAME_BPCORE;

/// Default depth of LNPBP-4 commitment tree
Expand Down Expand Up @@ -65,9 +65,9 @@ pub struct AnchorId(
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum VerifyError {
/// Tapret commitment verification failure.
/// invalid deterministic bitcoin commitment. Details: {0}
#[from]
Tapret(TapretError),
Dbc(AnchorError),

/// LNPBP-4 invalid proof. Details: {0}
#[from]
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Anchor<mpc::MerkleProof> {
protocol_id: impl Into<ProtocolId>,
message: Message,
tx: &Tx,
) -> Result<bool, VerifyError> {
) -> Result<(), VerifyError> {

Check warning on line 177 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L177

Added line #L177 was not covered by tests
self.dbc_proof
.verify(&self.mpc_proof.convolve(protocol_id.into(), message)?, tx)
.map_err(VerifyError::from)
Expand Down Expand Up @@ -236,6 +236,35 @@ impl Anchor<mpc::MerkleBlock> {
}
}

/// Errors covering failed anchor validation.
#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]

Check warning on line 240 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L240

Added line #L240 was not covered by tests
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 243 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L243

Added line #L243 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[display(doc_comments)]
pub enum AnchorError {
/// witness transaction {txid} contains invalid OP_RETURN commitment
/// {present:x} instead of {expected:x}.
OpretMismatch {
/// Transaction id
txid: Txid,
/// Commitment from the first OP_RETURN transaction output
present: ScriptPubkey,
/// Expected commitment absent in the first OP_RETURN transaction output
expected: ScriptPubkey,
},

/// witness transaction {0} does not contain any OP_RETURN commitment
/// required by the seal definition.
OpretAbsent(Txid),

#[from]
/// witness transaction does not contain a valid tapret commitment. {0}.
Tapret(ConvolveVerifyError),
}

/// Type and type-specific proof information of a deterministic bitcoin
/// commitment.
#[derive(Clone, PartialEq, Eq, Debug)]
Expand All @@ -260,17 +289,28 @@ pub enum Proof {

impl Proof {
/// Verifies validity of the proof.
pub fn verify(&self, msg: &mpc::Commitment, tx: &Tx) -> Result<bool, TapretError> {
pub fn verify(&self, msg: &mpc::Commitment, tx: &Tx) -> Result<(), AnchorError> {

Check warning on line 292 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L292

Added line #L292 was not covered by tests
match self {
Proof::OpretFirst => {
for txout in &tx.outputs {
if txout.script_pubkey.is_op_return() {
return Ok(txout.script_pubkey == ScriptPubkey::op_return(msg.as_slice()));
let expected = ScriptPubkey::op_return(msg.as_slice());
if txout.script_pubkey == expected {
return Ok(());

Check warning on line 299 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L297-L299

Added lines #L297 - L299 were not covered by tests
} else {
return Err(AnchorError::OpretMismatch {
txid: tx.txid(),
present: txout.script_pubkey.clone(),
expected,
});

Check warning on line 305 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L301-L305

Added lines #L301 - L305 were not covered by tests
}
}
}
Ok(false)
Err(AnchorError::OpretAbsent(tx.txid()))

Check warning on line 309 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L309

Added line #L309 was not covered by tests
}
Proof::TapretFirst(proof) => {
ConvolveCommitProof::<_, Tx, _>::verify(proof, msg, tx).map_err(AnchorError::from)

Check warning on line 312 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L311-L312

Added lines #L311 - L312 were not covered by tests
}
Proof::TapretFirst(proof) => ConvolveCommitProof::<_, Tx, _>::verify(proof, msg, tx),
}
}
}
24 changes: 6 additions & 18 deletions dbc/src/tapret/xonlypk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,8 @@ mod test {
internal_pk
});

assert!(
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(
&proof, &msg, &outer_key
)
.unwrap()
);
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(&proof, &msg, &outer_key)
.unwrap();
}

#[test]
Expand All @@ -153,12 +149,8 @@ mod test {
internal_pk
});

assert!(
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(
&proof, &msg, &outer_key
)
.unwrap()
);
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(&proof, &msg, &outer_key)
.unwrap();
}

#[test]
Expand All @@ -182,11 +174,7 @@ mod test {
internal_pk
});

assert!(
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(
&proof, &msg, &outer_key
)
.unwrap()
);
ConvolveCommitProof::<Commitment, InternalPk, Lnpbp12>::verify(&proof, &msg, &outer_key)
.unwrap();
}
}
8 changes: 4 additions & 4 deletions seals/src/txout/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// limitations under the License.

use bc::Outpoint;
use dbc::anchor::AnchorError;

/// Seal verification errors.
#[derive(Clone, PartialEq, Eq, Debug, Display, From, Error)]
Expand All @@ -39,11 +40,10 @@ pub enum VerifyError {
/// seal lacks witness transaction id information.
NoWitnessTxid,

/// tapret commitment is invalid.
///
/// Details: {0}
/// invalid anchor.
#[from]
InvalidTapretCommitment(dbc::tapret::TapretError),
#[display(inner)]
InvalidAnchor(AnchorError),
}

/// Error happening if the seal data holds only witness transaction output
Expand Down
4 changes: 2 additions & 2 deletions seals/src/txout/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<Seal: TxoSeal> SealWitness<Seal> for Witness {
type Message = mpc::Commitment;
type Error = VerifyError;

fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<bool, Self::Error> {
fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error> {

Check warning on line 59 in seals/src/txout/witness.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout/witness.rs#L59

Added line #L59 was not covered by tests
// 1. The seal must match tx inputs
let outpoint = seal.outpoint().ok_or(VerifyError::NoWitnessTxid)?;
if !self
Expand All @@ -76,7 +76,7 @@ impl<Seal: TxoSeal> SealWitness<Seal> for Witness {
&self,
seals: impl IntoIterator<Item = &'seal Seal>,
msg: &Self::Message,
) -> Result<bool, Self::Error>
) -> Result<(), Self::Error>

Check warning on line 79 in seals/src/txout/witness.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout/witness.rs#L79

Added line #L79 was not covered by tests
where
Seal: 'seal,
{
Expand Down

0 comments on commit 13b1a42

Please sign in to comment.