Skip to content

Commit eeb823f

Browse files
committed
Remove DummyKey
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
1 parent e267c78 commit eeb823f

File tree

10 files changed

+36
-237
lines changed

10 files changed

+36
-237
lines changed

fuzz/fuzz_targets/compile_descriptor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
extern crate miniscript;
22

33
use miniscript::Segwitv0;
4-
use miniscript::{policy, DummyKey, Miniscript};
4+
use miniscript::{policy, Miniscript};
55
use policy::Liftable;
66

77
use std::str::FromStr;
88

9-
type DummyScript = Miniscript<DummyKey, Segwitv0>;
10-
type DummyPolicy = policy::Concrete<DummyKey>;
9+
type Script = Miniscript<String, Segwitv0>;
10+
type Policy = policy::Concrete<String>;
1111

1212
fn do_test(data: &[u8]) {
1313
let data_str = String::from_utf8_lossy(data);
14-
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
14+
if let Ok(pol) = Policy::from_str(&data_str) {
1515
// Compile
1616
if let Ok(desc) = pol.compile::<Segwitv0>() {
1717
// Lift
1818
assert_eq!(desc.clone().lift().unwrap().sorted(), pol.clone().lift().unwrap().sorted());
1919
// Try to roundtrip the output of the compiler
2020
let output = desc.to_string();
21-
if let Ok(desc) = DummyScript::from_str(&output) {
21+
if let Ok(desc) = Script::from_str(&output) {
2222
let rtt = desc.to_string();
2323
assert_eq!(output.to_lowercase(), rtt.to_lowercase());
2424
} else {

fuzz/fuzz_targets/roundtrip_concrete.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extern crate miniscript;
22
extern crate regex;
3-
use miniscript::{policy, DummyKey};
3+
use miniscript::policy;
44
use regex::Regex;
55
use std::str::FromStr;
66

7-
type DummyPolicy = policy::Concrete<DummyKey>;
7+
type Policy = policy::Concrete<String>;
88

99
fn do_test(data: &[u8]) {
1010
let data_str = String::from_utf8_lossy(data);
11-
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
11+
if let Ok(pol) = Policy::from_str(&data_str) {
1212
let output = pol.to_string();
1313
//remove all instances of 1@
1414
let re = Regex::new("(\\D)1@").unwrap();

fuzz/fuzz_targets/roundtrip_descriptor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
extern crate miniscript;
22
extern crate regex;
33

4-
use miniscript::{Descriptor, DummyKey};
4+
use miniscript::Descriptor;
55
use regex::Regex;
66
use std::str::FromStr;
77

88
fn do_test(data: &[u8]) {
99
let s = String::from_utf8_lossy(data);
10-
if let Ok(desc) = Descriptor::<DummyKey>::from_str(&s) {
10+
if let Ok(desc) = Descriptor::<String>::from_str(&s) {
1111
let str2 = desc.to_string();
12-
let desc2 = Descriptor::<DummyKey>::from_str(&str2).unwrap();
12+
let desc2 = Descriptor::<String>::from_str(&str2).unwrap();
1313

1414
assert_eq!(desc, desc2);
1515
}

fuzz/fuzz_targets/roundtrip_miniscript_str.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ extern crate regex;
44
use regex::Regex;
55
use std::str::FromStr;
66

7-
use miniscript::DummyKey;
87
use miniscript::Miniscript;
98
use miniscript::Segwitv0;
109

1110
fn do_test(data: &[u8]) {
1211
let s = String::from_utf8_lossy(data);
13-
if let Ok(desc) = Miniscript::<DummyKey, Segwitv0>::from_str(&s) {
12+
if let Ok(desc) = Miniscript::<String, Segwitv0>::from_str(&s) {
1413
let str2 = desc.to_string();
15-
let desc2 = Miniscript::<DummyKey, Segwitv0>::from_str(&str2).unwrap();
14+
let desc2 = Miniscript::<String, Segwitv0>::from_str(&str2).unwrap();
1615

1716
assert_eq!(desc, desc2);
1817
}

fuzz/fuzz_targets/roundtrip_semantic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
extern crate miniscript;
22

3-
use miniscript::{policy, DummyKey};
3+
use miniscript::policy;
44
use std::str::FromStr;
55

6-
type DummyPolicy = policy::Semantic<DummyKey>;
6+
type Policy = policy::Semantic<String>;
77

88
fn do_test(data: &[u8]) {
99
let data_str = String::from_utf8_lossy(data);
10-
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
10+
if let Ok(pol) = Policy::from_str(&data_str) {
1111
let output = pol.to_string();
1212
assert_eq!(data_str.to_lowercase(), output.to_lowercase());
1313
}

src/descriptor/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,13 @@ mod tests {
951951
use crate::descriptor::{DescriptorPublicKey, DescriptorXKey, SinglePub};
952952
#[cfg(feature = "compiler")]
953953
use crate::policy;
954-
use crate::{hex_script, Descriptor, DummyKey, Error, Miniscript, Satisfier};
954+
use crate::{hex_script, Descriptor, Error, Miniscript, Satisfier};
955955

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

959959
fn roundtrip_descriptor(s: &str) {
960-
let desc = Descriptor::<DummyKey>::from_str(s).unwrap();
960+
let desc = Descriptor::<String>::from_str(s).unwrap();
961961
let output = desc.to_string();
962962
let normalize_aliases = s.replace("c:pk_k(", "pk(").replace("c:pk_h(", "pkh(");
963963
assert_eq!(
@@ -984,7 +984,7 @@ mod tests {
984984
StdDescriptor::from_str("(\u{7f}()3").unwrap_err();
985985
StdDescriptor::from_str("pk()").unwrap_err();
986986
StdDescriptor::from_str("nl:0").unwrap_err(); //issue 63
987-
let compressed_pk = DummyKey.to_string();
987+
let compressed_pk = String::from("");
988988
assert_eq!(
989989
StdDescriptor::from_str("sh(sortedmulti)")
990990
.unwrap_err()
@@ -1368,13 +1368,13 @@ mod tests {
13681368

13691369
#[test]
13701370
fn tr_roundtrip_key() {
1371-
let script = Tr::<DummyKey>::from_str("tr()").unwrap().to_string();
1371+
let script = Tr::<String>::from_str("tr()").unwrap().to_string();
13721372
assert_eq!(script, format!("tr()#x4ml3kxd"))
13731373
}
13741374

13751375
#[test]
13761376
fn tr_roundtrip_script() {
1377-
let descriptor = Tr::<DummyKey>::from_str("tr(,{pk(),pk()})")
1377+
let descriptor = Tr::<String>::from_str("tr(,{pk(),pk()})")
13781378
.unwrap()
13791379
.to_string();
13801380

src/lib.rs

Lines changed: 2 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ pub mod psbt;
120120
mod test_utils;
121121
mod util;
122122

123-
use core::str::FromStr;
124123
use core::{fmt, hash, str};
125124
#[cfg(feature = "std")]
126125
use std::error;
@@ -334,204 +333,6 @@ impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey {
334333
}
335334
}
336335

337-
/// Dummy key which de/serializes to the empty string; useful sometimes for testing
338-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
339-
pub struct DummyKey;
340-
341-
impl str::FromStr for DummyKey {
342-
type Err = &'static str;
343-
fn from_str(x: &str) -> Result<DummyKey, &'static str> {
344-
if x.is_empty() {
345-
Ok(DummyKey)
346-
} else {
347-
Err("non empty dummy key")
348-
}
349-
}
350-
}
351-
352-
impl MiniscriptKey for DummyKey {
353-
type Sha256 = DummySha256Hash;
354-
type Hash256 = DummyHash256Hash;
355-
type Ripemd160 = DummyRipemd160Hash;
356-
type Hash160 = DummyHash160Hash;
357-
358-
fn num_der_paths(&self) -> usize {
359-
0
360-
}
361-
}
362-
363-
impl hash::Hash for DummyKey {
364-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
365-
"DummyKey".hash(state);
366-
}
367-
}
368-
369-
impl fmt::Display for DummyKey {
370-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
371-
f.write_str("")
372-
}
373-
}
374-
375-
impl ToPublicKey for DummyKey {
376-
fn to_public_key(&self) -> bitcoin::PublicKey {
377-
bitcoin::PublicKey::from_str(
378-
"0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352",
379-
)
380-
.unwrap()
381-
}
382-
383-
fn to_sha256(_hash: &DummySha256Hash) -> sha256::Hash {
384-
sha256::Hash::from_str("50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352")
385-
.unwrap()
386-
}
387-
388-
fn to_hash256(_hash: &DummyHash256Hash) -> hash256::Hash {
389-
hash256::Hash::from_str("50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352")
390-
.unwrap()
391-
}
392-
393-
fn to_ripemd160(_: &DummyRipemd160Hash) -> ripemd160::Hash {
394-
ripemd160::Hash::from_str("f54a5851e9372b87810a8e60cdd2e7cfd80b6e31").unwrap()
395-
}
396-
397-
fn to_hash160(_: &DummyHash160Hash) -> hash160::Hash {
398-
hash160::Hash::from_str("f54a5851e9372b87810a8e60cdd2e7cfd80b6e31").unwrap()
399-
}
400-
}
401-
402-
/// Dummy keyhash which de/serializes to the empty string; useful sometimes for testing
403-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
404-
pub struct DummyKeyHash;
405-
406-
impl str::FromStr for DummyKeyHash {
407-
type Err = &'static str;
408-
fn from_str(x: &str) -> Result<DummyKeyHash, &'static str> {
409-
if x.is_empty() {
410-
Ok(DummyKeyHash)
411-
} else {
412-
Err("non empty dummy key")
413-
}
414-
}
415-
}
416-
417-
impl fmt::Display for DummyKeyHash {
418-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
419-
f.write_str("")
420-
}
421-
}
422-
423-
impl hash::Hash for DummyKeyHash {
424-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
425-
"DummyKeyHash".hash(state);
426-
}
427-
}
428-
429-
/// Dummy keyhash which de/serializes to the empty string; useful for testing
430-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
431-
pub struct DummySha256Hash;
432-
433-
impl str::FromStr for DummySha256Hash {
434-
type Err = &'static str;
435-
fn from_str(x: &str) -> Result<DummySha256Hash, &'static str> {
436-
if x.is_empty() {
437-
Ok(DummySha256Hash)
438-
} else {
439-
Err("non empty dummy hash")
440-
}
441-
}
442-
}
443-
444-
impl fmt::Display for DummySha256Hash {
445-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
446-
f.write_str("")
447-
}
448-
}
449-
450-
impl hash::Hash for DummySha256Hash {
451-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
452-
"DummySha256Hash".hash(state);
453-
}
454-
}
455-
456-
/// Dummy keyhash which de/serializes to the empty string; useful for testing
457-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
458-
pub struct DummyHash256Hash;
459-
460-
impl str::FromStr for DummyHash256Hash {
461-
type Err = &'static str;
462-
fn from_str(x: &str) -> Result<DummyHash256Hash, &'static str> {
463-
if x.is_empty() {
464-
Ok(DummyHash256Hash)
465-
} else {
466-
Err("non empty dummy hash")
467-
}
468-
}
469-
}
470-
471-
/// Dummy keyhash which de/serializes to the empty string; useful for testing
472-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
473-
pub struct DummyRipemd160Hash;
474-
475-
impl str::FromStr for DummyRipemd160Hash {
476-
type Err = &'static str;
477-
fn from_str(x: &str) -> Result<DummyRipemd160Hash, &'static str> {
478-
if x.is_empty() {
479-
Ok(DummyRipemd160Hash)
480-
} else {
481-
Err("non empty dummy hash")
482-
}
483-
}
484-
}
485-
486-
impl fmt::Display for DummyHash256Hash {
487-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
488-
f.write_str("")
489-
}
490-
}
491-
impl fmt::Display for DummyRipemd160Hash {
492-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
493-
f.write_str("")
494-
}
495-
}
496-
497-
impl hash::Hash for DummyHash256Hash {
498-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
499-
"DummySha256Hash".hash(state);
500-
}
501-
}
502-
503-
impl hash::Hash for DummyRipemd160Hash {
504-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
505-
"DummyRipemd160Hash".hash(state);
506-
}
507-
}
508-
509-
/// Dummy keyhash which de/serializes to the empty string; useful for testing
510-
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Default)]
511-
pub struct DummyHash160Hash;
512-
513-
impl str::FromStr for DummyHash160Hash {
514-
type Err = &'static str;
515-
fn from_str(x: &str) -> Result<DummyHash160Hash, &'static str> {
516-
if x.is_empty() {
517-
Ok(DummyHash160Hash)
518-
} else {
519-
Err("non empty dummy hash")
520-
}
521-
}
522-
}
523-
524-
impl fmt::Display for DummyHash160Hash {
525-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
526-
f.write_str("")
527-
}
528-
}
529-
530-
impl hash::Hash for DummyHash160Hash {
531-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
532-
"DummyHash160Hash".hash(state);
533-
}
534-
}
535336
/// Describes an object that can translate various keys and hashes from one key to the type
536337
/// associated with the other key. Used by the [`TranslatePk`] trait to do the actual translations.
537338
pub trait Translator<P, Q, E>
@@ -926,6 +727,8 @@ fn hex_script(s: &str) -> bitcoin::Script {
926727

927728
#[cfg(test)]
928729
mod tests {
730+
use core::str::FromStr;
731+
929732
use super::*;
930733

931734
#[test]

0 commit comments

Comments
 (0)