From 6e28268c2455d297d451e415a612d2353001acef Mon Sep 17 00:00:00 2001 From: Kadir Sekha Date: Wed, 14 Mar 2018 11:58:11 +0900 Subject: [PATCH] payment received socket now works on custom amount screen --- src/js/controllers/customAmount.js | 45 +++++++++++++++++++++++++++++- www/views/customAmount.html | 12 +++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/js/controllers/customAmount.js b/src/js/controllers/customAmount.js index af0a8525b..f6b96c22c 100644 --- a/src/js/controllers/customAmount.js +++ b/src/js/controllers/customAmount.js @@ -1,6 +1,10 @@ 'use strict'; -angular.module('copayApp.controllers').controller('customAmountController', function($scope, $ionicHistory, txFormatService, platformInfo, configService, profileService, walletService, popupService, bitcoinCashJsService) { +angular.module('copayApp.controllers').controller('customAmountController', function($scope, $ionicHistory, txFormatService, platformInfo, configService, profileService, walletService, popupService, bitcoinCashJsService, $timeout) { + + var currentAddressSocket = {}; + var paymentSubscriptionObj = { op:"addr_sub" } + $scope.showingPaymentReceived = false; var showErrorAndBack = function(title, msg) { popupService.showAlert(title, msg, function() { @@ -38,8 +42,10 @@ angular.module('copayApp.controllers').controller('customAmountController', func if ($scope.wallet.coin == 'bch') { bchAddresses = bitcoinCashJsService.translateAddresses(addr); $scope.address = bchAddresses[$scope.bchAddressType]; + prepareWebSocket(bchAddresses['legacy']); } else { $scope.address = addr; + prepareWebSocket(addr); } $scope.displayAddress = function(type) { @@ -70,6 +76,8 @@ angular.module('copayApp.controllers').controller('customAmountController', func $scope.amountBtc = amount; // BTC or BCH $scope.altAmountStr = txFormatService.formatAlternativeStr($scope.wallet.coin, parsedAmount.amountSat); } + + }); }); @@ -96,4 +104,39 @@ angular.module('copayApp.controllers').controller('customAmountController', func return protocol + $scope.address + '?amount=' + $scope.amountBtc; }; + var prepareWebSocket = function(address) { + if (currentAddressSocket.close === 'function') { + currentAddressSocket.close(); + } + + if ($scope.wallet.coin == 'bch') { + currentAddressSocket = new WebSocket('wss://ws.blockchain.info/bch/inv'); + } else { + currentAddressSocket = new WebSocket('wss://ws.blockchain.info/inv/'); + } + + paymentSubscriptionObj.addr = address; + var msg = JSON.stringify(paymentSubscriptionObj); + currentAddressSocket.onopen = function(event) { + currentAddressSocket.send(msg); + } + + currentAddressSocket.onmessage = function(event) { + receivedPayment(event.data); + } + + $timeout(function() { + $scope.$apply(); + }, 10); + }; + + var receivedPayment = function(data) { + data = JSON.parse(data); + + if (data.op == 'utx') { + $scope.showingPaymentReceived = true; + $scope.$apply(); + } + } + }); diff --git a/www/views/customAmount.html b/www/views/customAmount.html index 01956c287..26b84171e 100644 --- a/www/views/customAmount.html +++ b/www/views/customAmount.html @@ -19,7 +19,7 @@ -
+
@@ -62,5 +62,15 @@
+
+ + + + +

+
Payment Received!
+
Return To Address
+

+