tree

package
v0.0.0-...-e2bdea5 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 17 Imported by: 6

Documentation

Index

Constants

View Source
const (
	OP_INSPECTOUTPUTSCRIPTPUBKEY = 0xd1
	OP_INSPECTOUTPUTVALUE        = 0xcf
	OP_PUSHCURRENTINPUTINDEX     = 0xcd
	OP_INSPECTINPUTVALUE         = 0xc9
	OP_SUB64                     = 0xd8
)
View Source
const ConditionWitnessKey = "condition"

Variables

View Source
var (
	ErrParentNotFound = errors.New("parent not found")
	ErrLeafNotFound   = errors.New("leaf not found in vtxo tree")
)
View Source
var (
	ErrInvalidRoundTx           = fmt.Errorf("invalid round transaction")
	ErrInvalidRoundTxOutputs    = fmt.Errorf("invalid number of outputs in round transaction")
	ErrEmptyTree                = fmt.Errorf("empty vtxo tree")
	ErrInvalidRootLevel         = fmt.Errorf("root level must have only one node")
	ErrNoLeaves                 = fmt.Errorf("no leaves in the tree")
	ErrNodeTxEmpty              = fmt.Errorf("node transaction is empty")
	ErrNodeTxidEmpty            = fmt.Errorf("node txid is empty")
	ErrNodeParentTxidEmpty      = fmt.Errorf("node parent txid is empty")
	ErrNodeTxidDifferent        = fmt.Errorf("node txid differs from node transaction")
	ErrNumberOfInputs           = fmt.Errorf("node transaction should have only one input")
	ErrNumberOfOutputs          = fmt.Errorf("node transaction should have only three or two outputs")
	ErrParentTxidInput          = fmt.Errorf("parent txid should be the input of the node transaction")
	ErrNumberOfChildren         = fmt.Errorf("node branch transaction should have two children")
	ErrLeafChildren             = fmt.Errorf("leaf node should have max 1 child")
	ErrInvalidChildTxid         = fmt.Errorf("invalid child txid")
	ErrNumberOfTapscripts       = fmt.Errorf("input should have 1 tapscript leaf")
	ErrInternalKey              = fmt.Errorf("invalid taproot internal key")
	ErrInvalidTaprootScript     = fmt.Errorf("invalid taproot script")
	ErrInvalidTaprootScriptLen  = fmt.Errorf("invalid taproot script length (expected 32 bytes)")
	ErrInvalidLeafTaprootScript = fmt.Errorf("invalid leaf taproot script")
	ErrInvalidAmount            = fmt.Errorf("children amount is different from parent amount")
	ErrInvalidAsset             = errors.New("invalid output asset")
	ErrInvalidSweepSequence     = fmt.Errorf("invalid sweep sequence")
	ErrInvalidServer            = fmt.Errorf("invalid server")
	ErrMissingFeeOutput         = fmt.Errorf("missing fee output")
	ErrInvalidLeftOutput        = fmt.Errorf("invalid left output")
	ErrInvalidRightOutput       = fmt.Errorf("invalid right output")
	ErrMissingSweepTapscript    = fmt.Errorf("missing sweep tapscript")
	ErrMissingBranchTapscript   = errors.New("missing branch tapscript")
	ErrInvalidLeaf              = fmt.Errorf("leaf node shouldn't have children")
	ErrWrongRoundTxid           = fmt.Errorf("the input of the tree root is not the round tx's shared output")
)
View Source
var (
	CONDITION_WITNESS_KEY_PREFIX = []byte(ConditionWitnessKey)
)
View Source
var (
	ErrNoExitLeaf = fmt.Errorf("no exit leaf")
)

Functions

func AddConditionWitness

func AddConditionWitness(inIndex int, ptx *psetv2.Pset, witness wire.TxWitness) error

func BuildForfeitTxs

func BuildForfeitTxs(
	connectorTx *psetv2.Pset, vtxoInput psetv2.InputArgs,
	vtxoAmount, connectorAmount, feeAmount uint64,
	vtxoScript, serverScript []byte,
) (forfeitTxs []*psetv2.Pset, err error)

func ExecuteBoolScript

func ExecuteBoolScript(script []byte, witness wire.TxWitness) (bool, error)

ExecuteBoolScript run the script with the provided witness as argument the result must be a boolean value accepted by OP_IF / OP_NOTIF opcodes

func GetConditionWitness

func GetConditionWitness(in psetv2.Input) (wire.TxWitness, error)

func ReadTxWitness

func ReadTxWitness(witnessSerialized []byte) (wire.TxWitness, error)

func UnspendableKey

func UnspendableKey() *secp256k1.PublicKey

func ValidateVtxoTree

func ValidateVtxoTree(
	tree VtxoTree, roundTx string, serverPubkey *secp256k1.PublicKey,
	roundLifetime common.RelativeLocktime,
) error

