Make NodeInfo API public
This allows users to create TaprootSpendInfo using NodeInfo. This offers an alternative to TaprootBuilder.
This commit is contained in:
parent
ea80e6568a
commit
208eb65f1b
|
@ -296,8 +296,10 @@ impl TaprootSpendInfo {
|
||||||
self.output_key_parity
|
self.output_key_parity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal function to compute [`TaprootSpendInfo`] from NodeInfo
|
/// Compute [`TaprootSpendInfo`] from [`NodeInfo`], and internal key.
|
||||||
fn from_node_info<C: secp256k1::Verification>(
|
/// This is useful when you want to manually build a taproot tree wihtout
|
||||||
|
/// using [`TaprootBuilder`].
|
||||||
|
pub fn from_node_info<C: secp256k1::Verification>(
|
||||||
secp: &Secp256k1<C>,
|
secp: &Secp256k1<C>,
|
||||||
internal_key: UntweakedPublicKey,
|
internal_key: UntweakedPublicKey,
|
||||||
node: NodeInfo,
|
node: NodeInfo,
|
||||||
|
@ -497,10 +499,12 @@ impl TaprootBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internally used structure to represent the node information in taproot tree
|
/// Data structure used to represent node information in taproot tree.
|
||||||
|
/// You can use [`TaprootSpendInfo::from_node_info`] to a get [`TaprootSpendInfo`]
|
||||||
|
/// from the merkle root [`NodeInfo`].
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub(crate) struct NodeInfo {
|
pub struct NodeInfo {
|
||||||
/// Merkle Hash for this node
|
/// Merkle Hash for this node
|
||||||
pub(crate) hash: sha256::Hash,
|
pub(crate) hash: sha256::Hash,
|
||||||
/// information about leaves inside this node
|
/// information about leaves inside this node
|
||||||
|
@ -508,16 +512,16 @@ pub(crate) struct NodeInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NodeInfo {
|
impl NodeInfo {
|
||||||
// Create a new NodeInfo with omitted/hidden info
|
/// Creates a new [`NodeInfo`] with omitted/hidden info.
|
||||||
fn new_hidden(hash: sha256::Hash) -> Self {
|
pub fn new_hidden(hash: sha256::Hash) -> Self {
|
||||||
Self {
|
Self {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
leaves: vec![],
|
leaves: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new leaf with NodeInfo
|
/// Creates a new leaf [`NodeInfo`] with given [`Script`] and [`LeafVersion`].
|
||||||
fn new_leaf_with_ver(script: Script, ver: LeafVersion) -> Self {
|
pub fn new_leaf_with_ver(script: Script, ver: LeafVersion) -> Self {
|
||||||
let leaf = LeafInfo::new(script, ver);
|
let leaf = LeafInfo::new(script, ver);
|
||||||
Self {
|
Self {
|
||||||
hash: leaf.hash(),
|
hash: leaf.hash(),
|
||||||
|
@ -525,8 +529,8 @@ impl NodeInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine two NodeInfo's to create a new parent
|
/// Combines two [`NodeInfo`] to create a new parent.
|
||||||
fn combine(a: Self, b: Self) -> Result<Self, TaprootBuilderError> {
|
pub fn combine(a: Self, b: Self) -> Result<Self, TaprootBuilderError> {
|
||||||
let mut all_leaves = Vec::with_capacity(a.leaves.len() + b.leaves.len());
|
let mut all_leaves = Vec::with_capacity(a.leaves.len() + b.leaves.len());
|
||||||
for mut a_leaf in a.leaves {
|
for mut a_leaf in a.leaves {
|
||||||
a_leaf.merkle_branch.push(b.hash)?; // add hashing partner
|
a_leaf.merkle_branch.push(b.hash)?; // add hashing partner
|
||||||
|
|
Loading…
Reference in New Issue