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-01 16:07:25 -03:00
function ( $rootScope , $scope , $timeout , $log , backupService , storageService , profileService , platformInfo , notification , go , gettext , gettextCatalog ) {
2015-11-05 17:48:56 -03:00
var self = this ;
2016-06-01 16:07:25 -03:00
var isWP = platformInfo . isWP ;
var isAndroid = platformInfo . isAndroid ;
2015-11-05 17:48:56 -03:00
self . error = null ;
self . success = null ;
2016-05-20 09:50:44 -03:00
$scope . metaDataEnabled = true ;
2015-08-08 09:30:50 -03:00
var fc = profileService . focusedClient ;
2015-11-05 17:48:56 -03:00
self . isEncrypted = fc . isPrivKeyEncrypted ( ) ;
2015-08-08 09:30:50 -03:00
2015-11-05 17:48:56 -03:00
self . downloadWalletBackup = function ( ) {
2016-05-20 09:50:44 -03:00
self . getMetaData ( $scope . metaDataEnabled , function ( err , txsFromLocal , localAddressBook ) {
2015-08-08 09:30:50 -03:00
if ( err ) {
self . 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
historyCache : txsFromLocal ,
addressBook : localAddressBook
} ;
backupService . walletDownload ( self . password , opts , function ( err ) {
if ( err ) {
self . error = true ;
return ;
}
2015-11-27 20:47:23 -03:00
$rootScope . $emit ( 'Local/BackupDone' ) ;
2015-11-05 17:48:56 -03:00
notification . success ( gettext ( 'Success' ) , gettext ( 'Encrypted export file saved' ) ) ;
go . walletHome ( ) ;
} ) ;
2015-08-08 09:30:50 -03:00
} ) ;
} ;
2015-11-11 18:54:41 -03:00
self . getMetaData = function ( metaData , cb ) {
if ( metaData == false ) return cb ( ) ;
2015-11-05 17:48:56 -03:00
self . getHistoryCache ( function ( err , txsFromLocal ) {
2015-11-10 12:49:12 -03:00
if ( err ) return cb ( err ) ;
2015-11-05 17:48:56 -03:00
self . getAddressbook ( function ( err , localAddressBook ) {
2015-11-10 12:49:12 -03:00
if ( err ) return cb ( err ) ;
2015-11-05 17:48:56 -03:00
return cb ( null , txsFromLocal , localAddressBook )
} ) ;
} ) ;
}
2015-08-08 09:30:50 -03:00
2015-11-05 17:48:56 -03:00
self . getHistoryCache = function ( cb ) {
storageService . getTxHistory ( fc . credentials . walletId , function ( err , txs ) {
if ( err ) return cb ( err ) ;
var localTxs = [ ] ;
try {
localTxs = JSON . parse ( txs ) ;
} catch ( ex ) {
$log . warn ( ex ) ;
}
2015-11-10 12:49:12 -03:00
if ( ! localTxs [ 0 ] ) return cb ( null , null ) ;
2015-11-05 17:48:56 -03:00
return cb ( null , localTxs ) ;
} ) ;
}
self . getAddressbook = function ( cb ) {
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 ) ;
} ) ;
}
2015-11-10 12:34:03 -03:00
self . getBackup = function ( cb ) {
2016-05-20 09:50:44 -03:00
self . getMetaData ( $scope . metaDataEnabled , function ( err , txsFromLocal , localAddressBook ) {
2015-11-05 17:48:56 -03:00
if ( err ) {
self . 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
historyCache : txsFromLocal ,
addressBook : localAddressBook
} ;
var ew = backupService . walletExport ( self . password , opts ) ;
if ( ! ew ) {
self . error = true ;
} else {
self . error = false ;
2015-11-27 20:47:23 -03:00
$rootScope . $emit ( 'Local/BackupDone' ) ;
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
} ) ;
}
2015-08-08 09:30:50 -03:00
2015-11-05 17:48:56 -03:00
self . viewWalletBackup = function ( ) {
2015-08-08 09:30:50 -03:00
var self = this ;
$timeout ( function ( ) {
2015-11-10 12:34:03 -03:00
self . getBackup ( function ( backup ) {
var ew = backup ;
if ( ! ew ) return ;
self . backupWalletPlainText = ew ;
} ) ;
2015-08-08 09:30:50 -03:00
} , 100 ) ;
} ;
2015-11-05 17:48:56 -03:00
self . copyWalletBackup = function ( ) {
2015-11-10 12:34:03 -03:00
self . getBackup ( function ( backup ) {
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
} ;
2015-11-05 17:48:56 -03:00
self . 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 + ']' ;
}
2015-11-10 12:34:03 -03:00
self . getBackup ( function ( backup ) {
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
} ) ;