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-08-18 19:23:58 -03:00
function ( $rootScope , $scope , $timeout , $log , lodash , backupService , walletService , storageService , profileService , platformInfo , notification , go , gettext , gettextCatalog , $state , $stateParams ) {
2016-06-27 16:01:06 -03:00
var prevState ;
2016-06-01 16:07:25 -03:00
var isWP = platformInfo . isWP ;
var isAndroid = platformInfo . isAndroid ;
2016-08-18 19:23:58 -03:00
var wallet = profileService . getWallet ( $stateParams . walletId ) ;
$scope . isEncrypted = wallet . isPrivKeyEncrypted ( ) ;
2016-06-27 16:01:06 -03:00
$scope . isCordova = platformInfo . isCordova ;
$scope . isSafari = platformInfo . isSafari ;
2016-06-16 12:18:11 -03:00
$scope . error = null ;
2016-08-18 19:23:58 -03:00
$scope . init = function ( ) {
2016-06-30 15:59:57 -03:00
$scope . supported = true ;
2016-06-29 17:04:40 -03:00
$scope . exportQR = false ;
2016-06-29 15:18:49 -03:00
$scope . noSignEnabled = false ;
$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-06-27 16:01:06 -03:00
if ( err ) {
2016-08-18 19:23:58 -03:00
$log . warn ( err ) ;
return $state . go ( 'wallet.preferencesAdvanced' )
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 )
$scope . supported = false ;
else
$scope . 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-06-29 15:18:49 -03:00
$scope . noSignEnabledChange = function ( ) {
$scope . exportWalletInfo = encodeWalletInfo ( ) ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} , 1 ) ;
} ;
2016-06-29 17:04:40 -03:00
* /
2016-06-29 15:18:49 -03:00
2016-06-27 16:01:06 -03:00
$scope . $on ( '$destroy' , function ( ) {
2016-08-18 19:23:58 -03:00
walletService . lock ( wallet ) ;
2016-06-27 16:01:06 -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-06-13 16:13:35 -03:00
$scope . error = true ;
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-05-20 09:50:44 -03:00
noSign : $scope . noSignEnabled ,
2015-11-05 17:48:56 -03:00
addressBook : localAddressBook
} ;
2016-06-13 16:13:35 -03:00
backupService . walletDownload ( $scope . password , opts , function ( err ) {
2015-11-05 17:48:56 -03:00
if ( err ) {
2016-06-13 16:13:35 -03:00
$scope . error = true ;
2015-11-05 17:48:56 -03:00
return ;
}
notification . success ( gettext ( 'Success' ) , gettext ( 'Encrypted export file saved' ) ) ;
go . walletHome ( ) ;
} ) ;
2015-08-08 09:30:50 -03:00
} ) ;
} ;
2016-06-13 16:13:35 -03:00
$scope . getAddressbook = function ( cb ) {
2015-11-05 17:48:56 -03:00
storageService . getAddressbook ( fc . credentials . network , function ( err , addressBook ) {
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-06-13 16:13:35 -03:00
$scope . error = true ;
2015-11-10 12:34:03 -03:00
return cb ( null ) ;
2015-11-05 17:48:56 -03:00
}
var opts = {
2016-05-20 09:50:44 -03:00
noSign : $scope . noSignEnabled ,
2015-11-05 17:48:56 -03:00
addressBook : localAddressBook
} ;
2016-06-13 16:13:35 -03:00
var ew = backupService . walletExport ( $scope . password , opts ) ;
2015-11-05 17:48:56 -03:00
if ( ! ew ) {
2016-06-13 16:13:35 -03:00
$scope . error = true ;
2015-11-05 17:48:56 -03:00
} else {
2016-06-13 16:13:35 -03:00
$scope . error = false ;
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-05-20 09:50:44 -03:00
if ( $scope . 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
} ;
2015-11-10 12:34:03 -03:00
} ) ;