@@ -7,9 +7,10 @@ pub mod pow;
77pub mod vdf;
88pub mod vrf;
99
10- use crate :: error:: { ChainCraftError , CryptoError , Result } ;
10+ use crate :: error:: { ChaincraftError , CryptoError , Result } ;
1111use async_trait:: async_trait;
1212use ed25519_dalek:: { Signer , Verifier } ;
13+ use k256:: ecdsa:: signature:: Verifier as K256Verifier ;
1314use serde:: { Deserialize , Serialize } ;
1415use thiserror:: Error ;
1516
@@ -112,28 +113,28 @@ impl PublicKey {
112113 /// Create from hex string
113114 pub fn from_hex ( hex_str : & str , key_type : KeyType ) -> Result < Self > {
114115 let bytes = hex:: decode ( hex_str) . map_err ( |_| {
115- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
116+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
116117 reason : "Invalid hex encoding" . to_string ( ) ,
117118 } )
118119 } ) ?;
119120
120121 match key_type {
121122 KeyType :: Ed25519 => {
122123 let key_bytes: [ u8 ; 32 ] = bytes. try_into ( ) . map_err ( |_| {
123- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
124+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
124125 reason : "Invalid key length for Ed25519" . to_string ( ) ,
125126 } )
126127 } ) ?;
127128 let key = ed25519_dalek:: VerifyingKey :: from_bytes ( & key_bytes) . map_err ( |_| {
128- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
129+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
129130 reason : "Invalid Ed25519 key" . to_string ( ) ,
130131 } )
131132 } ) ?;
132133 Ok ( PublicKey :: Ed25519 ( key) )
133134 } ,
134135 KeyType :: Secp256k1 => {
135136 let key = k256:: PublicKey :: from_sec1_bytes ( & bytes) . map_err ( |_| {
136- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
137+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidPublicKey {
137138 reason : "Invalid Secp256k1 key" . to_string ( ) ,
138139 } )
139140 } ) ?;
@@ -149,18 +150,16 @@ impl PublicKey {
149150 }
150151 }
151152
152- pub fn verify ( & self , message : & [ u8 ] , signature : & Signature ) -> Result < bool > {
153+ pub fn verify ( & self , message : & [ u8 ] , signature : & Signature ) -> Result < bool > {
153154 match ( self , signature) {
154- ( PublicKey :: Ed25519 ( key) , Signature :: Ed25519 ( sig) ) => {
155- Ok ( key. verify ( message, sig) . is_ok ( ) )
156- } ,
157- ( PublicKey :: Secp256k1 ( key) , Signature :: Secp256k1 ( sig) ) => {
158- use k256:: ecdsa:: VerifyingKey ;
159- let verifying_key = VerifyingKey :: from ( key) ;
160- use k256:: ecdsa:: signature:: Verifier ;
155+ ( PublicKey :: Ed25519 ( pk) , Signature :: Ed25519 ( sig) ) => {
156+ Ok ( pk. verify ( message, sig) . is_ok ( ) )
157+ }
158+ ( PublicKey :: Secp256k1 ( pk) , Signature :: Secp256k1 ( sig) ) => {
159+ let verifying_key = k256:: ecdsa:: VerifyingKey :: from ( pk) ;
161160 Ok ( verifying_key. verify ( message, sig) . is_ok ( ) )
162- } ,
163- _ => Err ( ChainCraftError :: Crypto ( crate :: error :: CryptoError :: InvalidSignature ) ) ,
161+ }
162+ _ => Err ( ChaincraftError :: Crypto ( CryptoError :: InvalidSignature ) ) ,
164163 }
165164 }
166165}
@@ -226,15 +225,15 @@ impl PrivateKey {
226225 /// Create from hex string
227226 pub fn from_hex ( hex_str : & str , key_type : KeyType ) -> Result < Self > {
228227 let bytes = hex:: decode ( hex_str) . map_err ( |_| {
229- ChainCraftError :: Crypto ( crate :: error :: CryptoError :: InvalidPrivateKey {
228+ ChaincraftError :: Crypto ( CryptoError :: InvalidPrivateKey {
230229 reason : "Invalid hex encoding" . to_string ( ) ,
231230 } )
232231 } ) ?;
233232
234233 match key_type {
235234 KeyType :: Ed25519 => {
236235 let key_bytes: [ u8 ; 32 ] = bytes. try_into ( ) . map_err ( |_| {
237- ChainCraftError :: Crypto ( crate :: error :: CryptoError :: InvalidPrivateKey {
236+ ChaincraftError :: Crypto ( CryptoError :: InvalidPrivateKey {
238237 reason : "Invalid key length for Ed25519" . to_string ( ) ,
239238 } )
240239 } ) ?;
@@ -243,7 +242,7 @@ impl PrivateKey {
243242 } ,
244243 KeyType :: Secp256k1 => {
245244 let key = k256:: SecretKey :: from_slice ( & bytes) . map_err ( |_| {
246- ChainCraftError :: Crypto ( crate :: error :: CryptoError :: InvalidPrivateKey {
245+ ChaincraftError :: Crypto ( CryptoError :: InvalidPrivateKey {
247246 reason : "Invalid Secp256k1 key" . to_string ( ) ,
248247 } )
249248 } ) ?;
@@ -342,19 +341,19 @@ impl Signature {
342341 /// Create from hex string
343342 pub fn from_hex ( hex_str : & str , sig_type : KeyType ) -> Result < Self > {
344343 let bytes = hex:: decode ( hex_str)
345- . map_err ( |_| ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature ) ) ?;
344+ . map_err ( |_| ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature ) ) ?;
346345
347346 match sig_type {
348347 KeyType :: Ed25519 => {
349348 let sig_bytes: [ u8 ; 64 ] = bytes. try_into ( ) . map_err ( |_| {
350- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature )
349+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature )
351350 } ) ?;
352351 let sig = ed25519_dalek:: Signature :: from_bytes ( & sig_bytes) ;
353352 Ok ( Signature :: Ed25519 ( sig) )
354353 } ,
355354 KeyType :: Secp256k1 => {
356355 let sig = k256:: ecdsa:: Signature :: from_slice ( & bytes) . map_err ( |_| {
357- ChainCraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature )
356+ ChaincraftError :: Crypto ( crate :: error:: CryptoError :: InvalidSignature )
358357 } ) ?;
359358 Ok ( Signature :: Secp256k1 ( sig) )
360359 } ,
@@ -453,14 +452,14 @@ pub mod utils {
453452 let verifying_key = VerifyingKey :: from ( pk) ;
454453 Ok ( verifying_key. verify ( message, sig) . is_ok ( ) )
455454 } ,
456- _ => Err ( ChainCraftError :: Crypto ( CryptoError :: InvalidSignature ) ) ,
455+ _ => Err ( ChaincraftError :: Crypto ( CryptoError :: InvalidSignature ) ) ,
457456 }
458457 }
459458}
460459
461460// Utility functions for ed25519 signatures
462461pub mod ed25519_utils {
463- use crate :: error:: { ChainCraftError , CryptoError , Result } ;
462+ use crate :: error:: { ChaincraftError , CryptoError , Result } ;
464463 use ed25519_dalek:: Signature as Ed25519Signature ;
465464
466465 // Workaround for signature creation
@@ -476,3 +475,15 @@ pub use ecdsa::EcdsaSignature;
476475pub use hash:: * ;
477476pub use pow:: ProofOfWork ;
478477pub use vrf:: VerifiableRandomFunction ;
478+
479+ #[ cfg( test) ]
480+ mod tests {
481+ use super :: * ;
482+ use crate :: error:: { ChaincraftError , CryptoError , Result } ;
483+
484+ #[ test]
485+ fn test_key_generation ( ) -> Result < ( ) > {
486+ // ... existing code ...
487+ Ok ( ( ) )
488+ }
489+ }
0 commit comments