@@ -1057,9 +1057,16 @@ impl str::FromStr for KeyPair {
1057
1057
type Err = Error ;
1058
1058
1059
1059
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1060
- let ctx = unsafe {
1061
- Secp256k1 :: from_raw_all ( ffi:: secp256k1_context_no_precomp as * mut ffi:: Context )
1062
- } ;
1060
+ #[ cfg( feature = "global-context" ) ]
1061
+ let ctx = SECP256K1 ;
1062
+
1063
+ #[ cfg( all( not( feature = "global-context" ) , feature = "alloc" ) ) ]
1064
+ let ctx = Secp256k1 :: signing_only ( ) ;
1065
+
1066
+ #[ cfg( not( any( feature = "global-context" , feature = "alloc" ) ) ) ]
1067
+ let ctx: Secp256k1 < crate :: SignOnlyPreallocated > = panic ! ( "The previous implementation was panicking too" ) ;
1068
+
1069
+ #[ allow( clippy:: needless_borrow) ]
1063
1070
KeyPair :: from_seckey_str ( & ctx, s)
1064
1071
}
1065
1072
}
@@ -1082,7 +1089,7 @@ impl serde::Serialize for KeyPair {
1082
1089
}
1083
1090
}
1084
1091
1085
- #[ cfg( feature = "serde" ) ]
1092
+ #[ cfg( all ( feature = "serde" , feature = "alloc" ) ) ]
1086
1093
#[ cfg_attr( docsrs, doc( cfg( feature = "serde" ) ) ) ]
1087
1094
impl < ' de > serde:: Deserialize < ' de > for KeyPair {
1088
1095
fn deserialize < D : serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
@@ -1093,8 +1100,18 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
1093
1100
} else {
1094
1101
let visitor = super :: serde_util:: Tuple32Visitor :: new (
1095
1102
"raw 32 bytes KeyPair" ,
1096
- |data| unsafe {
1097
- let ctx = Secp256k1 :: from_raw_all ( ffi:: secp256k1_context_no_precomp as * mut ffi:: Context ) ;
1103
+ |data| {
1104
+ #[ cfg( feature = "global-context" ) ]
1105
+ let ctx = SECP256K1 ;
1106
+
1107
+ #[ cfg( all( not( feature = "global-context" ) , feature = "alloc" ) ) ]
1108
+ let ctx = Secp256k1 :: signing_only ( ) ;
1109
+
1110
+
1111
+ #[ cfg( not( any( feature = "global-context" , feature = "alloc" ) ) ) ]
1112
+ let ctx: Secp256k1 < crate :: SignOnlyPreallocated > = panic ! ( "The previous implementation was panicking too" ) ;
1113
+
1114
+ #[ allow( clippy:: needless_borrow) ]
1098
1115
KeyPair :: from_seckey_slice ( & ctx, data)
1099
1116
}
1100
1117
) ;
@@ -1630,12 +1647,14 @@ pub mod serde_keypair {
1630
1647
#[ cfg( test) ]
1631
1648
#[ allow( unused_imports) ]
1632
1649
mod test {
1650
+ use bitcoin_hashes:: hex:: ToHex ;
1633
1651
use super :: * ;
1634
1652
1635
1653
use core:: str:: FromStr ;
1636
1654
1637
1655
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
1638
1656
use rand:: { Error , RngCore , thread_rng, rngs:: mock:: StepRng } ;
1657
+ use serde_test:: { Configure , Token } ;
1639
1658
1640
1659
#[ cfg( target_arch = "wasm32" ) ]
1641
1660
use wasm_bindgen_test:: wasm_bindgen_test as test;
@@ -2431,6 +2450,41 @@ mod test {
2431
2450
assert_tokens ( & pk. readable ( ) , & [ Token :: Str ( PK_STR ) ] ) ;
2432
2451
assert_tokens ( & pk. readable ( ) , & [ Token :: String ( PK_STR ) ] ) ;
2433
2452
}
2453
+
2454
+ #[ test]
2455
+ #[ cfg( any( feature = "alloc" , feature = "global-context" ) ) ]
2456
+ fn test_keypair_from_str ( ) {
2457
+ let ctx = crate :: Secp256k1 :: new ( ) ;
2458
+ let keypair = KeyPair :: new ( & ctx, & mut thread_rng ( ) ) ;
2459
+ let msg = keypair. secret_key ( ) . secret_bytes ( ) . to_hex ( ) ;
2460
+ let parsed_key: KeyPair = msg. parse ( ) . unwrap ( ) ;
2461
+ assert_eq ! ( parsed_key, keypair) ;
2462
+ }
2463
+
2464
+ #[ test]
2465
+ #[ cfg( all( any( feature= "alloc" , feature = "global-context" ) , feature = "serde" ) ) ]
2466
+ fn test_keypair_deserialize_serde ( ) {
2467
+ let ctx = crate :: Secp256k1 :: new ( ) ;
2468
+ let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2469
+ let keypair = KeyPair :: from_seckey_str ( & ctx, sec_key_str) . unwrap ( ) ;
2470
+
2471
+ serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( & sec_key_str) ] ) ;
2472
+
2473
+ let sec_key_bytes = keypair. secret_key ( ) . secret_bytes ( ) ;
2474
+ let tokens = std:: iter:: once ( Token :: Tuple { len : 32 } )
2475
+ . chain ( sec_key_bytes. iter ( ) . copied ( ) . map ( Token :: U8 ) )
2476
+ . chain ( std:: iter:: once ( Token :: TupleEnd ) )
2477
+ . collect :: < Vec < _ > > ( ) ;
2478
+ serde_test:: assert_tokens ( & keypair. compact ( ) , & tokens) ;
2479
+ }
2480
+
2481
+ #[ test]
2482
+ #[ should_panic( expected = "The previous implementation was panicking too" ) ]
2483
+ #[ cfg( not( any( feature = "alloc" , feature = "global-context" ) ) ) ]
2484
+ fn test_parse_keypair_no_alloc_panic ( ) {
2485
+ let key_hex = "4242424242424242424242424242424242424242424242424242424242424242" ;
2486
+ let _: KeyPair = key_hex. parse ( ) . expect ( "We shouldn't even get this far" ) ;
2487
+ }
2434
2488
}
2435
2489
2436
2490
#[ cfg( bench) ]
0 commit comments