This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Core.js
1096 lines (993 loc) · 36.5 KB
/
Core.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @flow */
import EventEmitter from 'events';
import DataManager from '../data/DataManager';
import DeviceList from '../device/DeviceList';
import {
CORE_EVENT,
RESPONSE_EVENT,
TRANSPORT,
DEVICE,
POPUP,
UI,
IFRAME,
ERRORS,
} from '../constants';
import { UiMessage, DeviceMessage, TransportMessage, ResponseMessage } from '../message/builder';
import AbstractMethod from './methods/AbstractMethod';
import { find as findMethod } from './methods';
import { create as createDeferred } from '../utils/deferred';
import { resolveAfter } from '../utils/promiseUtils';
import { initLog } from '../utils/debug';
import { dispose as disposeBackend } from '../backend/BlockchainLink';
import InteractionTimeout from '../utils/interactionTimeout';
import type { IDevice } from '../device/Device';
import type {
ConnectSettings,
Device as DeviceTyped,
Deferred,
CoreMessage,
UiPromiseResponse,
TransportInfo,
} from '../types';
import type {
ButtonRequest,
PinMatrixRequestType,
WordRequestType,
} from '../types/trezor/protobuf';
// Public variables
// eslint-disable-next-line no-use-before-define
let _core: Core; // Class with event emitter
let _deviceList: ?DeviceList; // Instance of DeviceList
let _popupPromise: ?Deferred<void>; // Waiting for popup handshake
let _uiPromises: Deferred<UiPromiseResponse>[] = []; // Waiting for ui response
const _callMethods: AbstractMethod<any>[] = []; // generic type is irrelevant. only common functions are called at this level
let _preferredDevice: any; // TODO: type
let _interactionTimeout: InteractionTimeout;
// custom log
const _log = initLog('Core');
/**
* Emit message to listener (parent).
* Clear method reference from _callMethods
* @param {CoreMessage} message
* @returns {void}
* @memberof Core
*/
const postMessage = (message: CoreMessage) => {
if (message.event === RESPONSE_EVENT) {
const index: number = _callMethods.findIndex(
call => call && call.responseID === message.id,
);
if (index >= 0) {
_callMethods.splice(index, 1);
}
}
_core.emit(CORE_EVENT, message);
};
/**
* Creates an instance of _popupPromise.
* If Core is used without popup this promise should be always resolved automatically
* @param {boolean} requestWindow
* @returns {Deferred<void>}
* @memberof Core
*/
const getPopupPromise = (requestWindow: boolean = true) => {
// request ui window (used with modal)
if (requestWindow) {
postMessage(UiMessage(UI.REQUEST_UI_WINDOW));
}
if (!_popupPromise) {
_popupPromise = createDeferred();
}
return _popupPromise;
};
/**
* Start interaction timeout timer
*/
const interactionTimeout = () =>
_interactionTimeout.start(() => {
// eslint-disable-next-line no-use-before-define
onPopupClosed('Interaction timeout');
});
/**
* Creates an instance of uiPromise.
* @param {string} promiseEvent
* @param {IDevice} device
* @returns {Deferred<UiPromiseResponse>}
* @memberof Core
*/
const createUiPromise = (promiseEvent: string, device?: IDevice) => {
const uiPromise = createDeferred<UiPromiseResponse>(promiseEvent, device);
_uiPromises.push(uiPromise);
// Interaction timeout
interactionTimeout();
return uiPromise;
};
/**
* Finds an instance of uiPromise.
* @param {number} callId
* @param {string} promiseEvent
* @returns {Deferred<UiPromiseResponse> | void}
* @memberof Core
*/
const findUiPromise = (callId: number, promiseEvent: string) =>
_uiPromises.find(p => p.id === promiseEvent);
const removeUiPromise = (promise: Deferred<UiPromiseResponse>) => {
_uiPromises = _uiPromises.filter(p => p !== promise);
};
/**
* Handle incoming message.
* @param {CoreMessage} message
* @param {boolean} isTrustedOrigin
* @returns {void}
* @memberof Core
*/
export const handleMessage = (message: CoreMessage, isTrustedOrigin: boolean = false) => {
_log.log('handle message in core', isTrustedOrigin, message);
const safeMessages = [
IFRAME.CALL,
POPUP.CLOSED,
// UI.CHANGE_SETTINGS,
UI.CUSTOM_MESSAGE_RESPONSE,
UI.LOGIN_CHALLENGE_RESPONSE,
TRANSPORT.DISABLE_WEBUSB,
];
if (!isTrustedOrigin && safeMessages.indexOf(message.type) === -1) {
return;
}
switch (message.type) {
case POPUP.HANDSHAKE:
getPopupPromise(false).resolve();
break;
case POPUP.CLOSED:
// eslint-disable-next-line no-use-before-define
onPopupClosed(message.payload ? message.payload.error : null);
break;
// case UI.CHANGE_SETTINGS :
// enableLog(parseSettings(message.payload).debug);
// break;
case TRANSPORT.DISABLE_WEBUSB:
// eslint-disable-next-line no-use-before-define
disableWebUSBTransport();
break;
// messages from UI (popup/modal...)
case UI.RECEIVE_DEVICE:
case UI.RECEIVE_CONFIRMATION:
case UI.RECEIVE_PERMISSION:
case UI.RECEIVE_PIN:
case UI.RECEIVE_PASSPHRASE:
case UI.INVALID_PASSPHRASE_ACTION:
case UI.RECEIVE_ACCOUNT:
case UI.CHANGE_ACCOUNT:
case UI.RECEIVE_FEE:
case UI.CUSTOM_MESSAGE_RESPONSE:
case UI.RECEIVE_WORD:
case UI.LOGIN_CHALLENGE_RESPONSE: {
const uiPromise = findUiPromise(0, message.type);
if (uiPromise) {
uiPromise.resolve({ event: message.type, payload: message.payload });
removeUiPromise(uiPromise);
}
break;
}
// message from index
case IFRAME.CALL:
// eslint-disable-next-line no-use-before-define
onCall(message).catch(error => {
_log.debug('onCall error', error);
});
break;
// no default
}
};
/**
* Find device by device path. Returned device may be unacquired.
* @param {AbstractMethod} method
* @returns {Promise<IDevice>}
* @memberof Core
*/
const initDevice = async (method: AbstractMethod<any>) => {
if (!_deviceList) {
throw ERRORS.TypedError('Transport_Missing');
}
const isWebUsb = _deviceList.transportType() === 'WebUsbPlugin';
let device: IDevice | typeof undefined;
let showDeviceSelection = isWebUsb;
if (method.devicePath) {
device = _deviceList.getDevice(method.devicePath);
showDeviceSelection = !!device.unreadableError;
} else {
const devices = _deviceList.asArray();
if (devices.length === 1 && !isWebUsb) {
// there is only one device available. use it
device = _deviceList.getDevice(devices[0].path);
showDeviceSelection = !!device.unreadableError;
} else {
showDeviceSelection = true;
}
}
// show device selection when:
// - there are no devices
// - using webusb and method.devicePath is not set
// - device is in unreadable state
if (showDeviceSelection) {
// initialize uiPromise instance which will catch changes in _deviceList (see: handleDeviceSelectionChanges function)
// but do not wait for resolve yet
createUiPromise(UI.RECEIVE_DEVICE);
// wait for popup handshake
await getPopupPromise().promise;
// check again for available devices
// there is a possible race condition before popup open
const devices = _deviceList.asArray();
if (devices.length === 1 && devices[0].type !== 'unreadable' && !isWebUsb) {
// there is one device available. use it
device = _deviceList.getDevice(devices[0].path);
} else {
// request select device view
postMessage(
UiMessage(UI.SELECT_DEVICE, {
webusb: isWebUsb,
devices: _deviceList.asArray(),
}),
);
// wait for device selection
const uiPromise = findUiPromise(method.responseID, UI.RECEIVE_DEVICE);
if (uiPromise) {
const { payload } = await uiPromise.promise;
if (payload.remember) {
_preferredDevice = payload.device;
}
device = _deviceList.getDevice(payload.device.path);
}
}
}
if (!device) {
throw ERRORS.TypedError('Device_NotFound');
}
return device;
};
/**
* Processing incoming message.
* This method is async that's why it returns Promise but the real response is passed by postMessage(ResponseMessage)
* @param {CoreMessage} message
* @returns {Promise<void>}
* @memberof Core
*/
export const onCall = async (message: CoreMessage) => {
if (!message.id || !message.payload) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
'onCall: message.id or message.payload is missing',
);
}
const responseID = message.id;
const trustedHost = DataManager.getSettings('trustedHost');
const isUsingPopup = DataManager.getSettings('popup');
if (_preferredDevice && !message.payload.device) {
message.payload.device = _preferredDevice;
}
// find method and parse incoming params
let method: AbstractMethod<any>;
let messageResponse: ?CoreMessage;
try {
method = findMethod(message);
// bind callbacks
method.postMessage = postMessage;
method.getPopupPromise = getPopupPromise;
method.createUiPromise = createUiPromise;
method.findUiPromise = findUiPromise;
method.removeUiPromise = removeUiPromise;
// start validation process
method.init();
} catch (error) {
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
postMessage(ResponseMessage(responseID, false, { error }));
return Promise.resolve();
}
_callMethods.push(method);
// this method is not using the device, there is no need to acquire
if (!method.useDevice) {
try {
if (method.useUi) {
// wait for popup handshake
await getPopupPromise().promise;
} else {
// cancel popup request
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
}
const response = await method.run();
messageResponse = ResponseMessage(method.responseID, true, response);
} catch (error) {
messageResponse = ResponseMessage(method.responseID, false, { error });
}
postMessage(messageResponse);
return Promise.resolve();
}
if (!_deviceList && !DataManager.getSettings('transportReconnect')) {
// transport is missing try to initialize it once again
// eslint-disable-next-line no-use-before-define
await initTransport(DataManager.getSettings());
}
if (
isUsingPopup &&
method.requiredPermissions.includes('management') &&
!DataManager.isManagementAllowed()
) {
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
postMessage(
ResponseMessage(responseID, false, { error: ERRORS.TypedError('Method_NotAllowed') }),
);
return Promise.resolve();
}
// find device
let device: IDevice;
try {
device = await initDevice(method);
} catch (error) {
if (error.code === 'Transport_Missing') {
// wait for popup handshake
await getPopupPromise().promise;
// show message about transport
postMessage(UiMessage(UI.TRANSPORT));
} else {
// cancel popup request
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
}
// TODO: this should not be returned here before user agrees on "read" perms...
postMessage(ResponseMessage(responseID, false, { error }));
throw error;
}
method.setDevice(device);
// find pending calls to this device
const previousCall = _callMethods.filter(
call => call && call !== method && call.devicePath === method.devicePath,
);
if (previousCall.length > 0 && method.overridePreviousCall) {
// set flag for each pending method
previousCall.forEach(call => {
call.overridden = true;
});
// interrupt potential communication with device. this should throw error in try/catch block below
// this error will apply to the last item of pending methods
const overrideError = ERRORS.TypedError('Method_Override');
await device.override(overrideError);
// if current method was overridden while waiting for device.override result
// return response with status false
if (method.overridden) {
postMessage(ResponseMessage(method.responseID, false, { error: overrideError }));
throw overrideError;
}
} else if (device.isRunning()) {
if (!device.isLoaded()) {
// corner case
// device didn't finish loading for the first time. @see DeviceList._createAndSaveDevice
// wait for self-release and then carry on
await device.waitForFirstRun();
} else {
// cancel popup request
// postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
postMessage(
ResponseMessage(responseID, false, {
error: ERRORS.TypedError('Device_CallInProgress'),
}),
);
throw ERRORS.TypedError('Device_CallInProgress');
}
}
// set device instance. default is 0
device.setInstance(method.deviceInstance);
if (method.hasExpectedDeviceState) {
device.setExternalState(method.deviceState);
}
// device is available
// set public variables, listeners and run method
/* eslint-disable no-use-before-define */
device.on(DEVICE.BUTTON, (d, code) => {
onDeviceButtonHandler(d, code, method);
});
device.on(DEVICE.PIN, onDevicePinHandler);
device.on(DEVICE.WORD, onDeviceWordHandler);
device.on(
DEVICE.PASSPHRASE,
method.useEmptyPassphrase ? onEmptyPassphraseHandler : onDevicePassphraseHandler,
);
device.on(DEVICE.PASSPHRASE_ON_DEVICE, () => {
postMessage(
UiMessage(UI.REQUEST_PASSPHRASE_ON_DEVICE, { device: device.toMessageObject() }),
);
});
/* eslint-enable no-use-before-define */
// try to reconfigure messages before Initialize
if (_deviceList) {
await _deviceList.reconfigure(device.getVersion());
}
try {
let PIN_TRIES = 1;
const MAX_PIN_TRIES = 3;
// This function will run inside Device.run() after device will be acquired and initialized
const inner = async () => {
const firmwareException = await method.checkFirmwareRange(isUsingPopup);
if (firmwareException) {
if (isUsingPopup) {
await getPopupPromise().promise;
// show unexpected state information
postMessage(UiMessage(firmwareException, device.toMessageObject()));
// wait for device disconnect
await createUiPromise(DEVICE.DISCONNECT, device).promise;
// interrupt process and go to "final" block
return Promise.reject(ERRORS.TypedError('Method_Cancel'));
}
// return error if not using popup
return Promise.reject(ERRORS.TypedError('Device_FwException', firmwareException));
}
// check if device is in unexpected mode [bootloader, not-initialized, required firmware]
const unexpectedMode = device.hasUnexpectedMode(
method.allowDeviceMode,
method.requireDeviceMode,
);
if (unexpectedMode) {
device.keepSession = false;
if (isUsingPopup) {
// wait for popup handshake
await getPopupPromise().promise;
// show unexpected state information
postMessage(UiMessage(unexpectedMode, device.toMessageObject()));
// wait for device disconnect
await createUiPromise(DEVICE.DISCONNECT, device).promise;
// interrupt process and go to "final" block
return Promise.reject(
ERRORS.TypedError('Device_ModeException', unexpectedMode),
);
}
// throw error if not using popup
return Promise.reject(ERRORS.TypedError('Device_ModeException', unexpectedMode));
}
// check and request permissions [read, write...]
method.checkPermissions();
if (!trustedHost && method.requiredPermissions.length > 0) {
// show permissions in UI
const permitted = await method.requestPermissions();
if (!permitted) {
// interrupt process and go to "final" block
return Promise.reject(ERRORS.TypedError('Method_PermissionsNotGranted'));
}
}
const deviceNeedsBackup = device.features.needs_backup;
if (deviceNeedsBackup && typeof method.noBackupConfirmation === 'function') {
const permitted = await method.noBackupConfirmation();
if (!permitted) {
// interrupt process and go to "final" block
return Promise.reject(ERRORS.TypedError('Method_PermissionsNotGranted'));
}
}
if (deviceNeedsBackup) {
// wait for popup handshake
await getPopupPromise().promise;
// show notification
postMessage(UiMessage(UI.DEVICE_NEEDS_BACKUP, device.toMessageObject()));
}
// notify if firmware is outdated but not required
if (device.firmwareStatus === 'outdated') {
// wait for popup handshake
await getPopupPromise().promise;
// show notification
postMessage(UiMessage(UI.FIRMWARE_OUTDATED, device.toMessageObject()));
}
// ask for confirmation [export xpub, export info, sign message]
if (!trustedHost && typeof method.confirmation === 'function') {
// show confirmation in UI
const confirmed = await method.confirmation();
if (!confirmed) {
// interrupt process and go to "final" block
return Promise.reject(ERRORS.TypedError('Method_Cancel'));
}
}
if (_deviceList) {
// reconfigure protobuf messages
await _deviceList.reconfigure(device.getVersion());
}
// Make sure that device will display pin/passphrase
try {
const invalidDeviceState = method.useDeviceState
? await device.validateState(method.network)
: undefined;
if (invalidDeviceState) {
if (isUsingPopup) {
// initialize user response promise
const uiPromise = createUiPromise(UI.INVALID_PASSPHRASE_ACTION, device);
// request action view
postMessage(
UiMessage(UI.INVALID_PASSPHRASE, { device: device.toMessageObject() }),
);
// wait for user response
const uiResp = await uiPromise.promise;
if (uiResp.payload) {
// reset internal device state and try again
device.setInternalState(undefined);
await device.initialize(
method.useEmptyPassphrase,
method.useCardanoDerivation,
);
return inner();
}
// set new state as requested
device.setExternalState(invalidDeviceState);
} else {
throw ERRORS.TypedError('Device_InvalidState');
}
}
} catch (error) {
// catch wrong pin error
if (
error.message === ERRORS.INVALID_PIN_ERROR_MESSAGE &&
PIN_TRIES < MAX_PIN_TRIES
) {
PIN_TRIES++;
postMessage(UiMessage(UI.INVALID_PIN, { device: device.toMessageObject() }));
return inner();
}
// other error
// postMessage(ResponseMessage(method.responseID, false, { error }));
// closePopup();
// clear cached passphrase. it's not valid
device.setInternalState(undefined);
// interrupt process and go to "final" block
return Promise.reject(error);
}
if (method.useUi) {
// make sure that popup is opened
await getPopupPromise().promise;
} else {
// popup is not required
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
}
// run method
try {
// for CustomMessage method reconfigure transport with custom messages definitions
const customMessages = method.getCustomMessages();
if (_deviceList && customMessages) {
await _deviceList.reconfigure(customMessages, true);
}
const response = await method.run();
messageResponse = ResponseMessage(method.responseID, true, response);
} catch (error) {
return Promise.reject(error);
}
};
// run inner function
await device.run(inner, {
keepSession: method.keepSession,
useEmptyPassphrase: method.useEmptyPassphrase,
skipFinalReload: method.skipFinalReload,
useCardanoDerivation: method.useCardanoDerivation,
});
} catch (error) {
// corner case: Device was disconnected during authorization
// this device_id needs to be stored and penalized with delay on future connection
// this solves issue with U2F login (leaves space for requests from services which aren't using trezord)
if (_deviceList && error.code === 'Device_Disconnected') {
_deviceList.addAuthPenalty(device);
}
if (method) {
// corner case:
// thrown while acquiring device
// it's a race condition between two tabs
// workaround is to enumerate transport again and report changes to get a valid session number
if (_deviceList && error.message === ERRORS.WRONG_PREVIOUS_SESSION_ERROR_MESSAGE) {
_deviceList.enumerate();
}
messageResponse = ResponseMessage(method.responseID, false, { error });
}
} finally {
// Work done
_log.log('onCall::finally', messageResponse);
const response = messageResponse;
if (response) {
if (method.name === 'rebootToBootloader' && response.success) {
// Wait for device to switch to bootloader
// This delay is crucial see https://github.com/trezor/trezor-firmware/issues/1983
await resolveAfter(1000);
// call Device.run with empty function to fetch new Features
// (acquire > Initialize > nothing > release)
await device.run(() => Promise.resolve(), { skipFinalReload: true });
}
await device.cleanup();
// eslint-disable-next-line no-use-before-define
closePopup();
// eslint-disable-next-line no-use-before-define
cleanup();
if (method) {
method.dispose();
}
// restore default messages
if (_deviceList) {
if (response.success) {
_deviceList.removeAuthPenalty(device);
}
await _deviceList.restoreMessages();
}
postMessage(response);
}
}
};
/**
* Clean up all variables and references.
* @returns {void}
* @memberof Core
*/
const cleanup = () => {
// closePopup(); // this causes problem when action is interrupted (example: bootloader mode)
_popupPromise = null;
_uiPromises = []; // TODO: remove only promises with params callId
_interactionTimeout.stop();
_log.log('Cleanup...');
};
/**
* Force close popup.
* @returns {void}
* @memberof Core
*/
const closePopup = () => {
if (_popupPromise) {
postMessage(UiMessage(POPUP.CANCEL_POPUP_REQUEST));
}
postMessage(UiMessage(UI.CLOSE_UI_WINDOW));
};
/**
* Handle button request from Device.
* @param {IDevice} device
* @param {string} protobuf.ButtonRequest
* @returns {Promise<void>}
* @memberof Core
*/
const onDeviceButtonHandler = async (
device: IDevice,
request: ButtonRequest,
method: AbstractMethod<any>,
) => {
// wait for popup handshake
const addressRequest = request.code === 'ButtonRequest_Address';
if (!addressRequest || (addressRequest && method.useUi)) {
await getPopupPromise().promise;
}
const data =
typeof method.getButtonRequestData === 'function' && request.code
? method.getButtonRequestData(request.code)
: null;
// interaction timeout
interactionTimeout();
// request view
postMessage(DeviceMessage(DEVICE.BUTTON, { ...request, device: device.toMessageObject() }));
postMessage(
UiMessage(UI.REQUEST_BUTTON, {
...request,
device: device.toMessageObject(),
data,
}),
);
if (addressRequest && !method.useUi) {
postMessage(UiMessage(UI.ADDRESS_VALIDATION, data));
}
};
/**
* Handle pin request from Device.
* @param {IDevice} device
* @param {string} protobuf.PinMatrixRequestType
* @param {Function} callback
* @returns {Promise<void>}
* @memberof Core
*/
const onDevicePinHandler = async (
device: IDevice,
type: PinMatrixRequestType,
callback: (error: any, success: any) => void,
) => {
// wait for popup handshake
await getPopupPromise().promise;
// create ui promise
const uiPromise = createUiPromise(UI.RECEIVE_PIN, device);
// request pin view
postMessage(UiMessage(UI.REQUEST_PIN, { device: device.toMessageObject(), type }));
// wait for pin
const uiResp = await uiPromise.promise;
// callback.apply(null, [null, pin]);
callback(null, uiResp.payload);
};
const onDeviceWordHandler = async (
device: IDevice,
type: WordRequestType,
callback: (error: any, success: any) => void,
) => {
// wait for popup handshake
await getPopupPromise().promise;
// create ui promise
const uiPromise = createUiPromise(UI.RECEIVE_WORD, device);
postMessage(UiMessage(UI.REQUEST_WORD, { device: device.toMessageObject(), type }));
// wait for word
const uiResp = await uiPromise.promise;
callback(null, uiResp.payload);
};
/**
* Handle passphrase request from Device.
* @param {IDevice} device
* @param {Function} callback
* @returns {Promise<void>}
* @memberof Core
*/
const onDevicePassphraseHandler = async (device: IDevice, callback: (response: any) => void) => {
// wait for popup handshake
await getPopupPromise().promise;
// create ui promise
const uiPromise = createUiPromise(UI.RECEIVE_PASSPHRASE, device);
// request passphrase view
postMessage(UiMessage(UI.REQUEST_PASSPHRASE, { device: device.toMessageObject() }));
// wait for passphrase
const uiResp = await uiPromise.promise;
const { value, passphraseOnDevice, save } = uiResp.payload;
// send as PassphrasePromptResponse
callback({
passphrase: value.normalize('NFKD'),
passphraseOnDevice,
cache: save,
});
};
/**
* Handle passphrase request from Device and use empty
* @param {IDevice} device
* @param {Function} callback
* @returns {Promise<void>}
* @memberof Core
*/
const onEmptyPassphraseHandler = (device: IDevice, callback: (response: any) => void) => {
// send as PassphrasePromptResponse
callback({ passphrase: '' });
};
/**
* Handle popup closed by user.
* @returns {void}
* @memberof Core
*/
const onPopupClosed = (customErrorMessage: ?string) => {
const error = customErrorMessage
? ERRORS.TypedError('Method_Cancel', customErrorMessage)
: ERRORS.TypedError('Method_Interrupted');
// Device was already acquired. Try to interrupt running action which will throw error from onCall try/catch block
if (_deviceList && _deviceList.asArray().length > 0) {
_deviceList.allDevices().forEach(d => {
d.keepSession = false; // clear session on release
if (d.isUsedHere()) {
d.interruptionFromUser(error);
} else {
const uiPromise = findUiPromise(0, DEVICE.DISCONNECT);
if (uiPromise) {
uiPromise.resolve({ event: error.message, payload: null });
} else {
_callMethods.forEach(m => {
postMessage(ResponseMessage(m.responseID, false, { error }));
});
_callMethods.splice(0, _callMethods.length);
}
}
});
cleanup();
// Waiting for device. Throw error before onCall try/catch block
} else {
if (_uiPromises.length > 0) {
_uiPromises.forEach(p => {
p.reject(error);
});
_uiPromises = [];
}
if (_popupPromise) {
_popupPromise.reject(error);
_popupPromise = null;
}
cleanup();
}
};
/**
* Handle DeviceList changes.
* If there is uiPromise waiting for device selection update view.
* Used in initDevice function
* @param {DeviceTyped} interruptDevice
* @returns {void}
* @memberof Core
*/
const handleDeviceSelectionChanges = (interruptDevice: ?DeviceTyped = null) => {
// update list of devices in popup
const uiPromise = findUiPromise(0, UI.RECEIVE_DEVICE);
if (uiPromise && _deviceList) {
const list = _deviceList.asArray();
const isWebUsb = _deviceList.transportType().indexOf('webusb') >= 0;
if (list.length === 1 && !isWebUsb) {
// there is only one device. use it
// resolve uiPromise to looks like it's a user choice (see: handleMessage function)
uiPromise.resolve({ event: UI.RECEIVE_DEVICE, payload: { device: list[0] } });
removeUiPromise(uiPromise);
} else {
// update device selection list view
postMessage(
UiMessage(UI.SELECT_DEVICE, {
webusb: isWebUsb,
devices: list,
}),
);
}
}
// device was disconnected, interrupt pending uiPromises for this device
if (interruptDevice) {
const { path } = interruptDevice;
let shouldClosePopup = false;
_uiPromises.forEach(p => {
if (p.device && p.device.getDevicePath() === path) {
if (p.id === DEVICE.DISCONNECT) {
p.resolve({ event: DEVICE.DISCONNECT, payload: null });
}
shouldClosePopup = true;
}
});
if (_preferredDevice && _preferredDevice.path === path) {
_preferredDevice = null;
}
if (shouldClosePopup) {
closePopup();
cleanup();
}
}
};
/**
* Start DeviceList with listeners.
* @param {ConnectSettings} settings
* @returns {Promise<void>}
* @memberof Core
*/
const initDeviceList = async (settings: ConnectSettings) => {
try {
_deviceList = new DeviceList();
_deviceList.on(DEVICE.CONNECT, (device: DeviceTyped) => {
handleDeviceSelectionChanges();
postMessage(DeviceMessage(DEVICE.CONNECT, device));
});
_deviceList.on(DEVICE.CONNECT_UNACQUIRED, (device: DeviceTyped) => {
handleDeviceSelectionChanges();
postMessage(DeviceMessage(DEVICE.CONNECT_UNACQUIRED, device));
});
_deviceList.on(DEVICE.DISCONNECT, (device: DeviceTyped) => {
handleDeviceSelectionChanges(device);
postMessage(DeviceMessage(DEVICE.DISCONNECT, device));
});
_deviceList.on(DEVICE.CHANGED, (device: DeviceTyped) => {
postMessage(DeviceMessage(DEVICE.CHANGED, device));
});
_deviceList.on(TRANSPORT.ERROR, async error => {
_log.error('TRANSPORT ERROR', error);
if (_deviceList) {
_deviceList.disconnectDevices();
_deviceList.dispose();
}
_deviceList = null;
postMessage(TransportMessage(TRANSPORT.ERROR, { error }));
// if transport fails during app lifetime, try to reconnect
if (settings.transportReconnect) {
await resolveAfter(1000, null);
await initDeviceList(settings);
}
});
_deviceList.on(TRANSPORT.START, transportType =>
postMessage(TransportMessage(TRANSPORT.START, transportType)),
);
await _deviceList.init();
if (_deviceList) {
await _deviceList.waitForTransportFirstEvent();
}
} catch (error) {
_deviceList = null;
postMessage(TransportMessage(TRANSPORT.ERROR, { error }));
if (!settings.transportReconnect) {
throw error;
} else {
await resolveAfter(3000, null);
// try to reconnect
await initDeviceList(settings);
}
}
};
/**
* An event emitter for communication with parent
* @extends EventEmitter
* @memberof Core
*/
export class Core extends EventEmitter {
handleMessage(message: any, isTrustedOrigin: boolean) {
handleMessage(message, isTrustedOrigin);
}
dispose() {
if (_deviceList) {
_deviceList.dispose();
}
disposeBackend();
this.removeAllListeners();
}