<commit_before>angular.module("omniServices")
    .service("ModalManager", ["$modal", "$rootScope", "$timeout","TransactionGenerator","TransactionManager","Account","Wallet",
        function ModalManagerService($modal, $rootScope, $timeout, TransactionGenerator, TransactionManager, Account, Wallet) {
          var self = this;

          function decodeAddressFromPrivateKey(key) {
  
            //  Return the address decoded from the private key.
            var eckey = new Bitcoin.ECKey(key);
            var addr = eckey.getBitcoinAddress().toString();
        
            return addr;
          };
        
          function encodePrivateKey(key, passphrase) {
        
            //  Return encoded key.  Forget the passphrase forever.
            var eckey = new Bitcoin.ECKey(key);
            var enc = eckey.getEncryptedFormat(passphrase);
        
            return enc;
          };

          self.openCreateModal = function() {
              var modalScope = $rootScope.$new()
              modalScope.title ='Create New Wallet';
              modalScope.button ='Create Wallet';
              modalScope.bodyTemplate = "/views/modals/partials/create.html";
              modalScope.footerTemplate = "/views/modals/partials/create_footer.html";
              
              self.modalInstance = $modal.open({
                templateUrl: '/views/modals/base.html',
                controller: function CreateWalletController($scope, $location, $modalInstance, $idle, Account, AddressManager) {
                  $scope.dismiss = $modalInstance.dismiss;
                  
                  $scope.createWallet = function(create) {
                    $scope.validating=true;
                    $scope.serverError = $scope.invalidCaptcha =false;
                    Account.create(create).then(function(account){
                      var address = AddressManager.createAddress();
                      account.addAddress(address.hash,address.privkey).then(function(result){
                        $modalInstance.close()
                        $location.path('/wallet');
                        $idle.watch();
                      });
                    },function(error){
                      angular.extend($scope,error);
                    })
                  }

                  $scope.setFormScope = function(form){
                    $scope.createForm = form;
                  }

                  $scope.close = function() {
                    $modalInstance.dismiss('close');
                  };
                },
                scope: modalScope,
                backdrop:'static'
              });
          };

          self.openLoginModal = function() {  
              var modalScope = $rootScope.$new()
              modalScope.title = 'Login';
              modalScope.button = 'Open Wallet';
              modalScope.bodyTemplate = "/views/modals/partials/login.html";
              modalScope.footerTemplate = "/views/modals/partials/login_footer.html";
              

              self.modalInstance = $modal.open({
                templateUrl: '/views/modals/base.html',
                controller: LoginController,
                scope: modalScope,
                backdrop:'static'
              });
          };
          
          self.openConfirmationModal = function(modalConfig) {
              self.modalInstance = $modal.open({
                  templateUrl: "/views/modals/base.html",
                  controller: function ConfirmationModalController($scope, $rootScope, $modalInstance, $location, modalConfig, modalManager) {                        
                      angular.extend($scope, modalConfig.scope);

                      $scope.bodyTemplate = modalConfig.bodyTemplate || "/views/modals/confirmation.html";
                      $scope.footerTemplate = modalConfig.footerTemplate || "/views/modals/partials/confirmation_footer.html";
                      $scope.dataTemplate = modalConfig.dataTemplate || "";

                      $scope.signOffline = modalConfig.transaction.offline;

                      $scope.transaction = modalConfig.transaction;
                      
                      $scope.confirm = function() {
                          $scope.clicked = true;
                          $scope.waiting = true;

                          TransactionManager.processTransaction($scope.transaction).then(function(result){
                            if(result.transactionSuccess){
                              $location.path($scope.successRedirect)
                              $rootScope.notify({
                                message: "Operation success",
                                url : result.url
                              })
                              $modalInstance.dismiss('close');
                            } else if(result.readyToSign){
                              $scope.readyToSign = result.readyToSign;
                              $scope.unsignedTransaction = result.unsignedTransaction;
                            } else {
                              $rootScope.notifyError({
                                message: result.error || "Unknown Error"
                              })
                              $modalInstance.dismiss('close');
                            }
                          }, function(errorData){
                          	$modalInstance.dismiss('close');
                                $rootScope.notifyError({
                                message: errorData.errorMessage || "Unknown Error"
                              })
                          });
                      };

                      $scope.fromAddress = modalConfig.transaction.address.hash;

                      $scope.openBroadcastTransactionForm = function(address) {
                          $modalInstance.dismiss('close');
                          modalManager.openBroadcastTransactionModal(address);
                      };

                      $scope.saveUnsigned = function(unsignedHex) {
                          var exportBlob = new Blob([unsignedHex], {
                              type: 'application/json;charset=utf-8'
                          });
                          fileName = "tx" + (new Date).getTime() + ".unsigned.tx";
                          saveAs(exportBlob, fileName);
                          $scope.unsaved = false;
                          $scope.saved = true;
                      };

                      $scope.cancel = function() {
                          $modalInstance.dismiss('cancel');
                      };

                      $scope.close = function() {
                          $modalInstance.dismiss('close');
                      };
                  },
                  resolve: {
                      modalConfig: function() {
                          return modalConfig;
                      },
                      modalManager: function() {
                          return self;
                      },
                  }
              });
          };

          self.openBroadcastArmoryOfflineTransactionModal = function(address) {

              self.modalInstance = $modal.open({
                  templateUrl: "/views/modals/broadcast.html",
                  controller: function BroadcastModalController($scope, $modalInstance, address, broadcastTransaction) {
                      $scope.broadcastAddress = address;

                      $scope.ok = function(signedHex) {
                          $scope.clicked = true;
                          $scope.waiting = true;
                          broadcastTransaction(signedHex, address, $scope);
                      };

                      $scope.cancel = function() {
                          $modalInstance.dismiss('cancel');
                          self.modalInstance = null;
                      };

                      $scope.close = function() {
                          $modalInstance.dismiss('close');
                          self.modalInstance = null;
                      };
                  },
                  resolve: {
                      broadcastTransaction: function() {
                          return function(signedHex, from, $modalScope){
                            TransactionGenerator.getArmoryRaw(signedHex).then(function(result){
                              var finalTransaction = result.data.rawTransaction;
                            
                              //Showing the user the transaction hash doesn't work right now
                              //var transactionHash = Bitcoin.Util.bytesToHex(transaction.getHash().reverse());

                              TransactionGenerator.pushSignedTransaction(finalTransaction).then(function(successData) {
                                var successData = successData.data;
                                if (successData.pushed.match(/submitted|success/gi) != null) {
                                  $modalScope.waiting = false;
                                  $modalScope.transactionSuccess = true;
                                  $modalScope.url = 'http://blockchain.info/address/' + from + '?sort=0';
                                } else if (successData.status.match(/NOTOK/gi)) {
                                  $modalScope.waiting = false;
                                  $modalScope.transactionError = true;
                                  $modalScope.error = successData.pushed+" Reason: "+successData.message;<commit_msg>change from blockchain to blocktrail<commit_after>angular.module("omniServices")
    .service("ModalManager", ["$modal", "$rootScope", "$timeout","TransactionGenerator","TransactionManager","Account","Wallet",
        function ModalManagerService($modal, $rootScope, $timeout, TransactionGenerator, TransactionManager, Account, Wallet) {
          var self = this;

          function decodeAddressFromPrivateKey(key) {
  
            //  Return the address decoded from the private key.
            var eckey = new Bitcoin.ECKey(key);
            var addr = eckey.getBitcoinAddress().toString();
        
            return addr;
          };
        
          function encodePrivateKey(key, passphrase) {
        
            //  Return encoded key.  Forget the passphrase forever.
            var eckey = new Bitcoin.ECKey(key);
            var enc = eckey.getEncryptedFormat(passphrase);
        
            return enc;
          };

          self.openCreateModal = function() {
              var modalScope = $rootScope.$new()
              modalScope.title ='Create New Wallet';
              modalScope.button ='Create Wallet';
              modalScope.bodyTemplate = "/views/modals/partials/create.html";
              modalScope.footerTemplate = "/views/modals/partials/create_footer.html";
              
              self.modalInstance = $modal.open({
                templateUrl: '/views/modals/base.html',
                controller: function CreateWalletController($scope, $location, $modalInstance, $idle, Account, AddressManager) {
                  $scope.dismiss = $modalInstance.dismiss;
                  
                  $scope.createWallet = function(create) {
                    $scope.validating=true;
                    $scope.serverError = $scope.invalidCaptcha =false;
                    Account.create(create).then(function(account){
                      var address = AddressManager.createAddress();
                      account.addAddress(address.hash,address.privkey).then(function(result){
                        $modalInstance.close()
                        $location.path('/wallet');
                        $idle.watch();
                      });
                    },function(error){
                      angular.extend($scope,error);
                    })
                  }

                  $scope.setFormScope = function(form){
                    $scope.createForm = form;
                  }

                  $scope.close = function() {
                    $modalInstance.dismiss('close');
                  };
                },
                scope: modalScope,
                backdrop:'static'
              });
          };

          self.openLoginModal = function() {  
              var modalScope = $rootScope.$new()
              modalScope.title = 'Login';
              modalScope.button = 'Open Wallet';
              modalScope.bodyTemplate = "/views/modals/partials/login.html";
              modalScope.footerTemplate = "/views/modals/partials/login_footer.html";
              

              self.modalInstance = $modal.open({
                templateUrl: '/views/modals/base.html',
                controller: LoginController,
                scope: modalScope,
                backdrop:'static'
              });
          };
          
          self.openConfirmationModal = function(modalConfig) {
              self.modalInstance = $modal.open({
                  templateUrl: "/views/modals/base.html",
                  controller: function ConfirmationModalController($scope, $rootScope, $modalInstance, $location, modalConfig, modalManager) {                        
                      angular.extend($scope, modalConfig.scope);

                      $scope.bodyTemplate = modalConfig.bodyTemplate || "/views/modals/confirmation.html";
                      $scope.footerTemplate = modalConfig.footerTemplate || "/views/modals/partials/confirmation_footer.html";
                      $scope.dataTemplate = modalConfig.dataTemplate || "";

                      $scope.signOffline = modalConfig.transaction.offline;

                      $scope.transaction = modalConfig.transaction;
                      
                      $scope.confirm = function() {
                          $scope.clicked = true;
                          $scope.waiting = true;

                          TransactionManager.processTransaction($scope.transaction).then(function(result){
                            if(result.transactionSuccess){
                              $location.path($scope.successRedirect)
                              $rootScope.notify({
                                message: "Operation success",
                                url : result.url
                              })
                              $modalInstance.dismiss('close');
                            } else if(result.readyToSign){
                              $scope.readyToSign = result.readyToSign;
                              $scope.unsignedTransaction = result.unsignedTransaction;
                            } else {
                              $rootScope.notifyError({
                                message: result.error || "Unknown Error"
                              })
                              $modalInstance.dismiss('close');
                            }
                          }, function(errorData){
                          	$modalInstance.dismiss('close');
                                $rootScope.notifyError({
                                message: errorData.errorMessage || "Unknown Error"
                              })
                          });
                      };

                      $scope.fromAddress = modalConfig.transaction.address.hash;

                      $scope.openBroadcastTransactionForm = function(address) {
                          $modalInstance.dismiss('close');
                          modalManager.openBroadcastTransactionModal(address);
                      };

                      $scope.saveUnsigned = function(unsignedHex) {
                          var exportBlob = new Blob([unsignedHex], {
                              type: 'application/json;charset=utf-8'
                          });
                          fileName = "tx" + (new Date).getTime() + ".unsigned.tx";
                          saveAs(exportBlob, fileName);
                          $scope.unsaved = false;
                          $scope.saved = true;
                      };

                      $scope.cancel = function() {
                          $modalInstance.dismiss('cancel');
                      };

                      $scope.close = function() {
                          $modalInstance.dismiss('close');
                      };
                  },
                  resolve: {
                      modalConfig: function() {
                          return modalConfig;
                      },
                      modalManager: function() {
                          return self;
                      },
                  }
              });
          };

          self.openBroadcastArmoryOfflineTransactionModal = function(address) {

              self.modalInstance = $modal.open({
                  templateUrl: "/views/modals/broadcast.html",
                  controller: function BroadcastModalController($scope, $modalInstance, address, broadcastTransaction) {
                      $scope.broadcastAddress = address;

                      $scope.ok = function(signedHex) {
                          $scope.clicked = true;
                          $scope.waiting = true;
                          broadcastTransaction(signedHex, address, $scope);
                      };

                      $scope.cancel = function() {
                          $modalInstance.dismiss('cancel');
                          self.modalInstance = null;
                      };

                      $scope.close = function() {
                          $modalInstance.dismiss('close');
                          self.modalInstance = null;
                      };
                  },
                  resolve: {
                      broadcastTransaction: function() {
                          return function(signedHex, from, $modalScope){
                            TransactionGenerator.getArmoryRaw(signedHex).then(function(result){
                              var finalTransaction = result.data.rawTransaction;
                            
                              //Showing the user the transaction hash doesn't work right now
                              //var transactionHash = Bitcoin.Util.bytesToHex(transaction.getHash().reverse());

                              TransactionGenerator.pushSignedTransaction(finalTransaction).then(function(successData) {
                                var successData = successData.data;
                                if (successData.pushed.match(/submitted|success/gi) != null) {
                                  $modalScope.waiting = false;
                                  $modalScope.transactionSuccess = true;
                                  $modalScope.url = 'https://www.blocktrail.com/BTC/address/' + from + '/transactions';
                                } else if (successData.status.match(/NOTOK/gi)) {
                                  $modalScope.waiting = false;
                                  $modalScope.transactionError = true;
                                  $modalScope.error = successData.pushed+" Reason: "+successData.message;
{'fraction_non_alphanumeric': 0.09377124627373046, 'fraction_numerical': 0.00015689555985565608, 'mean_word_length': 1.1553201082055906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5922450', 'n_tokens_mistral': 4140, 'n_tokens_neox': 3913, 'n_words': 643}