/
rotate_eth_key.go
150 lines (125 loc) · 4.15 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package main
import (
"context"
"errors"
"fmt"
"zuluprotocol/zeta/core/config"
"zuluprotocol/zeta/core/nodewallets"
"zuluprotocol/zeta/core/txn"
"zuluprotocol/zeta/core/validators"
"zuluprotocol/zeta/libs/crypto"
vgjson "zuluprotocol/zeta/libs/json"
"zuluprotocol/zeta/logging"
"zuluprotocol/zeta/paths"
commandspb "zuluprotocol/zeta/protos/zeta/commands/v1"
"github.com/jessevdk/go-flags"
)
type RotateEthKeyCmd struct {
config.ZetaHomeFlag
config.OutputFlag
config.Passphrase `long:"passphrase-file"`
TargetBlock uint64 `short:"b" long:"target-block" description:"The future block height at which the rotation will take place" `
RotateFrom string `short:"r" long:"rotate-from" description:"Ethereum address being rotated from" `
SubmitterAddress string `short:"s" long:"submitter-address" description:"Ethereum address to use as a submitter to contract changes" `
}
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
}
zetaPaths := paths.New(opts.ZetaHome)
_, conf, err := config.EnsureNodeConfig(zetaPaths)
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, zetaPaths, 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, zetaPaths)
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
}