2016-11-14 14:52:10 -03:00
'use strict' ;
2016-11-18 15:29:53 -03:00
angular . module ( 'copayApp.controllers' ) . controller ( 'addressesController' , function ( $scope , $stateParams , $state , $timeout , $ionicHistory , $ionicPopover , $ionicScrollDelegate , configService , popupService , gettextCatalog , ongoingProcess , lodash , profileService , walletService , platformInfo ) {
2016-11-16 15:23:26 -03:00
var UNUSED _ADDRESS _LIMIT = 5 ;
var BALANCE _ADDRESS _LIMIT = 5 ;
2016-11-18 15:29:53 -03:00
var MENU _ITEM _HEIGHT = 55 ;
2016-11-15 17:40:44 -03:00
var config ;
var unitName ;
var unitToSatoshi ;
var satToUnit ;
var unitDecimals ;
2016-11-16 16:11:36 -03:00
var withBalance ;
2016-11-14 16:51:11 -03:00
$scope . showInfo = false ;
2016-11-17 13:17:28 -03:00
$scope . showMore = false ;
2016-11-18 15:29:53 -03:00
$scope . allAddressesView = false ;
$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
2016-11-16 15:23:26 -03:00
function init ( ) {
2016-11-17 11:20:06 -03:00
ongoingProcess . set ( 'gettingAddresses' , 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 ) {
2016-11-17 11:20:06 -03:00
ongoingProcess . set ( 'gettingAddresses' , false ) ;
2016-11-15 17:40:44 -03:00
return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
}
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 ) {
2016-11-17 11:20:06 -03:00
ongoingProcess . set ( 'gettingAddresses' , false ) ;
2016-11-16 15:23:26 -03:00
if ( err ) {
return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , err ) ;
}
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 ) ;
2016-11-16 16:11:36 -03:00
$scope . latestWithBalance = lodash . slice ( withBalance , 0 , BALANCE _ADDRESS _LIMIT ) ;
lodash . each ( withBalance , function ( a ) {
2016-11-16 15:23:26 -03:00
a . balanceStr = ( a . amount * satToUnit ) . toFixed ( unitDecimals ) + ' ' + unitName ;
} ) ;
$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 ) ;
2016-11-16 15:23:26 -03:00
$scope . $digest ( ) ;
2016-11-15 17:40:44 -03:00
} ) ;
2016-11-14 16:51:11 -03:00
} ) ;
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 ) ;
2016-11-17 13:17:28 -03:00
$scope . gapReached = true ;
$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.' ) ) ;
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 ( ) {
$state . go ( 'tabs.receive.allAddresses' , {
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-11-18 15:29:53 -03:00
$scope . showMenu = function ( allAddresses , $event ) {
var scanObj = {
text : gettextCatalog . getString ( 'Scan addresses for funds' ) ,
action : scan ,
} ;
var sendAddressesObj = {
text : gettextCatalog . getString ( 'Send addresses by email' ) ,
action : sendByEmail ,
}
$scope . items = allAddresses ? [ sendAddressesObj ] : [ scanObj ] ;
$scope . height = $scope . items . length * MENU _ITEM _HEIGHT ;
$ionicPopover . fromTemplateUrl ( 'views/includes/menu-popover.html' , {
scope : $scope
} ) . then ( function ( popover ) {
$scope . menu = popover ;
$scope . menu . show ( $event ) ;
} ) ;
} ;
var scan = function ( ) {
walletService . startScan ( $scope . wallet ) ;
$scope . menu . hide ( ) ;
$ionicHistory . clearHistory ( ) ;
$state . go ( 'tabs.home' ) ;
} ;
var sendByEmail = function ( ) {
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 ( ) {
var body = 'Copay Wallet "' + $scope . walletName + '" Addresses\n Only Main Addresses are shown.\n\n' ;
body += "\n" ;
body += $scope . allAddresses . map ( function ( v ) {
return ( '* ' + v . address + ' ' + 'xpub' + v . path . substring ( 1 ) + ' ' + formatDate ( v . createdOn ) ) ;
} ) . join ( "\n" ) ;
ongoingProcess . set ( 'sendingByEmail' , false ) ;
window . plugins . socialsharing . shareViaEmail (
body ,
'Copay Addresses' ,
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 ( ) { }
) ;
$scope . menu . hide ( ) ;
} ) ;
} ;
$scope . $on ( "$ionicView.beforeEnter" , function ( event , data ) {
$scope . allAddressesView = data . stateName == 'tabs.receive.allAddresses' ? true : false ;
$timeout ( function ( ) {
$scope . $apply ( ) ;
} ) ;
} ) ;
2016-11-17 13:30:52 -03:00
$scope . $on ( "$ionicView.afterEnter" , function ( event , data ) {
2016-11-16 16:11:36 -03:00
config = configService . getSync ( ) . wallet . settings ;
unitToSatoshi = config . unitToSatoshi ;
satToUnit = 1 / unitToSatoshi ;
unitName = config . unitName ;
unitDecimals = config . unitDecimals ;
if ( ! $scope . allAddresses || $scope . allAddresses . length < 0 ) init ( ) ;
} ) ;
2016-11-14 14:52:10 -03:00
} ) ;