tree

package
v0.0.0-...-d8d4545 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 18 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidBatchOutputsNum = fmt.Errorf(
		"invalid number of batch outputs in commitment transaction",
	)
	ErrEmptyTree                  = fmt.Errorf("empty vtxo tree")
	ErrNoLeaves                   = fmt.Errorf("no leaves in the tree")
	ErrInvalidTaprootScript       = fmt.Errorf("invalid taproot script")
	ErrMissingCosignersPublicKeys = fmt.Errorf("missing cosigners public keys")
	ErrInvalidAmount              = fmt.Errorf("children amount is different from parent amount")
	ErrBatchOutputMismatch        = fmt.Errorf(
		"invalid vtxo tree root, tx input does not match batch outpoint",
	)
)
View Source
var (
	ErrMissingVtxoTree = errors.New("missing vtxo tree")
)

Functions

func AggregateKeys

func AggregateKeys(pubkeys []*btcec.PublicKey, tweak []byte) (*musig2.AggregateKey, error)

AggregateKeys is a wrapper around musig2.AggregateKeys using the given tweak

func BuildBatchOutput

func BuildBatchOutput(receivers []Leaf, sweepTapTreeRoot []byte) ([]byte, int64, error)

BuildBatchOutput returns the taproot script and amount of a batch output of the commiment tx. The radix of the vtxo tree is hardcoded to 2.

func BuildConnectorOutput

func BuildConnectorOutput(receivers []Leaf) ([]byte, int64, error)

BuildConnectorOutput returns the taproot script and amount of a connector output of the commitment tx. The radix of the connector tree is hardcoded to 4.

func BuildForfeitTx

func BuildForfeitTx(
	inputs []*wire.OutPoint, sequences []uint32, prevouts []*wire.TxOut,
	signerScript []byte, txLocktime uint32,
) (*psbt.Packet, error)

func BuildForfeitTxWithOutput

func BuildForfeitTxWithOutput(
	inputs []*wire.OutPoint, sequences []uint32, prevouts []*wire.TxOut,
	forfeitOutput *wire.TxOut,
	txLocktime uint32,
) (*psbt.Packet, error)

func ValidateTreeSigs

func ValidateTreeSigs(
	batchOutSweepClosure []byte, batchOutAmount int64, vtxoTree *TxTree,
) error

ValidateTreeSigs validates the signatures on the given vtxo tree.

It validates that each input in the tree has a valid signature, and that the signature was generated using the aggregated public key of all cosigners.

The function returns an error if any of the signatures are invalid, or if there are any issues with the aggregated public key.

func ValidateVtxoTree

func ValidateVtxoTree(
	vtxoTree *TxTree, commitmentTx *psbt.Packet,
	signerPubkey *btcec.PublicKey, vtxoTreeExpiry arklib.RelativeLocktime,
) error

ValidateVtxoTree checks if the given vtxo tree is valid vtxoTree tx & commitmentTx are used to validate that the tree spends from the batch outpoint. signerPubkey & vtxoTreeExpiry are used to validate the sweep tapscript leaves. Along with that, the function validates: - the number of nodes - the number of leaves - children spend from parent - every control block and taproot output scripts - each tx matches input and output amounts

Types

type CoordinatorSession

type CoordinatorSession interface {
	AddNonce(*btcec.PublicKey, TreeNonces)
	AddSignatures(*btcec.PublicKey, TreePartialSigs)
	AggregateNonces() (TreeNonces, error)
	// SignTree combines the signatures and add them to the tree's psbts
	SignTree() (*TxTree, error)
}

func NewTreeCoordinatorSession

func NewTreeCoordinatorSession(
	batchOutSweepClosure []byte, batchOutAmount int64, vtxoTree *TxTree,
) (CoordinatorSession, error)

type FlatTxTree

type FlatTxTree []TxTreeNode

FlatTxTree is the flat representation of a tx tree. The purpose of this struct is to facilitate the persistance of the tx tree in storage services.

func (FlatTxTree) Leaves

func (c FlatTxTree) Leaves() []TxTreeNode

type Leaf

type Leaf struct {
	Script              string
	Amount              uint64
	CosignersPublicKeys []string
}

Leaf represents the output of a leaf transaction.

type Musig2Nonce

type Musig2Nonce struct {
	PubNonce [66]byte
}

