From 5cb77d393a6c8b4b20c6179374cb6e98fafce8b6 Mon Sep 17 00:00:00 2001 From: Matteo Muraca <56828990+muraca@users.noreply.github.com> Date: Mon, 20 May 2024 16:56:48 +0000 Subject: [PATCH 1/6] Implemented `sign_prehashed` for `eth::Keypair` --- signer/src/eth.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/signer/src/eth.rs b/signer/src/eth.rs index 63d23bf384..0fa394580a 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -106,11 +106,15 @@ impl Keypair { /// Signs an arbitrary message payload. pub fn sign(&self, signer_payload: &[u8]) -> Signature { - let message_hash = keccak(signer_payload); + self.sign_prehashed(&keccak(signer_payload)) + } + + /// Signs a pre-hashed message. + pub fn sign_prehashed(&self, message_hash: &H256) -> Signature { let wrapped = Message::from_digest_slice(message_hash.as_bytes()).expect("Message is 32 bytes; qed"); Signature(ecdsa::internal::sign(&self.0 .0.secret_key(), &wrapped)) - } + } } /// A derivation path. This can be parsed from a valid derivation path string like From 38634f147e1c9df8f1d6c7ead7222b43d9f938d9 Mon Sep 17 00:00:00 2001 From: Matteo Muraca <56828990+muraca@users.noreply.github.com> Date: Mon, 20 May 2024 17:02:33 +0000 Subject: [PATCH 2/6] missing import --- signer/src/eth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signer/src/eth.rs b/signer/src/eth.rs index 0fa394580a..f3bd37843f 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -10,7 +10,7 @@ use alloc::string::String; use core::fmt::{Display, Formatter}; use core::str::FromStr; use derive_more::Display; -use keccak_hash::keccak; +use keccak_hash::{keccak, H256}; use secp256k1::Message; const SECRET_KEY_LENGTH: usize = 32; From 9763bbd71bf7b52989a242ca8964dc49801cda19 Mon Sep 17 00:00:00 2001 From: Matteo Muraca <56828990+muraca@users.noreply.github.com> Date: Mon, 20 May 2024 17:02:58 +0000 Subject: [PATCH 3/6] fmt --- signer/src/eth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signer/src/eth.rs b/signer/src/eth.rs index f3bd37843f..952a98867c 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -114,7 +114,7 @@ impl Keypair { let wrapped = Message::from_digest_slice(message_hash.as_bytes()).expect("Message is 32 bytes; qed"); Signature(ecdsa::internal::sign(&self.0 .0.secret_key(), &wrapped)) - } + } } /// A derivation path. This can be parsed from a valid derivation path string like From 6973e442e246db91c61953f8c9a7fdd55e0d1a34 Mon Sep 17 00:00:00 2001 From: Matteo Muraca <56828990+muraca@users.noreply.github.com> Date: Wed, 22 May 2024 11:11:18 +0200 Subject: [PATCH 4/6] Implemented `sign_prehashed` for `ecdsa::Keypair` --- signer/src/ecdsa.rs | 6 +++++- signer/src/eth.rs | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/signer/src/ecdsa.rs b/signer/src/ecdsa.rs index 8531d4b51c..f9b6b759cf 100644 --- a/signer/src/ecdsa.rs +++ b/signer/src/ecdsa.rs @@ -160,7 +160,11 @@ impl Keypair { /// Sign some message. These bytes can be used directly in a Substrate `MultiSignature::Ecdsa(..)`. pub fn sign(&self, message: &[u8]) -> Signature { - let message_hash = sp_crypto_hashing::blake2_256(message); + self.sign_prehashed(&sp_crypto_hashing::blake2_256(message)); + } + + /// Signs a pre-hashed message. + pub fn sign_prehashed(&self, message_hash: &H256) -> Signature { let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed"); Signature(internal::sign(&self.0.secret_key(), &wrapped)) } diff --git a/signer/src/eth.rs b/signer/src/eth.rs index 952a98867c..aec55334d2 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -111,9 +111,7 @@ impl Keypair { /// Signs a pre-hashed message. pub fn sign_prehashed(&self, message_hash: &H256) -> Signature { - let wrapped = - Message::from_digest_slice(message_hash.as_bytes()).expect("Message is 32 bytes; qed"); - Signature(ecdsa::internal::sign(&self.0 .0.secret_key(), &wrapped)) + self.0.sign_prehashed(message_hash).into() } } @@ -171,6 +169,12 @@ impl AsRef<[u8; 65]> for Signature { } } +impl From for Signature { + fn from(s: ecdsa::Signature) -> Self { + Self(s.0) + } +} + /// A 20-byte cryptographic identifier. #[derive(Debug, Copy, Clone, PartialEq, Eq, codec::Encode)] pub struct AccountId20(pub [u8; 20]); From c1a4c80ad13b0f7f9d85b2f6afaf708da43e471d Mon Sep 17 00:00:00 2001 From: Matteo Muraca <56828990+muraca@users.noreply.github.com> Date: Wed, 22 May 2024 11:16:55 +0200 Subject: [PATCH 5/6] use `[u8; 32]` type --- signer/src/ecdsa.rs | 2 +- signer/src/eth.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/signer/src/ecdsa.rs b/signer/src/ecdsa.rs index f9b6b759cf..e3775dcf12 100644 --- a/signer/src/ecdsa.rs +++ b/signer/src/ecdsa.rs @@ -164,7 +164,7 @@ impl Keypair { } /// Signs a pre-hashed message. - pub fn sign_prehashed(&self, message_hash: &H256) -> Signature { + pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Signature { let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed"); Signature(internal::sign(&self.0.secret_key(), &wrapped)) } diff --git a/signer/src/eth.rs b/signer/src/eth.rs index aec55334d2..932331adbc 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -10,7 +10,7 @@ use alloc::string::String; use core::fmt::{Display, Formatter}; use core::str::FromStr; use derive_more::Display; -use keccak_hash::{keccak, H256}; +use keccak_hash::keccak; use secp256k1::Message; const SECRET_KEY_LENGTH: usize = 32; @@ -106,11 +106,11 @@ impl Keypair { /// Signs an arbitrary message payload. pub fn sign(&self, signer_payload: &[u8]) -> Signature { - self.sign_prehashed(&keccak(signer_payload)) + self.sign_prehashed(&keccak(signer_payload).0) } /// Signs a pre-hashed message. - pub fn sign_prehashed(&self, message_hash: &H256) -> Signature { + pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Signature { self.0.sign_prehashed(message_hash).into() } } From 60c447c4ddeab064e8c0a0242914086c437b6b59 Mon Sep 17 00:00:00 2001 From: Matteo Muraca Date: Wed, 22 May 2024 11:24:38 +0200 Subject: [PATCH 6/6] ditched From impl --- signer/src/ecdsa.rs | 4 ++-- signer/src/eth.rs | 8 +------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/signer/src/ecdsa.rs b/signer/src/ecdsa.rs index e3775dcf12..e5b6c4507c 100644 --- a/signer/src/ecdsa.rs +++ b/signer/src/ecdsa.rs @@ -160,12 +160,12 @@ impl Keypair { /// Sign some message. These bytes can be used directly in a Substrate `MultiSignature::Ecdsa(..)`. pub fn sign(&self, message: &[u8]) -> Signature { - self.sign_prehashed(&sp_crypto_hashing::blake2_256(message)); + self.sign_prehashed(&sp_crypto_hashing::blake2_256(message)) } /// Signs a pre-hashed message. pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Signature { - let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed"); + let wrapped = Message::from_digest_slice(message_hash).expect("Message is 32 bytes; qed"); Signature(internal::sign(&self.0.secret_key(), &wrapped)) } } diff --git a/signer/src/eth.rs b/signer/src/eth.rs index 932331adbc..2096c8a192 100644 --- a/signer/src/eth.rs +++ b/signer/src/eth.rs @@ -111,7 +111,7 @@ impl Keypair { /// Signs a pre-hashed message. pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Signature { - self.0.sign_prehashed(message_hash).into() + Signature(self.0.sign_prehashed(message_hash).0) } } @@ -169,12 +169,6 @@ impl AsRef<[u8; 65]> for Signature { } } -impl From for Signature { - fn from(s: ecdsa::Signature) -> Self { - Self(s.0) - } -} - /// A 20-byte cryptographic identifier. #[derive(Debug, Copy, Clone, PartialEq, Eq, codec::Encode)] pub struct AccountId20(pub [u8; 20]);