-
Notifications
You must be signed in to change notification settings - Fork 109
/
safeTransferCmd.go
305 lines (266 loc) · 8.95 KB
/
safeTransferCmd.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
package offline
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
erc20 "github.com/33cn/plugin/plugin/dapp/cross2eth/contracts/erc20/generated"
gnosis "github.com/33cn/plugin/plugin/dapp/cross2eth/contracts/gnosis/generated"
ebTypes "github.com/33cn/plugin/plugin/dapp/cross2eth/ebrelayer/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
)
type safeTxData struct {
Content []byte //json.RawMessage
To common.Address
Value *big.Int
TransferData []byte //json.RawMessage
GasLimit int64
GasPrice *big.Int
Nonce uint64
TxNonce uint64
CrontractAddr string
SendAddr string
name string
}
/*
./boss4x ethereum offline multisign_transfer_prepare -a 3 -r 0xC65B02a22B714b55D708518E2426a22ffB79113d -c 0xbf271b2B23DA4fA8Dc93Ce86D27dd09796a7Bf54 -d 0xd9dab021e74ecf475788ed7b61356056b2095830
./boss4x ethereum offline sign_multisign_tx -k 0x5e8aadb91eaa0fce4df0bcc8bd1af9e703a1d6db78e7a4ebffd6cf045e053574,0x0504bcb22b21874b85b15f1bfae19ad62fc2ad89caefc5344dc669c57efa60db,0x0c61f5a879d70807686e43eccc1f52987a15230ae0472902834af4d1933674f2,0x2809477ede1261da21270096776ba7dc68b89c9df5f029965eaa5fe7f0b80697
./boss4x ethereum offline create_multisign_tx
./boss4x ethereum offline sign -f create_multisign_tx.txt -k c9fa31d7984edf81b8ef3b40c761f1847f6fcd5711ab2462da97dc458f1f896b
./boss4x ethereum offline send -f deploysigntxs.txt
*/
// PrepareCreateMultisignTransferTxCmd 预备创建一个多签转帐交易 在线
func PrepareCreateMultisignTransferTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "multisign_transfer_prepare", //first step
Short: "prepare create multisign transfer tx",
Run: prepareCreateMultisignTransferTx,
}
addPrepareCreateMultisignTransferTxFlags(cmd)
return cmd
}
func addPrepareCreateMultisignTransferTxFlags(cmd *cobra.Command) {
cmd.Flags().StringP("sendAddr", "d", "", "send addr")
_ = cmd.MarkFlagRequired("sendAddr")
cmd.Flags().StringP("contract", "c", "", "mulSignAddr contract address")
_ = cmd.MarkFlagRequired("contract")
cmd.Flags().StringP("receiver", "r", "", "receive address")
_ = cmd.MarkFlagRequired("receiver")
cmd.Flags().Float64P("amount", "a", 0, "amount to transfer")
_ = cmd.MarkFlagRequired("amount")
cmd.Flags().StringP("token", "t", "", "erc20 address,not need to set for ETH(optional)")
}
func prepareCreateMultisignTransferTx(cmd *cobra.Command, _ []string) {
url, _ := cmd.Flags().GetString("rpc_laddr_ethereum")
receiver, _ := cmd.Flags().GetString("receiver")
tokenAddr, _ := cmd.Flags().GetString("token")
amount, _ := cmd.Flags().GetFloat64("amount")
sendAddr, _ := cmd.Flags().GetString("sendAddr")
multiSignAddrstr, _ := cmd.Flags().GetString("contract")
client, err := ethclient.Dial(url)
if err != nil {
fmt.Println("Dial Err:", err)
return
}
gnosisSafeAddr := common.HexToAddress(multiSignAddrstr)
gnosisSafeInt, err := gnosis.NewGnosisSafe(gnosisSafeAddr, client)
if nil != err {
fmt.Println("NewGnosisSafe Err:", err)
return
}
AddressZero := common.HexToAddress(ebTypes.EthNilAddr)
toAddr := common.HexToAddress(receiver)
sendData := []byte{'0', 'x'}
baseGas := big.NewInt(0)
value := big.NewInt(0)
safeTxGas := big.NewInt(10 * 10000)
opts := &bind.CallOpts{
From: common.HexToAddress(sendAddr),
Context: context.Background(),
}
txNonce, err := client.NonceAt(context.Background(), common.HexToAddress(sendAddr), nil)
if err != nil {
fmt.Println("NonceAt Err:", err)
return
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
fmt.Println("SuggestGasPrice Err:", err)
return
}
if tokenAddr == "" {
value.Mul(big.NewInt(int64(amount)), big.NewInt(int64(1e18)))
} else {
toAddr = common.HexToAddress(tokenAddr)
erc20Abi, err := abi.JSON(strings.NewReader(erc20.ERC20ABI))
if err != nil {
fmt.Println("strings.NewReader(erc20.ERC20ABI) Err:", err)
return
}
tokenInstance, err := erc20.NewERC20(toAddr, client)
if err != nil {
fmt.Println("NewERC20 Err:", err)
return
}
decimals, err := tokenInstance.Decimals(opts)
if err != nil {
fmt.Println("Decimals Err:", err)
return
}
dec, ok := ebTypes.DecimalsPrefix[decimals]
if !ok {
fmt.Println("dec Err:")
return
}
value.Mul(big.NewInt(int64(amount)), big.NewInt(dec))
sendData, err = erc20Abi.Pack("transfer", common.HexToAddress(receiver), value)
if err != nil {
fmt.Println("Pack Err:", err)
return
}
//对于erc20这种方式 最后需要将其设置为0
value = big.NewInt(0)
}
contracAddr := common.HexToAddress(multiSignAddrstr)
var msg ethereum.CallMsg
msg.Data = sendData
msg.From = common.HexToAddress(sendAddr)
msg.To = &contracAddr
msg.Value = big.NewInt(0)
//估算gas
gasLimit, err := client.EstimateGas(context.Background(), msg)
if err != nil {
fmt.Println("EstimateGas Err:", err)
return
}
if gasLimit < 100*10000 {
gasLimit = 100 * 10000
}
nonce, err := gnosisSafeInt.Nonce(opts)
if err != nil {
fmt.Println("NewGnosisSafe Err:", err)
return
}
signContent, err := gnosisSafeInt.GetTransactionHash(opts, toAddr, value, sendData, 0,
safeTxGas, baseGas, gasPrice, AddressZero, AddressZero, nonce)
if err != nil {
fmt.Println("GetTransactionHash Err:", err)
return
}
var txdata safeTxData
txdata.Content = signContent[:]
txdata.TransferData = sendData
txdata.GasLimit = int64(gasLimit)
txdata.GasPrice = gasPrice
txdata.To = toAddr
txdata.CrontractAddr = multiSignAddrstr
txdata.SendAddr = sendAddr
txdata.Value = value
txdata.TxNonce = txNonce
txdata.name = "multisign_transfer_prepare"
writeToFile(txdata.name+".txt", txdata)
}
// PreliminarySignMultisignTransferTxCmd 多签转帐交易 多签多个地址签名 离线
func PreliminarySignMultisignTransferTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sign_multisign_tx", //first step
Short: "sign multisign tx",
Run: preliminarySignMultisignTransferTx,
}
addPreliminarySignMultisignTransferTxFlag(cmd)
return cmd
}
func addPreliminarySignMultisignTransferTxFlag(cmd *cobra.Command) {
cmd.Flags().StringP("file", "f", "multisign_transfer_prepare.txt", "tx file, default: multisign_transfer_prepare.txt")
cmd.Flags().StringP("keys", "k", "", "owners' private key, separated by ','")
_ = cmd.MarkFlagRequired("keys")
}
//签名交易
func preliminarySignMultisignTransferTx(cmd *cobra.Command, _ []string) {
txFilePath, _ := cmd.Flags().GetString("file")
keys, _ := cmd.Flags().GetString("keys")
privateKeys := strings.Split(keys, ",")
var txinfo safeTxData
err := paraseFile(txFilePath, &txinfo)
if err != nil {
fmt.Println("paraseFile Err:", err)
return
}
sigs, err := buildSigs(txinfo.Content, privateKeys)
if err != nil {
fmt.Println("buildSigs Err:", err)
return
}
txinfo.Content = sigs
writeToFile("sign_multisign_tx.txt", txinfo)
}
// CreateMultisignTransferTxCmd 创建多签转帐交易
func CreateMultisignTransferTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create_multisign_tx",
Short: "create multisign transfer tx",
Run: CreateMultisignTransferTx,
}
addCreateMultisignTransferTxFlags(cmd)
return cmd
}
func addCreateMultisignTransferTxFlags(cmd *cobra.Command) {
cmd.Flags().StringP("file", "f", "sign_multisign_tx.txt", "tx file, default: sign_multisign_tx.txt")
}
func CreateMultisignTransferTx(cmd *cobra.Command, _ []string) {
url, _ := cmd.Flags().GetString("rpc_laddr_ethereum")
txFilePath, _ := cmd.Flags().GetString("file")
client, err := ethclient.Dial(url)
if err != nil {
fmt.Println("Dial Err:", err)
return
}
var txinfo safeTxData
err = paraseFile(txFilePath, &txinfo)
if err != nil {
fmt.Println("paraseFile Err:", err)
return
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
fmt.Println("SuggestGasPrice Err:", err)
return
}
gnoAbi, err := abi.JSON(strings.NewReader(gnosis.GnosisSafeABI))
if err != nil {
fmt.Println("JSON Err:", err)
return
}
zeroAddr := common.HexToAddress(ebTypes.EthNilAddr)
safeTxGas := big.NewInt(10 * 10000)
gnoData, err := gnoAbi.Pack("execTransaction", txinfo.To, txinfo.Value, txinfo.TransferData, uint8(0),
safeTxGas, big.NewInt(0), gasPrice, zeroAddr, zeroAddr, txinfo.Content)
if err != nil {
fmt.Println("Pack execTransaction Err:", err)
return
}
CreateTxInfoAndWrite(gnoData, txinfo.SendAddr, txinfo.CrontractAddr, "create_multisign_tx", url)
}
func buildSigs(data []byte, privateKeys []string) ([]byte, error) {
var sigs []byte
for _, privateKeyStr := range privateKeys {
privateKey, err := crypto.ToECDSA(common.FromHex(privateKeyStr))
if nil != err {
return nil, errors.New("paraseKey err:" + err.Error())
}
signature, err := crypto.Sign(data, privateKey)
if err != nil {
return nil, err
}
signature[64] += 27
sigs = append(sigs, signature[:]...)
}
return sigs, nil
}