Make TaprooBuilder::finalize able to return keyspend only

This commit is contained in:
Jeremy Rubin 2022-04-03 12:19:55 -04:00
parent cb4d34fd40
commit 7969b7a43e
1 changed files with 9 additions and 8 deletions

View File

@ -467,20 +467,21 @@ impl TaprootBuilder {
} }
/// Creates a [`TaprootSpendInfo`] with the given internal key. /// 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<C: secp256k1::Verification>( pub fn finalize<C: secp256k1::Verification>(
mut self, mut self,
secp: &Secp256k1<C>, secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey, internal_key: UntweakedPublicKey,
) -> Result<TaprootSpendInfo, TaprootBuilderError> { ) -> Result<TaprootSpendInfo, TaprootBuilderError> {
if self.branch.len() > 1 { match self.branch.pop() {
return Err(TaprootBuilderError::IncompleteTree); 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<NodeInfo>] { pub(crate) fn branch(&self) -> &[Option<NodeInfo>] {