@@ -40,12 +40,7 @@ use bip39::{Language, Mnemonic, MnemonicType};
40
40
#[ cfg( feature = "full_crypto" ) ]
41
41
use core:: convert:: { TryFrom , TryInto } ;
42
42
#[ cfg( feature = "full_crypto" ) ]
43
- use secp256k1:: { PublicKey , SecretKey } ;
44
- #[ cfg( feature = "std" ) ]
45
- use serde:: { de, Deserialize , Deserializer , Serialize , Serializer } ;
46
- use sp_runtime_interface:: pass_by:: PassByInner ;
47
- #[ cfg( feature = "std" ) ]
48
- use substrate_bip39:: seed_from_entropy;
43
+ use libsecp256k1:: { PublicKey , SecretKey } ;
49
44
50
45
/// An identifier used to match public keys against ecdsa keys
51
46
pub const CRYPTO_ID : CryptoTypeId = CryptoTypeId ( * b"ecds" ) ;
@@ -108,7 +103,7 @@ impl Public {
108
103
/// This will convert the full public key into the compressed format.
109
104
#[ cfg( feature = "std" ) ]
110
105
pub fn from_full ( full : & [ u8 ] ) -> Result < Self , ( ) > {
111
- secp256k1 :: PublicKey :: parse_slice ( full, None )
106
+ libsecp256k1 :: PublicKey :: parse_slice ( full, None )
112
107
. map ( |k| k. serialize_compressed ( ) )
113
108
. map ( Self )
114
109
. map_err ( |_| ( ) )
@@ -364,29 +359,29 @@ impl Signature {
364
359
/// Recover the public key from this signature and a message.
365
360
#[ cfg( feature = "full_crypto" ) ]
366
361
pub fn recover < M : AsRef < [ u8 ] > > ( & self , message : M ) -> Option < Public > {
367
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
362
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
368
363
let sig: ( _ , _ ) = self . try_into ( ) . ok ( ) ?;
369
- secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
364
+ libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
370
365
. ok ( )
371
366
. map ( |recovered| Public ( recovered. serialize_compressed ( ) ) )
372
367
}
373
368
374
369
/// Recover the public key from this signature and a pre-hashed message.
375
370
#[ cfg( feature = "full_crypto" ) ]
376
371
pub fn recover_prehashed ( & self , message : & [ u8 ; 32 ] ) -> Option < Public > {
377
- let message = secp256k1 :: Message :: parse ( message) ;
372
+ let message = libsecp256k1 :: Message :: parse ( message) ;
378
373
379
374
let sig: ( _ , _ ) = self . try_into ( ) . ok ( ) ?;
380
375
381
- secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
376
+ libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 )
382
377
. ok ( )
383
378
. map ( |key| Public ( key. serialize_compressed ( ) ) )
384
379
}
385
380
}
386
381
387
382
#[ cfg( feature = "full_crypto" ) ]
388
- impl From < ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) > for Signature {
389
- fn from ( x : ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) ) -> Signature {
383
+ impl From < ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) > for Signature {
384
+ fn from ( x : ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) ) -> Signature {
390
385
let mut r = Self :: default ( ) ;
391
386
r. 0 [ 0 ..64 ] . copy_from_slice ( & x. 0 . serialize ( ) [ ..] ) ;
392
387
r. 0 [ 64 ] = x. 1 . serialize ( ) ;
@@ -395,14 +390,12 @@ impl From<(secp256k1::Signature, secp256k1::RecoveryId)> for Signature {
395
390
}
396
391
397
392
#[ cfg( feature = "full_crypto" ) ]
398
- impl < ' a > TryFrom < & ' a Signature > for ( secp256k1 :: Signature , secp256k1 :: RecoveryId ) {
393
+ impl < ' a > TryFrom < & ' a Signature > for ( libsecp256k1 :: Signature , libsecp256k1 :: RecoveryId ) {
399
394
type Error = ( ) ;
400
- fn try_from (
401
- x : & ' a Signature ,
402
- ) -> Result < ( secp256k1:: Signature , secp256k1:: RecoveryId ) , Self :: Error > {
395
+ fn try_from ( x : & ' a Signature ) -> Result < ( libsecp256k1:: Signature , libsecp256k1:: RecoveryId ) , Self :: Error > {
403
396
Ok ( (
404
- secp256k1 :: Signature :: parse_slice ( & x. 0 [ 0 ..64 ] ) . expect ( "hardcoded to 64 bytes; qed" ) ,
405
- secp256k1 :: RecoveryId :: parse ( x. 0 [ 64 ] ) . map_err ( |_| ( ) ) ?,
397
+ libsecp256k1 :: Signature :: parse_standard_slice ( & x. 0 [ 0 ..64 ] ) . expect ( "hardcoded to 64 bytes; qed" ) ,
398
+ libsecp256k1 :: RecoveryId :: parse ( x. 0 [ 64 ] ) . map_err ( |_| ( ) ) ?,
406
399
) )
407
400
}
408
401
}
@@ -510,18 +503,15 @@ impl TraitPair for Pair {
510
503
511
504
/// Sign a message.
512
505
fn sign ( & self , message : & [ u8 ] ) -> Signature {
513
- let message = secp256k1 :: Message :: parse ( & blake2_256 ( message) ) ;
514
- secp256k1 :: sign ( & message, & self . secret ) . into ( )
506
+ let message = libsecp256k1 :: Message :: parse ( & blake2_256 ( message) ) ;
507
+ libsecp256k1 :: sign ( & message, & self . secret ) . into ( )
515
508
}
516
509
517
510
/// Verify a signature on a message. Returns true if the signature is good.
518
511
fn verify < M : AsRef < [ u8 ] > > ( sig : & Self :: Signature , message : M , pubkey : & Self :: Public ) -> bool {
519
- let message = secp256k1:: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
520
- let sig: ( _ , _ ) = match sig. try_into ( ) {
521
- Ok ( x) => x,
522
- _ => return false ,
523
- } ;
524
- match secp256k1:: recover ( & message, & sig. 0 , & sig. 1 ) {
512
+ let message = libsecp256k1:: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
513
+ let sig: ( _ , _ ) = match sig. try_into ( ) { Ok ( x) => x, _ => return false } ;
514
+ match libsecp256k1:: recover ( & message, & sig. 0 , & sig. 1 ) {
525
515
Ok ( actual) => pubkey. 0 [ ..] == actual. serialize_compressed ( ) [ ..] ,
526
516
_ => false ,
527
517
}
@@ -532,19 +522,11 @@ impl TraitPair for Pair {
532
522
/// This doesn't use the type system to ensure that `sig` and `pubkey` are the correct
533
523
/// size. Use it only if you're coming from byte buffers and need the speed.
534
524
fn verify_weak < P : AsRef < [ u8 ] > , M : AsRef < [ u8 ] > > ( sig : & [ u8 ] , message : M , pubkey : P ) -> bool {
535
- let message = secp256k1:: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
536
- if sig. len ( ) != 65 {
537
- return false
538
- }
539
- let ri = match secp256k1:: RecoveryId :: parse ( sig[ 64 ] ) {
540
- Ok ( x) => x,
541
- _ => return false ,
542
- } ;
543
- let sig = match secp256k1:: Signature :: parse_slice ( & sig[ 0 ..64 ] ) {
544
- Ok ( x) => x,
545
- _ => return false ,
546
- } ;
547
- match secp256k1:: recover ( & message, & sig, & ri) {
525
+ let message = libsecp256k1:: Message :: parse ( & blake2_256 ( message. as_ref ( ) ) ) ;
526
+ if sig. len ( ) != 65 { return false }
527
+ let ri = match libsecp256k1:: RecoveryId :: parse ( sig[ 64 ] ) { Ok ( x) => x, _ => return false } ;
528
+ let sig = match libsecp256k1:: Signature :: parse_standard_slice ( & sig[ 0 ..64 ] ) { Ok ( x) => x, _ => return false } ;
529
+ match libsecp256k1:: recover ( & message, & sig, & ri) {
548
530
Ok ( actual) => pubkey. as_ref ( ) == & actual. serialize ( ) [ 1 ..] ,
549
531
_ => false ,
550
532
}
@@ -577,21 +559,21 @@ impl Pair {
577
559
578
560
/// Sign a pre-hashed message
579
561
pub fn sign_prehashed ( & self , message : & [ u8 ; 32 ] ) -> Signature {
580
- let message = secp256k1 :: Message :: parse ( message) ;
581
- secp256k1 :: sign ( & message, & self . secret ) . into ( )
562
+ let message = libsecp256k1 :: Message :: parse ( message) ;
563
+ libsecp256k1 :: sign ( & message, & self . secret ) . into ( )
582
564
}
583
565
584
566
/// Verify a signature on a pre-hashed message. Return `true` if the signature is valid
585
567
/// and thus matches the given `public` key.
586
568
pub fn verify_prehashed ( sig : & Signature , message : & [ u8 ; 32 ] , public : & Public ) -> bool {
587
- let message = secp256k1 :: Message :: parse ( message) ;
569
+ let message = libsecp256k1 :: Message :: parse ( message) ;
588
570
589
571
let sig: ( _ , _ ) = match sig. try_into ( ) {
590
572
Ok ( x) => x,
591
573
_ => return false ,
592
574
} ;
593
575
594
- match secp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
576
+ match libsecp256k1 :: recover ( & message, & sig. 0 , & sig. 1 ) {
595
577
Ok ( actual) => public. 0 [ ..] == actual. serialize_compressed ( ) [ ..] ,
596
578
_ => false ,
597
579
}
@@ -839,8 +821,7 @@ mod test {
839
821
// `msg` shouldn't be mangled
840
822
let msg = [ 0u8 ; 32 ] ;
841
823
let sig1 = pair. sign_prehashed ( & msg) ;
842
- let sig2: Signature =
843
- secp256k1:: sign ( & secp256k1:: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
824
+ let sig2: Signature = libsecp256k1:: sign ( & libsecp256k1:: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
844
825
845
826
assert_eq ! ( sig1, sig2) ;
846
827
@@ -852,8 +833,7 @@ mod test {
852
833
// using pre-hashed `msg` works
853
834
let msg = keccak_256 ( b"this should be hashed" ) ;
854
835
let sig1 = pair. sign_prehashed ( & msg) ;
855
- let sig2: Signature =
856
- secp256k1:: sign ( & secp256k1:: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
836
+ let sig2: Signature = libsecp256k1:: sign ( & libsecp256k1:: Message :: parse ( & msg) , & pair. secret ) . into ( ) ;
857
837
858
838
assert_eq ! ( sig1, sig2) ;
859
839
}
0 commit comments