-
Notifications
You must be signed in to change notification settings - Fork 22
/
admin_rotate_key.go
200 lines (163 loc) · 6.19 KB
/
admin_rotate_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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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 api
import (
"context"
"encoding/base64"
"fmt"
"code.vegaprotocol.io/vega/commands"
"code.vegaprotocol.io/vega/libs/jsonrpc"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
"github.com/golang/protobuf/proto"
"github.com/mitchellh/mapstructure"
)
type AdminRotateKeyParams struct {
Wallet string `json:"wallet"`
FromPublicKey string `json:"fromPublicKey"`
ToPublicKey string `json:"toPublicKey"`
ChainID string `json:"chainID"`
SubmissionBlockHeight uint64 `json:"submissionBlockHeight"`
EnactmentBlockHeight uint64 `json:"enactmentBlockHeight"`
}
type AdminRotateKeyResult struct {
MasterPublicKey string `json:"masterPublicKey"`
EncodedTransaction string `json:"encodedTransaction"`
}
type AdminRotateKey struct {
walletStore WalletStore
}
// Handle create a transaction to rotate the keys.
func (h *AdminRotateKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateAdminRotateKeyParams(rawParams)
if err != nil {
return nil, InvalidParams(err)
}
if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
} else if !exist {
return nil, InvalidParams(ErrWalletDoesNotExist)
}
alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet)
if err != nil {
return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err))
}
if !alreadyUnlocked {
return nil, RequestNotPermittedError(ErrWalletIsLocked)
}
w, err := h.walletStore.GetWallet(ctx, params.Wallet)
if err != nil {
return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err))
}
if w.IsIsolated() {
return nil, InvalidParams(ErrCannotRotateKeysOnIsolatedWallet)
}
if !w.HasPublicKey(params.FromPublicKey) {
return nil, InvalidParams(ErrCurrentPublicKeyDoesNotExist)
}
if !w.HasPublicKey(params.ToPublicKey) {
return nil, InvalidParams(ErrNextPublicKeyDoesNotExist)
}
currentPublicKey, err := w.DescribePublicKey(params.FromPublicKey)
if err != nil {
return nil, InternalError(fmt.Errorf("could not retrieve the current public key: %w", err))
}
nextPublicKey, err := w.DescribePublicKey(params.ToPublicKey)
if err != nil {
return nil, InternalError(fmt.Errorf("could not retrieve the next public key: %w", err))
}
if nextPublicKey.IsTainted() {
return nil, InvalidParams(ErrNextPublicKeyIsTainted)
}
currentPubKeyHash, err := currentPublicKey.Hash()
if err != nil {
return nil, InternalError(fmt.Errorf("could not hash the current public key: %w", err))
}
inputData := commands.NewInputData(params.SubmissionBlockHeight)
inputData.Command = &commandspb.InputData_KeyRotateSubmission{
KeyRotateSubmission: &commandspb.KeyRotateSubmission{
NewPubKeyIndex: nextPublicKey.Index(),
NewPubKey: nextPublicKey.Key(),
TargetBlock: params.EnactmentBlockHeight,
CurrentPubKeyHash: currentPubKeyHash,
},
}
marshaledInputData, err := commands.MarshalInputData(inputData)
if err != nil {
return nil, InternalError(fmt.Errorf("could not build the key rotation transaction: %w", err))
}
masterKey, err := w.MasterKey()
if err != nil {
return nil, InternalError(fmt.Errorf("could not retrieve master key to sign the key rotation transaction: %w", err))
}
rotationSignature, err := masterKey.Sign(commands.BundleInputDataForSigning(marshaledInputData, params.ChainID))
if err != nil {
return nil, InternalError(fmt.Errorf("could not sign the key rotation transaction: %w", err))
}
protoSignature := &commandspb.Signature{
Value: rotationSignature.Value,
Algo: rotationSignature.Algo,
Version: rotationSignature.Version,
}
transaction := commands.NewTransaction(masterKey.PublicKey(), marshaledInputData, protoSignature)
rawTransaction, err := proto.Marshal(transaction)
if err != nil {
return nil, InternalError(fmt.Errorf("could not bundle the key rotation transaction: %w", err))
}
return AdminRotateKeyResult{
MasterPublicKey: masterKey.PublicKey(),
EncodedTransaction: base64.StdEncoding.EncodeToString(rawTransaction),
}, nil
}
func validateAdminRotateKeyParams(rawParams jsonrpc.Params) (AdminRotateKeyParams, error) {
if rawParams == nil {
return AdminRotateKeyParams{}, ErrParamsRequired
}
params := AdminRotateKeyParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return AdminRotateKeyParams{}, ErrParamsDoNotMatch
}
if params.Wallet == "" {
return AdminRotateKeyParams{}, ErrWalletIsRequired
}
if params.ChainID == "" {
return AdminRotateKeyParams{}, ErrChainIDIsRequired
}
if params.FromPublicKey == "" {
return AdminRotateKeyParams{}, ErrCurrentPublicKeyIsRequired
}
if params.ToPublicKey == "" {
return AdminRotateKeyParams{}, ErrNextPublicKeyIsRequired
}
if params.ToPublicKey == params.FromPublicKey {
return AdminRotateKeyParams{}, ErrNextAndCurrentPublicKeysCannotBeTheSame
}
if params.SubmissionBlockHeight == 0 {
return AdminRotateKeyParams{}, ErrSubmissionBlockHeightIsRequired
}
if params.EnactmentBlockHeight == 0 {
return AdminRotateKeyParams{}, ErrEnactmentBlockHeightIsRequired
}
if params.EnactmentBlockHeight <= params.SubmissionBlockHeight {
return AdminRotateKeyParams{}, ErrEnactmentBlockHeightMustBeGreaterThanSubmissionOne
}
return params, nil
}
func NewAdminRotateKey(
walletStore WalletStore,
) *AdminRotateKey {
return &AdminRotateKey{
walletStore: walletStore,
}
}