Skip to content

Commit

Permalink
Merge #508: Remove DummyKey
Browse files Browse the repository at this point in the history
eeb823f Remove DummyKey (Tobin C. Harding)

Pull request description:

  An alternative to #500 (because I just saw the issue #437)

  As described in issue #437 we can use `String` in place of the `DummyKey` in all current test use cases.

  Use `String` in tests and remove the `DummyKey` along with all the other (currently unused) `DummyFoo` types.

  Fix: #437

ACKs for top commit:
  apoelstra:
    ACK eeb823f

Tree-SHA512: 84a5e4bd218e915877a5c4a833c02c6f72ef93b247f7ed85d156c746289203954b9b41c47139e1da5c64664c51ec3ee32901d27be7be2f10ae1f9eb678c23e3a
  • Loading branch information
apoelstra committed Dec 24, 2022
2 parents 6ed008f + eeb823f commit 775941a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 237 deletions.
10 changes: 5 additions & 5 deletions fuzz/fuzz_targets/compile_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
extern crate miniscript;

use miniscript::Segwitv0;
use miniscript::{policy, DummyKey, Miniscript};
use miniscript::{policy, Miniscript};
use policy::Liftable;

use std::str::FromStr;

type DummyScript = Miniscript<DummyKey, Segwitv0>;
type DummyPolicy = policy::Concrete<DummyKey>;
type Script = Miniscript<String, Segwitv0>;
type Policy = policy::Concrete<String>;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
if let Ok(pol) = Policy::from_str(&data_str) {
// Compile
if let Ok(desc) = pol.compile::<Segwitv0>() {
// Lift
assert_eq!(desc.clone().lift().unwrap().sorted(), pol.clone().lift().unwrap().sorted());
// Try to roundtrip the output of the compiler
let output = desc.to_string();
if let Ok(desc) = DummyScript::from_str(&output) {
if let Ok(desc) = Script::from_str(&output) {
let rtt = desc.to_string();
assert_eq!(output.to_lowercase(), rtt.to_lowercase());
} else {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/roundtrip_concrete.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extern crate miniscript;
extern crate regex;
use miniscript::{policy, DummyKey};
use miniscript::policy;
use regex::Regex;
use std::str::FromStr;

type DummyPolicy = policy::Concrete<DummyKey>;
type Policy = policy::Concrete<String>;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
if let Ok(pol) = Policy::from_str(&data_str) {
let output = pol.to_string();
//remove all instances of 1@
let re = Regex::new("(\\D)1@").unwrap();
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/roundtrip_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate miniscript;
extern crate regex;

use miniscript::{Descriptor, DummyKey};
use miniscript::Descriptor;
use regex::Regex;
use std::str::FromStr;

fn do_test(data: &[u8]) {
let s = String::from_utf8_lossy(data);
if let Ok(desc) = Descriptor::<DummyKey>::from_str(&s) {
if let Ok(desc) = Descriptor::<String>::from_str(&s) {
let str2 = desc.to_string();
let desc2 = Descriptor::<DummyKey>::from_str(&str2).unwrap();
let desc2 = Descriptor::<String>::from_str(&str2).unwrap();

assert_eq!(desc, desc2);
}
Expand Down
5 changes: 2 additions & 3 deletions fuzz/fuzz_targets/roundtrip_miniscript_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ extern crate regex;
use regex::Regex;
use std::str::FromStr;

use miniscript::DummyKey;
use miniscript::Miniscript;
use miniscript::Segwitv0;

fn do_test(data: &[u8]) {
let s = String::from_utf8_lossy(data);
if let Ok(desc) = Miniscript::<DummyKey, Segwitv0>::from_str(&s) {
if let Ok(desc) = Miniscript::<String, Segwitv0>::from_str(&s) {
let str2 = desc.to_string();
let desc2 = Miniscript::<DummyKey, Segwitv0>::from_str(&str2).unwrap();
let desc2 = Miniscript::<String, Segwitv0>::from_str(&str2).unwrap();

assert_eq!(desc, desc2);
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/roundtrip_semantic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate miniscript;

use miniscript::{policy, DummyKey};
use miniscript::policy;
use std::str::FromStr;

type DummyPolicy = policy::Semantic<DummyKey>;
type Policy = policy::Semantic<String>;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
if let Ok(pol) = Policy::from_str(&data_str) {
let output = pol.to_string();
assert_eq!(data_str.to_lowercase(), output.to_lowercase());
}
Expand Down
10 changes: 5 additions & 5 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,13 +951,13 @@ mod tests {
use crate::descriptor::{DescriptorPublicKey, DescriptorXKey, SinglePub};
#[cfg(feature = "compiler")]
use crate::policy;
use crate::{hex_script, Descriptor, DummyKey, Error, Miniscript, Satisfier};
use crate::{hex_script, Descriptor, Error, Miniscript, Satisfier};

type StdDescriptor = Descriptor<PublicKey>;
const TEST_PK: &str = "pk(020000000000000000000000000000000000000000000000000000000000000002)";

fn roundtrip_descriptor(s: &str) {
let desc = Descriptor::<DummyKey>::from_str(s).unwrap();
let desc = Descriptor::<String>::from_str(s).unwrap();
let output = desc.to_string();
let normalize_aliases = s.replace("c:pk_k(", "pk(").replace("c:pk_h(", "pkh(");
assert_eq!(
Expand All @@ -984,7 +984,7 @@ mod tests {
StdDescriptor::from_str("(\u{7f}()3").unwrap_err();
StdDescriptor::from_str("pk()").unwrap_err();
StdDescriptor::from_str("nl:0").unwrap_err(); //issue 63
let compressed_pk = DummyKey.to_string();
let compressed_pk = String::from("");
assert_eq!(
StdDescriptor::from_str("sh(sortedmulti)")
.unwrap_err()
Expand Down Expand Up @@ -1368,13 +1368,13 @@ mod tests {

#[test]
fn tr_roundtrip_key() {
let script = Tr::<DummyKey>::from_str("tr()").unwrap().to_string();
let script = Tr::<String>::from_str("tr()").unwrap().to_string();
assert_eq!(script, format!("tr()#x4ml3kxd"))
}

#[test]
fn tr_roundtrip_script() {
let descriptor = Tr::<DummyKey>::from_str("tr(,{pk(),pk()})")
let descriptor = Tr::<String>::from_str("tr(,{pk(),pk()})")
.unwrap()
.to_string();

Expand Down
201 changes: 2 additions & 199 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ pub mod psbt;
mod test_utils;
mod util;

use core::str::FromStr;
use core::{fmt, hash, str};
#[cfg(feature = "std")]
use std::error;
Expand Down Expand Up @@ -334,204 +333,6 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
}
}

/// Dummy key which de/serializes to the empty string; useful sometimes for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummyKey;

impl str::FromStr for DummyKey {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummyKey, &'static str> {
if x.is_empty() {
Ok(DummyKey)
} else {
Err("non empty dummy key")
}
}
}

impl MiniscriptKey for DummyKey {
type Sha256 = DummySha256Hash;
type Hash256 = DummyHash256Hash;
type Ripemd160 = DummyRipemd160Hash;
type Hash160 = DummyHash160Hash;

fn num_der_paths(&self) -> usize {
0
}
}

impl hash::Hash for DummyKey {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummyKey".hash(state);
}
}

impl fmt::Display for DummyKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}

impl ToPublicKey for DummyKey {
fn to_public_key(&self) -> bitcoin::PublicKey {
bitcoin::PublicKey::from_str(
"0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352",
)
.unwrap()
}

fn to_sha256(_hash: &DummySha256Hash) -> sha256::Hash {
sha256::Hash::from_str("50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352")
.unwrap()
}

fn to_hash256(_hash: &DummyHash256Hash) -> hash256::Hash {
hash256::Hash::from_str("50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352")
.unwrap()
}

fn to_ripemd160(_: &DummyRipemd160Hash) -> ripemd160::Hash {
ripemd160::Hash::from_str("f54a5851e9372b87810a8e60cdd2e7cfd80b6e31").unwrap()
}

fn to_hash160(_: &DummyHash160Hash) -> hash160::Hash {
hash160::Hash::from_str("f54a5851e9372b87810a8e60cdd2e7cfd80b6e31").unwrap()
}
}

/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummyKeyHash;

impl str::FromStr for DummyKeyHash {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummyKeyHash, &'static str> {
if x.is_empty() {
Ok(DummyKeyHash)
} else {
Err("non empty dummy key")
}
}
}

impl fmt::Display for DummyKeyHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}

impl hash::Hash for DummyKeyHash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummyKeyHash".hash(state);
}
}

/// Dummy keyhash which de/serializes to the empty string; useful for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummySha256Hash;

impl str::FromStr for DummySha256Hash {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummySha256Hash, &'static str> {
if x.is_empty() {
Ok(DummySha256Hash)
} else {
Err("non empty dummy hash")
}
}
}

impl fmt::Display for DummySha256Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}

impl hash::Hash for DummySha256Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummySha256Hash".hash(state);
}
}

/// Dummy keyhash which de/serializes to the empty string; useful for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummyHash256Hash;

impl str::FromStr for DummyHash256Hash {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummyHash256Hash, &'static str> {
if x.is_empty() {
Ok(DummyHash256Hash)
} else {
Err("non empty dummy hash")
}
}
}

/// Dummy keyhash which de/serializes to the empty string; useful for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummyRipemd160Hash;

impl str::FromStr for DummyRipemd160Hash {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummyRipemd160Hash, &'static str> {
if x.is_empty() {
Ok(DummyRipemd160Hash)
} else {
Err("non empty dummy hash")
}
}
}

impl fmt::Display for DummyHash256Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}
impl fmt::Display for DummyRipemd160Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}

impl hash::Hash for DummyHash256Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummySha256Hash".hash(state);
}
}

impl hash::Hash for DummyRipemd160Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummyRipemd160Hash".hash(state);
}
}

/// Dummy keyhash which de/serializes to the empty string; useful for testing
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
pub struct DummyHash160Hash;

impl str::FromStr for DummyHash160Hash {
type Err = &'static str;
fn from_str(x: &str) -> Result<DummyHash160Hash, &'static str> {
if x.is_empty() {
Ok(DummyHash160Hash)
} else {
Err("non empty dummy hash")
}
}
}

impl fmt::Display for DummyHash160Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("")
}
}

impl hash::Hash for DummyHash160Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
"DummyHash160Hash".hash(state);
}
}
/// Describes an object that can translate various keys and hashes from one key to the type
/// associated with the other key. Used by the [`TranslatePk`] trait to do the actual translations.
pub trait Translator<P, Q, E>
Expand Down Expand Up @@ -926,6 +727,8 @@ fn hex_script(s: &str) -> bitcoin::Script {

#[cfg(test)]
mod tests {
use core::str::FromStr;

use super::*;

#[test]
Expand Down

0 comments on commit 775941a

Please sign in to comment.