Skip to content

Commit

Permalink
Amend
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Aug 9, 2024
1 parent 1cda0b5 commit 81cd51b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ pub enum Bip322Error {
MalformedSignature, // wrong length
NotSigned,
InvalidSigHash, // only sighash All and Default supported
ScriptSpendP2TR, // only single key path spend supported
NotKeyPathSpend, // only single key path spend supported
}
22 changes: 14 additions & 8 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ fn extract_pub_key(address: &Address) -> Result<XOnlyPublicKey> {
return Err(Bip322Error::InvalidAddress);
}

// TODO: assert single key path spend

if let bitcoin::address::Payload::WitnessProgram(witness_program) = address.payload() {
if witness_program.version().to_num() == 1 && witness_program.program().len() == 32 {
Ok(XOnlyPublicKey::from_slice(witness_program.program().as_bytes()).unwrap())
} else {
Err(Bip322Error::ScriptSpendP2TR)
if witness_program.version().to_num() != 1 {
return Err(Bip322Error::InvalidAddress);
}

if witness_program.program().len() != 32 {
return Err(Bip322Error::NotKeyPathSpend);
}

Ok(
XOnlyPublicKey::from_slice(witness_program.program().as_bytes())
.expect("should extract an xonly public key"),
)
} else {
Err(Bip322Error::ScriptSpendP2TR)
Err(Bip322Error::InvalidAddress)
}
}

Expand Down Expand Up @@ -70,7 +75,8 @@ fn decode_and_verify(
)
.expect("signature hash should compute");

let message = Message::from_digest_slice(sighash.as_ref()).map_err(|_| Bip322Error::Invalid)?;
let message =
Message::from_digest_slice(sighash.as_ref()).expect("should be cryptographically secure hash");

Secp256k1::verification_only()
.verify_schnorr(&signature, &message, pub_key)
Expand Down

0 comments on commit 81cd51b

Please sign in to comment.