Skip to content

Commit c03e348

Browse files
committed
Replace Pk with KeyExpr<Pk>
1 parent 102efbd commit c03e348

17 files changed

+402
-107
lines changed

src/descriptor/bare.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::expression::{self, FromTree};
2828
use crate::miniscript::context::ScriptContext;
2929
use crate::policy::{semantic, Liftable};
3030
use crate::prelude::*;
31+
use crate::miniscript::musig_key::KeyExpr;
3132
use crate::util::{varint_len, witness_to_scriptsig};
3233
use crate::{
3334
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
@@ -192,7 +193,7 @@ where
192193
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
193194
pub struct Pkh<Pk: MiniscriptKey> {
194195
/// underlying publickey
195-
pk: Pk,
196+
pk: Pk
196197
}
197198

198199
impl<Pk: MiniscriptKey> Pkh<Pk> {
@@ -219,7 +220,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
219220
/// sighash suffix. Includes the weight of the VarInts encoding the
220221
/// scriptSig and witness stack length.
221222
pub fn max_satisfaction_weight(&self) -> usize {
222-
4 * (1 + 73 + BareCtx::pk_len(&self.pk))
223+
4 * (1 + 73 + BareCtx::pk_len(&KeyExpr::SingleKey(self.pk)))
223224
}
224225
}
225226

src/descriptor/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use sync::Arc;
3535

3636
use self::checksum::verify_checksum;
3737
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
38+
use crate::miniscript::musig_key::KeyExpr;
3839
use crate::prelude::*;
3940
use crate::{
4041
expression, hash256, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator,
@@ -180,7 +181,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
180181
// roundabout way to constuct `c:pk_k(pk)`
181182
let ms: Miniscript<Pk, BareCtx> =
182183
Miniscript::from_ast(miniscript::decode::Terminal::Check(Arc::new(
183-
Miniscript::from_ast(miniscript::decode::Terminal::PkK(pk))
184+
Miniscript::from_ast(miniscript::decode::Terminal::PkK(KeyExpr::SingleKey(pk)))
184185
.expect("Type check cannot fail"),
185186
)))
186187
.expect("Type check cannot fail");

src/descriptor/segwitv0.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::expression::{self, FromTree};
2626
use crate::miniscript::context::{ScriptContext, ScriptContextError};
2727
use crate::policy::{semantic, Liftable};
2828
use crate::prelude::*;
29+
use crate::miniscript::musig_key::KeyExpr;
2930
use crate::util::varint_len;
3031
use crate::{
3132
Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey, TranslatePk,
@@ -330,7 +331,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
330331
/// sighash suffix. Includes the weight of the VarInts encoding the
331332
/// scriptSig and witness stack length.
332333
pub fn max_satisfaction_weight(&self) -> usize {
333-
4 + 1 + 73 + Segwitv0::pk_len(&self.pk)
334+
4 + 1 + 73 + Segwitv0::pk_len(&KeyExpr::SingleKey(self.pk))
334335
}
335336
}
336337

src/descriptor/sortedmulti.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bitcoin::blockdata::script;
2424

2525
use crate::miniscript::context::ScriptContext;
2626
use crate::miniscript::decode::Terminal;
27+
use crate::miniscript::musig_key::KeyExpr;
2728
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
2829
use crate::prelude::*;
2930
use crate::{
@@ -183,7 +184,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
183184
script_num_size(self.k)
184185
+ 1
185186
+ script_num_size(self.pks.len())
186-
+ self.pks.iter().map(|pk| Ctx::pk_len(pk)).sum::<usize>()
187+
+ self.pks.iter().map(|pk| Ctx::pk_len(&KeyExpr::SingleKey(*pk))).sum::<usize>()
187188
}
188189

189190
/// Maximum number of witness elements used to satisfy the Miniscript

src/interpreter/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ where
593593
Terminal::PkK(ref pk) => {
594594
debug_assert_eq!(node_state.n_evaluated, 0);
595595
debug_assert_eq!(node_state.n_satisfied, 0);
596-
let res = self.stack.evaluate_pk(&mut self.verify_sig, *pk);
596+
let res = match pk.single_key() {
597+
Some(pk) => self.stack.evaluate_pk(&mut self.verify_sig, *pk),
598+
None => None
599+
};
597600
if res.is_some() {
598601
return res;
599602
}
@@ -868,9 +871,11 @@ where
868871
// evaluate each key with as a pk
869872
// note that evaluate_pk will error on non-empty incorrect sigs
870873
// push 1 on satisfied sigs and push 0 on empty sigs
871-
match self
872-
.stack
873-
.evaluate_pk(&mut self.verify_sig, subs[node_state.n_evaluated])
874+
let temp = match subs[node_state.n_evaluated].single_key() {
875+
Some(pkk) => self.stack.evaluate_pk(&mut self.verify_sig, *pkk),
876+
None => None,
877+
};
878+
match temp
874879
{
875880
Some(Ok(x)) => {
876881
self.push_evaluation_state(

src/miniscript/astelem.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use sync::Arc;
2828
use crate::miniscript::context::SigType;
2929
use crate::miniscript::types::{self, Property};
3030
use crate::miniscript::ScriptContext;
31+
use crate::miniscript::musig_key::KeyExpr;
3132
use crate::prelude::*;
3233
use crate::util::MsKeyBuilder;
3334
use crate::{
@@ -80,7 +81,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
8081
Pk::RawPkHash: 'a,
8182
{
8283
match *self {
83-
Terminal::PkK(ref p) => pred(p),
84+
Terminal::PkK(ref p) => p.for_each_key(pred),
8485
Terminal::PkH(ref p) => pred(p),
8586
Terminal::RawPkH(..)
8687
| Terminal::After(..)
@@ -112,8 +113,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
112113
&& c.real_for_each_key(pred)
113114
}
114115
Terminal::Thresh(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(pred)),
115-
Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => {
116-
keys.iter().all(|key| pred(key))
116+
Terminal::Multi(_, ref keys) => keys.iter().all(|key| pred(key)),
117+
Terminal::MultiA(_, ref keys) => {
118+
keys.iter().all(|key| key.for_each_key(pred))
117119
}
118120
}
119121
}
@@ -125,7 +127,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
125127
T: Translator<Pk, Q, E>,
126128
{
127129
let frag: Terminal<Q, CtxQ> = match *self {
128-
Terminal::PkK(ref p) => Terminal::PkK(t.pk(p)?),
130+
Terminal::PkK(ref p) => Terminal::PkK(p.translate_pk(t)?),
129131
Terminal::PkH(ref p) => Terminal::PkH(t.pk(p)?),
130132
Terminal::RawPkH(ref p) => Terminal::RawPkH(t.pkh(p)?),
131133
Terminal::After(n) => Terminal::After(n),
@@ -186,7 +188,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
186188
Terminal::Multi(k, keys?)
187189
}
188190
Terminal::MultiA(k, ref keys) => {
189-
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.pk(k)).collect();
191+
let keys: Result<Vec<KeyExpr<Q>>, _> = keys.iter().map(|k| k.translate_pk(t)).collect();
190192
Terminal::MultiA(k, keys?)
191193
}
192194
};
@@ -455,7 +457,7 @@ impl_from_tree!(
455457
}
456458
let mut unwrapped = match (frag_name, top.args.len()) {
457459
("pk_k", 1) => {
458-
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkK))
460+
expression::unary(top, Terminal::PkK)
459461
}
460462
("pk_h", 1) => expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH)),
461463
("after", 1) => expression::terminal(&top.args[0], |x| {
@@ -522,15 +524,18 @@ impl_from_tree!(
522524
return Err(errstr("higher threshold than there were keys in multi"));
523525
}
524526

525-
let pks: Result<Vec<Pk>, _> = top.args[1..]
526-
.iter()
527-
.map(|sub| expression::terminal(sub, Pk::from_str))
528-
.collect();
529-
530527
if frag_name == "multi" {
528+
let pks: Result<Vec<Pk>, _> = top.args[1..]
529+
.iter()
530+
.map(|sub| expression::terminal(sub, Pk::from_str))
531+
.collect();
531532
pks.map(|pks| Terminal::Multi(k, pks))
532533
} else {
533534
// must be multi_a
535+
let pks: Result<Vec<KeyExpr<Pk>>, _> = top.args[1..]
536+
.iter()
537+
.map(|sub| expression::terminal(sub, KeyExpr::<Pk>::from_str))
538+
.collect();
534539
pks.map(|pks| Terminal::MultiA(k, pks))
535540
}
536541
}
@@ -734,7 +739,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
734739
builder = builder.push_ms_key::<_, Ctx>(&keys[0]);
735740
builder = builder.push_opcode(opcodes::all::OP_CHECKSIG);
736741
for pk in keys.iter().skip(1) {
737-
builder = builder.push_ms_key::<_, Ctx>(pk);
742+
builder = builder.push_ms_key::<_, Ctx>(&pk);
738743
builder = builder.push_opcode(opcodes::all::OP_CHECKSIGADD);
739744
}
740745
builder
@@ -793,7 +798,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
793798
script_num_size(k)
794799
+ 1
795800
+ script_num_size(pks.len())
796-
+ pks.iter().map(|pk| Ctx::pk_len(pk)).sum::<usize>()
801+
+ pks.iter().map(|pk| Ctx::pk_len(&KeyExpr::SingleKey(*pk))).sum::<usize>()
797802
}
798803
Terminal::MultiA(k, ref pks) => {
799804
script_num_size(k)

0 commit comments

Comments
 (0)