ValidateVtxoTree checks if the given vtxo tree is valid roundTxid & roundTxIndex & roundTxAmount are used to validate the root input outpoint serverPubkey & roundLifetime are used to validate the sweep tapscript leaves besides that, the function validates: - the number of nodes - the number of leaves - children coherence with parent - every control block and taproot output scripts - input and output amounts

Types

type CLTVMultisigClosure

type CLTVMultisigClosure struct {
	MultisigClosure
	Locktime common.AbsoluteLocktime
}

CLTVMultisigClosure is a closure that contains a list of public keys and a CHECKLOCKTIMEVERIFY. The witness size is 64 bytes per key, admitting the sighash type is SIGHASH_DEFAULT.

func (*CLTVMultisigClosure) Decode

func (d *CLTVMultisigClosure) Decode(script []byte) (bool, error)

func (*CLTVMultisigClosure) Script

func (d *CLTVMultisigClosure) Script() ([]byte, error)

func (*CLTVMultisigClosure) Witness

func (f *CLTVMultisigClosure) Witness(controlBlock []byte, signatures map[string][]byte) (wire.TxWitness, error)

func (*CLTVMultisigClosure) WitnessSize

func (f *CLTVMultisigClosure) WitnessSize(extraWitnessSizes ...int) int

type CSVMultisigClosure

type CSVMultisigClosure struct {
	MultisigClosure
	Locktime common.RelativeLocktime
}

CSVMultisigClosure is a closure that contains a list of public keys and a CHECKSEQUENCEVERIFY. The witness size is 64 bytes per key, admitting the sighash type is SIGHASH_DEFAULT.

func (*CSVMultisigClosure) Decode

func (d *CSVMultisigClosure) Decode(script []byte) (bool, error)

func (*CSVMultisigClosure) Script

func (d *CSVMultisigClosure) Script() ([]byte, error)

func (*CSVMultisigClosure) Witness

func (f *CSVMultisigClosure) Witness(controlBlock []byte, signatures map[string][]byte) (wire.TxWitness, error)

func (*CSVMultisigClosure) WitnessSize

func (f *CSVMultisigClosure) WitnessSize(extraWitnessSizes ...int) int

type Closure

type Closure interface {
	Script() ([]byte, error)
	Decode(script []byte) (bool, error)
	// WitnessSize returns the size of the witness excluding the script and control block
	// extraWitnessSize is here to count the condition witness size
	// or any other witness size that can't be computed from the script
	WitnessSize(extraWitnessSize ...int) int
	Witness(controlBlock []byte, signatures map[string][]byte) (wire.TxWitness, error)
}

func DecodeClosure

func DecodeClosure(script []byte) (Closure, error)

type ConditionCSVMultisigClosure

type ConditionCSVMultisigClosure struct {
	CSVMultisigClosure
	Condition []byte
}

ConditionCSVMultisigClosure is a closure that contains a condition and a CSV multisig closure. The condition is a boolean script that is executed with the multisig witness.

func (*ConditionCSVMultisigClosure) Decode

func (f *ConditionCSVMultisigClosure) Decode(script []byte) (bool, error)

func (*ConditionCSVMultisigClosure) Script

func (f *ConditionCSVMultisigClosure) Script() ([]byte, error)

func (*ConditionCSVMultisigClosure) Witness

func (f *ConditionCSVMultisigClosure) Witness(controlBlock []byte, args map[string][]byte) (wire.TxWitness, error)

func (*ConditionCSVMultisigClosure) WitnessSize

func (f *ConditionCSVMultisigClosure) WitnessSize(conditionWitnessSizes ...int) int

type ConditionMultisigClosure

type ConditionMultisigClosure struct {
	MultisigClosure
	Condition []byte
}

ConditionMultisigClosure is a closure that contains a condition and a multisig closure. The condition is a boolean script that is executed with the multisig witness.

func (*ConditionMultisigClosure) Decode

func (f *ConditionMultisigClosure) Decode(script []byte) (bool, error)

func (*ConditionMultisigClosure) Script

func (f *ConditionMultisigClosure) Script() ([]byte, error)

func (*ConditionMultisigClosure) Witness

func (f *ConditionMultisigClosure) Witness(controlBlock []byte, args map[string][]byte) (wire.TxWitness, error)

func (*ConditionMultisigClosure) WitnessSize

func (f *ConditionMultisigClosure) WitnessSize(conditionWitnessSizes ...int) int

type MultisigClosure

type MultisigClosure struct {
	PubKeys []*secp256k1.PublicKey
	Type    MultisigType
}

MultisigClosure is a closure that contains a list of public keys and a CHECKSIG for each key. The witness size is 64 bytes per key, admitting the sighash type is SIGHASH_DEFAULT.

