forked from BuxOrg/bux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeef_tx.go
117 lines (91 loc) · 2.8 KB
/
beef_tx.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package bux
import (
"context"
"encoding/hex"
"errors"
"fmt"
)
const maxBeefVer = uint32(0xFFFF) // value from BRC-62
// ToBeefHex generates BEEF Hex for transaction
func ToBeefHex(ctx context.Context, tx *Transaction) (string, error) {
beef, err := newBeefTx(ctx, 1, tx)
if err != nil {
return "", fmt.Errorf("ToBeefHex() error: %w", err)
}
beefBytes, err := beef.toBeefBytes()
if err != nil {
return "", fmt.Errorf("ToBeefHex() error: %w", err)
}
return hex.EncodeToString(beefBytes), nil
}
type beefTx struct {
version uint32
compoundMerklePaths CMPSlice
transactions []*Transaction
}
func newBeefTx(ctx context.Context, version uint32, tx *Transaction) (*beefTx, error) {
if version > maxBeefVer {
return nil, fmt.Errorf("version above 0x%X", maxBeefVer)
}
var err error
if err = hydrateTransaction(ctx, tx); err != nil {
return nil, err
}
if err = validateCompoundMerklePathes(tx.draftTransaction.CompoundMerklePathes); err != nil {
return nil, err
}
// get inputs parent transactions
inputs := tx.draftTransaction.Configuration.Inputs
transactions := make([]*Transaction, 0, len(inputs)+1)
for _, input := range inputs {
prevTxs, err := getParentTransactionsForInput(ctx, tx.client, input)
if err != nil {
return nil, fmt.Errorf("retrieve input parent transaction failed: %w", err)
}
transactions = append(transactions, prevTxs...)
}
// add current transaction
transactions = append(transactions, tx)
beef := &beefTx{
version: version,
compoundMerklePaths: tx.draftTransaction.CompoundMerklePathes,
transactions: kahnTopologicalSortTransactions(transactions),
}
return beef, nil
}
func hydrateTransaction(ctx context.Context, tx *Transaction) error {
if tx.draftTransaction == nil {
dTx, err := getDraftTransactionID(
ctx, tx.XPubID, tx.DraftID, tx.GetOptions(false)...,
)
if err != nil {
return fmt.Errorf("retrieve DraftTransaction failed: %w", err)
}
tx.draftTransaction = dTx
}
return nil
}
func validateCompoundMerklePathes(compountedPaths CMPSlice) error {
if len(compountedPaths) == 0 {
return errors.New("empty compounted paths slice")
}
for _, c := range compountedPaths {
if len(c) == 0 {
return errors.New("one of compounted merkle paths is empty")
}
}
return nil
}
func getParentTransactionsForInput(ctx context.Context, client ClientInterface, input *TransactionInput) ([]*Transaction, error) {
inputTx, err := client.GetTransactionByID(ctx, input.UtxoPointer.TransactionID)
if err != nil {
return nil, err
}
if err = hydrateTransaction(ctx, inputTx); err != nil {
return nil, err
}
if inputTx.MerkleProof.TxOrID != "" {
return []*Transaction{inputTx}, nil
}
return nil, fmt.Errorf("transaction is not mined yet (tx.ID: %s)", inputTx.ID) // TODO: handle it in next iterration
}