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-27 16:01:06 -03:00
function ( $rootScope , $scope , $timeout , $log , lodash , backupService , walletService , fingerprintService , configService , storageService , profileService , platformInfo , notification , go , gettext , gettextCatalog ) {
var prevState ;
2016-06-01 16:07:25 -03:00
var isWP = platformInfo . isWP ;
var isAndroid = platformInfo . isAndroid ;
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-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-06-16 14:43:10 -03:00
$scope . init = function ( state ) {
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-06-27 16:01:06 -03:00
prevState = state || 'walletHome' ;
2016-06-16 12:18:11 -03:00
2016-06-27 16:01:06 -03:00
fingerprintService . check ( fc , function ( err ) {
if ( err ) {
go . path ( prevState ) ;
return ;
}
2016-06-16 12:18:11 -03:00
2016-06-27 16:01:06 -03:00
handleEncryptedWallet ( fc , function ( err ) {
if ( err ) {
go . path ( prevState ) ;
return ;
}
2016-06-16 12:18:11 -03:00
2016-06-27 16:01:06 -03:00
$scope . exportWalletInfo = encodeWalletInfo ( ) ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} , 1 ) ;
} ) ;
} ) ;
} ;
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 ( ) {
walletService . lock ( fc ) ;
} ) ;
function handleEncryptedWallet ( client , cb ) {
if ( ! walletService . isEncrypted ( client ) ) {
$scope . credentialsEncrypted = false ;
return cb ( ) ;
}
$rootScope . $emit ( 'Local/NeedsPassword' , false , function ( err , password ) {
if ( err ) return cb ( err ) ;
return cb ( walletService . unlock ( client , password ) ) ;
2016-06-16 12:18:11 -03:00
} ) ;
} ;
2015-08-08 09:30:50 -03:00
2016-06-27 16:01:06 -03:00
function encodeWalletInfo ( ) {
var c = fc . credentials ;
2016-06-29 17:04:40 -03:00
var derivationPath = fc . credentials . getBaseAddressDerivationPath ( ) ;
2016-06-27 16:01:06 -03:00
var encodingType = {
mnemonic : 1 ,
xpriv : 2 ,
xpub : 3
} ;
var info ;
2016-06-30 15:59:57 -03:00
$scope . supported = ( c . derivationStrategy == 'BIP44' && c . canSign ( ) ) ;
if ( $scope . supported ) {
2016-06-27 16:01:06 -03:00
if ( c . mnemonic ) {
info = {
type : encodingType . mnemonic ,
2016-06-28 15:34:10 -03:00
data : c . mnemonic ,
2016-06-27 16:01:06 -03:00
}
} else {
info = {
type : encodingType . xpriv ,
data : c . xPrivKey
}
}
} else {
2016-06-29 17:04:40 -03:00
/ *
EXPORT WITHOUT PRIVATE KEY - PENDING
2016-06-27 16:01:06 -03:00
info = {
type : encodingType . xpub ,
data : c . xPubKey
}
2016-06-29 17:04:40 -03:00
* /
return null ;
2016-06-27 16:01:06 -03:00
}
2016-06-29 17:04:40 -03:00
var code = info . type + '|' + info . data + '|' + c . network . toLowerCase ( ) + '|' + derivationPath + '|' + ( c . mnemonicHasPassphrase ) ;
2016-06-29 10:19:49 -03:00
return code ;
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
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
} ) ;