-
Notifications
You must be signed in to change notification settings - Fork 19
/
client_connect_wallet.go
170 lines (144 loc) · 5.45 KB
/
client_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
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
package api
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/preferences"
"code.vegaprotocol.io/vega/wallet/wallet"
"github.com/mitchellh/mapstructure"
)
const WalletConnectionSuccessfullyEstablished = "The connection to the wallet has been successfully established."
type ClientConnectWallet struct {
walletStore WalletStore
interactor Interactor
sessions *Sessions
}
type ClientConnectWalletParams struct {
Hostname string `json:"hostname"`
}
type ClientConnectWalletResult 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 *ClientConnectWallet) 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)
}
if err := h.interactor.NotifyInteractionSessionBegan(ctx, traceID); err != nil {
return nil, internalError(err)
}
defer h.interactor.NotifyInteractionSessionEnded(ctx, traceID)
availableWallets, err := h.walletStore.ListWallets(ctx)
if err != nil {
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not list the available wallets: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
if len(availableWallets) == 0 {
h.interactor.NotifyError(ctx, traceID, ApplicationError, ErrNoWalletToConnectTo)
return nil, applicationCancellationError(ErrApplicationCanceledTheRequest)
}
var approval preferences.ConnectionApproval
for {
rawApproval, err := h.interactor.RequestWalletConnectionReview(ctx, traceID, params.Hostname)
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("reviewing the wallet connection failed: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
a, err := preferences.ParseConnectionApproval(rawApproval)
if err != nil {
h.interactor.NotifyError(ctx, traceID, UserError, err)
continue
}
approval = a
break
}
if isConnectionRejected(approval) {
return nil, userRejectionError()
}
// Wallet selection process.
var loadedWallet wallet.Wallet
for {
if ctx.Err() != nil {
return nil, requestInterruptedError(ErrRequestInterrupted)
}
selectedWallet, err := h.interactor.RequestWalletSelection(ctx, traceID, params.Hostname, availableWallets)
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.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.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not verify the wallet existence: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
} else if !exist {
h.interactor.NotifyError(ctx, traceID, UserError, ErrWalletDoesNotExist)
continue
}
w, err := h.walletStore.GetWallet(ctx, selectedWallet.Wallet, selectedWallet.Passphrase)
if err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
h.interactor.NotifyError(ctx, traceID, UserError, wallet.ErrWrongPassphrase)
continue
}
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not retrieve the wallet: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
loadedWallet = w
break
}
token, err := h.sessions.ConnectWallet(params.Hostname, loadedWallet)
if err != nil {
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not connect to a wallet: %w", err))
return nil, internalError(ErrCouldNotConnectToWallet)
}
h.interactor.NotifySuccessfulRequest(ctx, traceID, WalletConnectionSuccessfullyEstablished)
return ClientConnectWalletResult{
Token: token,
}, nil
}
func validateConnectWalletParams(rawParams jsonrpc.Params) (ClientConnectWalletParams, error) {
if rawParams == nil {
return ClientConnectWalletParams{}, ErrParamsRequired
}
params := ClientConnectWalletParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return ClientConnectWalletParams{}, ErrParamsDoNotMatch
}
if params.Hostname == "" {
return ClientConnectWalletParams{}, ErrHostnameIsRequired
}
return params, nil
}
func isConnectionRejected(approval preferences.ConnectionApproval) bool {
return approval != preferences.ApprovedOnlyThisTime
}
func NewConnectWallet(
walletStore WalletStore,
interactor Interactor,
sessions *Sessions,
) *ClientConnectWallet {
return &ClientConnectWallet{
walletStore: walletStore,
interactor: interactor,
sessions: sessions,
}
}