|
| 1 | + "use strict"; |
| 2 | + |
| 3 | + app.controller('WalletCtrl', ['$scope', '$location', 'MongoDB','$modal', '$connection_status','WebSocket', "Wallet", "WalletManager", |
| 4 | + function ($scope, $location, MongoDB, $modal, $connection_status, WebSocket ,Wallet, WalletManager) |
| 5 | + { |
| 6 | + |
| 7 | + $scope.curWallet = WalletManager.getCurrentWallet(); |
| 8 | + $scope.wallets = WalletManager.getWallets(); |
| 9 | + $rootScope.curWallet = $scope.curWallet; |
| 10 | + |
| 11 | + var backupFile; |
| 12 | + $scope.currentAddress = "1Yj564jDqoB6L7hg5ETYKhqRsB65WrWPB"; |
| 13 | + $scope.isActive = true; |
| 14 | + var fileClicked = false; |
| 15 | + setTimeout(function() { |
| 16 | + var el = document.getElementById("first"); |
| 17 | + angular.element(el).triggerHandler("click"); |
| 18 | + },0); |
| 19 | + |
| 20 | + $connection_status.check(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Add a currency |
| 24 | + */ |
| 25 | + $scope.create = function (ROR) |
| 26 | + { |
| 27 | + var lightchain = { |
| 28 | + currency: "Credit", |
| 29 | + incomeRate: ROR |
| 30 | + }; |
| 31 | + // Add an element |
| 32 | + $scope.userBlob.unshift("/lightchain", lightchain); |
| 33 | + |
| 34 | + MongoDB.collection($scope.userBlob.data.account_id); |
| 35 | + |
| 36 | + var wallet = {type: "contract", currency: lightchain.currency, taxRate: lightchain.incomeRate} |
| 37 | + |
| 38 | + new MongoDB(wallet).$save().then(function (data) {console.log(data);}); |
| 39 | + $connection_status.connect(); |
| 40 | + }; |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Remove currency |
| 45 | + * |
| 46 | + * @param index |
| 47 | + */ |
| 48 | + |
| 49 | + |
| 50 | + $scope.remove = function (currency) { |
| 51 | + |
| 52 | + // Update blob |
| 53 | + $scope.userBlob.unset('/lightchain_blob'); |
| 54 | + |
| 55 | + |
| 56 | + // remove from MongoDB |
| 57 | + MongoDB.collection($scope.userBlob.data.account_id); |
| 58 | + |
| 59 | + MongoDB.query({type: "contract"}).then(function(data){ |
| 60 | + |
| 61 | + var temp = data; |
| 62 | + var id; |
| 63 | + id = temp[0]._id; |
| 64 | + |
| 65 | + MongoDB.remove_one(id); |
| 66 | + |
| 67 | + }) |
| 68 | + |
| 69 | + $connection_status.disconnect(); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + MongoDB.collection($scope.userBlob.data.account_id) |
| 74 | + |
| 75 | + MongoDB.query({ type: "safety_net" }).then(function(data){ |
| 76 | + var temp = data; |
| 77 | + $scope.safety_net = data[0].total_pathway; |
| 78 | + |
| 79 | + }); |
| 80 | + |
| 81 | + MongoDB.query({ type: "lightchain_blob" }).then(function(data){ |
| 82 | + |
| 83 | + $scope.total_amount = data[0].total_amount; |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | + /*** |
| 88 | + updateBalance will work properly but only 30 seconds later since it is a callback. |
| 89 | + ***/ |
| 90 | + var updateBalance = function() { |
| 91 | + $scope.curWallet.updateBalance(); |
| 92 | + //setTimeout(function() { |
| 93 | + $scope.Balance = $rootScope.Balance = $scope.curWallet.getBalance(); |
| 94 | + console.log("Balance " + $scope.Balance); |
| 95 | + //}, 1000); |
| 96 | + }; |
| 97 | + updateBalance(); |
| 98 | + setInterval(updateBalance, 30000); |
| 99 | + |
| 100 | + |
| 101 | + /*** |
| 102 | + Helper function for download |
| 103 | + should be created into module |
| 104 | + ***/ |
| 105 | + function download(filename, data) { |
| 106 | + try { |
| 107 | + var a = document.createElement("a"); |
| 108 | + a.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(data)); |
| 109 | + a.setAttribute("download", filename); |
| 110 | + a.click(); |
| 111 | + } catch(e) { |
| 112 | + console.log("download failed " + e); |
| 113 | + } |
| 114 | + |
| 115 | + }; |
| 116 | + |
| 117 | + /*** |
| 118 | + Controller to back up current wallet |
| 119 | + View location is view\partials\main.html |
| 120 | + File download name is the wallet's name |
| 121 | + JSON is used to format wallet data |
| 122 | + ***/ |
| 123 | + $scope.backup = function() { |
| 124 | + var _success = function(password) { |
| 125 | + try { |
| 126 | + var _password = password || Security.get("password"); |
| 127 | + var data = Encryption.encrypt(JSON.stringify($scope.curWallet.getAllAddresses()), _password); |
| 128 | + var fileName = $scope.curWallet.getName(); |
| 129 | + download(fileName+".json", data); |
| 130 | + } catch(e) { |
| 131 | + console.log("backup failed " + e); |
| 132 | + } |
| 133 | + }; |
| 134 | + /* var checkPassword = { |
| 135 | + check : Security.get("password"), |
| 136 | + success : _success, |
| 137 | + fail : function() { |
| 138 | + modals.open("modalpassword", { |
| 139 | + "message":"Please input password", |
| 140 | + "databaseName":"password", |
| 141 | + "objectName":"password" |
| 142 | + }, _success); |
| 143 | + } |
| 144 | + };*/ |
| 145 | + //Security.check(checkPassword,"password","password"); |
| 146 | + }; |
| 147 | + |
| 148 | + /*** |
| 149 | + Controller to import a wallet that was previously backed up |
| 150 | + View location is view\partials\main.html |
| 151 | + Only imports when file is properly opened |
| 152 | + ***/ |
| 153 | + $scope.import = function() { |
| 154 | + var _success = function() { |
| 155 | + var el = document.getElementById("fileUpload"); |
| 156 | + angular.element(el).trigger("click"); |
| 157 | + var f = document.getElementById("file").files[0]; |
| 158 | + if(!f) { |
| 159 | + return; |
| 160 | + } |
| 161 | + var r = new FileReader(); |
| 162 | + r.onload = function(e){ |
| 163 | + backupFile = e.target.result; |
| 164 | + console.log(backupFile) |
| 165 | + $scope.fileLoaded = true; |
| 166 | + } |
| 167 | + r.readAsText(f); |
| 168 | + }; |
| 169 | + var checkPassword = { |
| 170 | + success : _success, |
| 171 | + fail : function() { |
| 172 | + modals.open("modalpassword", { |
| 173 | + "message":"Please input password", |
| 174 | + "databaseName":"password", |
| 175 | + "objectName":"password" |
| 176 | + }, _success); |
| 177 | + } |
| 178 | + }; |
| 179 | + //Security.check(checkPassword,"password","password"); |
| 180 | + }; |
| 181 | + |
| 182 | + /*** |
| 183 | + Controller to generate new address |
| 184 | + View location is view\partials\main.html |
| 185 | + Address generated will show QR code on html |
| 186 | + Address will be stored in current wallet and walletmanager |
| 187 | + will update. |
| 188 | + ***/ |
| 189 | + $scope.generateAddress = function() { |
| 190 | + var _success = function(passphrase) { |
| 191 | + var _passphrase = passphrase ;//|| Security.get("password"); |
| 192 | + $scope.currentAddress = $scope.curWallet.generatePublicAddress(_passphrase); |
| 193 | + WalletManager.updateCurrent(); |
| 194 | + }; |
| 195 | + /* var checkPassword = { |
| 196 | + check : Security.get("password"), |
| 197 | + success : _success, |
| 198 | + fail : function() { |
| 199 | + modals.open("modalpassword", { |
| 200 | + "message":"Please input password", |
| 201 | + "databaseName":"password", |
| 202 | + "objectName":"password" |
| 203 | + }, _success); |
| 204 | + } |
| 205 | + }; |
| 206 | + Security.check(checkPassword,"password","password");*/ |
| 207 | + }; |
| 208 | + |
| 209 | + /*** |
| 210 | + Controller to add a new wallet name after what user typed |
| 211 | + View location is view\partials\sidemenu.html |
| 212 | + WalletManager adds the newly created wallet |
| 213 | + scope.wallets updates list |
| 214 | + WalletManager is called to return current wallet in case user had no wallets to auto-select currently created wallet |
| 215 | + ***/ |
| 216 | + $scope.generateWallet = function(WalletName) { |
| 217 | + if(!WalletName || WalletName.length == 0) { |
| 218 | + throw new Error("Improper Wallet Name"); |
| 219 | + } else { |
| 220 | + WalletManager.addWallet(new Wallet(WalletName)); |
| 221 | + $scope.wallets = WalletManager.getWallets(); |
| 222 | + $rootScope.curWallet = $scope.curWallet = WalletManager.getCurrentWallet(); |
| 223 | + } |
| 224 | + }; |
| 225 | + |
| 226 | + /*** |
| 227 | + Controller to select current wallet in sidemenu |
| 228 | + View location is view\partials\sidemenu.html |
| 229 | + WalletManager,scope.curWallet, and rootScope.curWallet |
| 230 | + are all set to the selected wallet |
| 231 | + ***/ |
| 232 | + $scope.select = function(walletRef) { |
| 233 | + if(!walletRef) { |
| 234 | + throw new Error("Undefined Wallet"); |
| 235 | + } |
| 236 | + WalletManager.setWalletR(walletRef); |
| 237 | + $rootScope.curWallet = $scope.curWallet = walletRef; |
| 238 | + }; |
| 239 | + |
| 240 | + |
| 241 | + }]); |
| 242 | + |
| 243 | + |
0 commit comments