Skip to content

Commit 65c4f92

Browse files
committed
Change Semantic KeyHash(Pk::Hash) to Key(Pk)
This is one of the major breaking changes touching the parsing/display and logic of semantic policies. This should only affect the application/analysis layer of things, so it is okay to break somethings here for code clarity. My understanding is that will also help the simplicity bridge
1 parent fb8fd49 commit 65c4f92

File tree

8 files changed

+75
-83
lines changed

8 files changed

+75
-83
lines changed

src/descriptor/bare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
294294

295295
impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
296296
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
297-
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
297+
Ok(semantic::Policy::Key(self.pk.clone()))
298298
}
299299
}
300300

src/descriptor/segwitv0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Wpkh<Pk> {
407407

408408
impl<Pk: MiniscriptKey> Liftable<Pk> for Wpkh<Pk> {
409409
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
410-
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
410+
Ok(semantic::Policy::Key(self.pk.clone()))
411411
}
412412
}
413413

src/descriptor/sh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Sh<Pk> {
5959
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
6060
match self.inner {
6161
ShInner::Wsh(ref wsh) => wsh.lift(),
62-
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::KeyHash(pk.as_inner().to_pubkeyhash())),
62+
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::Key(pk.as_inner().clone())),
6363
ShInner::SortedMulti(ref smv) => smv.lift(),
6464
ShInner::Ms(ref ms) => ms.lift(),
6565
}

src/descriptor/sortedmulti.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> policy::Liftable<Pk> for SortedMulti
216216
let ret = policy::semantic::Policy::Threshold(
217217
self.k,
218218
self.pks
219-
.clone()
220-
.into_iter()
221-
.map(|k| policy::semantic::Policy::KeyHash(k.to_pubkeyhash()))
219+
.iter()
220+
.map(|k| policy::semantic::Policy::Key(k.clone()))
222221
.collect(),
223222
);
224223
Ok(ret)

src/descriptor/tr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,9 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
548548
match &self.tree {
549549
Some(root) => Ok(Policy::Threshold(
550550
1,
551-
vec![
552-
Policy::KeyHash(self.internal_key.to_pubkeyhash()),
553-
root.lift()?,
554-
],
551+
vec![Policy::Key(self.internal_key.clone()), root.lift()?],
555552
)),
556-
None => Ok(Policy::KeyHash(self.internal_key.to_pubkeyhash())),
553+
None => Ok(Policy::Key(self.internal_key.clone())),
557554
}
558555
}
559556
}

src/policy/concrete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
245245
for key in concrete_keys.into_iter() {
246246
if semantic_policy
247247
.clone()
248-
.satisfy_constraint(&Semantic::KeyHash(key.to_pubkeyhash()), true)
248+
.satisfy_constraint(&Semantic::Key(key.clone()), true)
249249
== Semantic::Trivial
250250
{
251251
match key_prob_map.get(&Concrete::Key(key.clone())) {

src/policy/mod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub enum LiftError {
6666
HeightTimelockCombination,
6767
/// Duplicate Public Keys
6868
BranchExceedResourceLimits,
69+
/// Cannot lift raw descriptors
70+
RawDescriptorLift,
6971
}
7072

7173
impl fmt::Display for LiftError {
@@ -77,6 +79,7 @@ impl fmt::Display for LiftError {
7779
LiftError::BranchExceedResourceLimits => f.write_str(
7880
"Cannot lift policies containing one branch that exceeds resource limits",
7981
),
82+
LiftError::RawDescriptorLift => f.write_str("Cannot lift raw descriptors"),
8083
}
8184
}
8285
}
@@ -87,7 +90,7 @@ impl error::Error for LiftError {
8790
use self::LiftError::*;
8891

8992
match self {
90-
HeightTimelockCombination | BranchExceedResourceLimits => None,
93+
HeightTimelockCombination | BranchExceedResourceLimits | RawDescriptorLift => None,
9194
}
9295
}
9396
}
@@ -124,8 +127,10 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Miniscript<Pk, Ctx>
124127
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
125128
fn lift(&self) -> Result<Semantic<Pk>, Error> {
126129
let ret = match *self {
127-
Terminal::PkK(ref pk) | Terminal::PkH(ref pk) => Semantic::KeyHash(pk.to_pubkeyhash()),
128-
Terminal::RawPkH(ref pkh) => Semantic::KeyHash(pkh.clone()),
130+
Terminal::PkK(ref pk) | Terminal::PkH(ref pk) => Semantic::Key(pk.clone()),
131+
Terminal::RawPkH(ref _pkh) => {
132+
return Err(Error::LiftError(LiftError::RawDescriptorLift))
133+
}
129134
Terminal::After(t) => Semantic::After(t),
130135
Terminal::Older(t) => Semantic::Older(t),
131136
Terminal::Sha256(ref h) => Semantic::Sha256(h.clone()),
@@ -161,12 +166,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
161166
let semantic_subs: Result<_, Error> = subs.iter().map(|s| s.node.lift()).collect();
162167
Semantic::Threshold(k, semantic_subs?)
163168
}
164-
Terminal::Multi(k, ref keys) | Terminal::MultiA(k, ref keys) => Semantic::Threshold(
165-
k,
166-
keys.iter()
167-
.map(|k| Semantic::KeyHash(k.to_pubkeyhash()))
168-
.collect(),
169-
),
169+
Terminal::Multi(k, ref keys) | Terminal::MultiA(k, ref keys) => {
170+
Semantic::Threshold(k, keys.iter().map(|k| Semantic::Key(k.clone())).collect())
171+
}
170172
}
171173
.normalized();
172174
Ok(ret)
@@ -200,7 +202,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
200202
let ret = match *self {
201203
Concrete::Unsatisfiable => Semantic::Unsatisfiable,
202204
Concrete::Trivial => Semantic::Trivial,
203-
Concrete::Key(ref pk) => Semantic::KeyHash(pk.to_pubkeyhash()),
205+
Concrete::Key(ref pk) => Semantic::Key(pk.clone()),
204206
Concrete::After(t) => Semantic::After(t),
205207
Concrete::Older(t) => Semantic::Older(t),
206208
Concrete::Sha256(ref h) => Semantic::Sha256(h.clone()),
@@ -279,9 +281,9 @@ mod tests {
279281
concrete_policy_rtt("or(99@pk(),1@pk())");
280282
concrete_policy_rtt("and(pk(),or(99@pk(),1@older(12960)))");
281283

282-
semantic_policy_rtt("pkh()");
283-
semantic_policy_rtt("or(pkh(),pkh())");
284-
semantic_policy_rtt("and(pkh(),pkh())");
284+
semantic_policy_rtt("pk()");
285+
semantic_policy_rtt("or(pk(),pk())");
286+
semantic_policy_rtt("and(pk(),pk())");
285287

286288
//fuzzer crashes
287289
assert!(ConcretePol::from_str("thresh()").is_err());
@@ -360,11 +362,11 @@ mod tests {
360362
Semantic::Threshold(
361363
2,
362364
vec![
363-
Semantic::KeyHash(key_a.pubkey_hash().as_hash()),
365+
Semantic::Key(key_a),
364366
Semantic::Older(Sequence::from_height(42))
365367
]
366368
),
367-
Semantic::KeyHash(key_b.pubkey_hash().as_hash())
369+
Semantic::Key(key_b)
368370
]
369371
),
370372
ms_str.lift().unwrap()

0 commit comments

Comments
 (0)