-
Notifications
You must be signed in to change notification settings - Fork 22
/
client_connect_wallet.go
183 lines (159 loc) · 6.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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"
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/preferences"
"code.vegaprotocol.io/vega/wallet/wallet"
)
const (
WalletConnectionSuccessfullyEstablished = "The connection to the wallet has been successfully established."
PassphraseRequestReasonUnlockWallet = "The application wants to unlock the wallet."
)
type ClientConnectWallet struct {
walletStore WalletStore
interactor Interactor
}
// 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, hostname string) (wallet.Wallet, *jsonrpc.ErrorDetails) {
traceID := jsonrpc.TraceIDFromContext(ctx)
if err := h.interactor.NotifyInteractionSessionBegan(ctx, traceID, WalletConnectionWorkflow, 4); err != nil {
return nil, RequestNotPermittedError(err)
}
defer h.interactor.NotifyInteractionSessionEnded(ctx, traceID)
availableWallets, err := h.walletStore.ListWallets(ctx)
if err != nil {
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("could not list the available wallets: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
if len(availableWallets) == 0 {
h.interactor.NotifyError(ctx, traceID, ApplicationErrorType, ErrNoWalletToConnectTo)
return nil, ApplicationCancellationError(ErrApplicationCancelledTheRequest)
}
var approval preferences.ConnectionApproval
for {
rawApproval, err := h.interactor.RequestWalletConnectionReview(ctx, traceID, 1, hostname)
if err != nil {
if errDetails := HandleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalErrorType, 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, UserErrorType, err)
continue
}
approval = a
break
}
if isConnectionRejected(approval) {
return nil, UserRejectionError(ErrUserRejectedWalletConnection)
}
var walletName string
if len(availableWallets) > 1 {
for {
if ctx.Err() != nil {
h.interactor.NotifyError(ctx, traceID, ApplicationErrorType, ErrRequestInterrupted)
return nil, RequestInterruptedError(ErrRequestInterrupted)
}
selectedWallet, err := h.interactor.RequestWalletSelection(ctx, traceID, 2, hostname, availableWallets)
if err != nil {
if errDetails := HandleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("requesting the wallet selection failed: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
if exist, err := h.walletStore.WalletExists(ctx, selectedWallet); err != nil {
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("could not verify the wallet existence: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
} else if !exist {
h.interactor.NotifyError(ctx, traceID, UserErrorType, ErrWalletDoesNotExist)
continue
}
walletName = selectedWallet
break
}
} else {
// There is single wallet available, it doesn't make sense to ask which
// wallet to use.
walletName = availableWallets[0]
}
alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, walletName)
if err != nil {
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
if !alreadyUnlocked {
for {
if ctx.Err() != nil {
h.interactor.NotifyError(ctx, traceID, ApplicationErrorType, ErrRequestInterrupted)
return nil, RequestInterruptedError(ErrRequestInterrupted)
}
walletPassphrase, err := h.interactor.RequestPassphrase(ctx, traceID, 3, walletName, PassphraseRequestReasonUnlockWallet)
if err != nil {
if errDetails := HandleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("requesting the wallet passphrase failed: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
if err := h.walletStore.UnlockWallet(ctx, walletName, walletPassphrase); err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
h.interactor.NotifyError(ctx, traceID, UserErrorType, wallet.ErrWrongPassphrase)
continue
}
if errDetails := HandleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return nil, errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("could not unlock the wallet: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
break
}
}
w, err := h.walletStore.GetWallet(ctx, walletName)
if err != nil {
h.interactor.NotifyError(ctx, traceID, InternalErrorType, fmt.Errorf("could not retrieve the wallet: %w", err))
return nil, InternalError(ErrCouldNotConnectToWallet)
}
h.interactor.NotifySuccessfulRequest(ctx, traceID, 4, WalletConnectionSuccessfullyEstablished)
return w, nil
}
func isConnectionRejected(approval preferences.ConnectionApproval) bool {
return approval != preferences.ApprovedOnlyThisTime
}
func NewConnectWallet(walletStore WalletStore, interactor Interactor) *ClientConnectWallet {
return &ClientConnectWallet{
walletStore: walletStore,
interactor: interactor,
}
}