-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_signer.go
372 lines (336 loc) · 12.3 KB
/
btc_signer.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package zetaclient
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"math/rand"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
)
const (
maxNoOfInputsPerTx = 20
consolidationRank = 10 // the rank below (or equal to) which we consolidate UTXOs
outTxBytesMin = 400 // 500B is an estimated size for a 2-input, 3-output SegWit tx
outTxBytesMax = 3250 // 3250B is an estimated size for a 21-input, 3-output SegWit tx
outTxBytesCap = 10_000 // in case of accident
// for ZRC20 configuration
bytesPerInput = 150 // each input is about 150 bytes
ZRC20GasLimit = outTxBytesMin + bytesPerInput*8 // 1600B a suggested ZRC20 GAS_LIMIT for a 10-input, 3-output SegWit tx
)
type BTCSigner struct {
tssSigner TSSSigner
rpcClient BTCRPCClient
logger zerolog.Logger
ts *TelemetryServer
}
var _ ChainSigner = &BTCSigner{}
func NewBTCSigner(cfg config.BTCConfig, tssSigner TSSSigner, logger zerolog.Logger, ts *TelemetryServer) (*BTCSigner, error) {
connCfg := &rpcclient.ConnConfig{
Host: cfg.RPCHost,
User: cfg.RPCUsername,
Pass: cfg.RPCPassword,
HTTPPostMode: true,
DisableTLS: true,
Params: cfg.RPCParams,
}
client, err := rpcclient.New(connCfg, nil)
if err != nil {
return nil, fmt.Errorf("error creating bitcoin rpc client: %s", err)
}
return &BTCSigner{
tssSigner: tssSigner,
rpcClient: client,
logger: logger.With().
Str("chain", "BTC").
Str("module", "BTCSigner").Logger(),
ts: ts,
}, nil
}
// SignWithdrawTx receives utxos sorted by value, amount in BTC, feeRate in BTC per Kb
func (signer *BTCSigner) SignWithdrawTx(
to *btcutil.AddressWitnessPubKeyHash,
amount float64,
gasPrice *big.Int,
sizeLimit uint64,
btcClient *BitcoinChainClient,
height uint64,
nonce uint64,
chain *common.Chain,
) (*wire.MsgTx, error) {
estimateFee := float64(gasPrice.Uint64()) * outTxBytesMax / 1e8
nonceMark := common.NonceMarkAmount(nonce)
// refresh unspent UTXOs and continue with keysign regardless of error
err := btcClient.FetchUTXOS()
if err != nil {
signer.logger.Error().Err(err).Msgf("SignWithdrawTx: FetchUTXOS error: nonce %d chain %d", nonce, chain.ChainId)
}
// select N UTXOs to cover the total expense
prevOuts, total, consolidatedUtxo, consolidatedValue, err := btcClient.SelectUTXOs(amount+estimateFee+float64(nonceMark)*1e-8, maxNoOfInputsPerTx, nonce, consolidationRank, false)
if err != nil {
return nil, err
}
// build tx with selected unspents
tx := wire.NewMsgTx(wire.TxVersion)
for _, prevOut := range prevOuts {
hash, err := chainhash.NewHashFromStr(prevOut.TxID)
if err != nil {
return nil, err
}
outpoint := wire.NewOutPoint(hash, prevOut.Vout)
txIn := wire.NewTxIn(outpoint, nil, nil)
tx.AddTxIn(txIn)
}
amountSatoshis, err := getSatoshis(amount)
if err != nil {
return nil, err
}
// size checking
// #nosec G701 check as positive
txSize := uint64(tx.SerializeSize())
if txSize > sizeLimit { // ZRC20 'withdraw' charged less fee from end user
signer.logger.Info().Msgf("sizeLimit %d is less than txSize %d for nonce %d", sizeLimit, txSize, nonce)
}
if txSize < outTxBytesMin { // outbound shouldn't be blocked a low sizeLimit
signer.logger.Warn().Msgf("sizeLimit %d is less than outTxBytesMin %d; use outTxBytesMin", sizeLimit, outTxBytesMin)
txSize = outTxBytesMin
}
if txSize > outTxBytesCap { // in case of accident
signer.logger.Warn().Msgf("sizeLimit %d is greater than outTxBytesCap %d; use outTxBytesCap", sizeLimit, outTxBytesCap)
txSize = outTxBytesCap
}
// fee calculation
// #nosec G701 always in range (checked above)
fees := new(big.Int).Mul(big.NewInt(int64(txSize)), gasPrice)
signer.logger.Info().Msgf("bitcoin outTx nonce %d gasPrice %s size %d fees %s consolidated %d utxos of value %v",
nonce, gasPrice.String(), txSize, fees.String(), consolidatedUtxo, consolidatedValue)
// calculate remaining btc to TSS self
tssAddrWPKH := signer.tssSigner.BTCAddressWitnessPubkeyHash()
payToSelf, err := payToWitnessPubKeyHashScript(tssAddrWPKH.WitnessProgram())
if err != nil {
return nil, err
}
remaining := total - amount
remainingSats, err := getSatoshis(remaining)
if err != nil {
return nil, err
}
remainingSats -= fees.Int64()
remainingSats -= nonceMark
if remainingSats < 0 {
fmt.Printf("BTCSigner: SignWithdrawTx: Remainder Value is negative! : %d\n", remainingSats)
fmt.Printf("BTCSigner: SignWithdrawTx: Number of inputs : %d\n", len(tx.TxIn))
return nil, fmt.Errorf("remainder value is negative")
} else if remainingSats == nonceMark {
fmt.Printf("BTCSigner: SignWithdrawTx: Adjust remainder value to avoid duplicate nonce-mark: %d\n", remainingSats)
remainingSats--
}
// 1st output: the nonce-mark btc to TSS self
txOut1 := wire.NewTxOut(nonceMark, payToSelf)
tx.AddTxOut(txOut1)
// 2nd output: the payment to the recipient
pkScript, err := payToWitnessPubKeyHashScript(to.WitnessProgram())
if err != nil {
return nil, err
}
txOut2 := wire.NewTxOut(amountSatoshis, pkScript)
tx.AddTxOut(txOut2)
// 3rd output: the remaining btc to TSS self
if remainingSats > 0 {
txOut3 := wire.NewTxOut(remainingSats, payToSelf)
tx.AddTxOut(txOut3)
}
// sign the tx
sigHashes := txscript.NewTxSigHashes(tx)
witnessHashes := make([][]byte, len(tx.TxIn))
for ix := range tx.TxIn {
amt, err := getSatoshis(prevOuts[ix].Amount)
if err != nil {
return nil, err
}
pkScript, err := hex.DecodeString(prevOuts[ix].ScriptPubKey)
if err != nil {
return nil, err
}
witnessHashes[ix], err = txscript.CalcWitnessSigHash(pkScript, sigHashes, txscript.SigHashAll, tx, ix, amt)
if err != nil {
return nil, err
}
}
tss, ok := signer.tssSigner.(*TSS)
if !ok {
return nil, fmt.Errorf("tssSigner is not a TSS")
}
sig65Bs, err := tss.SignBatch(witnessHashes, height, nonce, chain)
if err != nil {
return nil, fmt.Errorf("SignBatch error: %v", err)
}
for ix := range tx.TxIn {
sig65B := sig65Bs[ix]
R := big.NewInt(0).SetBytes(sig65B[:32])
S := big.NewInt(0).SetBytes(sig65B[32:64])
sig := btcec.Signature{
R: R,
S: S,
}
pkCompressed := signer.tssSigner.PubKeyCompressedBytes()
hashType := txscript.SigHashAll
txWitness := wire.TxWitness{append(sig.Serialize(), byte(hashType)), pkCompressed}
tx.TxIn[ix].Witness = txWitness
}
return tx, nil
}
func (signer *BTCSigner) Broadcast(signedTx *wire.MsgTx) error {
fmt.Printf("BTCSigner: Broadcasting: %s\n", signedTx.TxHash().String())
var outBuff bytes.Buffer
err := signedTx.Serialize(&outBuff)
if err != nil {
return err
}
str := hex.EncodeToString(outBuff.Bytes())
fmt.Printf("BTCSigner: Transaction Data: %s\n", str)
hash, err := signer.rpcClient.SendRawTransaction(signedTx, true)
if err != nil {
return err
}
signer.logger.Info().Msgf("Broadcasting BTC tx , hash %s ", hash)
return nil
}
func (signer *BTCSigner) TryProcessOutTx(
send *types.CrossChainTx,
outTxMan *OutTxProcessorManager,
outTxID string,
chainclient ChainClient,
zetaBridge ZetaCoreBridger,
height uint64,
) {
defer func() {
outTxMan.EndTryProcess(outTxID)
if err := recover(); err != nil {
signer.logger.Error().Msgf("BTC TryProcessOutTx: %s, caught panic error: %v", send.Index, err)
}
}()
logger := signer.logger.With().
Str("OutTxID", outTxID).
Str("SendHash", send.Index).
Logger()
params := send.GetCurrentOutTxParam()
if params.CoinType == common.CoinType_Zeta || params.CoinType == common.CoinType_ERC20 {
logger.Error().Msgf("BTC TryProcessOutTx: can only send BTC to a BTC network")
return
}
logger.Info().Msgf("BTC TryProcessOutTx: %s, value %d to %s", send.Index, params.Amount.BigInt(), params.Receiver)
btcClient, ok := chainclient.(*BitcoinChainClient)
if !ok {
logger.Error().Msgf("chain client is not a bitcoin client")
return
}
flags, err := zetaBridge.GetCrosschainFlags()
if err != nil {
logger.Error().Err(err).Msgf("cannot get crosschain flags")
return
}
if !flags.IsOutboundEnabled {
logger.Info().Msgf("outbound is disabled")
return
}
myid := zetaBridge.GetKeys().GetAddress()
// Early return if the send is already processed
// FIXME: handle revert case
outboundTxTssNonce := params.OutboundTxTssNonce
included, confirmed, err := btcClient.IsSendOutTxProcessed(send.Index, outboundTxTssNonce, common.CoinType_Gas, logger)
if err != nil {
logger.Error().Err(err).Msgf("cannot check if send %s is processed", send.Index)
return
}
if included || confirmed {
logger.Info().Msgf("CCTX %s already processed; exit signer", outTxID)
return
}
sizelimit := params.OutboundTxGasLimit
gasprice, ok := new(big.Int).SetString(params.OutboundTxGasPrice, 10)
if !ok || gasprice.Cmp(big.NewInt(0)) < 0 {
logger.Error().Msgf("cannot convert gas price %s ", params.OutboundTxGasPrice)
return
}
// Check receiver P2WPKH address
addr, err := btcutil.DecodeAddress(params.Receiver, config.BitconNetParams)
if err != nil {
logger.Error().Err(err).Msgf("cannot decode address %s ", params.Receiver)
return
}
if !addr.IsForNet(config.BitconNetParams) {
logger.Error().Msgf("address %s is not for network %s", params.Receiver, config.BitconNetParams.Name)
return
}
to, ok := addr.(*btcutil.AddressWitnessPubKeyHash)
if err != nil || !ok {
logger.Error().Err(err).Msgf("cannot convert address %s to P2WPKH address", params.Receiver)
return
}
// Add 1 satoshi/byte to gasPrice to avoid minRelayTxFee issue
networkInfo, err := signer.rpcClient.GetNetworkInfo()
if err != nil {
logger.Error().Err(err).Msgf("cannot get bitcoin network info")
return
}
satPerByte := feeRateToSatPerByte(networkInfo.RelayFee)
gasprice.Add(gasprice, satPerByte)
logger.Info().Msgf("SignWithdrawTx: to %s, value %d sats", addr.EncodeAddress(), params.Amount.Uint64())
logger.Info().Msgf("using utxos: %v", btcClient.utxos)
tx, err := signer.SignWithdrawTx(
to,
float64(params.Amount.Uint64())/1e8,
gasprice,
sizelimit,
btcClient,
height,
outboundTxTssNonce,
&btcClient.chain,
)
if err != nil {
logger.Warn().Err(err).Msgf("SignOutboundTx error: nonce %d chain %d", outboundTxTssNonce, params.ReceiverChainId)
return
}
logger.Info().Msgf("Key-sign success: %d => %s, nonce %d", send.InboundTxParams.SenderChainId, btcClient.chain.ChainName, outboundTxTssNonce)
// FIXME: add prometheus metrics
_, err = zetaBridge.GetObserverList(btcClient.chain)
if err != nil {
logger.Warn().Err(err).Msgf("unable to get observer list: chain %d observation %s", outboundTxTssNonce, observertypes.ObservationType_OutBoundTx.String())
}
if tx != nil {
outTxHash := tx.TxHash().String()
logger.Info().Msgf("on chain %s nonce %d, outTxHash %s signer %s", btcClient.chain.ChainName, outboundTxTssNonce, outTxHash, myid)
// TODO: pick a few broadcasters.
//if len(signers) == 0 || myid == signers[send.OutboundTxParams.Broadcaster] || myid == signers[int(send.OutboundTxParams.Broadcaster+1)%len(signers)] {
// retry loop: 1s, 2s, 4s, 8s, 16s in case of RPC error
for i := 0; i < 5; i++ {
// #nosec G404 randomness is not a security issue here
time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond) //random delay to avoid sychronized broadcast
err := signer.Broadcast(tx)
if err != nil {
logger.Warn().Err(err).Msgf("broadcasting tx %s to chain %s: nonce %d, retry %d", outTxHash, btcClient.chain.ChainName, outboundTxTssNonce, i)
continue
}
logger.Info().Msgf("Broadcast success: nonce %d to chain %s outTxHash %s", outboundTxTssNonce, btcClient.chain.String(), outTxHash)
zetaHash, err := zetaBridge.AddTxHashToOutTxTracker(btcClient.chain.ChainId, outboundTxTssNonce, outTxHash, nil, "", -1)
if err != nil {
logger.Err(err).Msgf("Unable to add to tracker on ZetaCore: nonce %d chain %s outTxHash %s", outboundTxTssNonce, btcClient.chain.ChainName, outTxHash)
}
logger.Info().Msgf("Broadcast to core successful %s", zetaHash)
// Save successfully broadcasted transaction to btc chain client
btcClient.SaveBroadcastedTx(outTxHash, outboundTxTssNonce)
break // successful broadcast; no need to retry
}
}
}