Skip to content

Commit 15a8c20

Browse files
committed
Merge #492: Fix broken serde::Deserialize and FromStr impl of keyPair
1f327b4 Bump version number to v0.24.1 (elsirion) 53c1354 Fix broken `serde::Deserialize` and `FromStr` impl of `keyPair` (elsirion) Pull request description: Fixes #491 ACKs for top commit: apoelstra: ACK 1f327b4 Tree-SHA512: 1af54667b7a1b310035fa35bd2aeb508e432d8c7f153ae1b9850431ba77dcc3e2194c1cda45a1ed5218d955d9284ba6512cf8ab6dafc673f23ccdad7c601b1b6
2 parents 5e1e012 + 1f327b4 commit 15a8c20

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.24.1 - 2022-10-25
2+
3+
* [Fix broken deserialization logic of `KeyPair`](https://github.com/rust-bitcoin/rust-secp256k1/issues/491) that previously always panicked. After the patch deserialization only panics if neither the `global-context` nor the `alloc` (default) feature is active.
14

25
# 0.24.0 - 2022-07-20
36

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "secp256k1"
3-
version = "0.24.0"
3+
version = "0.24.1"
44
authors = [ "Dawid Ciężarkiewicz <[email protected]>",
55
"Andrew Poelstra <[email protected]>" ]
66
license = "CC0-1.0"

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, please enable the global-context feature of rust-secp256k1");
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, please enable the global-context feature of rust-secp256k1");
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)