/
signCmd.go
61 lines (54 loc) · 1.54 KB
/
signCmd.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
package offline
import (
"fmt"
eoff "github.com/33cn/plugin/plugin/dapp/dex/boss/deploy/ethereum/offline"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
)
func SignCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sign", //first step
Short: "sign tx",
Run: signTx,
}
addSignFlag(cmd)
return cmd
}
func addSignFlag(cmd *cobra.Command) {
cmd.Flags().StringP("key", "k", "", "private key ")
_ = cmd.MarkFlagRequired("key")
cmd.Flags().StringP("file", "f", "deploytxs.txt", "tx file, default:deploytxs.txt")
}
func signTx(cmd *cobra.Command, _ []string) {
privatekey, _ := cmd.Flags().GetString("key")
txFilePath, _ := cmd.Flags().GetString("file")
chainEthId, _ := cmd.Flags().GetInt64("chainEthId")
deployPrivateKey, err := crypto.ToECDSA(common.FromHex(privatekey))
if err != nil {
panic(err)
}
var deployTxInfos = make([]DeployInfo, 0)
err = paraseFile(txFilePath, &deployTxInfos)
if err != nil {
fmt.Println("paraseFile,err", err.Error())
return
}
fmt.Println("deployTxInfos size:", len(deployTxInfos))
for i, info := range deployTxInfos {
var tx types.Transaction
err = tx.UnmarshalBinary(common.FromHex(info.RawTx))
if err != nil {
panic(err)
}
signedTx, txHash, err := eoff.SignEIP155Tx(deployPrivateKey, &tx, chainEthId)
if err != nil {
panic(err)
}
deployTxInfos[i].RawTx = signedTx
deployTxInfos[i].TxHash = txHash
}
//finsh write to file
writeToFile("deploysigntxs.txt", deployTxInfos)
}