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-06-17 11:46:40 -03:00
function ( $scope , $timeout , $log , backupService , fingerprintService , configService , storageService , profileService , platformInfo , notification , go , gettext , gettextCatalog ) {
2016-06-01 16:07:25 -03:00
var isWP = platformInfo . isWP ;
var isAndroid = platformInfo . isAndroid ;
2016-06-22 16:14:50 -03:00
var isCordova = platformInfo . isCordova ;
2016-06-17 11:46:40 -03:00
$scope . error = null ;
$scope . success = null ;
2015-08-08 09:30:50 -03:00
var fc = profileService . focusedClient ;
2016-06-13 16:13:35 -03:00
$scope . isEncrypted = fc . isPrivKeyEncrypted ( ) ;
2016-06-16 14:43:10 -03:00
$scope . touchidSuccess = null ;
$scope . touchidEnabled = null ;
2016-06-16 12:18:11 -03:00
$scope . error = null ;
2016-06-16 14:43:10 -03:00
$scope . init = function ( state ) {
2016-06-16 12:18:11 -03:00
if ( ! isCordova ) return ;
var config = configService . getSync ( ) ;
var touchidAvailable = fingerprintService . isAvailable ( ) ;
2016-06-16 14:43:10 -03:00
var touchidEnabled = $scope . touchidEnabled = config . touchIdFor ? config . touchIdFor [ fc . credentials . walletId ] : null ;
2016-06-16 12:18:11 -03:00
if ( ! touchidAvailable || ! touchidEnabled ) return ;
fingerprintService . check ( fc , function ( err ) {
if ( err )
2016-06-16 14:43:10 -03:00
go . path ( state || 'walletHome' ) ;
$scope . touchidSuccess = true ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} , 10 ) ;
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 . 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
var fc = profileService . focusedClient ;
window . plugins . toast . showShortCenter ( gettextCatalog . getString ( 'Preparing backup...' ) ) ;
var name = ( fc . credentials . walletName || fc . credentials . walletId ) ;
if ( fc . alias ) {
name = fc . alias + ' [' + name + ']' ;
}
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
} ) ;