|
| 1 | +extern crate bitcoin_hashes; |
| 2 | +extern crate secp256k1; |
| 3 | + |
| 4 | +use bitcoin_hashes::{sha256, Hash}; |
| 5 | +use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signature, Signing, Verification}; |
| 6 | + |
| 7 | +fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubkey: [u8; 33]) -> Result<bool, Error> { |
| 8 | + let msg = sha256::Hash::hash(msg); |
| 9 | + let msg = Message::from_slice(&msg)?; |
| 10 | + let sig = Signature::from_compact(&sig)?; |
| 11 | + let pubkey = PublicKey::from_slice(&pubkey)?; |
| 12 | + |
| 13 | + Ok(secp.verify(&msg, &sig, &pubkey).is_ok()) |
| 14 | +} |
| 15 | + |
| 16 | +fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<Signature, Error> { |
| 17 | + let msg = sha256::Hash::hash(msg); |
| 18 | + let msg = Message::from_slice(&msg)?; |
| 19 | + let seckey = SecretKey::from_slice(&seckey)?; |
| 20 | + Ok(secp.sign(&msg, &seckey)) |
| 21 | +} |
| 22 | + |
| 23 | +fn main() { |
| 24 | + let secp = Secp256k1::new(); |
| 25 | + |
| 26 | + let seckey = [59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28]; |
| 27 | + let pubkey = [2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54]; |
| 28 | + let msg = b"This is some message"; |
| 29 | + |
| 30 | + let signature = sign(&secp, msg, seckey).unwrap(); |
| 31 | + |
| 32 | + let serialize_sig = signature.serialize_compact(); |
| 33 | + |
| 34 | + assert!(verify(&secp, msg, serialize_sig, pubkey).unwrap()); |
| 35 | +} |
0 commit comments