Skip to content

Commit a7186ca

Browse files
committed
Fix broken serde::Deserialize and FromStr impl of keyPair
Fixes #491
1 parent 5e1e012 commit a7186ca

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

src/key.rs

+60-6
Original file line numberDiff line numberDiff line change
@@ -1057,9 +1057,16 @@ impl str::FromStr for KeyPair {
10571057
type Err = Error;
10581058

10591059
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)]
10631070
KeyPair::from_seckey_str(&ctx, s)
10641071
}
10651072
}
@@ -1082,7 +1089,7 @@ impl serde::Serialize for KeyPair {
10821089
}
10831090
}
10841091

1085-
#[cfg(feature = "serde")]
1092+
#[cfg(all(feature = "serde", feature = "alloc"))]
10861093
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
10871094
impl<'de> serde::Deserialize<'de> for KeyPair {
10881095
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
@@ -1093,8 +1100,18 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
10931100
} else {
10941101
let visitor = super::serde_util::Tuple32Visitor::new(
10951102
"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)]
10981115
KeyPair::from_seckey_slice(&ctx, data)
10991116
}
11001117
);
@@ -1630,12 +1647,14 @@ pub mod serde_keypair {
16301647
#[cfg(test)]
16311648
#[allow(unused_imports)]
16321649
mod test {
1650+
use bitcoin_hashes::hex::ToHex;
16331651
use super::*;
16341652

16351653
use core::str::FromStr;
16361654

16371655
#[cfg(any(feature = "alloc", feature = "std"))]
16381656
use rand::{Error, RngCore, thread_rng, rngs::mock::StepRng};
1657+
use serde_test::{Configure, Token};
16391658

16401659
#[cfg(target_arch = "wasm32")]
16411660
use wasm_bindgen_test::wasm_bindgen_test as test;
@@ -2431,6 +2450,41 @@ mod test {
24312450
assert_tokens(&pk.readable(), &[Token::Str(PK_STR)]);
24322451
assert_tokens(&pk.readable(), &[Token::String(PK_STR)]);
24332452
}
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+
}
24342488
}
24352489

24362490
#[cfg(bench)]

0 commit comments

Comments
 (0)