diff --git a/cli/Cargo.lock b/cli/Cargo.lock index 3dfc63f..2388344 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -603,6 +603,7 @@ dependencies = [ "serde_json", "strum", "strum_macros", + "tempfile", "thiserror", "tokio", "tokio-stream", diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 27b207f..8dcba94 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -704,6 +704,7 @@ dependencies = [ "strum", "strum_macros", "tempdir", + "tempfile", "thiserror", "tokio", "tokio-stream", diff --git a/lib/core/Cargo.toml b/lib/core/Cargo.toml index f8daa80..37d420d 100644 --- a/lib/core/Cargo.toml +++ b/lib/core/Cargo.toml @@ -48,6 +48,7 @@ reqwest = { version = "=0.11.20", features = ["json"] } electrum-client = { version = "0.19.0" } zbase32 = "0.1.2" x509-parser = { version = "0.16.0" } +tempfile = "3" [dev-dependencies] lazy_static = "1.5.0" diff --git a/lib/core/src/wallet.rs b/lib/core/src/wallet.rs index 68e59c6..c12f1d0 100644 --- a/lib/core/src/wallet.rs +++ b/lib/core/src/wallet.rs @@ -242,7 +242,7 @@ impl OnchainWallet for LiquidOnchainWallet { engine.write_all(LN_MESSAGE_PREFIX)?; engine.write_all(message.as_bytes())?; let hashed_msg = sha256::Hash::from_engine(engine); - let double_hashed_msg = Message::from_digest(hashed_msg.into_inner()); + let double_hashed_msg = Message::from_digest(sha256::Hash::hash(&hashed_msg).into_inner()); // Get message signature and encode to zbase32 let recoverable_sig = self.signer.sign_ecdsa_recoverable(&double_hashed_msg)?; Ok(zbase32::encode_full_bytes(recoverable_sig.as_slice())) @@ -253,3 +253,73 @@ impl OnchainWallet for LiquidOnchainWallet { Ok(verify(message.as_bytes(), signature, &pk)) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::model::Config; + use crate::signer::SdkSigner; + use crate::wallet::LiquidOnchainWallet; + use tempfile::TempDir; + + #[tokio::test] + async fn test_sign_and_check_message() { + let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; + let sdk_signer: Box = Box::new(SdkSigner::new(mnemonic, false).unwrap()); + let sdk_signer = Arc::new(sdk_signer); + + let config = Config::testnet(); + + // Create a temporary directory for working_dir + let temp_dir = TempDir::new().unwrap(); + let working_dir = temp_dir.path().to_str().unwrap().to_string(); + + let wallet: Arc = + Arc::new(LiquidOnchainWallet::new(sdk_signer.clone(), config, &working_dir).unwrap()); + + // Test message + let message = "Hello, Liquid!"; + + // Sign the message + let signature = wallet.sign_message(message).unwrap(); + + // Get the public key + let pubkey = wallet.pubkey().unwrap(); + + // Check the message + let is_valid = wallet.check_message(message, &pubkey, &signature).unwrap(); + assert!(is_valid, "Message signature should be valid"); + + // Check with an incorrect message + let incorrect_message = "Wrong message"; + let is_invalid = wallet + .check_message(incorrect_message, &pubkey, &signature) + .unwrap(); + assert!( + !is_invalid, + "Message signature should be invalid for incorrect message" + ); + + // Check with an incorrect public key + let incorrect_pubkey = "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc"; + let is_invalid = wallet + .check_message(message, incorrect_pubkey, &signature) + .unwrap(); + assert!( + !is_invalid, + "Message signature should be invalid for incorrect public key" + ); + + // Check with an incorrect signature + let incorrect_signature = zbase32::encode_full_bytes(&[0; 65]); + let is_invalid = wallet + .check_message(message, &pubkey, &incorrect_signature) + .unwrap(); + assert!( + !is_invalid, + "Message signature should be invalid for incorrect signature" + ); + + // The temporary directory will be automatically deleted when temp_dir goes out of scope + } +}