2016-11-14 14:52:10 -03:00
'use strict' ;
2018-01-11 12:59:26 +09:00
angular . module ( 'copayApp.controllers' ) . controller ( 'addressesController' , function ( $scope , $log , $stateParams , $state , $timeout , $ionicHistory , $ionicScrollDelegate , popupService , gettextCatalog , ongoingProcess , lodash , profileService , walletService , bwcError , platformInfo , appConfigService , txFormatService , feeService , bitcoinCashJsService ) {
2016-11-16 15:23:26 -03:00
var UNUSED _ADDRESS _LIMIT = 5 ;
var BALANCE _ADDRESS _LIMIT = 5 ;
2017-02-10 12:18:35 -03:00
var withBalance , cachedWallet ;
2016-11-18 15:29:53 -03:00
$scope . isCordova = platformInfo . isCordova ;
2016-11-16 16:11:36 -03:00
$scope . wallet = profileService . getWallet ( $stateParams . walletId ) ;
2016-11-15 17:40:44 -03:00
2017-02-10 12:18:35 -03:00
function resetValues ( ) {
$scope . loading = false ;
$scope . showInfo = false ;
$scope . showMore = false ;
$scope . allAddressesView = false ;
$scope . latestUnused = $scope . latestWithBalance = null ;
$scope . viewAll = {
value : false
} ;
} ;
$scope . init = function ( ) {
resetValues ( ) ;
$scope . loading = true ;
2016-11-16 15:23:26 -03:00
walletService . getMainAddresses ( $scope . wallet , { } , function ( err , addresses ) {
2016-11-15 17:40:44 -03:00
if ( err ) {
2017-02-10 12:18:35 -03:00
$scope . loading = false ;
2017-01-16 16:00:09 -03:00
return popupService . showAlert ( bwcError . msg ( err , gettextCatalog . getString ( 'Could not update wallet' ) ) ) ;
2016-11-15 17:40:44 -03:00
}
2016-11-14 16:51:11 -03:00
2016-11-16 16:11:36 -03:00
var allAddresses = addresses ;
2016-11-16 15:23:26 -03:00
walletService . getBalance ( $scope . wallet , { } , function ( err , resp ) {
if ( err ) {
2017-05-15 10:07:03 -03:00
$scope . loading = false ;
2017-01-16 16:00:09 -03:00
return popupService . showAlert ( bwcError . msg ( err , gettextCatalog . getString ( 'Could not update wallet' ) ) ) ;
2016-11-16 15:23:26 -03:00
}
2016-11-16 16:11:36 -03:00
withBalance = resp . byAddress ;
2016-11-16 15:23:26 -03:00
var idx = lodash . indexBy ( withBalance , 'address' ) ;
2016-11-17 11:20:06 -03:00
$scope . noBalance = lodash . reject ( allAddresses , function ( x ) {
2016-11-16 15:23:26 -03:00
return idx [ x . address ] ;
} ) ;
2016-11-17 11:20:06 -03:00
processPaths ( $scope . noBalance ) ;
2016-11-16 16:11:36 -03:00
processPaths ( withBalance ) ;
2016-11-17 11:20:06 -03:00
$scope . latestUnused = lodash . slice ( $scope . noBalance , 0 , UNUSED _ADDRESS _LIMIT ) ;
2018-03-02 14:31:44 +05:00
var withBalanceDict = lodash . indexBy ( withBalance , 'address' ) ;
lodash . each ( allAddresses , function ( a ) {
if ( withBalanceDict [ a . address ] ) {
withBalanceDict [ a . address ] . createdOn = a . createdOn ;
}
} ) ;
2016-11-16 16:11:36 -03:00
$scope . latestWithBalance = lodash . slice ( withBalance , 0 , BALANCE _ADDRESS _LIMIT ) ;
lodash . each ( withBalance , function ( a ) {
2017-08-31 10:08:26 -03:00
a . balanceStr = txFormatService . formatAmountStr ( $scope . wallet . coin , a . amount ) ;
2016-11-16 15:23:26 -03:00
} ) ;
$scope . viewAll = {
2016-11-17 11:20:06 -03:00
value : $scope . noBalance . length > UNUSED _ADDRESS _LIMIT || withBalance . length > BALANCE _ADDRESS _LIMIT
2016-11-16 15:23:26 -03:00
} ;
2016-11-17 11:20:06 -03:00
$scope . allAddresses = $scope . noBalance . concat ( withBalance ) ;
2017-02-10 12:18:35 -03:00
2018-01-11 12:59:26 +09:00
if ( $scope . wallet . coin == 'bch' ) {
lodash . each ( $scope . allAddresses , function ( a ) {
a . translatedAddresses = bitcoinCashJsService . translateAddresses ( a . address ) ;
} ) ;
$scope . addressType = {
2018-01-11 19:07:12 +09:00
type : 'cashaddr'
2018-01-11 12:59:26 +09:00
} ;
2018-01-11 15:03:41 +09:00
$scope . showAddressTypes = true ;
2018-01-11 12:59:26 +09:00
}
2017-02-10 12:18:35 -03:00
cachedWallet = $scope . wallet . id ;
2017-05-15 10:07:03 -03:00
$scope . loading = false ;
2017-02-10 12:18:35 -03:00
$log . debug ( 'Addresses cached for Wallet:' , cachedWallet ) ;
2017-05-15 10:07:03 -03:00
$timeout ( function ( ) {
$ionicScrollDelegate . resize ( ) ;
$scope . $digest ( ) ;
} ) ;
2016-11-15 17:40:44 -03:00
} ) ;
2016-11-14 16:51:11 -03:00
} ) ;
2017-06-22 17:00:20 -03:00
2017-08-29 15:47:39 -03:00
feeService . getFeeLevels ( $scope . wallet . coin , function ( err , levels ) {
2017-06-22 17:00:20 -03:00
walletService . getLowUtxos ( $scope . wallet , levels , function ( err , resp ) {
if ( err ) return ;
2017-08-30 15:54:30 -03:00
if ( resp && resp . allUtxos && resp . allUtxos . length ) {
2017-06-22 17:00:20 -03:00
var allSum = lodash . sum ( resp . allUtxos || 0 , 'satoshis' ) ;
var per = ( resp . minFee / allSum ) * 100 ;
$scope . lowWarning = resp . warning ;
$scope . lowUtxosNb = resp . lowUtxos . length ;
$scope . allUtxosNb = resp . allUtxos . length ;
2017-08-28 15:51:13 -03:00
$scope . lowUtxosSum = txFormatService . formatAmountStr ( $scope . wallet . coin , lodash . sum ( resp . lowUtxos || 0 , 'satoshis' ) ) ;
$scope . allUtxosSum = txFormatService . formatAmountStr ( $scope . wallet . coin , allSum ) ;
$scope . minFee = txFormatService . formatAmountStr ( $scope . wallet . coin , resp . minFee || 0 ) ;
2017-06-22 17:00:20 -03:00
$scope . minFeePer = per . toFixed ( 2 ) + '%' ;
}
} ) ;
} ) ;
2016-11-16 15:23:26 -03:00
} ;
2016-11-14 16:51:11 -03:00
2016-11-16 16:11:36 -03:00
function processPaths ( list ) {
lodash . each ( list , function ( n ) {
n . path = n . path . replace ( /^m/g , 'xpub' ) ;
} ) ;
} ;
2016-11-17 11:20:06 -03:00
$scope . newAddress = function ( ) {
2016-11-17 13:17:28 -03:00
if ( $scope . gapReached ) return ;
2016-11-17 11:20:06 -03:00
ongoingProcess . set ( 'generatingNewAddress' , true ) ;
walletService . getAddress ( $scope . wallet , true , function ( err , addr ) {
if ( err ) {
ongoingProcess . set ( 'generatingNewAddress' , false ) ;
2017-05-10 12:31:40 -03:00
if ( err . toString ( ) . match ( 'MAIN_ADDRESS_GAP_REACHED' ) ) {
$scope . gapReached = true ;
} else {
popupService . showAlert ( err ) ;
}
2016-11-17 13:17:28 -03:00
$timeout ( function ( ) {
$scope . $digest ( ) ;
} ) ;
return ;
2016-11-17 11:20:06 -03:00
}
walletService . getMainAddresses ( $scope . wallet , {
limit : 1
} , function ( err , _addr ) {
ongoingProcess . set ( 'generatingNewAddress' , false ) ;
if ( err ) return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
if ( addr != _addr [ 0 ] . address ) return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'New address could not be generated. Please try again.' ) ) ;
2018-01-11 15:03:41 +09:00
if ( $scope . wallet . coin == 'bch' ) {
_addr [ 0 ] . translatedAddresses = bitcoinCashJsService . translateAddresses ( _addr [ 0 ] . address ) ;
}
2018-01-11 19:07:12 +09:00
2016-11-17 11:58:02 -03:00
$scope . noBalance = [ _addr [ 0 ] ] . concat ( $scope . noBalance ) ;
$scope . latestUnused = lodash . slice ( $scope . noBalance , 0 , UNUSED _ADDRESS _LIMIT ) ;
2016-11-17 11:20:06 -03:00
$scope . viewAll = {
2016-11-17 11:58:02 -03:00
value : $scope . noBalance . length > UNUSED _ADDRESS _LIMIT
2016-11-17 11:20:06 -03:00
} ;
$scope . $digest ( ) ;
} ) ;
} ) ;
} ;
2016-11-17 09:49:29 -03:00
$scope . viewAllAddresses = function ( ) {
2017-06-28 15:25:58 -03:00
var fromView = $ionicHistory . currentStateName ( ) ;
var path ;
if ( fromView . indexOf ( 'settings' ) !== - 1 ) {
path = 'tabs.settings.allAddresses' ;
} else {
path = 'tabs.wallet.allAddresses' ;
}
$state . go ( path , {
2016-11-17 09:49:29 -03:00
walletId : $scope . wallet . id
} ) ;
} ;
2016-11-14 16:51:11 -03:00
$scope . showInformation = function ( ) {
$timeout ( function ( ) {
$scope . showInfo = ! $scope . showInfo ;
$ionicScrollDelegate . resize ( ) ;
2016-12-14 15:37:15 -03:00
} , 10 ) ;
2016-11-14 16:51:11 -03:00
} ;
2016-11-16 16:11:36 -03:00
2016-11-17 13:17:28 -03:00
$scope . readMore = function ( ) {
$timeout ( function ( ) {
$scope . showMore = ! $scope . showMore ;
$ionicScrollDelegate . resize ( ) ;
2016-12-14 15:37:15 -03:00
} , 10 ) ;
2016-11-17 13:17:28 -03:00
} ;
2016-12-27 12:44:47 -03:00
$scope . scan = function ( ) {
2016-11-18 15:29:53 -03:00
walletService . startScan ( $scope . wallet ) ;
2017-05-03 17:08:18 -03:00
$ionicHistory . nextViewOptions ( {
disableAnimate : true ,
historyRoot : true
} ) ;
2016-11-18 15:29:53 -03:00
$ionicHistory . clearHistory ( ) ;
2017-05-03 17:08:18 -03:00
$state . go ( 'tabs.home' ) . then ( function ( ) {
$state . transitionTo ( 'tabs.wallet' , {
walletId : $scope . wallet . credentials . walletId
} ) ;
} ) ;
2016-11-18 15:29:53 -03:00
} ;
2016-12-27 12:44:47 -03:00
$scope . sendByEmail = function ( ) {
2016-11-18 15:29:53 -03:00
function formatDate ( ts ) {
var dateObj = new Date ( ts * 1000 ) ;
if ( ! dateObj ) {
$log . debug ( 'Error formating a date' ) ;
return 'DateError' ;
}
if ( ! dateObj . toJSON ( ) ) {
return '' ;
}
return dateObj . toJSON ( ) ;
} ;
ongoingProcess . set ( 'sendingByEmail' , true ) ;
$timeout ( function ( ) {
2017-02-10 13:39:49 -03:00
var appName = appConfigService . nameCase ;
var body = appName + ' Wallet "' + $scope . wallet . name + '" Addresses\n Only Main Addresses are shown.\n\n' ;
2016-11-18 15:29:53 -03:00
body += "\n" ;
body += $scope . allAddresses . map ( function ( v ) {
2017-02-10 13:39:49 -03:00
return ( '* ' + v . address + ' xpub' + v . path . substring ( 1 ) + ' ' + formatDate ( v . createdOn ) ) ;
2016-11-18 15:29:53 -03:00
} ) . join ( "\n" ) ;
ongoingProcess . set ( 'sendingByEmail' , false ) ;
window . plugins . socialsharing . shareViaEmail (
body ,
2017-02-10 13:39:49 -03:00
appName + ' Addresses' ,
2016-11-18 15:29:53 -03:00
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 ( ) { }
) ;
} ) ;
} ;
2017-02-10 12:18:35 -03:00
function isCachedWallet ( walletId ) {
if ( cachedWallet && cachedWallet == walletId ) return true ;
else return false ;
} ;
2017-02-10 13:35:49 -03:00
$scope . $on ( "$ionicView.afterEnter" , function ( event , data ) {
2016-11-18 15:29:53 -03:00
$scope . allAddressesView = data . stateName == 'tabs.receive.allAddresses' ? true : false ;
2017-02-10 12:18:35 -03:00
if ( ! isCachedWallet ( $stateParams . walletId ) ) $scope . init ( ) ;
else $log . debug ( 'Addresses cached for Wallet:' , $stateParams . walletId ) ;
2016-11-16 16:11:36 -03:00
} ) ;
2016-11-14 14:52:10 -03:00
} ) ;