@@ -1057,9 +1057,13 @@ 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( not( feature = "global-context" ) ) ]
1064
+ let ctx = Secp256k1 :: signing_only ( ) ;
1065
+
1066
+ #[ allow( clippy:: needless_borrow) ]
1063
1067
KeyPair :: from_seckey_str ( & ctx, s)
1064
1068
}
1065
1069
}
@@ -1093,8 +1097,14 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
1093
1097
} else {
1094
1098
let visitor = super :: serde_util:: Tuple32Visitor :: new (
1095
1099
"raw 32 bytes KeyPair" ,
1096
- |data| unsafe {
1097
- let ctx = Secp256k1 :: from_raw_all ( ffi:: secp256k1_context_no_precomp as * mut ffi:: Context ) ;
1100
+ |data| {
1101
+ #[ cfg( feature = "global-context" ) ]
1102
+ let ctx = SECP256K1 ;
1103
+
1104
+ #[ cfg( not( feature = "global-context" ) ) ]
1105
+ let ctx = Secp256k1 :: signing_only ( ) ;
1106
+
1107
+ #[ allow( clippy:: needless_borrow) ]
1098
1108
KeyPair :: from_seckey_slice ( & ctx, data)
1099
1109
}
1100
1110
) ;
@@ -1630,6 +1640,7 @@ pub mod serde_keypair {
1630
1640
#[ cfg( test) ]
1631
1641
#[ allow( unused_imports) ]
1632
1642
mod test {
1643
+ use bitcoin_hashes:: hex:: ToHex ;
1633
1644
use super :: * ;
1634
1645
1635
1646
use core:: str:: FromStr ;
@@ -2431,6 +2442,16 @@ mod test {
2431
2442
assert_tokens ( & pk. readable ( ) , & [ Token :: Str ( PK_STR ) ] ) ;
2432
2443
assert_tokens ( & pk. readable ( ) , & [ Token :: String ( PK_STR ) ] ) ;
2433
2444
}
2445
+
2446
+ #[ test]
2447
+ #[ cfg( feature = "global-context" ) ]
2448
+ fn test_keypair_from_str ( ) {
2449
+ let ctx = crate :: Secp256k1 :: new ( ) ;
2450
+ let keypair = KeyPair :: new ( & ctx, & mut thread_rng ( ) ) ;
2451
+ let msg = keypair. secret_key ( ) . secret_bytes ( ) . to_hex ( ) ;
2452
+ let parsed_key: KeyPair = msg. parse ( ) . unwrap ( ) ;
2453
+ assert_eq ! ( parsed_key, keypair) ;
2454
+ }
2434
2455
}
2435
2456
2436
2457
#[ cfg( bench) ]
0 commit comments