-
Notifications
You must be signed in to change notification settings - Fork 114
/
MultiBitHD.java
465 lines (383 loc) · 17.1 KB
/
MultiBitHD.java
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
package org.multibit.hd.ui;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Uninterruptibles;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.multibit.commons.concurrent.SafeExecutors;
import org.multibit.hd.core.config.Configurations;
import org.multibit.hd.core.dto.WalletSummary;
import org.multibit.hd.core.events.CoreEvents;
import org.multibit.hd.core.events.ShutdownEvent;
import org.multibit.hd.core.logging.LoggingFactory;
import org.multibit.hd.core.managers.HttpsManager;
import org.multibit.hd.core.managers.InstallationManager;
import org.multibit.hd.core.managers.WalletManager;
import org.multibit.hd.core.services.CoreServices;
import org.multibit.hd.core.utils.OSUtils;
import org.multibit.hd.hardware.core.HardwareWalletService;
import org.multibit.hd.hardware.core.events.HardwareWalletEvents;
import org.multibit.hd.ui.audio.Sounds;
import org.multibit.hd.ui.controllers.HeaderController;
import org.multibit.hd.ui.controllers.MainController;
import org.multibit.hd.ui.events.controller.ControllerEvents;
import org.multibit.hd.ui.events.view.ViewEvents;
import org.multibit.hd.ui.platform.GenericApplicationFactory;
import org.multibit.hd.ui.platform.GenericApplicationSpecification;
import org.multibit.hd.ui.services.ExternalDataListeningService;
import org.multibit.hd.ui.views.MainView;
import org.multibit.hd.ui.views.themes.ThemeKey;
import org.multibit.hd.ui.views.themes.Themes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* <p>Main entry point to the application</p>
*/
public class MultiBitHD {
//////////////////////////// NO STATIC VARIABLES ALLOWED IN THIS CLASS! ////////////////////////
// See initialiseSystemProperties for explanation
// Special case logger that is not static
private final Logger log = LoggerFactory.getLogger(MultiBitHD.class);
private MainController mainController;
////////////////////////////////////////////////////////////////////////////////////////////////
/**
* <p>Main entry point to the application</p>
*
* @param args None specified
*/
public static void main(String[] args) throws Exception {
// These are called at the first line to avoid any other class loading
// interfering with them
initialiseSystemProperties();
// Hand over to an instance to simplify FEST tests
final MultiBitHD multiBitHD = new MultiBitHD();
if (!multiBitHD.start(args)) {
// Failed to start so issue a hard shutdown
CoreServices.shutdownNow(ShutdownEvent.ShutdownType.HARD);
} else {
// Initialise the UI views in the EDT, Nimbus etc
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
multiBitHD.initialiseUIViews();
}
});
}
}
/**
* Initialise any system properties that need to be in place before any other
* classes are loaded
*/
private static void initialiseSystemProperties() {
// Fix for "TimSort" clipboard failure - https://github.com/keepkey/multibit-hd/issues/645
// Suggested by https://www.java.net/node/700601
// See also http://stackoverflow.com/a/26829874/396747 for more details on ordering at startup
// Verified that java.util.Arrays has not been loaded at this stage
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
// Fix for Windows / Java 7 / VPN bug
System.setProperty("java.net.preferIPv4Stack", "true");
// Fix for version.txt not visible for Java 7
System.setProperty("jsse.enableSNIExtension", "true");
}
/**
* <p>Start this instance of MultiBit HD</p>
*
* @param args The command line arguments
*
* @return True if the application started successfully, false if a shutdown is required
*
* @throws Exception If something goes wrong
*/
public boolean start(String[] args) throws Exception {
// Start the logging factory (see later for instance) to get console logging up fast
LoggingFactory.bootstrap();
// Get the configuration fast (Bitcoin URI processing relies on it)
CoreServices.bootstrap();
// Analyse the command line
if (args != null && args.length > 0) {
// Show the command line arguments
for (int i = 0; i < args.length; i++) {
log.info("MultiBit launched with args[{}]: '{}'", i, args[i]);
}
} else {
// Provide empty arguments to avoid potential NPEs
log.info("No command line arguments");
args = new String[]{};
}
// Check for another instance as soon as possible
Optional<ExternalDataListeningService> externalDataListeningService = initialiseListeningService(args);
if (!externalDataListeningService.isPresent()) {
return false;
}
// Prepare the CA certs (run on a separate thread)
initialiseCaCerts();
// Start core services (logging, environment alerts, configuration, Bitcoin URI handling etc)
initialiseCore(args);
// Create controllers so that the generic app can access listeners
if (!initialiseUIControllers()) {
// Required to shut down
return false;
}
// Must be OK to be here
return true;
}
public void stop() {
log.debug("Stopping MultiBit HD");
mainController = null;
// Final purge in case anything gets missed
ViewEvents.unsubscribeAll();
ControllerEvents.unsubscribeAll();
CoreEvents.unsubscribeAll();
HardwareWalletEvents.unsubscribeAll();
}
/**
* @param args The command line arguments
*
* @return The external data listening service if it could be started successfully, absent implies another instance
*/
private Optional<ExternalDataListeningService> initialiseListeningService(String[] args) {
// Determine if another instance is running and shutdown if this is the case
ExternalDataListeningService externalDataListeningService = new ExternalDataListeningService(args);
if (externalDataListeningService.start()) {
return Optional.of(externalDataListeningService);
}
// Must have failed to be here
return Optional.absent();
}
/**
* <p>Initialise the CA certs</p>
*/
@SuppressFBWarnings({"DM_EXIT"})
private void initialiseCaCerts() throws Exception {
log.debug("Initialising CA certs...");
try {
// Execute the CA certificates download on a separate thread to avoid slowing
// the startup time
SafeExecutors.newSingleThreadExecutor("install-cacerts").submit(
new Runnable() {
@Override
public void run() {
HttpsManager.INSTANCE.installCACertificates(
InstallationManager.getOrCreateApplicationDataDirectory(),
InstallationManager.CA_CERTS_NAME,
null, // Use default host list
false // Do not force loading if they are already present
);
}
});
} catch (SecurityException se) {
log.error("Security exception: {} {}", se.getClass().getName(), se.getMessage());
}
}
/**
* <p>Initialise the UI controllers once all the core services are in place</p>
* <p>This creates the singleton controllers that respond to generic events</p>
* <p>At this stage none of the following will be running:</p>
* <ul>
* <li>Themes or views</li>
* <li>Wallet service</li>
* <li>Backup service</li>
* <li>Bitcoin network service</li>
* </ul>
*/
private boolean initialiseUIControllers() {
if (OSUtils.isWindowsXPOrEarlier()) {
log.error("Windows XP or earlier detected. Forcing shutdown.");
JOptionPane.showMessageDialog(
null, "This version of Windows is not supported for security reasons.\nPlease upgrade.", "Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
// Including the other controllers avoids dangling references during a soft shutdown
mainController = new MainController(
new HeaderController()
);
// Start the hardware wallet support to allow credentials screen to be selected
mainController.handleHardwareWallets();
// Set the tooltip delay to be slightly longer
ToolTipManager.sharedInstance().setInitialDelay(1000);
// Must be OK to be here
return true;
}
/**
* <p>Initialise the UI once all the core services are in place</p>
* <p>This creates the singleton views and controllers that respond to configuration
* and theme changes</p>
* <p>At this stage none of the following will be running:</p>
* <ul>
* <li>Wallet service</li>
* <li>Backup service</li>
* <li>Bitcoin network service</li>
* </ul>
* <p>Once the UI renders, control passes to the <code>MainController</code> to
* respond to the wizard close event which will trigger ongoing initialisation.</p>
*/
@SuppressFBWarnings({"DM_EXIT"})
public MainView initialiseUIViews() {
log.debug("Initialising UI...");
Preconditions.checkNotNull(mainController, "'mainController' must be present. FEST will cause this if another instance is running.");
Preconditions.checkState(SwingUtilities.isEventDispatchThread(), "Must execute on EDT. Check calling environment.");
// Give MultiBit Hardware a chance to process any attached hardware wallet
// and for MainController to subsequently process the events
// The delay observed in reality and FEST tests ranges from 1400-2200ms and if
// not included results in wiped hardware wallets being missed on startup
log.debug("Starting the clock for hardware wallet initialisation");
long hardwareInitialisationTime = System.currentTimeMillis();
// Perform time consuming tasks to use the hardware initialisation time to best effect
// Prepare platform-specific integration (protocol handlers, quit events etc)
initialiseGenericApp();
// Pre-load sound library
Sounds.initialise();
try {
// Set look and feel (expect ~1000ms to perform this)
log.debug("Loading Nimbus LaF...");
UIManager.setLookAndFeel(new NimbusLookAndFeel() {
// Require configurable error feedback through beeps
@Override
public void provideErrorFeedback(Component component) {
if (Configurations.currentConfiguration.getSound().isAlertSound()) {
super.provideErrorFeedback(component);
}
}
});
} catch (UnsupportedLookAndFeelException e) {
try {
log.warn("Falling back to cross platform LaF...");
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
log.error("No look and feel available. MultiBit HD requires Java 7 or higher.", e1);
System.exit(-1);
}
}
log.debug("LaF loaded OK");
// This must be performed immediately after the LaF has been set
if (OSUtils.isMac()) {
log.debug("Applying OSX key bindings...");
// Ensure the correct name is displayed in the application menu
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "multiBit HD");
// Ensure OSX key bindings are used for copy, paste etc
// Use the Nimbus keys and ensure this occurs before any component creation
addOSXKeyStrokes((InputMap) UIManager.get("EditorPane.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("PasswordField.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextField.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextPane.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
}
// Ensure that we are using the configured theme (must be after key bindings)
log.debug("Switching theme...");
ThemeKey themeKey = ThemeKey.valueOf(Configurations.currentConfiguration.getAppearance().getCurrentTheme());
Themes.switchTheme(themeKey.theme());
log.debug("Building MainView...");
// Build a new MainView
final MainView mainView = new MainView();
mainController.setMainView(mainView);
log.debug("Checking for pre-existing wallets...");
// Check for any pre-existing wallets in the application directory
File applicationDataDirectory = InstallationManager.getOrCreateApplicationDataDirectory();
List<File> walletDirectories = WalletManager.findWalletDirectories(applicationDataDirectory);
List<WalletSummary> softWalletSummaries = WalletManager.getSoftWalletSummaries(Optional.of(Configurations.currentConfiguration.getLocale()));
// Check for fresh install
boolean noWallets = walletDirectories.isEmpty();
boolean noSoftWallets = softWalletSummaries.isEmpty();
// HardwareWalletService needs HARDWARE_INITIALISATION_TIME milliseconds to initialise so sleep the rest
conditionallySleep(hardwareInitialisationTime);
boolean deviceAttached = false;
boolean deviceWiped = false;
// Check hardware wallet situation after first initialisation (may not be present or ready)
Optional<HardwareWalletService> hardwareWalletService = CoreServices.useFirstReadyHardwareWalletService();
if (hardwareWalletService.isPresent()) {
// Hardware wallet must be attached to be present
deviceAttached = true;
if (!hardwareWalletService.get().isWalletPresent()) {
log.debug("Wiped hardware wallet detected");
// Must show the welcome wizard in hardware wallet mode
// regardless of wallet or licence situation
// MainController should have handled the events
deviceWiped = true;
}
}
// Determine if welcome wizard should show
boolean showWelcomeWizard = (deviceAttached && deviceWiped) // We have a wiped hardware wallet so need to initialise
|| (noSoftWallets && !deviceAttached) // No soft wallets and no hardware wallet so need to create/restore one
|| noWallets; // No wallets at all so need to create/restore one (either hard or soft)
if (showWelcomeWizard) {
log.debug("Showing the welcome wizard");
mainView.setShowExitingWelcomeWizard(true);
mainView.setShowExitingCredentialsWizard(false);
} else {
log.debug("Showing the credentials wizard");
mainView.setShowExitingCredentialsWizard(true);
mainView.setShowExitingWelcomeWizard(false);
}
// Provide a backdrop to the user and trigger the showing of the wizard
mainView.refresh(false);
log.debug("MainView is ready");
// See the MainController wizard hide event for the next stage
return mainView;
}
/**
* <p>Initialise the platform-specific services</p>
*/
private void initialiseGenericApp() {
GenericApplicationSpecification specification = new GenericApplicationSpecification();
specification.getOpenURIEventListeners().add(mainController);
specification.getOpenFilesEventListeners().add(mainController);
specification.getPreferencesEventListeners().add(mainController);
specification.getAboutEventListeners().add(mainController);
specification.getQuitEventListeners().add(mainController);
GenericApplicationFactory.INSTANCE.buildGenericApplication(specification);
}
/**
* <p>Initialise the core services</p>
*
* @param args The command line arguments
*/
private void initialiseCore(String[] args) {
log.debug("Initialising Core...");
// Start the core services
CoreServices.main(args);
}
/**
* <p>Apply OSX key strokes to input map for consistent UX</p>
*
* @param inputMap The input map for the Swing component
*/
private void addOSXKeyStrokes(InputMap inputMap) {
// Undo and redo require more complex handling
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.ALT_DOWN_MASK), DefaultEditorKit.copyAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_DOWN_MASK), DefaultEditorKit.cutAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), DefaultEditorKit.selectAllAction);
}
/**
* Allow a delay of HARDWARE_INITIALISATION_TIME from the startTime
*
* @param startTime The reference time from which to measure the amount of sleep from
*/
private void conditionallySleep(long startTime) {
// Check if Trezor is required
if (!Configurations.currentConfiguration.isTrezor()) {
return;
}
final long HARDWARE_INITIALISATION_TIME = 2000; // milliseconds
long currentTime = System.currentTimeMillis();
long timeSpent = currentTime - startTime;
if (timeSpent < HARDWARE_INITIALISATION_TIME) {
long sleepFor = HARDWARE_INITIALISATION_TIME - timeSpent;
log.debug("Sleep for an extra {} milliseconds to allow hardware wallets to initialise", sleepFor);
Uninterruptibles.sleepUninterruptibly(sleepFor, TimeUnit.MILLISECONDS);
log.debug("Finished sleep");
} else {
log.debug("No need for extra sleep time to allow hardware wallets to initialise");
}
}
}