{"size": 5255, "ext": "js", "max_stars_repo_path": "src/js/controllers/preferencesInformation.js", "max_stars_repo_name": "waffle-iron/dagcoin", "max_stars_repo_stars_event_min_datetime": "2020-02-26T19:26:20.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-31T11:55:38.000Z", "max_issues_repo_path": "src/js/controllers/preferencesInformation.js", "max_issues_repo_name": "waffle-iron/dagcoin", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/controllers/preferencesInformation.js", "max_forks_repo_name": "waffle-iron/dagcoin", "max_forks_repo_forks_event_min_datetime": "2020-06-17T22:24:31.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-17T22:24:31.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 29.5224719101, "max_line_length": 116, "alphanum_fraction": 0.4862036156}
(function () {
  'use strict';

  angular.module('copayApp.controllers').controller('preferencesInformation',
    function (
      $scope,
      $log,
      $timeout,
      isMobile,
      gettextCatalog,
      lodash,
      profileService,
      storageService,
      go,
      configService,
      addressService,
      $rootScope,
      fundingExchangeClientService,
      proofingService,
      dagcoinProtocolService
    ) {
      const constants = require('byteballcore/constants.js');
      const fc = profileService.focusedClient;
      const c = fc.credentials;

      const indexScope = $scope.index;

      this.setAddress = function () {
        if (!fc) {
          return;
        }

        if (indexScope.shared_address) {
          throw Error('attempt to generate for shared address');
        }

        addressService.getAddress(fc.credentials.walletId, true, (err, addr) => {
          if (err) {
            $rootScope.$emit('Local/ShowAlert', err, 'fi-alert', () => { });
          } else if (addr) {
            $rootScope.$emit('Local/ShowAlert', 'New Address successfully generated.', 'fi-check', () => { });
            this.init();
            proofingService.proofCurrentAddress().then((proof) => {
              dagcoinProtocolService.sendRequest(
                fundingExchangeClientService.bytesProviderDeviceAddress,
                'link-address',
                proof
              );
            });
          }
        });
      };

      this.init = function () {
        const basePath = c.getBaseAddressDerivationPath();
        const config = configService.getSync().wallet.settings;

        $scope.walletName = c.walletName;
        $scope.walletId = c.walletId;
        $scope.network = c.network;
        $scope.derivationStrategy = c.derivationStrategy || 'BIP44';
        $scope.basePath = basePath;
        $scope.M = c.m;
        $scope.N = c.n;
        $scope.addrs = null;

        fc.getAddresses({
          doNotVerify: true,
        }, (err, addrs) => {
          if (err) {
            $log.warn(err);
            return;
          }
          /* var last10 = [],
           i = 0,
           e = addrs.pop();
           while (i++ < 10 && e) {
           e.path = e.path;
           last10.push(e);
           e = addrs.pop();
           }
           $scope.addrs = last10; */
          $scope.addrs = addrs;
          $timeout(() => {
            $scope.$apply();
          });
        });

        fc.getListOfBalancesOnAddresses((listOfBalances) => {
          const balanceList = listOfBalances.map((row) => {
            if (row.asset === 'base' || row.asset === constants.DAGCOIN_ASSET) {
              const assetName = row.asset !== 'base' ? 'DAG' : 'base';
              const unitName = row.asset !== 'base' ? config.dagUnitName : config.unitName;
              row.amount = `${profileService.formatAmount(row.amount, assetName, { dontRound: true })} ${unitName}`;
              return row;
            }
            return row;
          });
          // groupBy address
          const assocListOfBalances = {};
          balanceList.forEach((row) => {
            if (assocListOfBalances[row.address] === undefined) assocListOfBalances[row.address] = [];
            assocListOfBalances[row.address].push(row);
          });
          $scope.assocListOfBalances = assocListOfBalances;
          $timeout(() => {
            $scope.$apply();
          });
        });
      };

      $scope.hasListOfBalances = function () {
        return !!Object.keys($scope.assocListOfBalances || {}).length;
      };

      this.sendAddrs = function () {
        const self = this;

        if (isMobile.Android() || isMobile.Windows()) {
          window.ignoreMobilePause = true;
        }

        self.loading = true;

        function formatDate(ts) {
          const dateObj = new Date(ts * 1000);
          if (!dateObj) {
            $log.debug('Error formating a date');
            return 'DateError';
          }
          if (!dateObj.toJSON()) {
            return '';
          }
          return dateObj.toJSON();
        }

        $timeout(() => {
          fc.getAddresses({
            doNotVerify: true,
          }, (err, addrs) => {
            self.loading = false;
            if (err) {
              $log.warn(err);
              return;
            }

            let body = `Dagcoin Wallet "${$scope.walletName}" Addresses.\n\n`;
            body += '\n';
            body += addrs.map(v => (`* ${v.address} ${v.path} ${formatDate(v.createdOn)}`)).join('\n');

            window.plugins.socialsharing.shareViaEmail(
              body,
              'Dagcoin 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
              () => {},
              () => {}
            );

            $timeout(() => {
              $scope.$apply();
            }, 1000);
          });
        }, 100);
      };

      this.clearTransactionHistory = function () {
        $scope.$emit('Local/ClearHistory');

        $timeout(() => {
          go.walletHome();
        }, 100);
      };
    });
}());
