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-10-07 16:23:08 -03:00
function ( $scope , $timeout , $log , $ionicHistory , $ionicScrollDelegate , backupService , walletService , storageService , profileService , platformInfo , gettextCatalog , $state , $stateParams , popupService ) {
2016-08-18 19:23:58 -03:00
var wallet = profileService . getWallet ( $stateParams . walletId ) ;
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-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-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
walletService . getEncodedWalletInfo ( wallet , function ( err , code ) {
2016-08-29 16:13:18 -03:00
if ( err || ! code ) {
2016-08-18 19:23:58 -03:00
$log . warn ( err ) ;
2016-09-07 12:23:09 -03:00
return $ionicHistory . goBack ( ) ;
2016-06-27 16:01:06 -03:00
}
2016-06-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
if ( ! code )
2016-09-07 12:23:09 -03:00
$scope . formData . supported = false ;
else {
$scope . formData . supported = true ;
$scope . formData . exportWalletInfo = code ;
}
2016-06-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
$timeout ( function ( ) {
$scope . $apply ( ) ;
} , 1 ) ;
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-06-17 11:46:40 -03:00
$scope . getAddressbook ( function ( err , localAddressBook ) {
2015-08-08 09:30:50 -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 ;
2015-08-08 09:30:50 -03:00
}
2015-11-05 17:48:56 -03:00
var opts = {
2016-09-07 12:23:09 -03:00
noSign : $scope . formData . noSignEnabled ,
2015-11-05 17:48:56 -03:00
addressBook : localAddressBook
} ;
2016-09-07 12:23:09 -03:00
backupService . walletDownload ( $scope . formData . password , opts , function ( err ) {
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-09-23 12:42:33 -03:00
$ionicHistory . removeBackView ( ) ;
2016-08-19 13:07:18 -03:00
$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-06-17 11:46:40 -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-10 12:34:03 -03:00
return cb ( null ) ;
2015-11-05 17:48:56 -03:00
}
var opts = {
2016-09-07 12:23:09 -03:00
noSign : $scope . formData . noSignEnabled ,
2015-11-05 17:48:56 -03:00
addressBook : localAddressBook
} ;
2016-09-07 12:23:09 -03:00
var ew = backupService . walletExport ( $scope . formData . password , opts ) ;
2015-11-05 17:48:56 -03:00
if ( ! ew ) {
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
}
2015-11-10 12:34:03 -03:00
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-04-15 10:26:51 -03:00
var subject = 'Copay Wallet Backup: ' + name ;
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 ( ) ;
} ) ;
2015-11-10 12:34:03 -03:00
} ) ;