Skip to content

Commit

Permalink
Merge rust-bitcoin#492: Fix broken serde::Deserialize and FromStr
Browse files Browse the repository at this point in the history
… 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 rust-bitcoin#491

ACKs for top commit:
  apoelstra:
    ACK 1f327b4

Tree-SHA512: 1af54667b7a1b310035fa35bd2aeb508e432d8c7f153ae1b9850431ba77dcc3e2194c1cda45a1ed5218d955d9284ba6512cf8ab6dafc673f23ccdad7c601b1b6
  • Loading branch information
apoelstra committed Oct 25, 2022
2 parents 5e1e012 + 1f327b4 commit 15a8c20
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.24.1 - 2022-10-25

* [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.

# 0.24.0 - 2022-07-20

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "secp256k1"
version = "0.24.0"
version = "0.24.1"
authors = [ "Dawid Ciężarkiewicz <[email protected]>",
"Andrew Poelstra <[email protected]>" ]
license = "CC0-1.0"
Expand Down
63 changes: 58 additions & 5 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,16 @@ impl str::FromStr for KeyPair {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let ctx = unsafe {
Secp256k1::from_raw_all(ffi::secp256k1_context_no_precomp as *mut ffi::Context)
};
#[cfg(feature = "global-context")]
let ctx = SECP256K1;

#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
let ctx = Secp256k1::signing_only();

#[cfg(not(any(feature = "global-context", feature = "alloc")))]
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("The previous implementation was panicking too, please enable the global-context feature of rust-secp256k1");

#[allow(clippy::needless_borrow)]
KeyPair::from_seckey_str(&ctx, s)
}
}
Expand Down Expand Up @@ -1093,8 +1100,17 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
} else {
let visitor = super::serde_util::Tuple32Visitor::new(
"raw 32 bytes KeyPair",
|data| unsafe {
let ctx = Secp256k1::from_raw_all(ffi::secp256k1_context_no_precomp as *mut ffi::Context);
|data| {
#[cfg(feature = "global-context")]
let ctx = SECP256K1;

#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
let ctx = Secp256k1::signing_only();

#[cfg(not(any(feature = "global-context", feature = "alloc")))]
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("The previous implementation was panicking too, please enable the global-context feature of rust-secp256k1");

#[allow(clippy::needless_borrow)]
KeyPair::from_seckey_slice(&ctx, data)
}
);
Expand Down Expand Up @@ -1630,12 +1646,14 @@ pub mod serde_keypair {
#[cfg(test)]
#[allow(unused_imports)]
mod test {
use bitcoin_hashes::hex::ToHex;
use super::*;

use core::str::FromStr;

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

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down Expand Up @@ -2431,6 +2449,41 @@ mod test {
assert_tokens(&pk.readable(), &[Token::Str(PK_STR)]);
assert_tokens(&pk.readable(), &[Token::String(PK_STR)]);
}

#[test]
#[cfg(any(feature = "alloc", feature = "global-context"))]
fn test_keypair_from_str() {
let ctx = crate::Secp256k1::new();
let keypair = KeyPair::new(&ctx, &mut thread_rng());
let msg = keypair.secret_key().secret_bytes().to_hex();
let parsed_key: KeyPair = msg.parse().unwrap();
assert_eq!(parsed_key, keypair);
}

#[test]
#[cfg(all(any(feature= "alloc", feature = "global-context"), feature = "serde"))]
fn test_keypair_deserialize_serde() {
let ctx = crate::Secp256k1::new();
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242";
let keypair = KeyPair::from_seckey_str(&ctx, sec_key_str).unwrap();

serde_test::assert_tokens(&keypair.readable(), &[Token::String(&sec_key_str)]);

let sec_key_bytes = keypair.secret_key().secret_bytes();
let tokens = std::iter::once(Token::Tuple { len: 32 })
.chain(sec_key_bytes.iter().copied().map(Token::U8))
.chain(std::iter::once(Token::TupleEnd))
.collect::<Vec<_>>();
serde_test::assert_tokens(&keypair.compact(), &tokens);
}

#[test]
#[should_panic(expected = "The previous implementation was panicking too")]
#[cfg(not(any(feature = "alloc", feature = "global-context")))]
fn test_parse_keypair_no_alloc_panic() {
let key_hex = "4242424242424242424242424242424242424242424242424242424242424242";
let _: KeyPair = key_hex.parse().expect("We shouldn't even get this far");
}
}

#[cfg(bench)]
Expand Down

0 comments on commit 15a8c20

Please sign in to comment.