<commit_before>define(['./module', 'frontend/services', 'darkwallet', 'bitcoinjs-lib'],
function (controllers, Services, DarkWallet, Bitcoin) {
  'use strict';
  var BigInteger = Bitcoin.BigInteger;
  controllers.controller('WalletSendCtrl', ['$scope', 'notify', function($scope, notify) {
  $scope.send = {recipient: '', amount: 0.2, mixing: true};
  $scope.autoAddEnabled = false;
  $scope.sendPocket = 0;
  $scope.advanced = false;
  
  $scope.updateBtcFiat = function(field) {
    var tickerService = DarkWallet.service().getTickerService();
    var identity = DarkWallet.getIdentity();
    var currency = identity.settings.currency;
    var fiatCurrency = identity.settings.fiatCurrency;
    if (field.isFiatShown) {
      field.amount = tickerService.fiatToBtc(field.fiatAmount, currency, fiatCurrency);
    } else {
      field.fiatAmount = tickerService.btcToFiat(field.amount, currency, fiatCurrency);
    }
  }

  $scope.setPocket = function(pocket) {
      if (pocket == 'any') {
          $scope.sendPocketName = 'Any';
          // TODO: Any is only using default pocket right now.
          $scope.pocketIndex = 0;
      } else if (typeof pocket == 'number') {
          $scope.sendPocketName = $scope.identity.wallet.pockets.hdPockets[pocket].name;
          $scope.pocketIndex = pocket;
      } else {
          var idx = parseInt(pocket.split(':')[1])
          var fund = $scope.identity.wallet.multisig.funds[idx];
          $scope.sendPocketName = fund.name;
          $scope.pocketIndex = fund.address;
      }
  };

  var initIdentity = function(identity) {
      // init scope variables
      $scope.setPocket($scope.sendPocket);  
      $scope.selectedCurrency = identity.settings.currency;
      if ($scope.selectedCurrency == 'mBTC') {
          $scope.send.fee = $scope.defaultFee*1000;
      } else {
          $scope.send.fee = $scope.defaultFee;
      }
      if (!$scope.$$phase) {
          $scope.$apply();
      }
  }

  // Identity ready
  Services.connectNg('wallet', $scope, function(data) {
    if (data.type == 'ready') {
        // Set the default fee
        var identity = DarkWallet.getIdentity();
        initIdentity(identity);
    }
  })


  $scope.sendBitcoins = function() {
    
      $scope.openModal('ask-password', {text: 'Unlock password', password: ''}, $scope.finishSend)
  }
  $scope.finishSend = function(password) {
      // get a free change address
      var identity = DarkWallet.getIdentity();
      var changeAddress = $scope.getChangeAddress($scope.pocketIndex);

      // prepare amounts
      var satoshis;
      var currency = identity.settings.currency;
      if (currency == 'mBTC') {
          satoshis = 100000;
      } else {
          satoshis = 100000000;
      }
      // Prepare recipients
      var recipients = [];
      var totalAmount = 0;
      $scope.recipients.fields.forEach(function(recipient) {
          if (!recipient.amount || !recipient.address) {
              return;
          }
          var amount = parseInt(BigInteger.valueOf(recipient.amount * satoshis).toString());
          totalAmount += amount;
          recipients.push({address: recipient.address, amount: amount})
      });

      var fee = parseInt(BigInteger.valueOf($scope.send.fee * satoshis).toString());

      var onSent = function(error, task) {
          // this should actually be a starting note, but we don't have a finishing callback yet.
          // we can also use something to show radar progress
          if (error) {
              notify.error("Can't send", ""+error.message);
              console.log("error", error);
          } else if (task && task.radar) {
              console.log("radar", task.radar)
          } else if (task && task.type == 'mixer') {
              notify.note('Sent to mixer ('+task.task.state+')', (fee + totalAmount) + ' satoshis')
              console.log("mixer", task)
          } else if (task) {
              notify.note('Signatures pending', 'Sending ' + (fee + totalAmount) + ' satoshis')
              console.log("pending", task)
          } else {
              notify.success('Bitcoins sent', 'Sent ' + (fee + totalAmount) + ' satoshis');
          }
      }

      // prepare the transaction
      var walletService = DarkWallet.service().getWalletService();

      walletService.send($scope.pocketIndex,
                         recipients,
                         changeAddress,
                         fee,
                         $scope.send.mixing,
                         password,
                         onSent);
  }

  $scope.recipients = {
    fields: [
      { address: '', amount: '' }
    ],
    field_proto: { address: '', amount: '' }
  };

  $scope.addAddress = function(data, vars) {
    vars.field.address = data;
    $scope.autoAddField();
  };
  
  $scope.onQrModalOkSend = function(data, vars) {
    $scope.onQrModalOk(data, vars);
    $scope.autoAddField();
  }

  $scope.addField = function() {
    // add the new option to the model
    $scope.recipients.fields.push($scope.recipients.field_proto);
    // clear the option.
    $scope.recipients.field_proto = { address: '', amount: '' };
  };
  
  $scope.autoAddField = function() {
    if (!$scope.autoAddEnabled) {
      return;
    }
    var fields = $scope.recipients.fields;
    var lastFields = fields[fields.length - 1];
    var field_keys = Object.keys($scope.recipients.field_proto);
    var empty;
    field_keys.forEach(function(key) {
      empty = empty || lastFields[key];
    });
    if (empty) {
      $scope.addField();
    }
  };
  
  $scope.toggleCoinJoin = function() {
    $scope.send.mixing = !$scope.send.mixing;
  }

  $scope.enableAutoAddFields = function() {
    $scope.addField();
    $scope.autoAddEnabled = true;
  };
}]);
});
<commit_msg>Some more feedback for the radar.<commit_after>define(['./module', 'frontend/services', 'darkwallet', 'bitcoinjs-lib'],
function (controllers, Services, DarkWallet, Bitcoin) {
  'use strict';
  var BigInteger = Bitcoin.BigInteger;
  controllers.controller('WalletSendCtrl', ['$scope', 'notify', function($scope, notify) {
  $scope.send = {recipient: '', amount: 0.2, mixing: true};
  $scope.autoAddEnabled = false;
  $scope.sendPocket = 0;
  $scope.advanced = false;
  
  $scope.updateBtcFiat = function(field) {
    var tickerService = DarkWallet.service().getTickerService();
    var identity = DarkWallet.getIdentity();
    var currency = identity.settings.currency;
    var fiatCurrency = identity.settings.fiatCurrency;
    if (field.isFiatShown) {
      field.amount = tickerService.fiatToBtc(field.fiatAmount, currency, fiatCurrency);
    } else {
      field.fiatAmount = tickerService.btcToFiat(field.amount, currency, fiatCurrency);
    }
  }

  $scope.setPocket = function(pocket) {
      if (pocket == 'any') {
          $scope.sendPocketName = 'Any';
          // TODO: Any is only using default pocket right now.
          $scope.pocketIndex = 0;
      } else if (typeof pocket == 'number') {
          $scope.sendPocketName = $scope.identity.wallet.pockets.hdPockets[pocket].name;
          $scope.pocketIndex = pocket;
      } else {
          var idx = parseInt(pocket.split(':')[1])
          var fund = $scope.identity.wallet.multisig.funds[idx];
          $scope.sendPocketName = fund.name;
          $scope.pocketIndex = fund.address;
      }
  };

  var initIdentity = function(identity) {
      // init scope variables
      $scope.setPocket($scope.sendPocket);  
      $scope.selectedCurrency = identity.settings.currency;
      if ($scope.selectedCurrency == 'mBTC') {
          $scope.send.fee = $scope.defaultFee*1000;
      } else {
          $scope.send.fee = $scope.defaultFee;
      }
      if (!$scope.$$phase) {
          $scope.$apply();
      }
  }

  // Identity ready
  Services.connectNg('wallet', $scope, function(data) {
    if (data.type == 'ready') {
        // Set the default fee
        var identity = DarkWallet.getIdentity();
        initIdentity(identity);
    }
  })


  $scope.sendBitcoins = function() {
    
      $scope.openModal('ask-password', {text: 'Unlock password', password: ''}, $scope.finishSend)
  }
  $scope.finishSend = function(password) {
      // get a free change address
      var identity = DarkWallet.getIdentity();
      var changeAddress = $scope.getChangeAddress($scope.pocketIndex);

      // prepare amounts
      var satoshis;
      var currency = identity.settings.currency;
      if (currency == 'mBTC') {
          satoshis = 100000;
      } else {
          satoshis = 100000000;
      }
      // Prepare recipients
      var recipients = [];
      var totalAmount = 0;
      $scope.recipients.fields.forEach(function(recipient) {
          if (!recipient.amount || !recipient.address) {
              return;
          }
          var amount = parseInt(BigInteger.valueOf(recipient.amount * satoshis).toString());
          totalAmount += amount;
          recipients.push({address: recipient.address, amount: amount})
      });

      var fee = parseInt(BigInteger.valueOf($scope.send.fee * satoshis).toString());

      var onSent = function(error, task) {
          // this should actually be a starting note, but we don't have a finishing callback yet.
          // we can also use something to show radar progress
          if (error) {
              notify.error("Can't send", ""+error.message);
              console.log("error", error);
          } else if (task && (task.type == 'radar' || task.radar)) {
              console.log("radar", task.radar)
              notify.note('Propagating!', task.radar + ' %')
          } else if (task && task.type == 'mixer') {
              notify.note('Sent to mixer ('+task.task.state+')', (fee + totalAmount) + ' satoshis')
              console.log("mixer", task)
          } else if (task) {
              notify.note('Signatures pending', 'Sending ' + (fee + totalAmount) + ' satoshis')
              console.log("pending", task)
          } else {
              notify.success('Bitcoins sent', 'Sent ' + (fee + totalAmount) + ' satoshis');
          }
      }

      // prepare the transaction
      var walletService = DarkWallet.service().getWalletService();

      walletService.send($scope.pocketIndex,
                         recipients,
                         changeAddress,
                         fee,
                         $scope.send.mixing,
                         password,
                         onSent);
  }

  $scope.recipients = {
    fields: [
      { address: '', amount: '' }
    ],
    field_proto: { address: '', amount: '' }
  };

  $scope.addAddress = function(data, vars) {
    vars.field.address = data;
    $scope.autoAddField();
  };
  
  $scope.onQrModalOkSend = function(data, vars) {
    $scope.onQrModalOk(data, vars);
    $scope.autoAddField();
  }

  $scope.addField = function() {
    // add the new option to the model
    $scope.recipients.fields.push($scope.recipients.field_proto);
    // clear the option.
    $scope.recipients.field_proto = { address: '', amount: '' };
  };
  
  $scope.autoAddField = function() {
    if (!$scope.autoAddEnabled) {
      return;
    }
    var fields = $scope.recipients.fields;
    var lastFields = fields[fields.length - 1];
    var field_keys = Object.keys($scope.recipients.field_proto);
    var empty;
    field_keys.forEach(function(key) {
      empty = empty || lastFields[key];
    });
    if (empty) {
      $scope.addField();
    }
  };
  
  $scope.toggleCoinJoin = function() {
    $scope.send.mixing = !$scope.send.mixing;
  }

  $scope.enableAutoAddFields = function() {
    $scope.addField();
    $scope.autoAddEnabled = true;
  };
}]);
});

{'fraction_non_alphanumeric': 0.14048948046371834, 'fraction_numerical': 0.0044654358093602405, 'mean_word_length': 2.3245789323437056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2112510', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3458, 'n_words': 701}