Skip to content

Commit

Permalink
Improving verify_content_signature()
Browse files Browse the repository at this point in the history
* Changed Secp256k1::new() to Secp256k1::verification_only() for better performance
* Moved the signature check earlier to fail fast
* Simplified the hash to message conversion by combining steps
* Improved code organization for better readability
  • Loading branch information
grunch committed Dec 4, 2024
1 parent c3cd110 commit 7d6cc8f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,19 @@ impl MessageKind {
Some(c) => c,
_ => return false,
};
// Get signature or return false if None
let sig = match self.get_signature() {
Some(s) => s,
_ => return false,
}; // Create message hash
let json: Value = json!(content);
let content_str: String = json.to_string();
let hash: Sha256Hash = Sha256Hash::hash(content_str.as_bytes());
let hash = hash.to_byte_array();
let message: BitcoinMessage = BitcoinMessage::from_digest(hash);
let sig = match self.get_signature() {
Some(s) => s,
_ => return false,
};
let secp = Secp256k1::new();
// Create a verification-only context for better performance
let secp = Secp256k1::verification_only();
// Verify signature
pubkey.verify(&secp, &message, sig).is_ok()
}
}

0 comments on commit 7d6cc8f

Please sign in to comment.