This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validator id and verification to echo and ready messages (#298
) Signed-off-by: Simon Paitrault <simon.paitrault@gmail.com> Co-authored-by: Marko Atanasievski <atanmarko@users.noreply.github.com> Co-authored-by: Simon Paitrault <simon.paitrault@gmail.com> Co-authored-by: Sébastien Dan <sebastien.dan@toposware.com>
- Loading branch information
1 parent
b2d7ff6
commit cb2eb7f
Showing
38 changed files
with
586 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use ethers::signers::Signer; | ||
use ethers::signers::{LocalWallet, WalletError}; | ||
use ethers::types::{RecoveryMessage, SignatureError}; | ||
use ethers::utils::hash_message; | ||
use std::sync::Arc; | ||
use thiserror::Error; | ||
|
||
pub use ethers::types::{Address, Signature, H160}; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum MessageSignerError { | ||
#[error("Unable to parse private key")] | ||
PrivateKeyParsing, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MessageSigner { | ||
pub public_address: Address, | ||
wallet: LocalWallet, | ||
} | ||
|
||
impl MessageSigner { | ||
pub fn new(private_key: &str) -> Result<Arc<Self>, MessageSignerError> { | ||
let wallet: LocalWallet = private_key | ||
.parse() | ||
.map_err(|_| MessageSignerError::PrivateKeyParsing)?; | ||
|
||
Ok(Arc::new(Self { | ||
public_address: wallet.address(), | ||
wallet, | ||
})) | ||
} | ||
|
||
pub fn sign_message(&self, payload: &[u8]) -> Result<Signature, WalletError> { | ||
let hash = hash_message(payload); | ||
|
||
LocalWallet::sign_hash(&self.wallet, hash) | ||
} | ||
|
||
pub fn verify_signature( | ||
&self, | ||
signature: Signature, | ||
payload: &[u8], | ||
public_key: Address, | ||
) -> Result<(), SignatureError> { | ||
let message: RecoveryMessage = payload.into(); | ||
|
||
signature.verify(message, public_key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use rstest::*; | ||
use topos_core::uci::CertificateId; | ||
use topos_crypto::messages::MessageSigner; | ||
use topos_tce_transport::ValidatorId; | ||
|
||
#[rstest] | ||
pub fn test_signing_messages() { | ||
let message_signer_sender = | ||
MessageSigner::new("122f3ae6ade1fd136b292cea4f6243c7811160352c8821528547a1fe7c459daf") | ||
.unwrap(); | ||
let validator_id_sender = ValidatorId::from(message_signer_sender.public_address); | ||
let certificate_id = CertificateId::from_array([0u8; 32]); | ||
|
||
let mut payload = Vec::new(); | ||
payload.extend_from_slice(certificate_id.as_array()); | ||
payload.extend_from_slice(validator_id_sender.as_bytes()); | ||
|
||
let signature = message_signer_sender | ||
.sign_message(&payload) | ||
.expect("Cannot create Signature"); | ||
|
||
let message_signer_receiver = | ||
MessageSigner::new("a2e33a9bad88f7b7568228f51d5274c471a9217162d46f1533b6a290f0be1baf") | ||
.unwrap(); | ||
|
||
let verify = message_signer_receiver.verify_signature( | ||
signature, | ||
&payload, | ||
validator_id_sender.address(), | ||
); | ||
|
||
assert!(verify.is_ok()); | ||
} | ||
|
||
#[rstest] | ||
pub fn fails_to_verify_with_own_public_address() { | ||
let message_signer_sender = | ||
MessageSigner::new("122f3ae6ade1fd136b292cea4f6243c7811160352c8821528547a1fe7c459daf") | ||
.unwrap(); | ||
let validator_id_sender = ValidatorId::from(message_signer_sender.public_address); | ||
let certificate_id = CertificateId::from_array([0u8; 32]); | ||
|
||
let mut payload = Vec::new(); | ||
payload.extend_from_slice(certificate_id.as_array()); | ||
payload.extend_from_slice(validator_id_sender.as_bytes()); | ||
|
||
let signature = message_signer_sender | ||
.sign_message(&payload) | ||
.expect("Cannot create Signature"); | ||
|
||
let message_signer_receiver = | ||
MessageSigner::new("a2e33a9bad88f7b7568228f51d5274c471a9217162d46f1533b6a290f0be1baf") | ||
.unwrap(); | ||
let validator_id_receiver = ValidatorId::from(message_signer_receiver.public_address); | ||
|
||
let verify = message_signer_receiver.verify_signature( | ||
signature, | ||
&payload, | ||
validator_id_receiver.address(), | ||
); | ||
|
||
assert!(verify.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.