2015-08-08 09:30:50 -03:00
'use strict' ;
2015-11-18 18:37:42 -03:00
angular . module ( 'copayApp.controllers' ) . controller ( 'exportController' ,
2016-12-27 15:19:53 -03:00
function ( $scope , $timeout , $log , $ionicHistory , $ionicScrollDelegate , backupService , walletService , storageService , profileService , platformInfo , gettextCatalog , $state , $stateParams , popupService , appConfigService ) {
2016-08-18 19:23:58 -03:00
var wallet = profileService . getWallet ( $stateParams . walletId ) ;
2017-01-18 13:15:46 -03:00
$scope . wallet = wallet ;
2016-06-16 12:18:11 -03:00
2016-10-07 16:23:08 -03:00
$scope . showAdvChange = function ( ) {
$scope . showAdv = ! $scope . showAdv ;
$scope . resizeView ( ) ;
} ;
$scope . resizeView = function ( ) {
$timeout ( function ( ) {
$ionicScrollDelegate . resize ( ) ;
2016-12-14 15:37:15 -03:00
} , 10 ) ;
2016-10-07 16:23:08 -03:00
} ;
2016-10-14 11:14:10 -03:00
function getPassword ( cb ) {
2016-10-13 10:16:10 -03:00
if ( $scope . password ) return cb ( null , $scope . password ) ;
2016-10-12 18:11:02 -03:00
walletService . prepare ( wallet , function ( err , password ) {
2016-10-13 10:16:10 -03:00
if ( err ) return cb ( err ) ;
2016-10-12 18:11:02 -03:00
$scope . password = password ;
2016-10-14 11:14:10 -03:00
return cb ( null , password ) ;
2016-10-12 18:11:02 -03:00
} ) ;
} ;
$scope . generateQrCode = function ( ) {
2016-10-14 11:14:10 -03:00
if ( $scope . formData . exportWalletInfo || ! walletService . isEncrypted ( wallet ) ) {
2016-10-12 18:11:02 -03:00
$scope . file . value = false ;
}
2016-10-14 11:14:10 -03:00
getPassword ( function ( err , password ) {
2016-10-13 10:16:10 -03:00
if ( err ) {
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
return ;
}
2016-10-14 11:14:10 -03:00
walletService . getEncodedWalletInfo ( wallet , password , function ( err , code ) {
if ( err ) return cb ( err ) ;
2016-10-13 10:16:10 -03:00
2016-10-14 11:14:10 -03:00
if ( ! code )
$scope . formData . supported = false ;
else {
$scope . formData . supported = true ;
$scope . formData . exportWalletInfo = code ;
}
$scope . file . value = false ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} ) ;
2016-10-13 10:16:10 -03:00
} ) ;
2016-10-12 18:11:02 -03:00
} ) ;
} ;
2016-09-22 10:34:00 -03:00
var init = function ( ) {
2016-09-07 12:23:09 -03:00
$scope . formData = { } ;
$scope . isEncrypted = wallet . isPrivKeyEncrypted ( ) ;
$scope . isCordova = platformInfo . isCordova ;
$scope . isSafari = platformInfo . isSafari ;
$scope . formData . noSignEnabled = false ;
2016-06-29 15:18:49 -03:00
$scope . showAdvanced = false ;
2016-08-18 19:23:58 -03:00
$scope . wallet = wallet ;
$scope . canSign = wallet . canSign ( ) ;
2016-06-27 16:01:06 -03:00
} ;
2016-06-16 14:43:10 -03:00
2016-06-29 17:04:40 -03:00
/ *
EXPORT WITHOUT PRIVATE KEY - PENDING
2016-09-07 12:23:09 -03:00
* /
2016-06-29 17:04:40 -03:00
2016-06-29 15:18:49 -03:00
$scope . noSignEnabledChange = function ( ) {
2016-09-07 12:27:28 -03:00
if ( ! $scope . formData . supported ) return ;
2016-09-07 12:23:09 -03:00
walletService . getEncodedWalletInfo ( wallet , function ( err , code ) {
if ( err ) {
$log . error ( err ) ;
$scope . formData . supported = false ;
$scope . formData . exportWalletInfo = null ;
} else {
$scope . formData . supported = true ;
$scope . formData . exportWalletInfo = code ;
}
$timeout ( function ( ) {
$scope . $apply ( ) ;
} , 1 ) ;
} ) ;
2016-06-29 15:18:49 -03:00
} ;
2016-06-13 16:13:35 -03:00
$scope . downloadWalletBackup = function ( ) {
2016-10-14 11:14:10 -03:00
getPassword ( function ( err , password ) {
2016-10-13 10:16:10 -03:00
if ( err ) {
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
return ;
}
2015-11-05 17:48:56 -03:00
2016-10-12 18:11:02 -03:00
$scope . getAddressbook ( function ( err , localAddressBook ) {
2015-11-05 17:48:56 -03:00
if ( err ) {
2016-09-01 10:56:13 -03:00
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Failed to export' ) ) ;
2015-11-05 17:48:56 -03:00
return ;
}
2016-10-12 18:11:02 -03:00
var opts = {
noSign : $scope . formData . noSignEnabled ,
addressBook : localAddressBook ,
password : password
} ;
backupService . walletDownload ( $scope . formData . password , opts , function ( err ) {
if ( err ) {
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Failed to export' ) ) ;
return ;
}
$ionicHistory . removeBackView ( ) ;
$state . go ( 'tabs.home' ) ;
} ) ;
2015-11-05 17:48:56 -03:00
} ) ;
2015-08-08 09:30:50 -03:00
} ) ;
} ;
2016-06-13 16:13:35 -03:00
$scope . getAddressbook = function ( cb ) {
2016-08-19 13:09:27 -03:00
storageService . getAddressbook ( wallet . credentials . network , function ( err , addressBook ) {
2015-11-05 17:48:56 -03:00
if ( err ) return cb ( err ) ;
var localAddressBook = [ ] ;
try {
localAddressBook = JSON . parse ( addressBook ) ;
} catch ( ex ) {
$log . warn ( ex ) ;
}
2015-11-10 12:49:12 -03:00
2015-11-05 17:48:56 -03:00
return cb ( null , localAddressBook ) ;
} ) ;
2016-06-16 12:18:11 -03:00
} ;
2015-11-05 17:48:56 -03:00
2016-06-13 16:13:35 -03:00
$scope . getBackup = function ( cb ) {
2016-10-14 11:14:10 -03:00
getPassword ( function ( err , password ) {
2015-11-05 17:48:56 -03:00
if ( err ) {
2016-10-13 10:16:10 -03:00
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
return ;
2015-11-05 17:48:56 -03:00
}
2016-10-13 10:16:10 -03:00
$scope . getAddressbook ( function ( err , localAddressBook ) {
if ( err ) {
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Failed to export' ) ) ;
return cb ( null ) ;
}
var opts = {
noSign : $scope . formData . noSignEnabled ,
addressBook : localAddressBook ,
password : password
} ;
var ew = backupService . walletExport ( $scope . formData . password , opts ) ;
if ( ! ew ) {
popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Failed to export' ) ) ;
}
return cb ( ew ) ;
} ) ;
2015-11-05 17:48:56 -03:00
} ) ;
2016-06-16 12:18:11 -03:00
} ;
2015-08-08 09:30:50 -03:00
2016-06-13 16:13:35 -03:00
$scope . viewWalletBackup = function ( ) {
2015-08-08 09:30:50 -03:00
$timeout ( function ( ) {
2016-06-13 16:13:35 -03:00
$scope . getBackup ( function ( backup ) {
2015-11-10 12:34:03 -03:00
var ew = backup ;
if ( ! ew ) return ;
2016-06-13 16:13:35 -03:00
$scope . backupWalletPlainText = ew ;
2015-11-10 12:34:03 -03:00
} ) ;
2015-08-08 09:30:50 -03:00
} , 100 ) ;
} ;
2016-06-13 16:13:35 -03:00
$scope . copyWalletBackup = function ( ) {
$scope . getBackup ( function ( backup ) {
2015-11-10 12:34:03 -03:00
var ew = backup ;
if ( ! ew ) return ;
window . cordova . plugins . clipboard . copy ( ew ) ;
window . plugins . toast . showShortCenter ( gettextCatalog . getString ( 'Copied to clipboard' ) ) ;
} ) ;
2015-08-08 09:30:50 -03:00
} ;
2016-06-13 16:13:35 -03:00
$scope . sendWalletBackup = function ( ) {
2015-08-08 09:30:50 -03:00
window . plugins . toast . showShortCenter ( gettextCatalog . getString ( 'Preparing backup...' ) ) ;
2016-08-18 19:23:58 -03:00
var name = ( wallet . credentials . walletName || wallet . credentials . walletId ) ;
if ( wallet . alias ) {
name = wallet . alias + ' [' + name + ']' ;
2015-08-08 09:30:50 -03:00
}
2016-06-13 16:13:35 -03:00
$scope . getBackup ( function ( backup ) {
2015-11-10 12:34:03 -03:00
var ew = backup ;
if ( ! ew ) return ;
2016-09-07 12:23:09 -03:00
if ( $scope . formData . noSignEnabled )
2015-11-10 12:34:03 -03:00
name = name + '(No Private Key)' ;
2016-12-27 15:19:53 -03:00
var subject = appConfigService . nameCase + ' Wallet Backup: ' + name ;
2016-04-15 10:26:51 -03:00
var body = 'Here is the encrypted backup of the wallet ' + name + ': \n\n' + ew + '\n\n To import this backup, copy all text between {...}, including the symbols {}' ;
window . plugins . socialsharing . shareViaEmail (
body ,
subject ,
null , // TO: must be null or an array
null , // CC: must be null or an array
null , // BCC: must be null or an array
null , // FILES: can be null, a string, or an array
function ( ) { } ,
function ( ) { }
) ;
2015-11-10 12:34:03 -03:00
} ) ;
2015-08-08 09:30:50 -03:00
} ;
2016-09-23 12:42:33 -03:00
$scope . $on ( "$ionicView.beforeEnter" , function ( event , data ) {
2016-09-22 10:34:00 -03:00
init ( ) ;
2016-10-12 18:11:02 -03:00
$scope . file = {
value : true
} ;
$scope . formData . exportWalletInfo = null ;
$scope . password = null ;
2016-09-22 10:34:00 -03:00
} ) ;
2015-11-10 12:34:03 -03:00
} ) ;