-
Notifications
You must be signed in to change notification settings - Fork 162
/
sendbtc.go
186 lines (168 loc) · 4.86 KB
/
sendbtc.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
// nolint:dupl // keep it
package main
import (
"encoding/hex"
"fmt"
"strings"
"github.com/anyswap/CrossChain-Bridge/cmd/utils"
"github.com/anyswap/CrossChain-Bridge/common"
"github.com/anyswap/CrossChain-Bridge/log"
"github.com/anyswap/CrossChain-Bridge/tokens"
"github.com/anyswap/CrossChain-Bridge/tokens/btc"
"github.com/anyswap/CrossChain-Bridge/tools"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58"
"github.com/urfave/cli/v2"
)
var (
// nolint:lll // allow long line of example
sendBtcCommand = &cli.Command{
Action: sendBtc,
Name: "sendbtc",
Usage: "send btc",
ArgsUsage: " ",
Description: `
send btc command, sign tx with WIF or private key.
Example:
./swaptools sendbtc --gateway http://1.2.3.4:5555 --net testnet3 --wif ./wif.txt --from maiApsjjnceZ7Cx1UMj344JRU3R8A2Say6 --to mtc4xaZgJJZpN6BdoWk7pHFho1GTUnd5aP --value 10000 --to mfwanCuht2b4Lvb5XTds4Rvzy3jZ2ZWraL --value 20000 --memo "test send btc" --dryrun
`,
Flags: []cli.Flag{
utils.GatewayFlag,
networkFlag,
wifFileFlag,
priKeyFileFlag,
senderFlag,
receiverSliceFlag,
valueSliceFlag,
memoFlag,
relayFeePerKbFlag,
dryRunFlag,
},
}
)
type btcTxSender struct {
gateway string
netID string
wifFile string
priFile string
sender string
receivers []string
amounts []int64
memo string
relayFeePerKb int64
dryRun bool
}
var (
btcBridge *btc.Bridge
btcSender = &btcTxSender{}
)
func (bts *btcTxSender) initArgs(ctx *cli.Context) {
bts.gateway = ctx.String(utils.GatewayFlag.Name)
bts.netID = ctx.String(networkFlag.Name)
bts.wifFile = ctx.String(wifFileFlag.Name)
bts.priFile = ctx.String(priKeyFileFlag.Name)
bts.sender = ctx.String(senderFlag.Name)
bts.receivers = ctx.StringSlice(receiverSliceFlag.Name)
bts.amounts = ctx.Int64Slice(valueSliceFlag.Name)
bts.memo = ctx.String(memoFlag.Name)
bts.relayFeePerKb = ctx.Int64(relayFeePerKbFlag.Name)
bts.dryRun = ctx.Bool(dryRunFlag.Name)
if bts.netID == "" {
log.Fatal("must specify '-net' flag")
}
if bts.wifFile == "" && bts.priFile == "" {
log.Fatal("must specify '-wif' or '-pri' flag")
}
if bts.sender == "" {
log.Fatal("must specify '-from' flag")
}
if len(bts.receivers) == 0 {
log.Fatal("must specify '-to' flag")
}
if len(bts.amounts) == 0 {
log.Fatal("must specify '-value' flag")
}
if len(bts.receivers) != len(bts.amounts) {
log.Fatal("count of receivers and values are not equal")
}
}
func sendBtc(ctx *cli.Context) error {
utils.SetLogger(ctx)
btcSender.initArgs(ctx)
btcSender.initBridge()
wifStr := btcSender.loadWIFForAddress()
rawTx, err := btcBridge.BuildTransaction(btcSender.sender, btcSender.receivers, btcSender.amounts, btcSender.memo, btcSender.relayFeePerKb)
if err != nil {
log.Fatal("BuildRawTransaction error", "err", err)
}
signedTx, txHash, err := btcBridge.SignTransactionWithWIF(rawTx, wifStr)
if err != nil {
log.Fatal("SignTransaction failed", "err", err)
}
log.Info("SignTransaction success", "txHash", txHash)
fmt.Println(btc.AuthoredTxToString(signedTx, true))
if !btcSender.dryRun {
_, err = btcBridge.SendTransaction(signedTx)
if err != nil {
log.Error("SendTransaction failed", "err", err)
}
} else {
log.Info("------------ dry run, does not sendtx -------------")
}
return nil
}
func (bts *btcTxSender) initBridge() {
btcBridge = btc.NewCrossChainBridge(true)
btcBridge.ChainConfig = &tokens.ChainConfig{
BlockChain: "Bitcoin",
NetID: bts.netID,
}
btcBridge.GatewayConfig = &tokens.GatewayConfig{
APIAddress: []string{bts.gateway},
}
}
func (bts *btcTxSender) loadWIFForAddress() string {
var wifStr string
if bts.wifFile != "" {
wifdata, err := tools.SafeReadFile(bts.wifFile)
if err != nil {
log.Fatal("Read WIF file failed", "err", err)
}
wifStr = strings.TrimSpace(string(wifdata))
} else {
pridata, err := tools.SafeReadFile(bts.priFile)
if err != nil {
log.Fatal("Read private key file failed", "err", err)
}
priKey := strings.TrimSpace(string(pridata))
var pribs []byte
if common.IsHex(priKey) {
pribs, err = hex.DecodeString(priKey)
if err != nil {
log.Fatal("failed to decode hex private key string")
}
} else {
pribs, _, err = base58.CheckDecode(priKey)
if err != nil {
pribs = base58.Decode(priKey)
}
}
pri, _ := btcec.PrivKeyFromBytes(btcec.S256(), pribs)
wif, err := btcutil.NewWIF(pri, btcBridge.GetChainParams(), true)
if err != nil {
log.Fatal("failed to parse private key")
}
wifStr = wif.String()
}
wif, err := btcutil.DecodeWIF(wifStr)
if err != nil {
log.Fatal("failed to decode WIF to verify")
}
pkdata := wif.SerializePubKey()
pkaddr, _ := btcBridge.NewAddressPubKeyHash(pkdata)
if pkaddr.EncodeAddress() != bts.sender {
log.Fatal("address mismatch", "decoded", pkaddr.EncodeAddress(), "from", bts.sender)
}
return wifStr
}