Skip to content

Commit f5fa688

Browse files
authored
Merge pull request #83 from MostroP2P/refactoring-sign-and-verify
Refactoring sign and verify_signature functions
2 parents 7e53875 + 605f288 commit f5fa688

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mostro-core"
3-
version = "0.6.25"
3+
version = "0.6.26"
44
edition = "2021"
55
license = "MIT"
66
authors = ["Francisco Calderón <negrunch@grunch.dev>"]

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,18 @@ mod test {
127127
Some(payload),
128128
));
129129
assert!(test_message.verify());
130+
let test_message_json = test_message.as_json().unwrap();
130131
// Message should be signed with the trade keys
131132
let trade_keys =
132133
Keys::parse("110e43647eae221ab1da33ddc17fd6ff423f2b2f49d809b9ffa40794a2ab996c")
133134
.unwrap();
134-
let sig = test_message.get_inner_message_kind().sign(&trade_keys);
135+
let sig = Message::sign(test_message_json.clone(), &trade_keys);
135136

136-
assert!(test_message
137-
.get_inner_message_kind()
138-
.verify_signature(trade_keys.public_key(), sig));
137+
assert!(Message::verify_signature(
138+
test_message_json,
139+
trade_keys.public_key(),
140+
sig
141+
));
139142
}
140143

141144
#[test]

src/message.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,27 @@ impl Message {
186186
| Message::Dm(m) => m.verify(),
187187
}
188188
}
189+
190+
pub fn sign(message: String, keys: &Keys) -> Signature {
191+
let hash: Sha256Hash = Sha256Hash::hash(message.as_bytes());
192+
let hash = hash.to_byte_array();
193+
let hash_str = hex::encode(hash);
194+
println!("hash en sign() en core: {:?}", hash_str);
195+
let message: BitcoinMessage = BitcoinMessage::from_digest(hash);
196+
197+
keys.sign_schnorr(&message)
198+
}
199+
200+
pub fn verify_signature(message: String, pubkey: PublicKey, sig: Signature) -> bool {
201+
// Create payload hash
202+
let hash: Sha256Hash = Sha256Hash::hash(message.as_bytes());
203+
let hash = hash.to_byte_array();
204+
let message: BitcoinMessage = BitcoinMessage::from_digest(hash);
205+
// Create a verification-only context for better performance
206+
let secp = Secp256k1::verification_only();
207+
// Verify signature
208+
pubkey.verify(&secp, &message, &sig).is_ok()
209+
}
189210
}
190211

191212
/// Use this Message to establish communication between users and Mostro
@@ -391,25 +412,4 @@ impl MessageKind {
391412
}
392413
(false, 0)
393414
}
394-
395-
pub fn sign(&self, keys: &Keys) -> Signature {
396-
let message = self.as_json().unwrap();
397-
let hash: Sha256Hash = Sha256Hash::hash(message.as_bytes());
398-
let hash = hash.to_byte_array();
399-
let message: BitcoinMessage = BitcoinMessage::from_digest(hash);
400-
401-
keys.sign_schnorr(&message)
402-
}
403-
404-
pub fn verify_signature(&self, pubkey: PublicKey, sig: Signature) -> bool {
405-
// Create message hash
406-
let message = self.as_json().unwrap();
407-
let hash: Sha256Hash = Sha256Hash::hash(message.as_bytes());
408-
let hash = hash.to_byte_array();
409-
let message: BitcoinMessage = BitcoinMessage::from_digest(hash);
410-
// Create a verification-only context for better performance
411-
let secp = Secp256k1::verification_only();
412-
// Verify signature
413-
pubkey.verify(&secp, &message, &sig).is_ok()
414-
}
415415
}

0 commit comments

Comments
 (0)