/
tx.go
178 lines (151 loc) · 4.26 KB
/
tx.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
package main
import (
"context"
"time"
cli "github.com/jawher/mow.cli"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/xlab/closer"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/peggo/orchestrator/cosmos"
"github.com/InjectiveLabs/sdk-go/chain/client"
"github.com/InjectiveLabs/sdk-go/chain/peggy/types"
)
// txCmdSubset contains actions that can sign and send messages to Cosmos module
// as well as Ethereum transactions to Peggy contract.
//
// $ peggo tx
func txCmdSubset(cmd *cli.Cmd) {
cmd.Command(
"register-eth-key",
"Submits an Ethereum key that will be used to sign messages on behalf of your Validator",
registerEthKeyCmd,
)
}
func registerEthKeyCmd(cmd *cli.Cmd) {
var (
// Cosmos params
cosmosChainID *string
cosmosGRPC *string
tendermintRPC *string
cosmosGasPrices *string
// Cosmos Key Management
cosmosKeyringDir *string
cosmosKeyringAppName *string
cosmosKeyringBackend *string
cosmosKeyFrom *string
cosmosKeyPassphrase *string
cosmosPrivKey *string
cosmosUseLedger *bool
// Ethereum Key Management
ethKeystoreDir *string
ethKeyFrom *string
ethPassphrase *string
ethPrivKey *string
ethUseLedger *bool
// Misc
alwaysAutoConfirm *bool
)
initCosmosOptions(
cmd,
&cosmosChainID,
&cosmosGRPC,
&tendermintRPC,
&cosmosGasPrices,
)
initCosmosKeyOptions(
cmd,
&cosmosKeyringDir,
&cosmosKeyringAppName,
&cosmosKeyringBackend,
&cosmosKeyFrom,
&cosmosKeyPassphrase,
&cosmosPrivKey,
&cosmosUseLedger,
)
initEthereumKeyOptions(
cmd,
ðKeystoreDir,
ðKeyFrom,
ðPassphrase,
ðPrivKey,
ðUseLedger,
)
initInteractiveOptions(
cmd,
&alwaysAutoConfirm,
)
cmd.Action = func() {
// ensure a clean exit
defer closer.Close()
if *ethUseLedger {
log.Warningln("beware: you cannot really use Ledger for orchestrator, so make sure the Ethereum key is accessible outside of it")
}
valAddress, cosmosKeyring, err := initCosmosKeyring(
cosmosKeyringDir,
cosmosKeyringAppName,
cosmosKeyringBackend,
cosmosKeyFrom,
cosmosKeyPassphrase,
cosmosPrivKey,
cosmosUseLedger,
)
if err != nil {
log.WithError(err).Fatalln("failed to init Cosmos keyring")
}
ethKeyFromAddress, _, personalSignFn, err := initEthereumAccountsManager(
0,
ethKeystoreDir,
ethKeyFrom,
ethPassphrase,
ethPrivKey,
ethUseLedger,
)
if err != nil {
log.WithError(err).Fatalln("failed to init Ethereum account")
}
log.Infoln("Using Cosmos ValAddress", valAddress.String())
log.Infoln("Using Ethereum address", ethKeyFromAddress.String())
actionConfirmed := *alwaysAutoConfirm || stdinConfirm("Confirm UpdatePeggyOrchestratorAddresses transaction? [y/N]: ")
if !actionConfirmed {
return
}
clientCtx, err := client.NewClientContext(*cosmosChainID, valAddress.String(), cosmosKeyring)
if err != nil {
log.WithError(err).Fatalln("failed to initialize cosmos client context")
}
clientCtx = clientCtx.WithNodeURI(*tendermintRPC)
tmRPC, err := rpchttp.New(*tendermintRPC, "/websocket")
if err != nil {
log.WithError(err)
}
clientCtx = clientCtx.WithClient(tmRPC)
daemonClient, err := client.NewCosmosClient(clientCtx, *cosmosGRPC, client.OptionGasPrices(*cosmosGasPrices))
if err != nil {
log.WithError(err).WithFields(log.Fields{
"endpoint": *cosmosGRPC,
}).Fatalln("failed to connect to Cosmos daemon")
}
log.Infoln("Waiting for injectived GRPC")
time.Sleep(1 * time.Second)
daemonWaitCtx, cancelWait := context.WithTimeout(context.Background(), time.Minute)
grpcConn := daemonClient.QueryClient()
waitForService(daemonWaitCtx, grpcConn)
peggyQuerier := types.NewQueryClient(grpcConn)
peggyBroadcaster := cosmos.NewPeggyBroadcastClient(
peggyQuerier,
daemonClient,
nil,
personalSignFn,
)
cancelWait()
broadcastCtx, cancelFn := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelFn()
if err = peggyBroadcaster.UpdatePeggyOrchestratorAddresses(broadcastCtx, ethKeyFromAddress, valAddress); err != nil {
log.WithError(err).Errorln("failed to broadcast Tx")
time.Sleep(time.Second)
return
}
log.Infof("Registered Ethereum address %s for validator address %s",
ethKeyFromAddress, valAddress.String())
}
}