-
Notifications
You must be signed in to change notification settings - Fork 13
/
transaction.go
146 lines (112 loc) · 3.62 KB
/
transaction.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package boltz
import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"math"
)
type OutputType int
const (
SegWit OutputType = iota
Compatibility
Legacy
)
type OutputDetails struct {
LockupTransaction *btcutil.Tx
Vout uint32
OutputType OutputType
RedeemScript []byte
PrivateKey *btcec.PrivateKey
// Should be set to an empty array in case of a refund
Preimage []byte
// Can be zero in case of a claim transaction
TimeoutBlockHeight uint32
}
func ConstructTransaction(outputs []OutputDetails, outputAddress btcutil.Address, satPerVbyte int64) (*wire.MsgTx, error) {
noFeeTransaction, err := constructTransaction(outputs, outputAddress, 0)
if err != nil {
return nil, err
}
witnessSize := noFeeTransaction.SerializeSize() - noFeeTransaction.SerializeSizeStripped()
vByte := int64(noFeeTransaction.SerializeSizeStripped()) + int64(math.Ceil(float64(witnessSize)/4))
return constructTransaction(outputs, outputAddress, vByte*satPerVbyte)
}
func constructTransaction(outputs []OutputDetails, outputAddress btcutil.Address, fee int64) (*wire.MsgTx, error) {
transaction := wire.NewMsgTx(wire.TxVersion)
var inputSum int64
for _, output := range outputs {
// Set the highest timeout block height as locktime
if output.TimeoutBlockHeight > transaction.LockTime {
transaction.LockTime = output.TimeoutBlockHeight
}
// Calculate the sum of all inputs
inputSum += output.LockupTransaction.MsgTx().TxOut[output.Vout].Value
// Add the input to the transaction
input := wire.NewTxIn(wire.NewOutPoint(output.LockupTransaction.Hash(), output.Vout), nil, nil)
input.Sequence = 0
transaction.AddTxIn(input)
}
// Add the output
outputScript, err := txscript.PayToAddrScript(outputAddress)
if err != nil {
return nil, err
}
transaction.AddTxOut(&wire.TxOut{
PkScript: outputScript,
Value: inputSum - fee,
})
// Construct the signature script and witnesses and sign the inputs
for i, output := range outputs {
switch output.OutputType {
case Legacy:
// Set the signed signature script for legacy output
signature, err := txscript.RawTxInSignature(
transaction,
i,
output.RedeemScript,
txscript.SigHashAll,
output.PrivateKey,
)
if err != nil {
return nil, err
}
signatureScriptBuilder := txscript.NewScriptBuilder()
signatureScriptBuilder.AddData(signature)
signatureScriptBuilder.AddData(output.Preimage)
signatureScriptBuilder.AddData(output.RedeemScript)
signatureScript, err := signatureScriptBuilder.Script()
if err != nil {
return nil, err
}
transaction.TxIn[i].SignatureScript = signatureScript
case Compatibility:
// Set the signature script for compatibility outputs
signatureScriptBuilder := txscript.NewScriptBuilder()
signatureScriptBuilder.AddData(createNestedP2shScript(output.RedeemScript))
signatureScript, err := signatureScriptBuilder.Script()
if err != nil {
return nil, err
}
transaction.TxIn[i].SignatureScript = signatureScript
}
// Add the signed witness in case the output is not a legacy one
if output.OutputType != Legacy {
signatureHash := txscript.NewTxSigHashes(transaction)
signature, err := txscript.RawTxInWitnessSignature(
transaction,
signatureHash,
i,
output.LockupTransaction.MsgTx().TxOut[output.Vout].Value,
output.RedeemScript,
txscript.SigHashAll,
output.PrivateKey,
)
if err != nil {
return nil, err
}
transaction.TxIn[i].Witness = wire.TxWitness{signature, output.Preimage, output.RedeemScript}
}
}
return transaction, nil
}