-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvtxo.go
65 lines (53 loc) · 1.97 KB
/
vtxo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package common
import (
"errors"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
var (
ErrWrongDescriptor = errors.New("wrong descriptor, cannot parse vtxo script")
)
type TaprootMerkleProof struct {
ControlBlock []byte
Script []byte
}
// TaprootTree is an interface wrapping the methods needed to spend a vtxo taproot contract
// the implementation depends on the chain (liquid or bitcoin)
type TaprootTree interface {
GetLeaves() []chainhash.Hash
GetTaprootMerkleProof(leafhash chainhash.Hash) (*TaprootMerkleProof, error)
GetRoot() chainhash.Hash
}
/*
A vtxo script is defined as a taproot contract with at least 1 forfeit closure (User && Server) and 1 exit closure (A after t).
It may also contain others closures implementing specific use cases.
VtxoScript abstracts the taproot complexity behind vtxo contracts.
it is compiled, transferred and parsed using descriptor string.
// TODO gather common and tree package to prevent circular dependency and move C generic
*/
type VtxoScript[T TaprootTree, C interface{}] interface {
Validate(server *secp256k1.PublicKey, minLocktime RelativeLocktime) error
TapTree() (taprootKey *secp256k1.PublicKey, taprootScriptTree T, err error)
Encode() ([]string, error)
Decode(scripts []string) error
SmallestExitDelay() (*RelativeLocktime, error)
ForfeitClosures() []C
ExitClosures() []C
}
// BiggestLeafMerkleProof returns the leaf with the biggest witness size (for fee estimation)
// we need this to estimate the fee without knowning the exact leaf that will be spent
func BiggestLeafMerkleProof(t TaprootTree) (*TaprootMerkleProof, error) {
var biggest *TaprootMerkleProof
var biggestSize int
for _, leaf := range t.GetLeaves() {
proof, err := t.GetTaprootMerkleProof(leaf)
if err != nil {
return nil, err
}
if len(proof.ControlBlock)+len(proof.Script) > biggestSize {
biggest = proof
biggestSize = len(proof.ControlBlock) + len(proof.Script)
}
}
return biggest, nil
}