-
Notifications
You must be signed in to change notification settings - Fork 19
/
admin_sign_message.go
106 lines (86 loc) · 2.8 KB
/
admin_sign_message.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
package api
import (
"context"
"encoding/base64"
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/wallet"
"github.com/mitchellh/mapstructure"
)
type AdminSignMessageParams struct {
Wallet string `jso:"wallet"`
Passphrase string `json:"passphrase"`
PubKey string `json:"pubKey"`
EncodedMessage string `json:"encodedMessage"`
}
type AdminSignMessageResult struct {
Base64Signature string `json:"encodedSignature"`
}
type AdminSignMessage struct {
walletStore WalletStore
}
func (h *AdminSignMessage) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateAdminSignMessageParams(rawParams)
if err != nil {
return nil, invalidParams(err)
}
m, err := base64.StdEncoding.DecodeString(params.EncodedMessage)
if err != nil {
return nil, invalidParams(ErrEncodedMessageIsNotValidBase64String)
}
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)
}
if err := h.walletStore.UnlockWallet(ctx, params.Wallet, params.Passphrase); err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
return nil, invalidParams(err)
}
return nil, internalError(fmt.Errorf("could not unlock the wallet: %w", err))
}
w, err := h.walletStore.GetWallet(ctx, params.Wallet)
if err != nil {
return nil, internalError(fmt.Errorf("could not retrieve the wallet: %w", err))
}
signature, err := w.SignAny(params.PubKey, m)
if err != nil {
return nil, internalError(fmt.Errorf("could not sign the message: %w", err))
}
return AdminSignMessageResult{
Base64Signature: base64.StdEncoding.EncodeToString(signature),
}, nil
}
func NewAdminSignMessage(walletStore WalletStore) *AdminSignMessage {
return &AdminSignMessage{
walletStore: walletStore,
}
}
func validateAdminSignMessageParams(rawParams jsonrpc.Params) (AdminSignMessageParams, error) {
if rawParams == nil {
return AdminSignMessageParams{}, ErrParamsRequired
}
params := AdminSignMessageParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return AdminSignMessageParams{}, ErrParamsDoNotMatch
}
if params.Wallet == "" {
return AdminSignMessageParams{}, ErrWalletIsRequired
}
if params.Passphrase == "" {
return AdminSignMessageParams{}, ErrPassphraseIsRequired
}
if params.PubKey == "" {
return AdminSignMessageParams{}, ErrPublicKeyIsRequired
}
if params.EncodedMessage == "" {
return AdminSignMessageParams{}, ErrMessageIsRequired
}
return AdminSignMessageParams{
Wallet: params.Wallet,
Passphrase: params.Passphrase,
PubKey: params.PubKey,
EncodedMessage: params.EncodedMessage,
}, nil
}