-
Notifications
You must be signed in to change notification settings - Fork 19
/
import.go
176 lines (146 loc) · 5.39 KB
/
import.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
// 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 nodewallet
import (
"encoding/json"
"errors"
"fmt"
"code.vegaprotocol.io/vega/core/config"
"code.vegaprotocol.io/vega/core/nodewallets"
vgfmt "code.vegaprotocol.io/vega/libs/fmt"
vgfs "code.vegaprotocol.io/vega/libs/fs"
vgjson "code.vegaprotocol.io/vega/libs/json"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
"github.com/jessevdk/go-flags"
tmconfig "github.com/tendermint/tendermint/config"
)
var (
ErrOneOfTendermintFlagIsRequired = errors.New("one of --tendermint-home or --tendermint-pubkey flag is required")
ErrTendermintFlagsAreMutuallyExclusive = errors.New("--tendermint-home and --tendermint-pubkey are mutually exclusive")
ErrClefOptionMissing = errors.New("--clef-account and --clef-address must both be set to import a clef wallet")
)
type importCmd struct {
config.OutputFlag
Config nodewallets.Config
WalletPassphrase config.Passphrase `long:"wallet-passphrase-file"`
Chain string `short:"c" long:"chain" required:"true" description:"The chain to be imported" choice:"vega" choice:"ethereum" choice:"tendermint"`
WalletPath config.PromptString `long:"wallet-path" description:"The path to the wallet file to import"`
Force bool `long:"force" description:"Should the command re-write an existing nodewallet file if it exists"`
// clef flags
EthereumClefAddress string `long:"ethereum-clef-address" description:"The URL to the clef instance that Vega will use."`
EthereumClefAccount string `long:"ethereum-clef-account" description:"The Ethereum account to be imported by Vega from Clef. In hex."`
// tendermint flags
TendermintPubkey string `long:"tendermint-pubkey" description:"The tendermint pubkey of the tendermint validator node"`
TendermintHome string `long:"tendermint-home" description:"The tendermint home from which to look the for the pubkey"`
}
func (opts *importCmd) Execute(_ []string) error {
output, err := opts.GetOutput()
if err != nil {
return err
}
log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
defer log.AtExit()
if (opts.EthereumClefAccount != "") != (opts.EthereumClefAddress != "") {
return ErrClefOptionMissing
}
registryPass, err := rootCmd.PassphraseFile.Get("node wallet", false)
if err != nil {
return err
}
vegaPaths := paths.New(rootCmd.VegaHome)
_, conf, err := config.EnsureNodeConfig(vegaPaths)
if err != nil {
return err
}
opts.Config = conf.NodeWallet
if _, err := flags.NewParser(opts, flags.Default|flags.IgnoreUnknown).Parse(); err != nil {
return err
}
var walletPass, walletPath string
if opts.Chain == vegaChain || (opts.Chain == ethereumChain && opts.EthereumClefAddress == "") {
walletPass, err = opts.WalletPassphrase.Get("blockchain wallet", false)
if err != nil {
return err
}
walletPath, err = opts.WalletPath.Get("wallet path", "wallet-path")
if err != nil {
return err
}
}
var data map[string]string
switch opts.Chain {
case ethereumChain:
data, err = nodewallets.ImportEthereumWallet(
vegaPaths,
registryPass,
walletPass,
opts.EthereumClefAccount,
opts.EthereumClefAddress,
walletPath,
opts.Force,
)
if err != nil {
return fmt.Errorf("couldn't import Ethereum node wallet: %w", err)
}
case vegaChain:
data, err = nodewallets.ImportVegaWallet(vegaPaths, registryPass, walletPass, walletPath, opts.Force)
if err != nil {
return fmt.Errorf("couldn't import Vega node wallet: %w", err)
}
case tendermintChain:
if len(opts.TendermintHome) == 0 && len(opts.TendermintPubkey) == 0 {
return ErrOneOfTendermintFlagIsRequired
}
if len(opts.TendermintHome) > 0 && len(opts.TendermintPubkey) > 0 {
return ErrTendermintFlagsAreMutuallyExclusive
}
tendermintPubkey := opts.TendermintPubkey
if len(opts.TendermintHome) > 0 {
tendermintPubkey, err = getLocalTendermintPubkey(opts.TendermintHome)
if err != nil {
return fmt.Errorf("couldn't retrieve tendermint public key: %w", err)
}
}
data, err = nodewallets.ImportTendermintPubkey(vegaPaths, registryPass, tendermintPubkey, opts.Force)
if err != nil {
return fmt.Errorf("couldn't import Tendermint pubkey: %w", err)
}
}
if output.IsHuman() {
fmt.Println(green("import successful:"))
vgfmt.PrettyPrint(data)
} else if output.IsJSON() {
if err := vgjson.Print(data); err != nil {
return err
}
}
return nil
}
func getLocalTendermintPubkey(tendermintHome string) (string, error) {
tmConfig := tmconfig.DefaultConfig()
tmConfig.SetRoot(tendermintHome)
genesisFilePath := tmConfig.PrivValidatorKeyFile()
data, err := vgfs.ReadFile(genesisFilePath)
if err != nil {
return "", err
}
privValidatorKey := struct {
PubKey struct {
Value string `json:"value"`
} `json:"pub_key"`
}{}
if err = json.Unmarshal(data, &privValidatorKey); err != nil {
return "", fmt.Errorf("could not read priv_validator_key.json: %w", err)
}
return privValidatorKey.PubKey.Value, nil
}