type SignerSession

type SignerSession interface {
	Init(batchOutSweepClosure []byte, batchOutAmount int64, vtxoTree *TxTree) error
	GetPublicKey() string
	GetNonces() (TreeNonces, error) // generate tree nonces for this session
	SetAggregatedNonces(TreeNonces) // set the aggregated nonces
	Sign() (TreePartialSigs, error) // sign the tree
}

func NewTreeSignerSession

func NewTreeSignerSession(signer *btcec.PrivateKey) SignerSession

type TreeNonces

type TreeNonces map[string]*Musig2Nonce // txid -> public nonces only

TreeNonces is a map of txid to public nonces only it implements json.Marshaler and json.Unmarshaler

func (TreeNonces) MarshalJSON

func (n TreeNonces) MarshalJSON() ([]byte, error)

func (*TreeNonces) UnmarshalJSON

func (n *TreeNonces) UnmarshalJSON(data []byte) error

type TreePartialSigs

type TreePartialSigs map[string]*musig2.PartialSignature // txid -> partial signature

TreePartialSigs is a map of txid to partial signature it implements json.Marshaler and json.Unmarshaler

func (TreePartialSigs) MarshalJSON

func (s TreePartialSigs) MarshalJSON() ([]byte, error)

func (*TreePartialSigs) UnmarshalJSON

func (s *TreePartialSigs) UnmarshalJSON(data []byte) error

type TxTree

type TxTree struct {
	Root     *psbt.Packet
	Children map[uint32]*TxTree // output index -> sub-tree
}

TxTree is the recursive reprensation of tree of transactions. It is used to represent the vtxo and connector trees.

func BuildConnectorTree

func BuildConnectorTree(rootInput *wire.OutPoint, receivers []Leaf) (*TxTree, error)

BuildConnectorTree creates the connector tree, ie the tree of transactions from the one spending the connector output to those creating the connectors used to forfeit vtxos in the batch process. The radix of the tree is hardcoded to 4.

func BuildVtxoTree

func BuildVtxoTree(
	rootInput *wire.OutPoint, receivers []Leaf,
	sweepTapTreeRoot []byte, vtxoTreeExpiry arklib.RelativeLocktime,
) (*TxTree, error)

BuildVtxoTree creates the vtxo tree, ie. the tree of transactions from the one spending the batch output to those creating the vtxos (the leaves of the tree). The radix of the tree is hardcoded to 2.

func NewTxTree

func NewTxTree(flatTxTree FlatTxTree) (*TxTree, error)

NewTxTree converts a flat list of nodes to a tree of transactions.

func (*TxTree) Apply

func (t *TxTree) Apply(fn func(tx *TxTree) (bool, error)) error

Apply executes the given function to all txs in the tx tree. The function returns a boolean to indicate whether it should continue applying to the children.

func (*TxTree) Find

func (t *TxTree) Find(txid string) *TxTree

Find returns the tx in the tree that matches the provided txid.

func (*TxTree) Leaves

func (t *TxTree) Leaves() []*psbt.Packet

Leaves returns the leaves of the tx tree, ie. the nodes that don't have any children.

func (*TxTree) Serialize

func (t *TxTree) Serialize() (FlatTxTree, error)

Serialize converts the tx tree into a flat list of nodes.

func (*TxTree) SerializeNode

func (t *TxTree) SerializeNode() (*TxTreeNode, error)

SerializeNode converts the node of a tx tree into its flat representation.

func (*TxTree) SubTree

func (t *TxTree) SubTree(txids []string) (*TxTree, error)

SubTree returns the sub-tree that contains the given txs, ie all paths from root to the given txids, if they exist in the tree.

func (*TxTree) Validate

func (t *TxTree) Validate() error

Validate verifies the validity of the tx tree. It verifies : - every tx is a valid partial transaction. - every tx has exactly one input. - the child txs spend the right parent's output - the sum of the child txs' output amounts matches the parent tx input amount

type TxTreeNode

type TxTreeNode struct {
	Txid string
	// Tx is the base64 encoded root PSBT
	Tx string
	// Children maps root output index to child txid
	Children map[uint32]string
}

TxTreeNode is a flat represenation of a node of tx tree. The purpose of this struct is to facilitate the persistance of the tx tree in storage services.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL