-
Notifications
You must be signed in to change notification settings - Fork 19
/
rotate_eth_key.go
153 lines (128 loc) · 4.45 KB
/
rotate_eth_key.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package commands
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/vega/core/config"
"code.vegaprotocol.io/vega/core/nodewallets"
"code.vegaprotocol.io/vega/core/txn"
"code.vegaprotocol.io/vega/core/validators"
"code.vegaprotocol.io/vega/libs/crypto"
vgjson "code.vegaprotocol.io/vega/libs/json"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
"github.com/jessevdk/go-flags"
)
type RotateEthKeyCmd struct {
config.VegaHomeFlag
config.OutputFlag
config.Passphrase `long:"passphrase-file"`
TargetBlock uint64 `description:"The future block height at which the rotation will take place" long:"target-block" short:"b"`
RotateFrom string `description:"Ethereum address being rotated from" long:"rotate-from" short:"r"`
SubmitterAddress string `description:"Ethereum address to use as a submitter to contract changes" long:"submitter-address" short:"s"`
}
var rotateEthKeyCmd RotateEthKeyCmd
func (opts *RotateEthKeyCmd) Execute(_ []string) error {
log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
defer log.AtExit()
output, err := opts.GetOutput()
if err != nil {
return err
}
vegaPaths := paths.New(opts.VegaHome)
_, conf, err := config.EnsureNodeConfig(vegaPaths)
if err != nil {
return err
}
if !conf.IsValidator() {
return errors.New("node is not a validator")
}
registryPass, err := opts.Get("node wallet", false)
if err != nil {
return err
}
nodeWallets, err := nodewallets.GetNodeWallets(conf.NodeWallet, vegaPaths, registryPass)
if err != nil {
return fmt.Errorf("couldn't get node wallets: %w", err)
}
// ensure the nodewallet is setup properly, if not we could not complete the command
if err := nodeWallets.Verify(); err != nil {
return fmt.Errorf("node wallet misconfigured: %w", err)
}
cmd := commandspb.EthereumKeyRotateSubmission{
CurrentAddress: crypto.EthereumChecksumAddress(opts.RotateFrom),
NewAddress: nodeWallets.Ethereum.PubKey().Hex(),
TargetBlock: opts.TargetBlock,
SubmitterAddress: opts.SubmitterAddress,
}
if len(cmd.SubmitterAddress) != 0 {
cmd.SubmitterAddress = crypto.EthereumChecksumAddress(cmd.SubmitterAddress)
}
if err := validators.SignEthereumKeyRotation(&cmd, nodeWallets.Ethereum); err != nil {
return err
}
commander, _, cfunc, err := getNodeWalletCommander(log, registryPass, vegaPaths)
if err != nil {
return fmt.Errorf("failed to get commander: %w", err)
}
defer cfunc()
var txHash string
ch := make(chan struct{})
commander.Command(
context.Background(),
txn.RotateEthereumKeySubmissionCommand,
&cmd,
func(h string, e error) {
txHash, err = h, e
close(ch)
}, nil)
<-ch
if err != nil {
return err
}
if output.IsHuman() {
fmt.Printf("ethereum key rotation successfully sent\ntxHash: %s\nethereum signature: %v\nRotating from: %s\nRotating to: %s",
txHash,
cmd.EthereumSignature.Value,
opts.RotateFrom,
cmd.NewAddress,
)
} else if output.IsJSON() {
return vgjson.Print(struct {
TxHash string `json:"txHash"`
EthereumSignature string `json:"ethereumSignature"`
RotateFrom string `json:"rotateFrom"`
RotateTo string `json:"rotateTo"`
}{
TxHash: txHash,
RotateFrom: opts.RotateFrom,
RotateTo: cmd.NewAddress,
EthereumSignature: cmd.EthereumSignature.Value,
})
}
return nil
}
func RotateEthKey(ctx context.Context, parser *flags.Parser) error {
announceNodeCmd = AnnounceNodeCmd{}
var (
short = "Send a transaction to rotate to current ethereum key"
long = "Send a transaction to rotate to current ethereum key"
)
_, err := parser.AddCommand("rotate_eth_key", short, long, &rotateEthKeyCmd)
return err
}