importAccount.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. Template Controllers
  3. @module Templates
  4. */
  5. /**
  6. The importAccount import template
  7. @class [template] popupWindows_importAccount
  8. @constructor
  9. */
  10. Template['popupWindows_importAccount'].helpers({
  11. /**
  12. Show password
  13. @method showPassword
  14. */
  15. showPassword: function() {
  16. return TemplateVar.get('showPassword') ? 'text' : 'password';
  17. }
  18. });
  19. Template['popupWindows_importAccount'].events({
  20. /**
  21. On drag enter, change class
  22. @event dragenter .dropable
  23. */
  24. 'dragenter .dropable': function(e) {
  25. $(e.currentTarget).addClass('active');
  26. },
  27. /**
  28. On drag leave, change class
  29. @event dragleave .dropable
  30. */
  31. 'dragleave .dropable': function(e) {
  32. $(e.currentTarget).removeClass('active');
  33. },
  34. /**
  35. When the file is droped, read the path
  36. @event drop .dropable
  37. */
  38. 'drop .dropable': function(e, template) {
  39. e.preventDefault();
  40. if (e.originalEvent.dataTransfer) {
  41. files = e.originalEvent.dataTransfer.files;
  42. }
  43. if (files.length) {
  44. ipc.send('backendAction_checkWalletFile', files[0].path);
  45. ipc.on('uiAction_checkedWalletFile', function(ev, error, type) {
  46. switch (type) {
  47. case 'presale':
  48. console.log(`Imported ${type} account`);
  49. TemplateVar.set(template, 'filePath', files[0].path);
  50. Tracker.afterFlush(function() {
  51. template.$('.password').focus();
  52. });
  53. break;
  54. case 'web3':
  55. console.log(`Imported ${type} account`);
  56. TemplateVar.set(template, 'filePath', files[0].path);
  57. TemplateVar.set(template, 'importing', true);
  58. setTimeout(function() {
  59. ipc.send('backendAction_closePopupWindow');
  60. }, 750);
  61. break;
  62. default:
  63. GlobalNotification.warning({
  64. content: TAPi18n.__(
  65. 'mist.popupWindows.importAccount.errors.unknownFile'
  66. ),
  67. duration: 4
  68. });
  69. }
  70. });
  71. }
  72. $(e.currentTarget).removeClass('active');
  73. },
  74. /**
  75. On drag over prevent redirect
  76. @event dragover .dropable
  77. */
  78. 'dragover .dropable': function(e) {
  79. e.preventDefault();
  80. },
  81. /**
  82. On show password
  83. @event click .show-password
  84. */
  85. 'click .show-password': function(e) {
  86. TemplateVar.set('showPassword', e.currentTarget.checked);
  87. },
  88. /**
  89. Checks the password match sends the file path and password to the mist backend to import
  90. @event submit form
  91. */
  92. 'submit form': function(e, template) {
  93. var pw = template.find('input.password').value;
  94. ipc.send('backendAction_importWalletFile', TemplateVar.get('filePath'), pw);
  95. TemplateVar.set('importing', true);
  96. ipc.on('uiAction_importedWalletFile', function(ev, error, address) {
  97. TemplateVar.set(template, 'importing', false);
  98. TemplateVar.set(template, 'filePath', false);
  99. if (address) {
  100. ipc.removeAllListeners('uiAction_importedWalletFile');
  101. console.log('Imported account: ', address);
  102. // move to add account screen, when in the importAccount window
  103. if ($('.importAccount-start')[0]) {
  104. TemplateVar.setTo(
  105. '.importAccount-account',
  106. 'newAccount',
  107. web3.utils.toChecksumAddress(address)
  108. );
  109. TemplateVar.setTo(
  110. '.importAccount-screen',
  111. 'currentActive',
  112. 'account'
  113. );
  114. // otherwise simply close the window
  115. } else {
  116. ipc.send('backendAction_closePopupWindow');
  117. }
  118. } else {
  119. console.log('Import failed', error);
  120. if (error === 'Decryption Failed') {
  121. GlobalNotification.warning({
  122. content: TAPi18n.__(
  123. 'mist.popupWindows.importAccount.errors.wrongPassword'
  124. ),
  125. duration: 4
  126. });
  127. } else {
  128. GlobalNotification.warning({
  129. content: TAPi18n.__(
  130. 'mist.popupWindows.importAccount.errors.importFailed',
  131. {
  132. error
  133. }
  134. ),
  135. duration: 4
  136. });
  137. }
  138. }
  139. });
  140. // clear form
  141. template.find('input.password').value = '';
  142. pw = null;
  143. }
  144. });