From f985fd787917e344b17ae90edd7b99bc1c9f3a7c Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Fri, 21 Oct 2022 13:12:23 +0200 Subject: [PATCH] descriptors: remove as_inner method We'll change the semantic of the descriptor, so we need to make sure nothing accesses it with the old semantic. --- src/config.rs | 11 ++--------- src/descriptors.rs | 14 +++++++++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/config.rs b/src/config.rs index f4ef1ca3..a873abf4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,7 @@ use crate::descriptors::InheritanceDescriptor; use std::{net::SocketAddr, path::PathBuf, str::FromStr, time::Duration}; -use miniscript::{bitcoin::Network, DescriptorPublicKey, ForEachKey}; +use miniscript::bitcoin::Network; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; @@ -203,14 +203,7 @@ impl Config { Network::Bitcoin => Network::Bitcoin, _ => Network::Testnet, }; - let unexpected_net = self.main_descriptor.as_inner().for_each_key(|xpub| { - if let DescriptorPublicKey::XPub(xpub) = xpub { - xpub.xkey.network != expected_network - } else { - false - } - }); - if unexpected_net { + if !self.main_descriptor.all_xpubs_net_is(expected_network) { return Err(ConfigError::Unexpected(format!( "Our bitcoin network is {} but one xpub is not for network {}", self.bitcoin_config.network, expected_network diff --git a/src/descriptors.rs b/src/descriptors.rs index 8a595f46..a39ed120 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -9,7 +9,8 @@ use miniscript::{ descriptor, hash256, miniscript::{decode::Terminal, Miniscript}, policy::{Liftable, Semantic as SemanticPolicy}, - translate_hash_clone, MiniscriptKey, ScriptContext, ToPublicKey, TranslatePk, Translator, + translate_hash_clone, ForEachKey, MiniscriptKey, ScriptContext, ToPublicKey, TranslatePk, + Translator, }; use std::{collections::BTreeMap, convert::TryFrom, error, fmt, str, sync}; @@ -315,8 +316,15 @@ impl InheritanceDescriptor { ))) } - pub fn as_inner(&self) -> &descriptor::Descriptor { - &self.0 + /// Whether all xpubs contained in this descriptor are for the passed expected network. + pub fn all_xpubs_net_is(&self, expected_net: bitcoin::Network) -> bool { + self.0.for_each_key(|xpub| { + if let descriptor::DescriptorPublicKey::XPub(xpub) = xpub { + xpub.xkey.network == expected_net + } else { + false + } + }) } /// Derive this descriptor at a given index.