Skip to content

Commit da5a96b

Browse files
committed
key: remove std/alloc/global-context gates from serde::deserialize and FromStr
Since we have a no-feature-gate global context now, we can remove the feature gates from these things. No API change (other than an expansion of the API for users without features enabled).
1 parent 69901b3 commit da5a96b

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/key.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use crate::ThirtyTwoByteHash;
2020
#[cfg(feature = "global-context")]
2121
use crate::SECP256K1;
2222
use crate::{
23-
constants, ecdsa, from_hex, schnorr, Message, Scalar, Secp256k1, Signing, Verification,
23+
constants, ecdsa, from_hex, schnorr, AllPreallocated, Message, Scalar, Secp256k1, Signing,
24+
Verification,
2425
};
2526

2627
/// Secret key - a 256-bit key used to create ECDSA and Taproot signatures.
@@ -1076,20 +1077,14 @@ impl<'a> From<&'a Keypair> for PublicKey {
10761077
fn from(pair: &'a Keypair) -> Self { PublicKey::from_keypair(pair) }
10771078
}
10781079

1079-
#[cfg(any(feature = "global-context", feature = "alloc"))]
10801080
impl str::FromStr for Keypair {
10811081
type Err = Error;
10821082

1083-
#[allow(unused_variables, unreachable_code)] // When built with no default features.
10841083
fn from_str(s: &str) -> Result<Self, Self::Err> {
1085-
#[cfg(feature = "global-context")]
1086-
let ctx = SECP256K1;
1087-
1088-
#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
1089-
let ctx = Secp256k1::signing_only();
1090-
1091-
#[allow(clippy::needless_borrow)]
1092-
Keypair::from_seckey_str(&ctx, s)
1084+
crate::with_global_context(
1085+
|secp: &Secp256k1<AllPreallocated>| Self::from_seckey_str(secp, s),
1086+
None,
1087+
)
10931088
}
10941089
}
10951090

@@ -1113,8 +1108,6 @@ impl serde::Serialize for Keypair {
11131108
}
11141109

11151110
#[cfg(feature = "serde")]
1116-
#[allow(unused_variables)] // For `data` under some feature combinations (the unconditional panic below).
1117-
#[cfg(all(feature = "serde", any(feature = "global-context", feature = "alloc")))]
11181111
impl<'de> serde::Deserialize<'de> for Keypair {
11191112
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
11201113
if d.is_human_readable() {
@@ -1123,14 +1116,10 @@ impl<'de> serde::Deserialize<'de> for Keypair {
11231116
))
11241117
} else {
11251118
let visitor = super::serde_util::Tuple32Visitor::new("raw 32 bytes Keypair", |data| {
1126-
#[cfg(feature = "global-context")]
1127-
let ctx = SECP256K1;
1128-
1129-
#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
1130-
let ctx = Secp256k1::signing_only();
1131-
1132-
#[allow(clippy::needless_borrow)]
1133-
Keypair::from_seckey_byte_array(&ctx, data)
1119+
crate::with_global_context(
1120+
|secp: &Secp256k1<AllPreallocated>| Self::from_seckey_byte_array(secp, data),
1121+
None,
1122+
)
11341123
});
11351124
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
11361125
}

0 commit comments

Comments
 (0)