Skip to content

Commit 3fcf2f7

Browse files
committed
policy: Implement Simplicity key traits for Miniscript equivalents
This is the easiest solution to use Miniscript keys in Simplicity contexts. We focused on subtraits before, which would require changes to (Elements) Miniscript, but we can also use generic impl blocks, as long as they are inside rust-simplicity (orphan rule). We removed elements-miniscript from rust-simplicity to avoid a cyclic dependency. MiniscriptKey is defined in rust-miniscript, so importing it here does not create a cycle. We get a diamond pattern where both elements-miniscript and rust-simplicity depend on the same library, which should be okay.
1 parent dc0d175 commit 3fcf2f7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ path = "src/lib.rs"
1717

1818
[dependencies]
1919
bitcoin = { version = "0.30.0", optional = true }
20+
bitcoin-miniscript = { package = "miniscript", version = "10.0" }
2021
byteorder = "1.3"
2122
elements = { version = "0.22.0", optional = true }
2223
hashes = { package = "bitcoin_hashes", version = "0.12" }

src/policy/key.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bitcoin_miniscript::{MiniscriptKey, ToPublicKey};
12
use elements::bitcoin::key::XOnlyPublicKey;
23
use hashes::sha256;
34
use std::fmt::{Debug, Display};
@@ -8,8 +9,8 @@ pub trait SimplicityKey: Clone + Eq + Ord + Debug + Display + std::hash::Hash {
89
type Sha256: Clone + Eq + Ord + Display + Debug + std::hash::Hash;
910
}
1011

11-
impl SimplicityKey for XOnlyPublicKey {
12-
type Sha256 = sha256::Hash;
12+
impl<Pk: MiniscriptKey> SimplicityKey for Pk {
13+
type Sha256 = <Pk as MiniscriptKey>::Sha256;
1314
}
1415

1516
/// Public key which can be converted to a (x-only) public key which can be used in Simplicity.
@@ -21,13 +22,13 @@ pub trait ToXOnlyPubkey: SimplicityKey {
2122
fn to_sha256(hash: &Self::Sha256) -> sha256::Hash;
2223
}
2324

24-
impl ToXOnlyPubkey for XOnlyPublicKey {
25+
impl<Pk: ToPublicKey> ToXOnlyPubkey for Pk {
2526
fn to_x_only_pubkey(&self) -> XOnlyPublicKey {
26-
*self
27+
<Pk as ToPublicKey>::to_x_only_pubkey(self)
2728
}
2829

2930
fn to_sha256(hash: &Self::Sha256) -> sha256::Hash {
30-
*hash
31+
<Pk as ToPublicKey>::to_sha256(hash)
3132
}
3233
}
3334

0 commit comments

Comments
 (0)