From e43b8d8ef49c84fa0f5303287ad03bd9229584f2 Mon Sep 17 00:00:00 2001 From: peg Date: Wed, 8 Nov 2023 10:40:17 +0100 Subject: [PATCH 1/3] Add function to create a SignedMessage without using types from sp-core version 6 --- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0492253..f8c7481 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -201,6 +201,17 @@ impl SignedMessage { }) } + /// Creates a `SignedMessage` without needing any types from sp-core + // This is useful for environments using newer versions of sp-core + pub fn new_with_keypair_seed( + sr25519_keypair_seed: &[u8; 32], + msg: Vec, + recip: &PublicKey, + ) -> Result { + let pair = sr25519::Pair::from_seed(sr25519_keypair_seed); + Self::new(&pair, &Bytes(msg), recip) + } + /// Decrypts the message and returns the plaintext. pub fn decrypt(&self, sk: &sr25519::Pair) -> Result, ValidationErr> { let mut static_secret = derive_static_secret(sk); @@ -342,4 +353,39 @@ mod tests { // Check the encrypted message != the plaintext. assert_ne!(encrypted_message.msg, plaintext); } + + #[test] + fn test_new_with_seed() { + let plaintext = vec![69, 42, 0]; + + let mnemonic = new_mnemonic(); + let (_pair, alice_seed) = + ::from_phrase(mnemonic.phrase(), None).unwrap(); + + let bob = mnemonic_to_pair(&new_mnemonic()).unwrap(); + let bob_secret = derive_static_secret(&bob); + let bob_public_key = PublicKey::from(&bob_secret); + + // Test encryption & signing. + let encrypt_result = + SignedMessage::new_with_keypair_seed(&alice_seed, plaintext.clone(), &bob_public_key); + // Assert no error received in encryption. + assert!(encrypt_result.is_ok()); + let encrypted_message = encrypt_result.unwrap(); + + // Test signature validity + assert!(encrypted_message.verify()); + + // Test decryption + let decrypt_result = encrypted_message.decrypt(&bob); + // Assert no error received in decryption. + assert!(decrypt_result.is_ok()); + let decrypted_result = decrypt_result.unwrap(); + + // Check the decrypted message equals the plaintext. + assert_eq!(decrypted_result, plaintext); + + // Check the encrypted message != the plaintext. + assert_ne!(encrypted_message.msg, Bytes(plaintext)); + } } From 4bc2cbcf1be217b519adb622b214fd33b512a3eb Mon Sep 17 00:00:00 2001 From: peg Date: Mon, 20 Nov 2023 08:44:59 +0100 Subject: [PATCH 2/3] Apply suggestion changing comment to doccomment Co-authored-by: Hernando Castano --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f8c7481..71ddd47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -202,7 +202,8 @@ impl SignedMessage { } /// Creates a `SignedMessage` without needing any types from sp-core - // This is useful for environments using newer versions of sp-core + /// + /// This is useful for environments using newer versions of sp-core pub fn new_with_keypair_seed( sr25519_keypair_seed: &[u8; 32], msg: Vec, From 3c7b6a9e9a9383425f9abc5a61580b3d352705e4 Mon Sep 17 00:00:00 2001 From: peg Date: Mon, 20 Nov 2023 08:47:58 +0100 Subject: [PATCH 3/3] Rename variable following review --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 71ddd47..5439d38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -207,10 +207,10 @@ impl SignedMessage { pub fn new_with_keypair_seed( sr25519_keypair_seed: &[u8; 32], msg: Vec, - recip: &PublicKey, + recipient: &PublicKey, ) -> Result { let pair = sr25519::Pair::from_seed(sr25519_keypair_seed); - Self::new(&pair, &Bytes(msg), recip) + Self::new(&pair, &Bytes(msg), recipient) } /// Decrypts the message and returns the plaintext.