From 7969b7a43e0f1415f60baa6729a3f4b5963718ed Mon Sep 17 00:00:00 2001 From: Jeremy Rubin Date: Sun, 3 Apr 2022 12:19:55 -0400 Subject: [PATCH] Make TaprooBuilder::finalize able to return keyspend only --- src/util/taproot.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index b7880402f2..1d8edd96a5 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -467,20 +467,21 @@ impl TaprootBuilder { } /// Creates a [`TaprootSpendInfo`] with the given internal key. + /// + // TODO: in a future breaking API change, this no longer needs to return result pub fn finalize( mut self, secp: &Secp256k1, internal_key: UntweakedPublicKey, ) -> Result { - if self.branch.len() > 1 { - return Err(TaprootBuilderError::IncompleteTree); + match self.branch.pop() { + None => Ok(TaprootSpendInfo::new_key_spend(secp, internal_key, None)), + Some(Some(node)) => { + Ok(TaprootSpendInfo::from_node_info(secp, internal_key, node)) + } + _ => Err(TaprootBuilderError::IncompleteTree), + } - let node = self - .branch - .pop() - .ok_or(TaprootBuilderError::EmptyTree)? - .expect("Builder invariant: last element of the branch must be some"); - Ok(TaprootSpendInfo::from_node_info(secp, internal_key, node)) } pub(crate) fn branch(&self) -> &[Option] {