forked from FourthState/plasma-mvp-sidechain
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sendtx.go
130 lines (116 loc) · 3.54 KB
/
sendtx.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
package cmd
import (
"encoding/hex"
"errors"
"fmt"
"github.com/FourthState/plasma-mvp-sidechain/client"
"github.com/FourthState/plasma-mvp-sidechain/client/context"
"github.com/FourthState/plasma-mvp-sidechain/types"
"github.com/ethereum/go-ethereum/common"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
flagTo = "to"
flagPositions = "position"
// ConfirmSigs possibly to be taken out
flagConfirmSigs = "confirmSigs"
flagAmounts = "amounts"
)
func init() {
rootCmd.AddCommand(sendTxCmd)
sendTxCmd.Flags().String(flagTo, "", "Addresses sending to (separated by commas)")
// Format for positions can be adjusted
sendTxCmd.Flags().String(flagPositions, "", "UTXO Positions to be spent, format: blknum1.txindex1.oindex1.depositnonce1::blknum2.txindex2.oindex2.depositnonce2")
// ConfirmSigs possibly to be taken out
sendTxCmd.Flags().String(flagConfirmSigs, "", "Confirmation Signatures for inputs to be spent")
sendTxCmd.Flags().String(flagAmounts, "", "Amounts to be spent, format: amount1, amount2, fee")
sendTxCmd.Flags().String(client.FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
sendTxCmd.Flags().String(client.FlagAddress, "", "Address to sign with")
viper.BindPFlags(sendTxCmd.Flags())
}
var sendTxCmd = &cobra.Command{
Use: "send",
Short: "Build, Sign, and Send transactions",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewClientContextFromViper()
// get the directory for our keystore
dir := viper.GetString(FlagHomeDir)
// get the from/to address
from, err := ctx.GetInputAddresses(dir)
if err != nil {
return err
}
toStr := viper.GetString(flagTo)
if toStr == "" {
return errors.New("must provide an address to send to")
}
toAddrs := strings.Split(toStr, ",")
if len(toAddrs) > 2 {
return errors.New("incorrect amount of addresses provided")
}
var addr1, addr2 common.Address
addr1, err = client.StrToAddress(toAddrs[0])
if err != nil {
return err
}
if len(toAddrs) > 1 && toAddrs[1] != "" {
addr2, err = client.StrToAddress(toAddrs[1])
if err != nil {
return err
}
}
csStr := viper.GetString(flagConfirmSigs)
cs := strings.Split(csStr, ",")
switch len(cs) {
case 1:
cs = append(cs, "", "", "")
case 2:
cs = append(cs, "", "")
case 3:
cs = append(cs, "")
}
confirmSigs1, err := getConfirmSigs(cs[0], cs[1])
if err != nil {
return err
}
confirmSigs2, err := getConfirmSigs(cs[2], cs[3])
if err != nil {
return err
}
// Get positions for transaction inputs
posStr := viper.GetString(flagPositions)
position, err := client.ParsePositions(posStr)
if err != nil {
return err
}
// Get amounts and fee
amtStr := viper.GetString(flagAmounts)
amounts, err := client.ParseAmounts(amtStr)
msg := client.BuildMsg(from[0], from[1], addr1, addr2, position[0], position[1], confirmSigs1, confirmSigs2, amounts[0], amounts[1], amounts[2])
res, err := ctx.SignBuildBroadcast(from, msg, dir)
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash %s\n", res.Height, res.Hash.String())
return nil
},
}
func getConfirmSigs(sig1, sig2 string) (sigs [2]types.Signature, err error) {
var cs1, cs2 []byte
if sig1 != "" {
cs1, err = hex.DecodeString(strings.TrimSpace(sig1))
if err != nil {
return sigs, err
}
}
if sig2 != "" {
cs2, err = hex.DecodeString(strings.TrimSpace(sig2))
if err != nil {
return sigs, err
}
}
sigs = [2]types.Signature{types.Signature{cs1}, types.Signature{cs2}}
return sigs, nil
}