@@ -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
}
@@ -1093,8 +1100,17 @@ 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
+ #[ cfg( not( any( feature = "global-context" , feature = "alloc" ) ) ) ]
1111
+ let ctx: Secp256k1 < crate :: SignOnlyPreallocated > = panic ! ( "The previous implementation was panicking too" ) ;
1112
+
1113
+ #[ allow( clippy:: needless_borrow) ]
1098
1114
KeyPair :: from_seckey_slice ( & ctx, data)
1099
1115
}
1100
1116
) ;
@@ -1630,12 +1646,14 @@ pub mod serde_keypair {
1630
1646
#[ cfg( test) ]
1631
1647
#[ allow( unused_imports) ]
1632
1648
mod test {
1649
+ use bitcoin_hashes:: hex:: ToHex ;
1633
1650
use super :: * ;
1634
1651
1635
1652
use core:: str:: FromStr ;
1636
1653
1637
1654
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
1638
1655
use rand:: { Error , RngCore , thread_rng, rngs:: mock:: StepRng } ;
1656
+ use serde_test:: { Configure , Token } ;
1639
1657
1640
1658
#[ cfg( target_arch = "wasm32" ) ]
1641
1659
use wasm_bindgen_test:: wasm_bindgen_test as test;
@@ -2431,6 +2449,41 @@ mod test {
2431
2449
assert_tokens ( & pk. readable ( ) , & [ Token :: Str ( PK_STR ) ] ) ;
2432
2450
assert_tokens ( & pk. readable ( ) , & [ Token :: String ( PK_STR ) ] ) ;
2433
2451
}
2452
+
2453
+ #[ test]
2454
+ #[ cfg( any( feature = "alloc" , feature = "global-context" ) ) ]
2455
+ fn test_keypair_from_str ( ) {
2456
+ let ctx = crate :: Secp256k1 :: new ( ) ;
2457
+ let keypair = KeyPair :: new ( & ctx, & mut thread_rng ( ) ) ;
2458
+ let msg = keypair. secret_key ( ) . secret_bytes ( ) . to_hex ( ) ;
2459
+ let parsed_key: KeyPair = msg. parse ( ) . unwrap ( ) ;
2460
+ assert_eq ! ( parsed_key, keypair) ;
2461
+ }
2462
+
2463
+ #[ test]
2464
+ #[ cfg( all( any( feature= "alloc" , feature = "global-context" ) , feature = "serde" ) ) ]
2465
+ fn test_keypair_deserialize_serde ( ) {
2466
+ let ctx = crate :: Secp256k1 :: new ( ) ;
2467
+ let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242" ;
2468
+ let keypair = KeyPair :: from_seckey_str ( & ctx, sec_key_str) . unwrap ( ) ;
2469
+
2470
+ serde_test:: assert_tokens ( & keypair. readable ( ) , & [ Token :: String ( & sec_key_str) ] ) ;
2471
+
2472
+ let sec_key_bytes = keypair. secret_key ( ) . secret_bytes ( ) ;
2473
+ let tokens = std:: iter:: once ( Token :: Tuple { len : 32 } )
2474
+ . chain ( sec_key_bytes. iter ( ) . copied ( ) . map ( Token :: U8 ) )
2475
+ . chain ( std:: iter:: once ( Token :: TupleEnd ) )
2476
+ . collect :: < Vec < _ > > ( ) ;
2477
+ serde_test:: assert_tokens ( & keypair. compact ( ) , & tokens) ;
2478
+ }
2479
+
2480
+ #[ test]
2481
+ #[ should_panic( expected = "The previous implementation was panicking too" ) ]
2482
+ #[ cfg( not( any( feature = "alloc" , feature = "global-context" ) ) ) ]
2483
+ fn test_parse_keypair_no_alloc_panic ( ) {
2484
+ let key_hex = "4242424242424242424242424242424242424242424242424242424242424242" ;
2485
+ let _: KeyPair = key_hex. parse ( ) . expect ( "We shouldn't even get this far" ) ;
2486
+ }
2434
2487
}
2435
2488
2436
2489
#[ cfg( bench) ]
0 commit comments