-
Notifications
You must be signed in to change notification settings - Fork 19
/
connect_wallet.go
142 lines (121 loc) · 4.39 KB
/
connect_wallet.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
package api
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/wallet"
"github.com/mitchellh/mapstructure"
)
type ConnectWallet struct {
walletStore WalletStore
pipeline Pipeline
sessions *Sessions
}
type ConnectWalletParams struct {
Hostname string `json:"hostname"`
}
type ConnectWalletResult struct {
Token string `json:"token"`
}
// Handle initiates the wallet connection between the API and a third-party
// application.
//
// It triggers a selection of the wallet the client wants to use for this
// connection. The wallet is then loaded in memory. All changes done to that wallet
// will start in-memory, and then, be saved in the wallet file. Any changes done
// to the wallet outside the JSON-RPC session (via the command-line for example)
// will be overridden. For the effects to be taken into account, the wallet has
// to be disconnected first, and then re-connected.
//
// All sessions have to be initialized by using this handler. Otherwise, a call
// to any other handlers will be rejected.
func (h *ConnectWallet) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
traceID := TraceIDFromContext(ctx)
params, err := validateConnectWalletParams(rawParams)
if err != nil {
return nil, invalidParams(err)
}
approved, err := h.pipeline.RequestWalletConnectionReview(ctx, traceID, params.Hostname)
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.pipeline, err); errDetails != nil {
return nil, errDetails
}
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("reviewing the wallet connection failed: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
if !approved {
return nil, clientRejectionError()
}
availableWallets, err := h.walletStore.ListWallets(ctx)
if err != nil {
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("couldn't list available wallets: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
// Wallet selection process.
var loadedWallet wallet.Wallet
for {
if ctx.Err() != nil {
return nil, requestInterruptedError(ErrRequestInterrupted)
}
selectedWallet, err := h.pipeline.RequestWalletSelection(ctx, traceID, params.Hostname, availableWallets)
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.pipeline, err); errDetails != nil {
return nil, errDetails
}
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("requesting the wallet selection failed: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
if exist, err := h.walletStore.WalletExists(ctx, selectedWallet.Wallet); err != nil {
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("couldn't verify wallet existence: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
} else if !exist {
h.pipeline.NotifyError(ctx, traceID, ClientError, ErrWalletDoesNotExist)
continue
}
w, err := h.walletStore.GetWallet(ctx, selectedWallet.Wallet, selectedWallet.Passphrase)
if err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
h.pipeline.NotifyError(ctx, traceID, ClientError, wallet.ErrWrongPassphrase)
continue
}
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("couldn't retrieve the wallet: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
loadedWallet = w
break
}
token, err := h.sessions.ConnectWallet(params.Hostname, loadedWallet)
if err != nil {
h.pipeline.NotifyError(ctx, traceID, InternalError, fmt.Errorf("couldn't connect to a wallet: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
h.pipeline.NotifySuccessfulRequest(ctx, traceID)
return ConnectWalletResult{
Token: token,
}, nil
}
func validateConnectWalletParams(rawParams jsonrpc.Params) (ConnectWalletParams, error) {
if rawParams == nil {
return ConnectWalletParams{}, ErrParamsRequired
}
params := ConnectWalletParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return ConnectWalletParams{}, ErrParamsDoNotMatch
}
if params.Hostname == "" {
return ConnectWalletParams{}, ErrHostnameIsRequired
}
return params, nil
}
func NewConnectWallet(
walletStore WalletStore,
pipeline Pipeline,
sessions *Sessions,
) *ConnectWallet {
return &ConnectWallet{
walletStore: walletStore,
pipeline: pipeline,
sessions: sessions,
}
}