diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 512d6a9f83..a50a7d5363 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -479,20 +479,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] {