Files
ark/common/bitcointree/vtxo.go
Louis Singer 06dd01ecb1 Change representation of taproot trees & Internal fixes (#384)
* migrate descriptors --> tapscripts

* fix covenantless

* dynamic boarding exit delay

* remove duplicates in tree and bitcointree

* agnostic signatures validation

* revert GetInfo change

* renaming VtxoScript var

* Agnostic script server (#6)

* Hotfix: Prevent ZMQ-based bitcoin wallet to panic  (#383)

* Hotfix bct embedded wallet w/ ZMQ

* Fixes

* Rename vtxo is_oor to is_pending (#385)

* Rename vtxo is_oor > is_pending

* Clean swaggers

* Revert changes to client and sdk

* descriptor in oneof

* support CHECKSIG_ADD in MultisigClosure

* use right witness size in OOR tx fee estimation

* Revert changes

---------

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
2024-11-20 18:51:03 +01:00

94 lines
2.4 KiB
Go

package bitcointree
import (
"fmt"
"github.com/ark-network/ark/common"
"github.com/ark-network/ark/common/tree"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
type VtxoScript common.VtxoScript[bitcoinTapTree, *tree.MultisigClosure, *tree.CSVSigClosure]
func ParseVtxoScript(scripts []string) (VtxoScript, error) {
types := []VtxoScript{
&TapscriptsVtxoScript{},
}
for _, v := range types {
if err := v.Decode(scripts); err == nil {
return v, nil
}
}
return nil, fmt.Errorf("invalid vtxo scripts: %s", scripts)
}
func NewDefaultVtxoScript(owner, asp *secp256k1.PublicKey, exitDelay uint) VtxoScript {
base := tree.NewDefaultVtxoScript(owner, asp, exitDelay)
return &TapscriptsVtxoScript{*base}
}
type TapscriptsVtxoScript struct {
tree.TapscriptsVtxoScript
}
func (v *TapscriptsVtxoScript) TapTree() (*secp256k1.PublicKey, bitcoinTapTree, error) {
leaves := make([]txscript.TapLeaf, len(v.Closures))
for i, closure := range v.Closures {
script, err := closure.Script()
if err != nil {
return nil, bitcoinTapTree{}, fmt.Errorf("failed to get script for closure %d: %w", i, err)
}
leaves[i] = txscript.NewBaseTapLeaf(script)
}
tapTree := txscript.AssembleTaprootScriptTree(leaves...)
root := tapTree.RootNode.TapHash()
taprootKey := txscript.ComputeTaprootOutputKey(
UnspendableKey(),
root[:],
)
return taprootKey, bitcoinTapTree{tapTree}, nil
}
// bitcoinTapTree is a wrapper around txscript.IndexedTapScriptTree to implement the common.TaprootTree interface
type bitcoinTapTree struct {
*txscript.IndexedTapScriptTree
}
func (b bitcoinTapTree) GetRoot() chainhash.Hash {
return b.RootNode.TapHash()
}
func (b bitcoinTapTree) GetTaprootMerkleProof(leafhash chainhash.Hash) (*common.TaprootMerkleProof, error) {
index, ok := b.LeafProofIndex[leafhash]
if !ok {
return nil, fmt.Errorf("leaf %s not found in tree", leafhash.String())
}
proof := b.LeafMerkleProofs[index]
controlBlock := proof.ToControlBlock(UnspendableKey())
controlBlockBytes, err := controlBlock.ToBytes()
if err != nil {
return nil, err
}
return &common.TaprootMerkleProof{
ControlBlock: controlBlockBytes,
Script: proof.Script,
}, nil
}
func (b bitcoinTapTree) GetLeaves() []chainhash.Hash {
leafHashes := make([]chainhash.Hash, 0)
for hash := range b.LeafProofIndex {
leafHashes = append(leafHashes, hash)
}
return leafHashes
}