Skip to content

Commit 73990f2

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

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/key.rs

+58-5
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
}
@@ -1093,8 +1100,17 @@ 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+
#[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)]
10981114
KeyPair::from_seckey_slice(&ctx, data)
10991115
}
11001116
);
@@ -1630,12 +1646,14 @@ pub mod serde_keypair {
16301646
#[cfg(test)]
16311647
#[allow(unused_imports)]
16321648
mod test {
1649+
use bitcoin_hashes::hex::ToHex;
16331650
use super::*;
16341651

16351652
use core::str::FromStr;
16361653

16371654
#[cfg(any(feature = "alloc", feature = "std"))]
16381655
use rand::{Error, RngCore, thread_rng, rngs::mock::StepRng};
1656+
use serde_test::{Configure, Token};
16391657

16401658
#[cfg(target_arch = "wasm32")]
16411659
use wasm_bindgen_test::wasm_bindgen_test as test;
@@ -2431,6 +2449,41 @@ mod test {
24312449
assert_tokens(&pk.readable(), &[Token::Str(PK_STR)]);
24322450
assert_tokens(&pk.readable(), &[Token::String(PK_STR)]);
24332451
}
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+
}
24342487
}
24352488

24362489
#[cfg(bench)]

0 commit comments

Comments
 (0)