-
Notifications
You must be signed in to change notification settings - Fork 18
/
nodewallets.go
168 lines (139 loc) · 4.71 KB
/
nodewallets.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
// 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 admin
import (
"fmt"
"net/http"
"code.vegaprotocol.io/vega/core/nodewallets"
"code.vegaprotocol.io/vega/core/nodewallets/eth/clef"
"code.vegaprotocol.io/vega/core/nodewallets/eth/keystore"
"code.vegaprotocol.io/vega/core/nodewallets/registry"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
)
type wallet interface {
Name() string
PubKey() crypto.PublicKey
}
type Wallet struct {
Name string `json:"name"`
PublicKey string `json:"publicKey"`
}
func newWallet(w wallet) Wallet {
return Wallet{
Name: w.Name(),
PublicKey: w.PubKey().Hex(),
}
}
type NodeWalletArgs struct {
Chain string
}
type NodeWalletReloadReply struct {
OldWallet Wallet `json:"oldWallet"`
NewWallet Wallet `json:"newWallet"`
}
type NodeWallet struct {
log *logging.Logger
nodeWallets *nodewallets.NodeWallets
registryLoader *registry.Loader
nodeWalletPassphrase string
vegaPaths paths.Paths
}
func NewNodeWallet(
log *logging.Logger,
vegaPaths paths.Paths,
nodeWalletPassphrase string,
nodeWallets *nodewallets.NodeWallets,
) (*NodeWallet, error) {
registryLoader, err := registry.NewLoader(vegaPaths, nodeWalletPassphrase)
if err != nil {
return nil, err
}
return &NodeWallet{
log: log.Named(nodeWalletNamedLogger),
nodeWallets: nodeWallets,
registryLoader: registryLoader,
nodeWalletPassphrase: nodeWalletPassphrase,
vegaPaths: vegaPaths,
}, nil
}
func (nw *NodeWallet) Reload(_ *http.Request, args *NodeWalletArgs, reply *NodeWalletReloadReply) error {
nw.log.Info("Reloading node wallet", logging.String("chain", args.Chain))
switch args.Chain {
case "vega":
oW := newWallet(nw.nodeWallets.Vega)
reg, err := nw.registryLoader.Get(nw.nodeWalletPassphrase)
if err != nil {
return fmt.Errorf("couldn't load node wallet registry: %v", err)
}
if err := nw.nodeWallets.Vega.Reload(*reg.Vega); err != nil {
nw.log.Error("Reloading node wallet failed", logging.Error(err))
return fmt.Errorf("failed to reload Vega wallet: %w", err)
}
nW := newWallet(nw.nodeWallets.Vega)
reply.NewWallet = nW
reply.OldWallet = oW
nw.log.Info("Reloaded node wallet", logging.String("chain", args.Chain))
return nil
case "ethereum":
oW := newWallet(nw.nodeWallets.Ethereum)
reg, err := nw.registryLoader.Get(nw.nodeWalletPassphrase)
if err != nil {
return fmt.Errorf("couldn't load node wallet registry: %v", err)
}
algoType := nw.nodeWallets.Ethereum.Algo()
_, isKeyStoreWallet := reg.Ethereum.Details.(registry.EthereumKeyStoreWallet)
_, isClefWallet := reg.Ethereum.Details.(registry.EthereumClefWallet)
if isKeyStoreWallet && algoType != keystore.KeyStoreAlgoType {
w, err := nodewallets.GetEthereumWalletWithRegistry(nw.vegaPaths, reg)
if err != nil {
return fmt.Errorf("failed reload key: %w", err)
}
nw.nodeWallets.SetEthereumWallet(w)
} else if isClefWallet && algoType != clef.ClefAlgoType {
w, err := nodewallets.GetEthereumWalletWithRegistry(
nw.vegaPaths,
reg,
)
if err != nil {
return fmt.Errorf("failed reload key: %w", err)
}
nw.nodeWallets.SetEthereumWallet(w)
} else {
if err := nw.nodeWallets.Ethereum.Reload(reg.Ethereum.Details); err != nil {
nw.log.Error("Reloading node wallet failed", logging.Error(err))
return fmt.Errorf("failed to reload Ethereum wallet: %w", err)
}
}
nW := newWallet(nw.nodeWallets.Ethereum)
reply.NewWallet = nW
reply.OldWallet = oW
nw.log.Info("Reloaded node wallet", logging.String("chain", args.Chain))
return nil
}
return fmt.Errorf("failed to reload wallet for non existing chain %q", args.Chain)
}
func (nw *NodeWallet) Show(_ *http.Request, args *NodeWalletArgs, reply *Wallet) error {
switch args.Chain {
case "vega":
*reply = newWallet(nw.nodeWallets.Vega)
return nil
case "ethereum":
*reply = newWallet(nw.nodeWallets.Ethereum)
return nil
}
return fmt.Errorf("failed to show wallet for non existing chain %q", args.Chain)
}