-
Notifications
You must be signed in to change notification settings - Fork 82
/
maketx.go
204 lines (191 loc) · 7.1 KB
/
maketx.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2018 Shift Devices AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package maketx
import (
"sort"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/txsort"
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts/errors"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/addresses"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/backend/signing"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
"github.com/sirupsen/logrus"
)
// TxProposal is the data needed for a new transaction to be able to display it and sign it.
type TxProposal struct {
// Coin is the coin this tx was made for.
Coin coin.Coin
AccountConfiguration *signing.Configuration
// Amount is the amount that is sent out. The fee is not included and is deducted on top.
Amount btcutil.Amount
// Fee is the mining fee used.
Fee btcutil.Amount
Transaction *wire.MsgTx
// ChangeAddress is the address of the wallet to which the change of the transaction is sent.
ChangeAddress *addresses.AccountAddress
}
// Total is amount+fee.
func (txProposal *TxProposal) Total() btcutil.Amount {
return txProposal.Amount + txProposal.Fee
}
type byValue struct {
outPoints []wire.OutPoint
outputs map[wire.OutPoint]*wire.TxOut
}
func (p *byValue) Len() int { return len(p.outPoints) }
func (p *byValue) Less(i, j int) bool {
if p.outputs[p.outPoints[i]].Value == p.outputs[p.outPoints[j]].Value {
// Secondary sort to make coin selection deterministic.
return chainhash.HashH(p.outputs[p.outPoints[i]].PkScript).String() < chainhash.HashH(p.outputs[p.outPoints[j]].PkScript).String()
}
return p.outputs[p.outPoints[i]].Value < p.outputs[p.outPoints[j]].Value
}
func (p *byValue) Swap(i, j int) { p.outPoints[i], p.outPoints[j] = p.outPoints[j], p.outPoints[i] }
func coinSelection(
minAmount btcutil.Amount,
outputs map[wire.OutPoint]*wire.TxOut,
) (btcutil.Amount, []wire.OutPoint, error) {
outPoints := []wire.OutPoint{}
for outPoint := range outputs {
outPoints = append(outPoints, outPoint)
}
sort.Sort(sort.Reverse(&byValue{outPoints, outputs}))
selectedOutPoints := []wire.OutPoint{}
outputsSum := btcutil.Amount(0)
for _, outPoint := range outPoints {
if outputsSum >= minAmount {
break
}
selectedOutPoints = append(selectedOutPoints, outPoint)
outputsSum += btcutil.Amount(outputs[outPoint].Value)
}
if outputsSum < minAmount {
return 0, nil, errp.WithStack(errors.ErrInsufficientFunds)
}
return outputsSum, selectedOutPoints, nil
}
// NewTxSpendAll creates a transaction which spends all available unspent outputs.
func NewTxSpendAll(
coin coin.Coin,
inputConfiguration *signing.Configuration,
spendableOutputs map[wire.OutPoint]*wire.TxOut,
outputPkScript []byte,
feePerKb btcutil.Amount,
log *logrus.Entry,
) (*TxProposal, error) {
selectedOutPoints := []wire.OutPoint{}
inputs := []*wire.TxIn{}
outputsSum := btcutil.Amount(0)
for outPoint, output := range spendableOutputs {
outPoint := outPoint // avoid reference reuse due to range loop
selectedOutPoints = append(selectedOutPoints, outPoint)
outputsSum += btcutil.Amount(output.Value)
inputs = append(inputs, wire.NewTxIn(&outPoint, nil, nil))
}
txSize := estimateTxSize(len(selectedOutPoints), inputConfiguration, len(outputPkScript), 0)
maxRequiredFee := feeForSerializeSize(feePerKb, txSize, log)
if outputsSum < maxRequiredFee {
return nil, errp.WithStack(errors.ErrInsufficientFunds)
}
output := wire.NewTxOut(int64(outputsSum-maxRequiredFee), outputPkScript)
unsignedTransaction := &wire.MsgTx{
Version: wire.TxVersion,
TxIn: inputs,
TxOut: []*wire.TxOut{output},
LockTime: 0,
}
txsort.InPlaceSort(unsignedTransaction)
log.WithField("fee", maxRequiredFee).Debug("Preparing transaction to spend all outputs")
return &TxProposal{
Coin: coin,
AccountConfiguration: inputConfiguration,
Amount: btcutil.Amount(output.Value),
Fee: maxRequiredFee,
Transaction: unsignedTransaction,
}, nil
}
// NewTx creates a transaction from a set of unspent outputs, targeting an output value. A subset of
// the unspent outputs is selected to cover the needed amount. A change output is added if needed.
func NewTx(
coin coin.Coin,
inputConfiguration *signing.Configuration,
spendableOutputs map[wire.OutPoint]*wire.TxOut,
output *wire.TxOut,
feePerKb btcutil.Amount,
getChangeAddress func() *addresses.AccountAddress,
log *logrus.Entry,
) (*TxProposal, error) {
targetAmount := btcutil.Amount(output.Value)
if targetAmount <= 0 {
panic("amount must be positive")
}
outputs := []*wire.TxOut{output}
changeAddress := getChangeAddress()
changePKScript := changeAddress.PubkeyScript()
estimatedSize := estimateTxSize(1, inputConfiguration, len(output.PkScript), len(changePKScript))
targetFee := feeForSerializeSize(feePerKb, estimatedSize, log)
for {
selectedOutputsSum, selectedOutPoints, err := coinSelection(
targetAmount+targetFee,
spendableOutputs,
)
if err != nil {
return nil, err
}
txSize := estimateTxSize(len(selectedOutPoints), inputConfiguration, len(output.PkScript), len(changePKScript))
maxRequiredFee := feeForSerializeSize(feePerKb, txSize, log)
if selectedOutputsSum-targetAmount < maxRequiredFee {
targetFee = maxRequiredFee
continue
}
inputs := make([]*wire.TxIn, len(selectedOutPoints))
for i, outPoint := range selectedOutPoints {
outPoint := outPoint // avoids referencing the same variable across loop iterations
inputs[i] = wire.NewTxIn(&outPoint, nil, nil)
}
unsignedTransaction := &wire.MsgTx{
Version: wire.TxVersion,
TxIn: inputs,
TxOut: outputs,
LockTime: 0,
}
changeAmount := selectedOutputsSum - targetAmount - maxRequiredFee
changeIsDust := isDustAmount(
changeAmount, len(changePKScript), changeAddress.Configuration, feePerKb)
finalFee := maxRequiredFee
if changeIsDust {
log.Info("change is dust")
finalFee = selectedOutputsSum - targetAmount
}
if changeAmount != 0 && !changeIsDust {
unsignedTransaction.TxOut = append(unsignedTransaction.TxOut,
wire.NewTxOut(int64(changeAmount), changePKScript))
} else {
changeAddress = nil
}
txsort.InPlaceSort(unsignedTransaction)
log.WithField("fee", finalFee).Debug("Preparing transaction")
return &TxProposal{
Coin: coin,
AccountConfiguration: inputConfiguration,
Amount: targetAmount,
Fee: finalFee,
Transaction: unsignedTransaction,
ChangeAddress: changeAddress,
}, nil
}
}