func (*MultisigClosure) Decode

func (f *MultisigClosure) Decode(script []byte) (bool, error)

func (*MultisigClosure) Script

func (f *MultisigClosure) Script() ([]byte, error)

func (*MultisigClosure) Witness

func (f *MultisigClosure) Witness(controlBlock []byte, signatures map[string][]byte) (wire.TxWitness, error)

func (*MultisigClosure) WitnessSize

func (f *MultisigClosure) WitnessSize(_ ...int) int

type MultisigType

type MultisigType int
const (
	MultisigTypeChecksig MultisigType = iota
	MultisigTypeChecksigAdd
)

type Node

type Node struct {
	Txid       string
	Tx         string
	ParentTxid string
	Leaf       bool
}

Node is a struct embedding the transaction and the parent txid of a vtxo tree node

type TapscriptsVtxoScript

type TapscriptsVtxoScript struct {
	Closures []Closure
}

TapscriptsVtxoScript represents a taproot script that contains a list of tapscript leaves the key-path is always unspendable

func NewDefaultVtxoScript

func NewDefaultVtxoScript(owner, server *secp256k1.PublicKey, exitDelay common.RelativeLocktime) *TapscriptsVtxoScript

func (*TapscriptsVtxoScript) Decode

func (v *TapscriptsVtxoScript) Decode(scripts []string) error

func (*TapscriptsVtxoScript) Encode

func (v *TapscriptsVtxoScript) Encode() ([]string, error)

func (*TapscriptsVtxoScript) ExitClosures

func (v *TapscriptsVtxoScript) ExitClosures() []Closure

func (*TapscriptsVtxoScript) ForfeitClosures

func (v *TapscriptsVtxoScript) ForfeitClosures() []Closure

func (*TapscriptsVtxoScript) SmallestExitDelay

func (v *TapscriptsVtxoScript) SmallestExitDelay() (*common.RelativeLocktime, error)

func (*TapscriptsVtxoScript) TapTree

func (v *TapscriptsVtxoScript) TapTree() (*secp256k1.PublicKey, elementsTapTree, error)

func (*TapscriptsVtxoScript) Validate

func (v *TapscriptsVtxoScript) Validate(server *secp256k1.PublicKey, minLocktime common.RelativeLocktime) error

type TreeFactory

type TreeFactory func(outpoint psetv2.InputArgs) (VtxoTree, error)

func BuildVtxoTree

func BuildVtxoTree(
	asset string, serverPubkey *secp256k1.PublicKey, receivers []VtxoLeaf,
	feeSatsPerNode uint64, roundLifetime common.RelativeLocktime,
) (
	factoryFn TreeFactory,
	sharedOutputScript []byte, sharedOutputAmount uint64, err error,
)

type UnrollClosure

type UnrollClosure struct {
	LeftKey, RightKey       *secp256k1.PublicKey
	LeftAmount, RightAmount uint64
	MinRelayFee             uint64
}

UnrollClosure is liquid-only tapscript letting to enforce unrollable UTXO without musig.

func (*UnrollClosure) Decode

func (c *UnrollClosure) Decode(script []byte) (valid bool, err error)

func (*UnrollClosure) Script

func (c *UnrollClosure) Script() ([]byte, error)

func (*UnrollClosure) Witness

func (c *UnrollClosure) Witness(controlBlock []byte, _ map[string][]byte) (wire.TxWitness, error)

func (*UnrollClosure) WitnessSize

func (c *UnrollClosure) WitnessSize(_ ...int) int

type VtxoLeaf

type VtxoLeaf struct {
	PubKey string
	Amount uint64
}

type VtxoScript

type VtxoScript common.VtxoScript[elementsTapTree, Closure]

func ParseVtxoScript

func ParseVtxoScript(scripts []string) (VtxoScript, error)

type VtxoTree

type VtxoTree [][]Node

VtxoTree is reprensented as a matrix of TreeNode struct the first level of the matrix is the root of the tree

func (VtxoTree) Branch

func (c VtxoTree) Branch(vtxoTxid string) ([]Node, error)

Branch returns the branch of the given vtxo txid from root to leaf in the order of the vtxo tree

func (VtxoTree) Children

func (c VtxoTree) Children(nodeTxid string) []Node

Children returns all the nodes that have the given node as parent

func (VtxoTree) Leaves

func (c VtxoTree) Leaves() []Node

Leaves returns the leaves of the vtxo tree

func (VtxoTree) NumberOfNodes

func (c VtxoTree) NumberOfNodes() int

NumberOfNodes returns the total number of pset in the vtxo tree

func (VtxoTree) Root

func (c VtxoTree) Root() (Node, error)

Root returns the root node of the vtxo tree

Jump to

Keyboard shortcuts

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