Skip to content

Commit 3c64411

Browse files
committed
extension: introduce TranslateExtParam trait
Right now the TranslateExt trait confusingly translates both Exts and ExtParams. We will split this into two traits, and the ExtParam one will work specifically with CovExt (other extensions will need to implement their own traits).
1 parent dd1a688 commit 3c64411

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

src/extensions/csfs.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use elements::hashes::hex;
1010
use elements::{self, opcodes, secp256k1_zkp};
1111

1212
use super::{ArgFromStr, CovExtArgs, ExtParam, ParseableExt, TxEnv};
13+
use super::param::{ExtParamTranslator, TranslateExtParam};
1314
use crate::miniscript::context::ScriptContextError;
1415
use crate::miniscript::lex::{Token as Tk, TokenIter};
1516
use crate::miniscript::limits::MAX_STANDARD_P2WSH_STACK_ITEM_SIZE;
@@ -317,6 +318,26 @@ where
317318
}
318319
}
319320

321+
impl<PArg, QArg> TranslateExtParam<PArg, QArg> for CheckSigFromStack<PArg>
322+
where
323+
PArg: ExtParam,
324+
QArg: ExtParam,
325+
{
326+
type Output = CheckSigFromStack<QArg>;
327+
328+
fn translate_ext<T, E>(&self, t: &mut T) -> Result<Self::Output, E>
329+
where
330+
T: ExtParamTranslator<PArg, QArg, E>,
331+
PArg: ExtParam,
332+
QArg: ExtParam,
333+
{
334+
Ok(CheckSigFromStack {
335+
pk: t.ext(&self.pk)?,
336+
msg: t.ext(&self.msg)?,
337+
})
338+
}
339+
}
340+
320341
#[cfg(test)]
321342
mod tests {
322343
use bitcoin::XOnlyPublicKey;

src/extensions/introspect_ops.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use elements::opcodes::all::*;
1212
use elements::{confidential, encode, script, Address, AddressParams};
1313

1414
use super::{ArgFromStr, CovExtArgs, EvalError, ExtParam, ParseableExt, TxEnv};
15+
use super::param::{ExtParamTranslator, TranslateExtParam};
1516
use crate::expression::{FromTree, Tree};
1617
use crate::miniscript::context::ScriptContextError;
1718
use crate::miniscript::lex::{Token as Tk, TokenIter};
@@ -1110,6 +1111,32 @@ where
11101111
}
11111112
}
11121113

1114+
impl<PArg, QArg> TranslateExtParam<PArg, QArg> for CovOps<PArg>
1115+
where
1116+
PArg: ExtParam,
1117+
QArg: ExtParam,
1118+
{
1119+
type Output = CovOps<QArg>;
1120+
1121+
fn translate_ext<T, E>(&self, t: &mut T) -> Result<Self::Output, E>
1122+
where
1123+
T: ExtParamTranslator<PArg, QArg, E>,
1124+
{
1125+
match self {
1126+
CovOps::IsExpAsset(a) => Ok(CovOps::IsExpAsset(a._translate_ext(t)?)),
1127+
CovOps::IsExpValue(v) => Ok(CovOps::IsExpValue(v._translate_ext(t)?)),
1128+
CovOps::AssetEq(x, y) => {
1129+
Ok(CovOps::AssetEq(x._translate_ext(t)?, y._translate_ext(t)?))
1130+
}
1131+
CovOps::ValueEq(x, y) => {
1132+
Ok(CovOps::ValueEq(x._translate_ext(t)?, y._translate_ext(t)?))
1133+
}
1134+
CovOps::SpkEq(x, y) => Ok(CovOps::SpkEq(x._translate_ext(t)?, y._translate_ext(t)?)),
1135+
CovOps::CurrIndEq(i) => Ok(CovOps::CurrIndEq(*i)),
1136+
}
1137+
}
1138+
}
1139+
11131140
#[cfg(test)]
11141141
mod tests {
11151142
use bitcoin::XOnlyPublicKey;

src/extensions/param.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bitcoin::hashes::hex::ToHex;
66
use elements::confidential;
77
use elements::encode::serialize;
88

9-
use crate::Error;
9+
use crate::{Error, ExtTranslator};
1010
use super::csfs::{CsfsKey, CsfsMsg};
1111
use super::introspect_ops::Spk;
1212

@@ -176,3 +176,44 @@ impl ArgFromStr for CovExtArgs {
176176
}
177177
}
178178

179+
/// Trait for translating different parameter types for covenant extensions
180+
pub trait ExtParamTranslator<PArg, QArg, E>
181+
where
182+
PArg: ExtParam,
183+
QArg: ExtParam,
184+
{
185+
/// Translates one extension to another
186+
fn ext(&mut self, e: &PArg) -> Result<QArg, E>;
187+
}
188+
189+
// Use ExtParamTranslator as a ExTTranslator
190+
impl<T, PArg, QArg, E> ExtTranslator<PArg, QArg, E> for T
191+
where
192+
T: ExtParamTranslator<PArg, QArg, E>,
193+
PArg: ExtParam,
194+
QArg: ExtParam,
195+
{
196+
/// Translates one extension to another
197+
fn ext(&mut self, e: &PArg) -> Result<QArg, E> {
198+
ExtParamTranslator::ext(self, e)
199+
}
200+
}
201+
202+
203+
/// Converts a descriptor using abstract extension parameters to one using concrete ones,
204+
/// or vice-versa
205+
pub trait TranslateExtParam<PArg, QArg>
206+
where
207+
PArg: ExtParam,
208+
QArg: ExtParam,
209+
{
210+
/// The associated output type.
211+
type Output;
212+
213+
/// Translates a struct from one generic to another where the translations
214+
/// for Pk are provided by the given [`Translator`].
215+
fn translate_ext<T, E>(&self, translator: &mut T) -> Result<Self::Output, E>
216+
where
217+
T: ExtParamTranslator<PArg, QArg, E>;
218+
}
219+

src/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::str::FromStr;
66
use bitcoin::hashes::{hash160, sha256};
77
use bitcoin::secp256k1;
88

9-
use crate::extensions::CovExtArgs;
10-
use crate::{ExtTranslator, MiniscriptKey, Translator};
9+
use crate::extensions::{param::ExtParamTranslator, CovExtArgs};
10+
use crate::{MiniscriptKey, Translator};
1111

1212
/// Translate from a String MiniscriptKey type to bitcoin::PublicKey
1313
/// If the hashmap is populated, this will lookup for keys in HashMap
@@ -156,7 +156,7 @@ pub struct StrExtTransalator {
156156
pub ext_map: HashMap<String, CovExtArgs>,
157157
}
158158

159-
impl ExtTranslator<String, CovExtArgs, ()> for StrExtTransalator {
159+
impl ExtParamTranslator<String, CovExtArgs, ()> for StrExtTransalator {
160160
fn ext(&mut self, e: &String) -> Result<CovExtArgs, ()> {
161161
let x = self.ext_map.get(e).expect("Ext Mapping not found");
162162
Ok(x.clone())

0 commit comments

Comments
 (0)