From 2334b58e58c11a7d3d23e8f3ef6903b8f642d241 Mon Sep 17 00:00:00 2001 From: Andrew Burger Date: Thu, 14 Nov 2024 17:36:02 +0100 Subject: [PATCH] adding generate pop to all crypto types --- .../application-crypto/src/bandersnatch.rs | 7 +++++++ .../primitives/application-crypto/src/bls381.rs | 7 +++++++ .../primitives/application-crypto/src/ecdsa.rs | 7 +++++++ .../application-crypto/src/ecdsa_bls381.rs | 13 ++++++++++--- .../primitives/application-crypto/src/ed25519.rs | 12 ++++++------ .../primitives/application-crypto/src/sr25519.rs | 7 +++++++ 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/substrate/primitives/application-crypto/src/bandersnatch.rs b/substrate/primitives/application-crypto/src/bandersnatch.rs index 75764aa2659e..7cb58983ebdd 100644 --- a/substrate/primitives/application-crypto/src/bandersnatch.rs +++ b/substrate/primitives/application-crypto/src/bandersnatch.rs @@ -54,6 +54,13 @@ impl RuntimePublic for Public { false } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + let pub_key_as_bytes = self.to_raw_vec(); + let pop_context_tag: &[u8] = b"POP_"; + let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); + sp_io::crypto::bandersnatch_sign(key_type, self, pop_statement.as_slice()) + } + fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); diff --git a/substrate/primitives/application-crypto/src/bls381.rs b/substrate/primitives/application-crypto/src/bls381.rs index 04377b0e59cd..3d5dbbf63d59 100644 --- a/substrate/primitives/application-crypto/src/bls381.rs +++ b/substrate/primitives/application-crypto/src/bls381.rs @@ -55,6 +55,13 @@ impl RuntimePublic for Public { false } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + let pub_key_as_bytes = self.to_raw_vec(); + let pop_context_tag: &[u8] = b"POP_"; + let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); + sp_io::crypto::bls381_sign(key_type, self, pop_statement.as_slice()) + } + fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); diff --git a/substrate/primitives/application-crypto/src/ecdsa.rs b/substrate/primitives/application-crypto/src/ecdsa.rs index 5548939d27fd..9e50a3de4fcc 100644 --- a/substrate/primitives/application-crypto/src/ecdsa.rs +++ b/substrate/primitives/application-crypto/src/ecdsa.rs @@ -52,6 +52,13 @@ impl RuntimePublic for Public { sp_io::crypto::ecdsa_verify(signature, msg.as_ref(), self) } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + let pub_key_as_bytes = self.to_raw_vec(); + let pop_context_tag: &[u8] = b"POP_"; + let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); + sp_io::crypto::ecdsa_sign(key_type, self, pop_statement.as_slice()) + } + fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); diff --git a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs index 925113e88843..9c8864e3dbdf 100644 --- a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs +++ b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs @@ -52,9 +52,16 @@ impl RuntimePublic for Public { false } - // fn verify_pop(&self, pop: &Self::Signature) -> bool { - // AppPair::verify_proof_of_possession(pop.as_ptr(), self) - // } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + // TODO: Implement Special Case + None + } + + fn verify_pop(&self, pop: &Self::Signature) -> bool { + let pop = AppSignature::from(pop.clone()); + let pub_key = AppPublic::from(self.clone()); + AppPair::verify_proof_of_possession(&pop, &pub_key) + } fn to_raw_vec(&self) -> Vec { sp_core::crypto::ByteArray::to_raw_vec(self) diff --git a/substrate/primitives/application-crypto/src/ed25519.rs b/substrate/primitives/application-crypto/src/ed25519.rs index 1682d629e80c..83ec49a4d2fc 100644 --- a/substrate/primitives/application-crypto/src/ed25519.rs +++ b/substrate/primitives/application-crypto/src/ed25519.rs @@ -52,12 +52,12 @@ impl RuntimePublic for Public { sp_io::crypto::ed25519_verify(signature, msg.as_ref(), self) } - // fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { - // let pub_key_as_bytes = self.public().to_raw_vec(); - // let pop_context_tag: &[u8] = b"POP_"; - // let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); - // self.sign(key_type, pop_statement.as_slice()) - // } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + let pub_key_as_bytes = self.to_raw_vec(); + let pop_context_tag: &[u8] = b"POP_"; + let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); + sp_io::crypto::ed25519_sign(key_type, self, pop_statement.as_slice()) + } fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); diff --git a/substrate/primitives/application-crypto/src/sr25519.rs b/substrate/primitives/application-crypto/src/sr25519.rs index 36857e2a229c..db5aadd274ec 100644 --- a/substrate/primitives/application-crypto/src/sr25519.rs +++ b/substrate/primitives/application-crypto/src/sr25519.rs @@ -52,6 +52,13 @@ impl RuntimePublic for Public { sp_io::crypto::sr25519_verify(signature, msg.as_ref(), self) } + fn generate_pop(&mut self, key_type: KeyTypeId) -> Option { + let pub_key_as_bytes = self.to_raw_vec(); + let pop_context_tag: &[u8] = b"POP_"; + let pop_statement = [pop_context_tag, pub_key_as_bytes.as_slice()].concat(); + sp_io::crypto::sr25519_sign(key_type, self, pop_statement.as_slice()) + } + fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone());