1
- import {
2
- getBootloaderVersion ,
3
- getFirmwareVersion ,
4
- hasBitcoinOnlyFirmware ,
5
- isBitcoinOnlyDevice ,
6
- } from '@trezor/device-utils' ;
7
- import { analytics , EventType } from '@trezor/suite-analytics' ;
1
+ import { hasBitcoinOnlyFirmware , isBitcoinOnlyDevice } from '@trezor/device-utils' ;
8
2
import TrezorConnect , { FirmwareType } from '@trezor/connect' ;
9
3
import { createThunk } from '@suite-common/redux-utils' ;
10
4
import { TrezorDevice } from '@suite-common/suite-types' ;
@@ -13,7 +7,7 @@ import { selectFirmware } from './firmwareReducer';
13
7
import { FIRMWARE_MODULE_PREFIX , firmwareActions } from './firmwareActions' ;
14
8
import { getBinFilesBaseUrlThunk } from './getBinFilesBaseUrlThunk' ;
15
9
16
- const handleFwHashError = createThunk (
10
+ export const handleFwHashError = createThunk (
17
11
`${ FIRMWARE_MODULE_PREFIX } /handleFwHashError` ,
18
12
( { device, errorMessage } : { device : TrezorDevice ; errorMessage : string } , { dispatch } ) => {
19
13
// device.id should always be present here (device is initialized and in normal mode) during successful TrezorConnect.getFirmwareHash call
@@ -25,12 +19,6 @@ const handleFwHashError = createThunk(
25
19
`${ errorMessage } . Unable to validate firmware hash. If you want to check authenticity of newly installed firmware please proceed to device settings and reinstall firmware.` ,
26
20
) ,
27
21
) ;
28
- analytics . report ( {
29
- type : EventType . FirmwareValidateHashError ,
30
- payload : {
31
- error : errorMessage ,
32
- } ,
33
- } ) ;
34
22
} ,
35
23
) ;
36
24
@@ -44,9 +32,6 @@ const handleFwHashMismatch = createThunk(
44
32
dispatch ( firmwareActions . setHashInvalid ( device . id ) ) ;
45
33
}
46
34
dispatch ( firmwareActions . setFirmwareUpdateError ( INVALID_HASH_ERROR ) ) ;
47
- analytics . report ( {
48
- type : EventType . FirmwareValidateHashMismatch ,
49
- } ) ;
50
35
} ,
51
36
) ;
52
37
@@ -62,11 +47,27 @@ const handleFwHashValid = createThunk(
62
47
} ,
63
48
) ;
64
49
65
- export const firmwareUpdate = createThunk (
50
+ type FirmwareUpdateProps = {
51
+ firmwareType ?: FirmwareType ;
52
+ binary ?: ArrayBuffer ;
53
+ } ;
54
+
55
+ type FirmwareUpdateResult = {
56
+ device ?: TrezorDevice ;
57
+ toFwVersion ?: string ;
58
+ toBtcOnly ?: boolean ;
59
+ error ?: string ;
60
+ } ;
61
+
62
+ export const firmwareUpdate = createThunk <
63
+ FirmwareUpdateResult ,
64
+ FirmwareUpdateProps ,
65
+ { rejectValue : FirmwareUpdateResult }
66
+ > (
66
67
`${ FIRMWARE_MODULE_PREFIX } /firmwareUpdate` ,
67
68
async (
68
- { firmwareType, binary } : { firmwareType ?: FirmwareType ; binary ?: ArrayBuffer } ,
69
- { dispatch, getState, extra } ,
69
+ { firmwareType, binary } ,
70
+ { dispatch, getState, extra, fulfillWithValue , rejectWithValue } ,
70
71
) => {
71
72
dispatch ( firmwareActions . setStatus ( 'started' ) ) ;
72
73
@@ -92,7 +93,9 @@ export const firmwareUpdate = createThunk(
92
93
dispatch ( firmwareActions . setStatus ( 'error' ) ) ;
93
94
dispatch ( firmwareActions . setFirmwareUpdateError ( 'Device not connected' ) ) ;
94
95
95
- return ;
96
+ return rejectWithValue ( {
97
+ error : 'Device not connected' ,
98
+ } ) ;
96
99
}
97
100
98
101
// Cache device when firmware installation starts so that we can reference the original firmware version and type during the installation process.
@@ -130,32 +133,22 @@ export const firmwareUpdate = createThunk(
130
133
language : device . firmware === 'none' ? targetTranslationLanguage : undefined ,
131
134
} ) ;
132
135
133
- // condition to satisfy TS
134
- if ( device . features ) {
135
- const targetProperties = binary
136
- ? { }
137
- : {
138
- toFwVersion : device ?. firmwareRelease ?. release . version . join ( '.' ) ,
139
- toBtcOnly : toBitcoinOnlyFirmware ,
140
- } ;
141
- analytics . report ( {
142
- type : EventType . DeviceUpdateFirmware ,
143
- payload : {
144
- model : device . features . internal_model ,
145
- fromFwVersion :
146
- device ?. firmware === 'none' ? 'none' : getFirmwareVersion ( device ) ,
147
- fromBlVersion : getBootloaderVersion ( device ) ,
148
- error : ! firmwareUpdateResponse . success
149
- ? firmwareUpdateResponse . payload . error
150
- : '' ,
151
- ...targetProperties ,
152
- } ,
153
- } ) ;
154
- }
136
+ const targetProperties = binary
137
+ ? { }
138
+ : {
139
+ toFwVersion : device ?. firmwareRelease ?. release . version . join ( '.' ) ,
140
+ toBtcOnly : toBitcoinOnlyFirmware ,
141
+ } ;
155
142
156
143
if ( ! firmwareUpdateResponse . success ) {
157
144
dispatch ( firmwareActions . setStatus ( 'error' ) ) ;
158
145
dispatch ( firmwareActions . setFirmwareUpdateError ( firmwareUpdateResponse . payload . error ) ) ;
146
+
147
+ return rejectWithValue ( {
148
+ device,
149
+ error : firmwareUpdateResponse . payload . error ,
150
+ ...targetProperties ,
151
+ } ) ;
159
152
} else {
160
153
const { check } = firmwareUpdateResponse . payload ;
161
154
if ( check === 'mismatch' ) {
@@ -172,6 +165,11 @@ export const firmwareUpdate = createThunk(
172
165
} else {
173
166
dispatch ( handleFwHashValid ( device ) ) ;
174
167
}
168
+
169
+ return fulfillWithValue ( {
170
+ device,
171
+ ...targetProperties ,
172
+ } ) ;
175
173
}
176
174
} ,
177
175
) ;
0 commit comments