From 870c0cd9463369da3809866a35c00d402265a9c1 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 7 Jun 2022 11:32:23 +1000 Subject: [PATCH] Remove f_ prefix from translator method names We can shorten the unusual `Translator` trait method names with no loss of meaning. It is not immediately obvious what the `f_` prefix means, it is a relic of history from when we used function pointers. Remove the `f_` prefix, shortening the translator method names to just the thing they are translating. --- src/descriptor/bare.rs | 2 +- src/descriptor/mod.rs | 20 ++++++++++---------- src/descriptor/segwitv0.rs | 2 +- src/descriptor/sortedmulti.rs | 2 +- src/descriptor/tr.rs | 2 +- src/interpreter/inner.rs | 9 ++++----- src/lib.rs | 20 ++++++++++---------- src/miniscript/astelem.rs | 10 +++++----- src/miniscript/iter.rs | 12 ++++++------ src/policy/concrete.rs | 10 +++++----- src/policy/semantic.rs | 10 +++++----- src/psbt/mod.rs | 10 +++++----- src/test_utils.rs | 12 ++++++------ tests/setup/test_util.rs | 16 ++++++++-------- 14 files changed, 68 insertions(+), 69 deletions(-) diff --git a/src/descriptor/bare.rs b/src/descriptor/bare.rs index 2c8ac0b64..0b3159ab6 100644 --- a/src/descriptor/bare.rs +++ b/src/descriptor/bare.rs @@ -346,6 +346,6 @@ where where T: Translator, { - Ok(Pkh::new(t.f_pk(&self.pk)?)) + Ok(Pkh::new(t.pk(&self.pk)?)) } } diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index d81f9fe35..46d1adfa9 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -527,11 +527,11 @@ impl Descriptor { struct Derivator(u32); impl PkTranslator for Derivator { - fn f_pk(&mut self, pk: &DescriptorPublicKey) -> Result { + fn pk(&mut self, pk: &DescriptorPublicKey) -> Result { Ok(pk.clone().derive(self.0)) } - fn f_pkh(&mut self, pkh: &DescriptorPublicKey) -> Result { + fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result { Ok(pkh.clone().derive(self.0)) } } @@ -574,14 +574,14 @@ impl Descriptor { PkTranslator for Derivator<'a, C> { - fn f_pk( + fn pk( &mut self, pk: &DerivedDescriptorKey, ) -> Result { pk.derive_public_key(&self.0) } - fn f_pkh( + fn pkh( &mut self, pkh: &DerivedDescriptorKey, ) -> Result { @@ -633,15 +633,15 @@ impl Descriptor { impl<'a, C: secp256k1::Signing> Translator for KeyMapWrapper<'a, C> { - fn f_pk(&mut self, pk: &String) -> Result { + fn pk(&mut self, pk: &String) -> Result { parse_key(pk, &mut self.0, self.1) } - fn f_pkh(&mut self, pkh: &String) -> Result { + fn pkh(&mut self, pkh: &String) -> Result { parse_key(pkh, &mut self.0, self.1) } - fn f_sha256(&mut self, sha256: &String) -> Result { + fn sha256(&mut self, sha256: &String) -> Result { let hash = sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?; Ok(hash) @@ -661,15 +661,15 @@ impl Descriptor { struct KeyMapLookUp<'a>(&'a KeyMap); impl<'a> Translator for KeyMapLookUp<'a> { - fn f_pk(&mut self, pk: &DescriptorPublicKey) -> Result { + fn pk(&mut self, pk: &DescriptorPublicKey) -> Result { key_to_string(pk, self.0) } - fn f_pkh(&mut self, pkh: &DescriptorPublicKey) -> Result { + fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result { key_to_string(pkh, self.0) } - fn f_sha256(&mut self, sha256: &sha256::Hash) -> Result { + fn sha256(&mut self, sha256: &sha256::Hash) -> Result { Ok(sha256.to_string()) } } diff --git a/src/descriptor/segwitv0.rs b/src/descriptor/segwitv0.rs index 2d7502325..070d23934 100644 --- a/src/descriptor/segwitv0.rs +++ b/src/descriptor/segwitv0.rs @@ -459,6 +459,6 @@ where where T: Translator, { - Ok(Wpkh::new(t.f_pk(&self.pk)?).expect("Uncompressed keys in Wpkh")) + Ok(Wpkh::new(t.pk(&self.pk)?).expect("Uncompressed keys in Wpkh")) } } diff --git a/src/descriptor/sortedmulti.rs b/src/descriptor/sortedmulti.rs index 8e3421de6..d3c21a239 100644 --- a/src/descriptor/sortedmulti.rs +++ b/src/descriptor/sortedmulti.rs @@ -102,7 +102,7 @@ impl SortedMultiVec { T: Translator, Q: MiniscriptKey, { - let pks: Result, _> = self.pks.iter().map(|pk| t.f_pk(pk)).collect(); + let pks: Result, _> = self.pks.iter().map(|pk| t.pk(pk)).collect(); Ok(SortedMultiVec { k: self.k, pks: pks?, diff --git a/src/descriptor/tr.rs b/src/descriptor/tr.rs index 15c88e9f9..7fde28171 100644 --- a/src/descriptor/tr.rs +++ b/src/descriptor/tr.rs @@ -611,7 +611,7 @@ where T: Translator, { let translate_desc = Tr { - internal_key: translate.f_pk(&self.internal_key)?, + internal_key: translate.pk(&self.internal_key)?, tree: match &self.tree { Some(tree) => Some(tree.translate_helper(translate)?), None => None, diff --git a/src/interpreter/inner.rs b/src/interpreter/inner.rs index af7f42552..efe6a44aa 100644 --- a/src/interpreter/inner.rs +++ b/src/interpreter/inner.rs @@ -378,16 +378,15 @@ impl ToNoChecks for Miniscript { struct TranslateFullPk; impl PkTranslator for TranslateFullPk { - fn f_pk(&mut self, pk: &bitcoin::PublicKey) -> Result { + fn pk(&mut self, pk: &bitcoin::PublicKey) -> Result { Ok(BitcoinKey::Fullkey(*pk)) } - fn f_pkh(&mut self, pkh: &hash160::Hash) -> Result { + fn pkh(&mut self, pkh: &hash160::Hash) -> Result { Ok(TypedHash160::FullKey(*pkh)) } } - // specify the () error type as this cannot error self.real_translate_pk(&mut TranslateFullPk) .expect("Translation should succeed") } @@ -399,11 +398,11 @@ impl ToNoChecks for Miniscript struct TranslateXOnlyPk; impl PkTranslator for TranslateXOnlyPk { - fn f_pk(&mut self, pk: &bitcoin::XOnlyPublicKey) -> Result { + fn pk(&mut self, pk: &bitcoin::XOnlyPublicKey) -> Result { Ok(BitcoinKey::XOnlyPublicKey(*pk)) } - fn f_pkh(&mut self, pkh: &hash160::Hash) -> Result { + fn pkh(&mut self, pkh: &hash160::Hash) -> Result { Ok(TypedHash160::XonlyKey(*pkh)) } } diff --git a/src/lib.rs b/src/lib.rs index 90636a39e..711e3fe65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -397,13 +397,13 @@ where Q: MiniscriptKey, { /// Translates public keys P -> Q. - fn f_pk(&mut self, pk: &P) -> Result; + fn pk(&mut self, pk: &P) -> Result; /// Translates public key hashes P::Hash -> Q::Hash. - fn f_pkh(&mut self, pkh: &P::Hash) -> Result; + fn pkh(&mut self, pkh: &P::Hash) -> Result; /// Translates sha256 hashes from P::Sha256 -> Q::Sha256 - fn f_sha256(&mut self, sha256: &P::Sha256) -> Result; + fn sha256(&mut self, sha256: &P::Sha256) -> Result; } /// Provides the conversion information required in [`TranslatePk`]. @@ -415,10 +415,10 @@ where Q: MiniscriptKey, { /// Provides the translation public keys P -> Q - fn f_pk(&mut self, pk: &P) -> Result; + fn pk(&mut self, pk: &P) -> Result; /// Provides the translation public keys hashes P::Hash -> Q::Hash - fn f_pkh(&mut self, pkh: &P::Hash) -> Result; + fn pkh(&mut self, pkh: &P::Hash) -> Result; } impl Translator for T @@ -427,15 +427,15 @@ where P: MiniscriptKey, Q: MiniscriptKey, { - fn f_pk(&mut self, pk: &P) -> Result { - >::f_pk(self, pk) + fn pk(&mut self, pk: &P) -> Result { + >::pk(self, pk) } - fn f_pkh(&mut self, pkh: &

::Hash) -> Result<::Hash, E> { - >::f_pkh(self, pkh) + fn pkh(&mut self, pkh: &

::Hash) -> Result<::Hash, E> { + >::pkh(self, pkh) } - fn f_sha256(&mut self, sha256: &

::Sha256) -> Result<::Sha256, E> { + fn sha256(&mut self, sha256: &

::Sha256) -> Result<::Sha256, E> { Ok(sha256.clone()) } } diff --git a/src/miniscript/astelem.rs b/src/miniscript/astelem.rs index dab864224..932d5135e 100644 --- a/src/miniscript/astelem.rs +++ b/src/miniscript/astelem.rs @@ -129,11 +129,11 @@ impl Terminal { T: Translator, { let frag: Terminal = match *self { - Terminal::PkK(ref p) => Terminal::PkK(t.f_pk(p)?), - Terminal::PkH(ref p) => Terminal::PkH(t.f_pkh(p)?), + Terminal::PkK(ref p) => Terminal::PkK(t.pk(p)?), + Terminal::PkH(ref p) => Terminal::PkH(t.pkh(p)?), Terminal::After(n) => Terminal::After(n), Terminal::Older(n) => Terminal::Older(n), - Terminal::Sha256(ref x) => Terminal::Sha256(t.f_sha256(&x)?), + Terminal::Sha256(ref x) => Terminal::Sha256(t.sha256(&x)?), Terminal::Hash256(x) => Terminal::Hash256(x), Terminal::Ripemd160(x) => Terminal::Ripemd160(x), Terminal::Hash160(x) => Terminal::Hash160(x), @@ -185,11 +185,11 @@ impl Terminal { Terminal::Thresh(k, subs?) } Terminal::Multi(k, ref keys) => { - let keys: Result, _> = keys.iter().map(|k| t.f_pk(k)).collect(); + let keys: Result, _> = keys.iter().map(|k| t.pk(k)).collect(); Terminal::Multi(k, keys?) } Terminal::MultiA(k, ref keys) => { - let keys: Result, _> = keys.iter().map(|k| t.f_pk(k)).collect(); + let keys: Result, _> = keys.iter().map(|k| t.pk(k)).collect(); Terminal::MultiA(k, keys?) } }; diff --git a/src/miniscript/iter.rs b/src/miniscript/iter.rs index 5207c0ce7..fc9e28044 100644 --- a/src/miniscript/iter.rs +++ b/src/miniscript/iter.rs @@ -121,7 +121,7 @@ impl Miniscript { /// NB: The function analyzes only single miniscript item and not any of its descendants in AST. /// To obtain a list of all public keys within AST use [Miniscript::iter_pk()] function, for example /// `miniscript.iter_pubkeys().collect()`. - pub fn get_leaf_pk(&self) -> Vec { + pub fn get_leapk(&self) -> Vec { match self.node { Terminal::PkK(ref key) => vec![key.clone()], Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => keys.clone(), @@ -138,7 +138,7 @@ impl Miniscript { /// NB: The function analyzes only single miniscript item and not any of its descendants in AST. /// To obtain a list of all public key hashes within AST use [Miniscript::iter_pkh()] function, /// for example `miniscript.iter_pubkey_hashes().collect()`. - pub fn get_leaf_pkh(&self) -> Vec { + pub fn get_leapkh(&self) -> Vec { match self.node { Terminal::PkH(ref hash) => vec![hash.clone()], Terminal::PkK(ref key) => vec![key.to_pubkeyhash()], @@ -156,7 +156,7 @@ impl Miniscript { /// NB: The function analyzes only single miniscript item and not any of its descendants in AST. /// To obtain a list of all public keys or hashes within AST use [Miniscript::iter_pk_pkh()] /// function, for example `miniscript.iter_pubkeys_and_hashes().collect()`. - pub fn get_leaf_pk_pkh(&self) -> Vec> { + pub fn get_leapk_pkh(&self) -> Vec> { match self.node { Terminal::PkH(ref hash) => vec![PkPkh::HashedPubkey(hash.clone())], Terminal::PkK(ref key) => vec![PkPkh::PlainPubkey(key.clone())], @@ -605,7 +605,7 @@ pub mod test { return; } let ms = *ms.branches().first().unwrap_or(&&ms); - assert_eq!(ms.get_leaf_pk(), k); + assert_eq!(ms.get_leapk(), k); }) } @@ -624,7 +624,7 @@ pub mod test { .collect(); // In our test cases we always have plain keys going first all.extend(h); - assert_eq!(ms.get_leaf_pkh(), all); + assert_eq!(ms.get_leapkh(), all); }) } @@ -642,7 +642,7 @@ pub mod test { } else { k.into_iter().map(|k| PkPkh::PlainPubkey(k)).collect() }; - assert_eq!(ms.get_leaf_pk_pkh(), r); + assert_eq!(ms.get_leapk_pkh(), r); }) } diff --git a/src/policy/concrete.rs b/src/policy/concrete.rs index 266fbddf4..a007ea881 100644 --- a/src/policy/concrete.rs +++ b/src/policy/concrete.rs @@ -344,17 +344,17 @@ impl Policy { /// // we would use the general Translator Trait. /// impl Translator for StrPkTranslator { /// // Provides the translation public keys P -> Q - /// fn f_pk(&mut self, pk: &String) -> Result { + /// fn pk(&mut self, pk: &String) -> Result { /// self.pk_map.get(pk).copied().ok_or(()) // Dummy Err /// } /// /// // If our policy also contained other fragments, we could provide the translation here. - /// fn f_pkh(&mut self, pkh: &String) -> Result { + /// fn pkh(&mut self, pkh: &String) -> Result { /// unreachable!("Policy does not contain any pkh fragment"); /// } /// /// // If our policy also contained other fragments, we could provide the translation here. - /// fn f_sha256(&mut self, sha256: &String) -> Result { + /// fn sha256(&mut self, sha256: &String) -> Result { /// unreachable!("Policy does not contain any sha256 fragment"); /// } /// } @@ -385,8 +385,8 @@ impl Policy { match *self { Policy::Unsatisfiable => Ok(Policy::Unsatisfiable), Policy::Trivial => Ok(Policy::Trivial), - Policy::Key(ref pk) => t.f_pk(pk).map(Policy::Key), - Policy::Sha256(ref h) => t.f_sha256(h).map(Policy::Sha256), + Policy::Key(ref pk) => t.pk(pk).map(Policy::Key), + Policy::Sha256(ref h) => t.sha256(h).map(Policy::Sha256), Policy::Hash256(ref h) => Ok(Policy::Hash256(*h)), Policy::Ripemd160(ref h) => Ok(Policy::Ripemd160(*h)), Policy::Hash160(ref h) => Ok(Policy::Hash160(*h)), diff --git a/src/policy/semantic.rs b/src/policy/semantic.rs index cb7372092..58452429d 100644 --- a/src/policy/semantic.rs +++ b/src/policy/semantic.rs @@ -99,18 +99,18 @@ impl Policy { /// // If we also wanted to provide mapping of other associated types(sha256, older etc), /// // we would use the general Translator Trait. /// impl Translator for StrPkTranslator { - /// fn f_pk(&mut self, pk: &String) -> Result { + /// fn pk(&mut self, pk: &String) -> Result { /// unreachable!("Policy does not contain any pk fragment"); /// } /// /// // Provides the translation public keys P::Hash -> Q::Hash /// // If our policy also contained other fragments, we could provide the translation here. - /// fn f_pkh(&mut self, pkh: &String) -> Result { + /// fn pkh(&mut self, pkh: &String) -> Result { /// self.pk_map.get(pkh).copied().ok_or(()) // Dummy Err /// } /// /// // If our policy also contained other fragments, we could provide the translation here. - /// fn f_sha256(&mut self, sha256: &String) -> Result { + /// fn sha256(&mut self, sha256: &String) -> Result { /// unreachable!("Policy does not contain any sha256 fragment"); /// } /// } @@ -141,8 +141,8 @@ impl Policy { match *self { Policy::Unsatisfiable => Ok(Policy::Unsatisfiable), Policy::Trivial => Ok(Policy::Trivial), - Policy::KeyHash(ref pkh) => t.f_pkh(pkh).map(Policy::KeyHash), - Policy::Sha256(ref h) => t.f_sha256(h).map(Policy::Sha256), + Policy::KeyHash(ref pkh) => t.pkh(pkh).map(Policy::KeyHash), + Policy::Sha256(ref h) => t.sha256(h).map(Policy::Sha256), Policy::Hash256(ref h) => Ok(Policy::Hash256(*h)), Policy::Ripemd160(ref h) => Ok(Policy::Ripemd160(*h)), Policy::Hash160(ref h) => Ok(Policy::Hash160(*h)), diff --git a/src/psbt/mod.rs b/src/psbt/mod.rs index 7e41f5c39..cc8a9f7c1 100644 --- a/src/psbt/mod.rs +++ b/src/psbt/mod.rs @@ -940,14 +940,14 @@ struct XOnlyHashLookUp( impl PkTranslator for XOnlyHashLookUp { - fn f_pk( + fn pk( &mut self, xpk: &DescriptorPublicKey, ) -> Result { xpk.derive_public_key(&self.1) } - fn f_pkh( + fn pkh( &mut self, xpk: &DescriptorPublicKey, ) -> Result { @@ -969,7 +969,7 @@ struct KeySourceLookUp( impl PkTranslator for KeySourceLookUp { - fn f_pk( + fn pk( &mut self, xpk: &DescriptorPublicKey, ) -> Result { @@ -981,11 +981,11 @@ impl PkTranslator Result { - Ok(self.f_pk(xpk)?.to_pubkeyhash()) + Ok(self.pk(xpk)?.to_pubkeyhash()) } } diff --git a/src/test_utils.rs b/src/test_utils.rs index 5840d085c..dc3d61005 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -20,7 +20,7 @@ pub struct StrKeyTranslator { } impl Translator for StrKeyTranslator { - fn f_pk(&mut self, pk: &String) -> Result { + fn pk(&mut self, pk: &String) -> Result { let key = self.pk_map.get(pk).copied().unwrap_or_else(|| { bitcoin::PublicKey::from_str( "02c2122e30e73f7fe37986e3f81ded00158e94b7ad472369b83bbdd28a9a198a39", @@ -30,14 +30,14 @@ impl Translator for StrKeyTranslator { Ok(key) } - fn f_pkh(&mut self, pkh: &String) -> Result { + fn pkh(&mut self, pkh: &String) -> Result { let hash = self.pkh_map.get(pkh).copied().unwrap_or_else(|| { hash160::Hash::from_str("be8f27af36217ba89c793c419f058cd4e2a54e26").unwrap() }); Ok(hash) } - fn f_sha256(&mut self, _sha256: &String) -> Result { + fn sha256(&mut self, _sha256: &String) -> Result { // hard coded value let hash = sha256::Hash::from_str( "4ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260", @@ -56,7 +56,7 @@ pub struct StrXOnlyKeyTranslator { } impl Translator for StrXOnlyKeyTranslator { - fn f_pk(&mut self, pk: &String) -> Result { + fn pk(&mut self, pk: &String) -> Result { let key = self.pk_map.get(pk).copied().unwrap_or_else(|| { bitcoin::XOnlyPublicKey::from_str( "c2122e30e73f7fe37986e3f81ded00158e94b7ad472369b83bbdd28a9a198a39", @@ -66,14 +66,14 @@ impl Translator for StrXOnlyKeyTranslator { Ok(key) } - fn f_pkh(&mut self, pkh: &String) -> Result { + fn pkh(&mut self, pkh: &String) -> Result { let hash = self.pkh_map.get(pkh).copied().unwrap_or_else(|| { hash160::Hash::from_str("be8f27af36217ba89c793c419f058cd4e2a54e26").unwrap() }); Ok(hash) } - fn f_sha256(&mut self, _sha256: &String) -> Result { + fn sha256(&mut self, _sha256: &String) -> Result { // hard coded value let hash = sha256::Hash::from_str( "4ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260", diff --git a/tests/setup/test_util.rs b/tests/setup/test_util.rs index 5d37c9744..820d5c811 100644 --- a/tests/setup/test_util.rs +++ b/tests/setup/test_util.rs @@ -165,7 +165,7 @@ pub fn parse_insane_ms( struct StrDescPubKeyTranslator<'a>(usize, &'a PubData); impl<'a> Translator for StrDescPubKeyTranslator<'a> { - fn f_pk(&mut self, pk_str: &String) -> Result { + fn pk(&mut self, pk_str: &String) -> Result { let avail = !pk_str.ends_with("!"); if avail { self.0 = self.0 + 1; @@ -190,11 +190,11 @@ impl<'a> Translator for StrDescPubKeyTranslator } } - fn f_pkh(&mut self, pkh: &String) -> Result { - self.f_pk(pkh) + fn pkh(&mut self, pkh: &String) -> Result { + self.pk(pkh) } - fn f_sha256(&mut self, sha256: &String) -> Result { + fn sha256(&mut self, sha256: &String) -> Result { let sha = sha256::Hash::from_str(sha256).unwrap(); Ok(sha) } @@ -207,7 +207,7 @@ impl<'a> Translator for StrDescPubKeyTranslator struct StrTranslatorLoose<'a>(usize, &'a PubData); impl<'a> Translator for StrTranslatorLoose<'a> { - fn f_pk(&mut self, pk_str: &String) -> Result { + fn pk(&mut self, pk_str: &String) -> Result { let avail = !pk_str.ends_with("!"); if avail { self.0 = self.0 + 1; @@ -236,11 +236,11 @@ impl<'a> Translator for StrTranslatorLoose<'a> } } - fn f_pkh(&mut self, pkh: &String) -> Result { - self.f_pk(pkh) + fn pkh(&mut self, pkh: &String) -> Result { + self.pk(pkh) } - fn f_sha256(&mut self, sha256: &String) -> Result { + fn sha256(&mut self, sha256: &String) -> Result { let sha = sha256::Hash::from_str(sha256).unwrap(); Ok(sha) }