-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
signVerifyActions.ts
237 lines (218 loc) · 7.86 KB
/
signVerifyActions.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import TrezorConnect, { ButtonRequestMessage, UI, Unsuccessful, Success } from 'trezor-connect';
import { SIGN_VERIFY } from './constants';
import { addToast } from '@suite-actions/notificationActions';
import { PROTOCOL_SCHEME } from '@suite-constants/protocol';
import { openModal, openDeferredModal } from '@suite-actions/modalActions';
import { getProtocolInfo } from '@suite-utils/parseUri';
import { isHex } from '@wallet-utils/ethUtils';
import type { Dispatch, GetState, TrezorDevice } from '@suite-types';
import type { Account } from '@wallet-types';
export type SignVerifyAction =
| { type: typeof SIGN_VERIFY.SIGN_SUCCESS; signSignature: string }
| { type: typeof SIGN_VERIFY.VERIFY_SUCCESS };
type StateParams = {
device: TrezorDevice;
account: Account;
coin: Account['symbol'];
useEmptyPassphrase: boolean;
};
const getStateParams = (getState: GetState): Promise<StateParams> => {
const {
suite: { device },
wallet: {
selectedAccount: { account },
},
} = getState();
return !device || !device.connected || !device.available || !account
? Promise.reject(new Error('Device not found'))
: Promise.resolve({
device,
account,
useEmptyPassphrase: device.useEmptyPassphrase,
coin: account.symbol,
});
};
const showAddressByNetwork =
(dispatch: Dispatch, address: string, path: string) =>
({ account, device, coin, useEmptyPassphrase }: StateParams) => {
const buttonRequestHandler = (event: ButtonRequestMessage['payload']) => {
if (!event || event.code !== 'ButtonRequest_Address') return;
dispatch(
openModal({
type: 'address',
device,
address,
addressPath: path,
networkType: account.networkType,
symbol: account.symbol,
}),
);
};
const bindRequestButton = <T>(response: Promise<T>) => {
TrezorConnect.on(UI.REQUEST_BUTTON, buttonRequestHandler);
return response.finally(() =>
TrezorConnect.off(UI.REQUEST_BUTTON, buttonRequestHandler),
);
};
const params = {
device,
address,
path,
coin,
useEmptyPassphrase,
};
switch (account.networkType) {
case 'bitcoin':
return bindRequestButton(TrezorConnect.getAddress(params));
case 'ethereum':
return bindRequestButton(TrezorConnect.ethereumGetAddress(params));
default:
return Promise.reject(new Error('ShowAddress not supported'));
}
};
const signByNetwork =
(path: string | number[], message: string, hex: boolean, aopp: boolean) =>
({ account, device, coin, useEmptyPassphrase }: StateParams) => {
const params = {
device,
path,
coin,
message,
useEmptyPassphrase,
hex,
no_script_type: aopp,
};
switch (account.networkType) {
case 'bitcoin':
return TrezorConnect.signMessage(params);
case 'ethereum':
return TrezorConnect.ethereumSignMessage(params);
default:
return Promise.reject(new Error('Signing not supported'));
}
};
const verifyByNetwork =
(address: string, message: string, signature: string, hex: boolean) =>
({ account, device, coin, useEmptyPassphrase }: StateParams) => {
const params = {
device,
address,
coin,
message,
signature,
useEmptyPassphrase,
hex,
};
switch (account.networkType) {
case 'bitcoin':
return TrezorConnect.verifyMessage(params);
case 'ethereum':
return TrezorConnect.ethereumVerifyMessage(params);
default:
return Promise.reject(new Error('Verifying not supported'));
}
};
const onSignSuccess =
(dispatch: Dispatch) =>
({ signature }: { signature: string }) => {
dispatch(
addToast({
type: 'sign-message-success',
}),
);
return dispatch({
type: SIGN_VERIFY.SIGN_SUCCESS,
signSignature: signature,
});
};
const onVerifySuccess = (dispatch: Dispatch) => () => {
dispatch(
addToast({
type: 'verify-message-success',
}),
);
return dispatch({
type: SIGN_VERIFY.VERIFY_SUCCESS,
});
};
const throwWhenFailed = <T>(response: Unsuccessful | Success<T>) =>
response.success
? Promise.resolve(response.payload)
: Promise.reject(new Error(response.payload.error));
const onError =
(
dispatch: Dispatch,
type: 'sign-message-error' | 'verify-message-error' | 'verify-address-error',
) =>
(error: Error) =>
dispatch(
addToast({
type,
error: error.message,
}),
);
export const showAddress =
(address: string, path: string) => (dispatch: Dispatch, getState: GetState) =>
getStateParams(getState)
.then(showAddressByNetwork(dispatch, address, path))
.then(throwWhenFailed)
.catch(onError(dispatch, 'verify-address-error'));
export const sign =
(path: string | number[], message: string, hex = false, aopp = false) =>
(dispatch: Dispatch, getState: GetState) =>
getStateParams(getState)
.then(signByNetwork(path, message, hex, aopp))
.then(throwWhenFailed)
.then(onSignSuccess(dispatch))
.catch(onError(dispatch, 'sign-message-error'));
export const verify =
(address: string, message: string, signature: string, hex = false) =>
(dispatch: Dispatch, getState: GetState) =>
getStateParams(getState)
.then(verifyByNetwork(address, message, signature, hex))
.then(throwWhenFailed)
.then(onVerifySuccess(dispatch))
.catch(onError(dispatch, 'verify-message-error'));
export const importAopp = (symbol?: Account['symbol']) => async (dispatch: Dispatch) => {
const result = await dispatch(openDeferredModal({ type: 'qr-reader', allowPaste: true }));
if (!result?.uri) return;
const info = getProtocolInfo(result.uri);
if (info?.scheme !== PROTOCOL_SCHEME.AOPP) return;
if (info.asset && symbol && info.asset !== symbol) return;
return {
asset: info.asset,
message: info.msg,
callback: info.callback,
format: info.format,
};
};
export const sendAopp =
(address: string, sig: string, callback: string) =>
async (dispatch: Dispatch, getState: GetState) => {
const network = getState().wallet.selectedAccount.account?.networkType;
const signature =
network === 'ethereum' && isHex(sig) ? Buffer.from(sig, 'hex').toString('base64') : sig;
const confirm = await dispatch(
openDeferredModal({ type: 'send-aopp-message', address, signature, callback }),
);
if (!confirm) return;
const { success, error } = await fetch(callback, {
method: 'POST',
headers: { 'content-type': 'application/json; utf-8' },
body: JSON.stringify({
version: 0,
address,
signature,
}),
})
.then(res => ({
success: res.status === 204,
error: undefined,
}))
.catch(err => ({
success: false,
error: err.message,
}));
if (success) dispatch(addToast({ type: 'aopp-success' }));
else dispatch(addToast({ type: 'aopp-error', error }));
};