-
Notifications
You must be signed in to change notification settings - Fork 22
/
admin_describe_key.go
100 lines (82 loc) · 2.58 KB
/
admin_describe_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
package api
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/wallet"
"github.com/mitchellh/mapstructure"
)
type AdminDescribeKeyParams struct {
Wallet string `json:"wallet"`
Passphrase string `json:"passphrase"`
PublicKey string `json:"publicKey"`
}
type AdminDescribeKeyResult struct {
PublicKey string `json:"publicKey"`
Name string `json:"name"`
Algorithm wallet.Algorithm `json:"algorithm"`
Metadata []wallet.Metadata `json:"metadata"`
IsTainted bool `json:"isTainted"`
}
type AdminDescribeKey struct {
walletStore WalletStore
}
// Handle retrieves key's information.
func (h *AdminDescribeKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateDescribeKeyParams(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 existence: %w", err))
} else if !exist {
return nil, invalidParams(ErrWalletDoesNotExist)
}
w, err := h.walletStore.GetWallet(ctx, params.Wallet, params.Passphrase)
if err != nil {
return nil, internalError(fmt.Errorf("could not retrieve the wallet: %w", err))
}
if !w.HasPublicKey(params.PublicKey) {
return nil, invalidParams(ErrPublicKeyDoesNotExist)
}
publicKey, err := w.DescribePublicKey(params.PublicKey)
if err != nil {
return nil, internalError(fmt.Errorf("could not retrieve the key: %w", err))
}
return AdminDescribeKeyResult{
PublicKey: publicKey.Key(),
Name: publicKey.Name(),
Algorithm: wallet.Algorithm{
Name: publicKey.AlgorithmName(),
Version: publicKey.AlgorithmVersion(),
},
Metadata: publicKey.Metadata(),
IsTainted: publicKey.IsTainted(),
}, nil
}
func validateDescribeKeyParams(rawParams jsonrpc.Params) (AdminDescribeKeyParams, error) {
if rawParams == nil {
return AdminDescribeKeyParams{}, ErrParamsRequired
}
params := AdminDescribeKeyParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return AdminDescribeKeyParams{}, ErrParamsDoNotMatch
}
if params.Wallet == "" {
return AdminDescribeKeyParams{}, ErrWalletIsRequired
}
if params.PublicKey == "" {
return AdminDescribeKeyParams{}, ErrPublicKeyIsRequired
}
if params.Passphrase == "" {
return AdminDescribeKeyParams{}, ErrPassphraseIsRequired
}
return params, nil
}
func NewAdminDescribeKey(
walletStore WalletStore,
) *AdminDescribeKey {
return &AdminDescribeKey{
walletStore: walletStore,
}
}