2014-03-26 05:18:42 -07:00
|
|
|
'use strict';
|
2014-06-12 13:42:26 -07:00
|
|
|
var bitcore = require('bitcore');
|
2014-09-05 16:16:06 -07:00
|
|
|
var preconditions = require('preconditions').singleton();
|
2014-03-26 05:18:42 -07:00
|
|
|
|
2014-06-03 13:42:36 -07:00
|
|
|
angular.module('copayApp.controllers').controller('SendController',
|
2014-12-09 10:13:17 -08:00
|
|
|
function($scope, $rootScope, $window, $timeout, $modal, $filter, notification, isMobile, rateService, txStatus) {
|
2014-12-08 13:26:18 -08:00
|
|
|
|
2014-12-09 11:55:32 -08:00
|
|
|
var satToUnit;
|
2014-12-08 13:26:18 -08:00
|
|
|
|
|
|
|
$scope.init = function() {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-08 13:26:18 -08:00
|
|
|
preconditions.checkState(w);
|
|
|
|
preconditions.checkState(w.settings.unitToSatoshi);
|
|
|
|
|
2014-12-09 17:01:52 -08:00
|
|
|
$rootScope.title = w.isShared() ? 'Send Proposal' : 'Send';
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.loading = false;
|
|
|
|
$scope.error = $scope.success = null;
|
|
|
|
|
|
|
|
satToUnit = 1 / w.settings.unitToSatoshi;
|
|
|
|
|
|
|
|
$scope.alternativeName = w.settings.alternativeName;
|
|
|
|
$scope.alternativeIsoCode = w.settings.alternativeIsoCode;
|
|
|
|
|
|
|
|
$scope.isRateAvailable = false;
|
|
|
|
$scope.rateService = rateService;
|
|
|
|
$scope.showScanner = false;
|
|
|
|
$scope.myId = w.getMyCopayerId();
|
|
|
|
$scope.isMobile = isMobile.any();
|
|
|
|
|
|
|
|
if ($rootScope.pendingPayment) {
|
2014-12-09 10:46:03 -08:00
|
|
|
$timeout(function() {
|
|
|
|
$scope.setFromUri($rootScope.pendingPayment)
|
|
|
|
$rootScope.pendingPayment = null;
|
2014-12-09 11:55:32 -08:00
|
|
|
}, 100);
|
2014-12-08 13:26:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
$scope.setInputs();
|
|
|
|
$scope.setScanner();
|
|
|
|
|
2014-11-29 13:35:48 -08:00
|
|
|
rateService.whenAvailable(function() {
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.isRateAvailable = true;
|
|
|
|
$scope.$digest();
|
2014-09-10 11:09:11 -07:00
|
|
|
});
|
2014-12-08 13:26:18 -08:00
|
|
|
}
|
2014-10-29 14:34:59 -07:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.setInputs = function() {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
|
|
|
var unitToSat = w.settings.unitToSatoshi;
|
2014-12-08 13:26:18 -08:00
|
|
|
/**
|
|
|
|
* Setting the two related amounts as properties prevents an infinite
|
|
|
|
* recursion for watches while preserving the original angular updates
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Object.defineProperty($scope,
|
2014-12-08 18:45:34 -08:00
|
|
|
"_alternative", {
|
2014-12-08 13:26:18 -08:00
|
|
|
get: function() {
|
2014-12-08 18:45:34 -08:00
|
|
|
return this.__alternative;
|
2014-12-08 13:26:18 -08:00
|
|
|
},
|
|
|
|
set: function(newValue) {
|
2014-12-08 18:45:34 -08:00
|
|
|
this.__alternative = newValue;
|
2014-12-08 13:26:18 -08:00
|
|
|
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
|
|
|
|
this._amount = parseFloat(
|
|
|
|
(rateService.fromFiat(newValue, $scope.alternativeIsoCode) * satToUnit).toFixed(w.settings.unitDecimals), 10);
|
|
|
|
} else {
|
|
|
|
this._amount = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty($scope,
|
2014-12-08 18:45:34 -08:00
|
|
|
"_amount", {
|
2014-12-08 13:26:18 -08:00
|
|
|
get: function() {
|
2014-12-08 18:45:34 -08:00
|
|
|
return this.__amount;
|
2014-12-08 13:26:18 -08:00
|
|
|
},
|
|
|
|
set: function(newValue) {
|
2014-12-08 18:45:34 -08:00
|
|
|
this.__amount = newValue;
|
2014-12-08 13:26:18 -08:00
|
|
|
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
|
2014-12-08 18:45:34 -08:00
|
|
|
this.__alternative = parseFloat(
|
2014-12-08 13:26:18 -08:00
|
|
|
(rateService.toFiat(newValue * unitToSat, $scope.alternativeIsoCode)).toFixed(2), 10);
|
|
|
|
} else {
|
2014-12-08 18:45:34 -08:00
|
|
|
this.__alternative = 0;
|
2014-12-08 13:26:18 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
2014-03-31 08:48:35 -07:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
Object.defineProperty($scope,
|
2014-12-08 18:45:34 -08:00
|
|
|
"_address", {
|
2014-12-08 13:26:18 -08:00
|
|
|
get: function() {
|
2014-12-08 18:45:34 -08:00
|
|
|
return this.__address;
|
2014-12-08 13:26:18 -08:00
|
|
|
},
|
|
|
|
set: function(newValue) {
|
2014-12-08 18:45:34 -08:00
|
|
|
this.__address = $scope.onAddressChange(newValue);
|
2014-12-08 13:26:18 -08:00
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
};
|
2014-11-12 13:32:18 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.setScanner = function() {
|
2014-12-08 14:31:15 -08:00
|
|
|
navigator.getUserMedia = navigator.getUserMedia ||
|
|
|
|
navigator.webkitGetUserMedia || navigator.mozGetUserMedia ||
|
2014-12-08 13:26:18 -08:00
|
|
|
navigator.msGetUserMedia;
|
2014-12-08 14:31:15 -08:00
|
|
|
window.URL = window.URL || window.webkitURL ||
|
2014-12-08 13:26:18 -08:00
|
|
|
window.mozURL || window.msURL;
|
2014-08-06 09:59:33 -07:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
if (!window.cordova && !navigator.getUserMedia)
|
|
|
|
$scope.disableScanner = 1;
|
|
|
|
};
|
2014-04-19 00:55:32 -07:00
|
|
|
|
2014-09-08 13:07:43 -07:00
|
|
|
|
2014-12-08 18:45:34 -08:00
|
|
|
$scope.setError = function(err) {
|
2014-12-09 12:22:58 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-08 18:45:34 -08:00
|
|
|
copay.logger.warn(err);
|
2014-11-22 14:17:35 -08:00
|
|
|
|
|
|
|
var msg = err.toString();
|
|
|
|
if (msg.match('BIG'))
|
2014-11-23 10:52:39 -08:00
|
|
|
msg = 'The transaction have too many inputs. Try creating many transactions for smaller amounts'
|
2014-11-22 14:17:35 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
if (msg.match('totalNeededAmount') || msg.match('unspent not set'))
|
|
|
|
msg = 'Insufficient funds'
|
2014-12-05 12:59:15 -08:00
|
|
|
|
2014-12-08 20:29:06 -08:00
|
|
|
if (msg.match('expired'))
|
|
|
|
msg = 'The payment request has expired';
|
|
|
|
|
2014-11-23 10:52:39 -08:00
|
|
|
var message = 'The transaction' + (w.isShared() ? ' proposal' : '') +
|
|
|
|
' could not be created: ' + msg;
|
2014-11-22 14:17:35 -08:00
|
|
|
|
|
|
|
$scope.error = message;
|
2014-12-08 18:45:34 -08:00
|
|
|
|
2014-12-08 20:29:06 -08:00
|
|
|
$timeout(function() {
|
2014-12-08 18:45:34 -08:00
|
|
|
$scope.$digest();
|
2014-12-08 20:29:06 -08:00
|
|
|
}, 1);
|
2014-11-22 14:17:35 -08:00
|
|
|
};
|
|
|
|
|
2014-04-19 00:55:32 -07:00
|
|
|
$scope.submitForm = function(form) {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
|
|
|
var unitToSat = w.settings.unitToSatoshi;
|
2014-12-08 20:29:06 -08:00
|
|
|
|
2014-04-19 00:55:32 -07:00
|
|
|
if (form.$invalid) {
|
2014-11-03 11:06:17 -08:00
|
|
|
$scope.error = 'Unable to send transaction proposal';
|
2014-04-19 00:55:32 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:43:19 -07:00
|
|
|
$scope.loading = true;
|
2014-12-08 18:45:34 -08:00
|
|
|
var comment = form.comment.$modelValue;
|
2014-12-08 20:29:06 -08:00
|
|
|
var merchantData = $scope._merchantData;
|
|
|
|
var address, amount;
|
|
|
|
if (!merchantData) {
|
|
|
|
address = form.address.$modelValue;
|
|
|
|
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
|
|
|
|
}
|
2014-08-28 19:42:11 -07:00
|
|
|
|
2014-11-23 10:52:39 -08:00
|
|
|
w.spend({
|
2014-12-08 20:29:06 -08:00
|
|
|
merchantData: merchantData,
|
2014-11-23 10:52:39 -08:00
|
|
|
toAddress: address,
|
|
|
|
amountSat: amount,
|
2014-12-08 18:45:34 -08:00
|
|
|
comment: comment,
|
2014-11-23 10:52:39 -08:00
|
|
|
}, function(err, txid, status) {
|
2014-11-30 13:40:03 -08:00
|
|
|
$scope.loading = false;
|
2014-12-08 20:29:06 -08:00
|
|
|
if (err)
|
2014-12-08 18:45:34 -08:00
|
|
|
return $scope.setError(err);
|
2014-12-10 13:18:28 -08:00
|
|
|
txStatus.notify(status);
|
|
|
|
$scope.resetForm();
|
2014-11-23 10:52:39 -08:00
|
|
|
});
|
2014-05-06 13:02:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// QR code Scanner
|
|
|
|
var cameraInput;
|
|
|
|
var video;
|
|
|
|
var canvas;
|
|
|
|
var $video;
|
|
|
|
var context;
|
|
|
|
var localMediaStream;
|
|
|
|
|
|
|
|
var _scan = function(evt) {
|
|
|
|
if ($scope.isMobile) {
|
|
|
|
$scope.scannerLoading = true;
|
|
|
|
var files = evt.target.files;
|
|
|
|
|
|
|
|
if (files.length === 1 && files[0].type.indexOf('image/') === 0) {
|
|
|
|
var file = files[0];
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = (function(theFile) {
|
|
|
|
return function(e) {
|
|
|
|
var mpImg = new MegaPixImage(file);
|
2014-06-12 13:42:26 -07:00
|
|
|
mpImg.render(canvas, {
|
|
|
|
maxWidth: 200,
|
|
|
|
maxHeight: 200,
|
|
|
|
orientation: 6
|
|
|
|
});
|
2014-05-06 13:02:49 -07:00
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
qrcode.width = canvas.width;
|
|
|
|
qrcode.height = canvas.height;
|
|
|
|
qrcode.imagedata = context.getImageData(0, 0, qrcode.width, qrcode.height);
|
|
|
|
|
|
|
|
try {
|
|
|
|
qrcode.decode();
|
|
|
|
} catch (e) {
|
2014-06-03 14:38:56 -07:00
|
|
|
// error decoding QR
|
2014-05-06 13:02:49 -07:00
|
|
|
}
|
|
|
|
}, 1500);
|
|
|
|
};
|
|
|
|
})(file);
|
|
|
|
|
|
|
|
// Read in the file as a data URL
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (localMediaStream) {
|
|
|
|
context.drawImage(video, 0, 0, 300, 225);
|
|
|
|
|
|
|
|
try {
|
|
|
|
qrcode.decode();
|
2014-06-12 13:42:26 -07:00
|
|
|
} catch (e) {
|
2014-05-06 13:02:49 -07:00
|
|
|
//qrcodeError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$timeout(_scan, 500);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var _successCallback = function(stream) {
|
|
|
|
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
|
|
|
localMediaStream = stream;
|
|
|
|
video.play();
|
|
|
|
$timeout(_scan, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
var _scanStop = function() {
|
|
|
|
$scope.scannerLoading = false;
|
|
|
|
$scope.showScanner = false;
|
|
|
|
if (!$scope.isMobile) {
|
|
|
|
if (localMediaStream && localMediaStream.stop) localMediaStream.stop();
|
|
|
|
localMediaStream = null;
|
|
|
|
video.src = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var _videoError = function(err) {
|
|
|
|
_scanStop();
|
|
|
|
};
|
|
|
|
|
|
|
|
qrcode.callback = function(data) {
|
|
|
|
_scanStop();
|
2014-05-07 07:21:30 -07:00
|
|
|
$scope.$apply(function() {
|
2014-10-17 14:36:12 -07:00
|
|
|
$scope.sendForm.address.$setViewValue(data);
|
2014-11-03 10:13:53 -08:00
|
|
|
$scope.sendForm.address.$render();
|
2014-05-07 07:21:30 -07:00
|
|
|
});
|
2014-05-06 13:02:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.cancelScanner = function() {
|
|
|
|
_scanStop();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.openScanner = function() {
|
2014-07-29 22:55:59 -07:00
|
|
|
if (window.cordova) return $scope.scannerIntent();
|
2014-05-06 13:02:49 -07:00
|
|
|
$scope.showScanner = true;
|
|
|
|
|
|
|
|
// Wait a moment until the canvas shows
|
|
|
|
$timeout(function() {
|
|
|
|
canvas = document.getElementById('qr-canvas');
|
|
|
|
context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
if ($scope.isMobile) {
|
|
|
|
cameraInput = document.getElementById('qrcode-camera');
|
|
|
|
cameraInput.addEventListener('change', _scan, false);
|
|
|
|
} else {
|
|
|
|
video = document.getElementById('qrcode-scanner-video');
|
|
|
|
$video = angular.element(video);
|
|
|
|
canvas.width = 300;
|
|
|
|
canvas.height = 225;
|
|
|
|
context.clearRect(0, 0, 300, 225);
|
2014-04-24 18:43:19 -07:00
|
|
|
|
2014-06-12 13:42:26 -07:00
|
|
|
navigator.getUserMedia({
|
|
|
|
video: true
|
|
|
|
}, _successCallback, _videoError);
|
2014-05-06 13:02:49 -07:00
|
|
|
}
|
|
|
|
}, 500);
|
2014-04-24 18:43:19 -07:00
|
|
|
};
|
2014-06-17 21:00:32 -07:00
|
|
|
|
2014-07-25 07:08:29 -07:00
|
|
|
$scope.scannerIntent = function() {
|
|
|
|
cordova.plugins.barcodeScanner.scan(
|
|
|
|
function onSuccess(result) {
|
|
|
|
if (result.cancelled) return;
|
|
|
|
|
2014-11-27 06:43:44 -08:00
|
|
|
$timeout(function() {
|
|
|
|
var data = result.text;
|
|
|
|
$scope.$apply(function() {
|
|
|
|
$scope.sendForm.address.$setViewValue(result.text);
|
|
|
|
$scope.sendForm.address.$render();
|
|
|
|
});
|
|
|
|
}, 1000);
|
2014-07-25 07:08:29 -07:00
|
|
|
},
|
|
|
|
function onError(error) {
|
|
|
|
alert('Scanning error');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-12 13:32:18 -08:00
|
|
|
$scope.setTopAmount = function() {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-08 18:45:34 -08:00
|
|
|
var form = $scope.sendForm;
|
2014-12-09 11:55:32 -08:00
|
|
|
if (form) {
|
|
|
|
form.amount.$setViewValue(w.balanceInfo.topAmount);
|
|
|
|
form.amount.$render();
|
|
|
|
form.amount.$isValid = true;
|
|
|
|
}
|
2014-06-19 11:55:04 -07:00
|
|
|
};
|
2014-08-06 09:59:33 -07:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.setForm = function(to, amount, comment) {
|
|
|
|
var form = $scope.sendForm;
|
2014-12-08 20:29:06 -08:00
|
|
|
if (to) {
|
|
|
|
form.address.$setViewValue(to);
|
|
|
|
form.address.$isValid = true;
|
|
|
|
form.address.$render();
|
|
|
|
$scope.lockAddress = true;
|
|
|
|
}
|
2014-12-08 13:26:18 -08:00
|
|
|
|
2014-12-08 14:31:15 -08:00
|
|
|
if (amount) {
|
2014-12-08 20:29:06 -08:00
|
|
|
form.amount.$setViewValue("" + amount);
|
2014-12-08 14:31:15 -08:00
|
|
|
form.amount.$isValid = true;
|
2014-12-08 18:45:34 -08:00
|
|
|
form.amount.$render();
|
2014-12-08 14:31:15 -08:00
|
|
|
$scope.lockAmount = true;
|
|
|
|
}
|
2014-12-08 13:26:18 -08:00
|
|
|
|
2014-12-08 14:31:15 -08:00
|
|
|
if (comment) {
|
2014-12-08 18:45:34 -08:00
|
|
|
form.comment.$setViewValue(comment);
|
|
|
|
form.comment.$isValid = true;
|
|
|
|
form.comment.$render();
|
2014-12-08 14:31:15 -08:00
|
|
|
}
|
2014-12-08 13:26:18 -08:00
|
|
|
};
|
|
|
|
|
2014-12-10 13:18:28 -08:00
|
|
|
$scope.resetForm = function() {
|
2014-12-08 13:26:18 -08:00
|
|
|
var form = $scope.sendForm;
|
|
|
|
|
|
|
|
$scope.fetchingURL = null;
|
2014-12-08 20:29:06 -08:00
|
|
|
$scope._merchantData = $scope._domain = null;
|
|
|
|
|
2014-12-08 18:45:34 -08:00
|
|
|
$scope.lockAddress = false;
|
|
|
|
$scope.lockAmount = false;
|
2014-12-08 20:29:06 -08:00
|
|
|
|
|
|
|
$scope._amount = $scope._address = null;
|
|
|
|
|
|
|
|
form.amount.$pristine = true;
|
2014-11-20 06:42:56 -08:00
|
|
|
form.amount.$setViewValue('');
|
2014-12-01 14:01:10 -08:00
|
|
|
form.amount.$render();
|
2014-12-08 20:29:06 -08:00
|
|
|
|
2014-11-20 06:42:56 -08:00
|
|
|
form.comment.$setViewValue('');
|
2014-12-01 14:01:10 -08:00
|
|
|
form.comment.$render();
|
2014-11-20 06:42:56 -08:00
|
|
|
form.$setPristine();
|
2014-12-08 18:45:34 -08:00
|
|
|
|
2014-12-09 07:47:19 -08:00
|
|
|
if (form.address) {
|
|
|
|
form.address.$pristine = true;
|
|
|
|
form.address.$setViewValue('');
|
|
|
|
form.address.$render();
|
|
|
|
}
|
2014-12-08 20:29:06 -08:00
|
|
|
$timeout(function() {
|
2014-12-08 18:45:34 -08:00
|
|
|
$rootScope.$digest();
|
2014-12-08 20:29:06 -08:00
|
|
|
}, 1);
|
2014-12-08 13:26:18 -08:00
|
|
|
};
|
2014-11-20 06:42:56 -08:00
|
|
|
|
2014-12-08 20:29:06 -08:00
|
|
|
var $oscope = $scope;
|
|
|
|
$scope.openPPModal = function(merchantData) {
|
2014-12-08 13:26:18 -08:00
|
|
|
var ModalInstanceCtrl = function($scope, $modalInstance) {
|
2014-12-08 20:29:06 -08:00
|
|
|
$scope.md = merchantData;
|
|
|
|
$scope.alternative = $oscope._alternative;
|
|
|
|
$scope.alternativeIsoCode = $oscope.alternativeIsoCode;
|
|
|
|
$scope.isRateAvailable = $oscope.isRateAvailable;
|
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.cancel = function() {
|
|
|
|
$modalInstance.dismiss('cancel');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
$modal.open({
|
|
|
|
templateUrl: 'views/modals/paypro.html',
|
|
|
|
windowClass: 'tiny',
|
|
|
|
controller: ModalInstanceCtrl,
|
|
|
|
});
|
|
|
|
};
|
2014-12-01 14:01:10 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
|
2014-12-08 14:31:15 -08:00
|
|
|
$scope.setFromPayPro = function(uri) {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-08 14:31:15 -08:00
|
|
|
$scope.fetchingURL = uri;
|
2014-11-19 22:10:43 -08:00
|
|
|
$scope.loading = true;
|
2014-09-30 11:36:24 -07:00
|
|
|
|
2014-08-13 15:42:04 -07:00
|
|
|
// Payment Protocol URI (BIP-72)
|
2014-12-08 14:31:15 -08:00
|
|
|
w.fetchPaymentRequest({
|
|
|
|
url: uri
|
2014-11-23 10:52:39 -08:00
|
|
|
}, function(err, merchantData) {
|
2014-11-19 22:10:43 -08:00
|
|
|
$scope.loading = false;
|
|
|
|
$scope.fetchingURL = null;
|
2014-08-13 15:42:04 -07:00
|
|
|
|
|
|
|
if (err) {
|
2014-12-08 20:29:06 -08:00
|
|
|
copay.logger.warn(err);
|
2014-12-10 13:18:28 -08:00
|
|
|
$scope.resetForm();
|
|
|
|
$scope.error = err.toString();
|
2014-12-08 13:26:18 -08:00
|
|
|
} else {
|
2014-12-08 20:29:06 -08:00
|
|
|
$scope._merchantData = merchantData;
|
|
|
|
$scope._domain = merchantData.domain;
|
|
|
|
$scope.setForm(null, merchantData.unitTotal);
|
2014-08-13 15:42:04 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-12-01 14:01:10 -08:00
|
|
|
|
2014-12-08 14:31:15 -08:00
|
|
|
$scope.setFromUri = function(uri) {
|
|
|
|
var form = $scope.sendForm;
|
|
|
|
|
|
|
|
var parsed = new bitcore.BIP21(uri);
|
|
|
|
if (!parsed.isValid() || !parsed.address.isValid()) {
|
|
|
|
$scope.error = 'Invalid bitcoin URL';
|
|
|
|
form.address.$isValid = false;
|
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
|
|
|
var addr = parsed.address.toString();
|
|
|
|
if (parsed.data.merchant)
|
|
|
|
return $scope.setFromPayPro(parsed.data.merchant);
|
|
|
|
|
|
|
|
var amount = (parsed.data && parsed.data.amount) ?
|
2014-12-08 18:45:34 -08:00
|
|
|
(parsed.data.amount * 100000000).toFixed(0) * satToUnit : 0;
|
2014-12-08 14:31:15 -08:00
|
|
|
|
|
|
|
$scope.setForm(addr, amount, parsed.data.message, true);
|
|
|
|
return addr;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.onAddressChange = function(value) {
|
|
|
|
$scope.error = $scope.success = null;
|
|
|
|
if (!value) return '';
|
|
|
|
|
|
|
|
if (value.indexOf('bitcoin:') === 0) {
|
|
|
|
return $scope.setFromUri(value);
|
|
|
|
} else if (/^https?:\/\//.test(value)) {
|
|
|
|
return $scope.setFromPayPro(value);
|
|
|
|
}
|
2014-12-09 10:13:17 -08:00
|
|
|
|
2014-12-08 14:31:15 -08:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2014-12-05 00:41:44 -08:00
|
|
|
$scope.openAddressBook = function() {
|
2014-12-09 11:55:32 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-05 00:41:44 -08:00
|
|
|
var modalInstance = $modal.open({
|
|
|
|
templateUrl: 'views/modals/address-book.html',
|
2014-12-08 13:26:18 -08:00
|
|
|
windowClass: 'large',
|
|
|
|
controller: function($scope, $modalInstance) {
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.showForm = null;
|
|
|
|
$scope.addressBook = w.addressBook;
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.hasEntry = function() {
|
|
|
|
return _.keys($scope.addressBook).length > 0 ? true : false;
|
|
|
|
};
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.toggleAddressBookEntry = function(key) {
|
|
|
|
w.toggleAddressBookEntry(key);
|
|
|
|
};
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.copyToSend = function(addr) {
|
|
|
|
$modalInstance.close(addr);
|
|
|
|
};
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.cancel = function() {
|
|
|
|
$scope.error = $scope.success = null;
|
|
|
|
$scope.toggleForm();
|
|
|
|
};
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.toggleForm = function() {
|
|
|
|
$scope.showForm = !$scope.showForm;
|
|
|
|
};
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-09 07:47:19 -08:00
|
|
|
// TODO change to modal
|
2014-12-08 13:26:18 -08:00
|
|
|
$scope.submitAddressBook = function(form) {
|
|
|
|
if (form.$invalid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$timeout(function() {
|
|
|
|
var errorMsg;
|
|
|
|
var entry = {
|
|
|
|
"address": form.newaddress.$modelValue,
|
2014-12-05 00:41:44 -08:00
|
|
|
"label": form.newlabel.$modelValue
|
2014-12-08 13:26:18 -08:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
w.setAddressBook(entry.address, entry.label);
|
|
|
|
} catch (e) {
|
2014-12-09 07:47:19 -08:00
|
|
|
copay.logger.warn(e);
|
2014-12-08 13:26:18 -08:00
|
|
|
errorMsg = e.message;
|
|
|
|
}
|
2014-12-05 00:41:44 -08:00
|
|
|
|
2014-12-08 13:26:18 -08:00
|
|
|
if (errorMsg) {
|
|
|
|
$scope.error = errorMsg;
|
|
|
|
} else {
|
|
|
|
$scope.toggleForm();
|
2014-12-09 07:47:19 -08:00
|
|
|
notification.success('Entry created', 'New addressbook entry created')
|
2014-12-08 13:26:18 -08:00
|
|
|
}
|
|
|
|
$rootScope.$digest();
|
2014-12-09 07:47:19 -08:00
|
|
|
}, 1);
|
2014-12-08 13:26:18 -08:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.close = function() {
|
|
|
|
$modalInstance.dismiss('cancel');
|
|
|
|
};
|
|
|
|
},
|
2014-12-05 00:41:44 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
modalInstance.result.then(function(addr) {
|
2014-12-09 10:13:17 -08:00
|
|
|
$scope.setForm(addr);
|
2014-12-05 00:41:44 -08:00
|
|
|
});
|
|
|
|
};
|
2014-09-03 12:24:25 -07:00
|
|
|
});
|