2014-03-14 13:38:27 -07:00
|
|
|
'use strict';
|
2014-04-22 12:07:14 -07:00
|
|
|
|
2014-06-03 12:39:06 -07:00
|
|
|
angular.module('copayApp.directives')
|
2014-08-13 15:07:57 -07:00
|
|
|
.directive('validAddress', function($rootScope, notification) {
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var Address = bitcore.Address;
|
|
|
|
var bignum = bitcore.Bignum;
|
2014-04-22 12:07:14 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
var validator = function(value) {
|
|
|
|
var uri;
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
if (/^https?:\/\//.test(value)) {
|
|
|
|
uri = {
|
|
|
|
merchant: value
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
uri = copay.HDPath.parseBitcoinURI(value);
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
// Regular Address
|
|
|
|
if (!uri || !uri.merchant) {
|
|
|
|
var a = new Address(value);
|
|
|
|
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName);
|
|
|
|
return value;
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
notification.info('Fetching Payment',
|
|
|
|
'Retrieving Payment Request from ' + uri.merchant);
|
2014-08-07 11:29:09 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
// Payment Protocol URI (BIP-72)
|
|
|
|
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
|
|
|
|
var balance = $rootScope.availableBalance;
|
|
|
|
var available = +(balance * config.unitToSatoshi).toFixed(0);
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
if (merchantData && available < +merchantData.total) {
|
|
|
|
err = new Error('No unspent outputs available.');
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
if (err) {
|
|
|
|
scope.sendForm.address.$isValid = false;
|
|
|
|
notification.error('Error', err.message || 'Bad payment server.');
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
merchantData.unitTotal = (+merchantData.total / config.unitToSatoshi) + '';
|
|
|
|
merchantData.expiration = new Date(
|
|
|
|
merchantData.pr.pd.expires * 1000).toISOString();
|
2014-08-08 12:51:50 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
$rootScope.merchant = merchantData;
|
2014-08-08 12:51:50 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
scope.sendForm.address.$isValid = true;
|
2014-08-08 12:51:50 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
scope.sendForm.amount.$setViewValue(merchantData.unitTotal);
|
|
|
|
scope.sendForm.amount.$render();
|
|
|
|
scope.sendForm.amount.$isValid = true;
|
2014-08-08 12:51:50 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
// If the address changes to a non-payment-protocol one,
|
|
|
|
// delete the `merchant` property from the scope.
|
|
|
|
var unregister = scope.$watch('address', function() {
|
|
|
|
var val = scope.sendForm.address.$viewValue || '';
|
|
|
|
var uri = copay.HDPath.parseBitcoinURI(val);
|
|
|
|
if (!uri || !uri.merchant) {
|
|
|
|
delete $rootScope.merchant;
|
|
|
|
scope.sendForm.amount.$setViewValue('');
|
|
|
|
scope.sendForm.amount.$render();
|
|
|
|
unregister();
|
|
|
|
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
|
|
|
|
$rootScope.$apply();
|
2014-08-08 12:51:50 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 09:17:03 -07:00
|
|
|
});
|
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
|
|
|
|
$rootScope.$apply();
|
|
|
|
}
|
2014-07-30 18:15:03 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
notification.info('Payment Request',
|
|
|
|
'Server is requesting ' + merchantData.unitTotal + ' ' + config.unitName + '.' + ' Message: ' + merchantData.pr.pd.memo);
|
|
|
|
});
|
2014-05-07 15:04:36 -07:00
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
ctrl.$setValidity('validAddress', true);
|
|
|
|
|
|
|
|
return 'Merchant: ' + uri.merchant;
|
|
|
|
};
|
|
|
|
|
|
|
|
ctrl.$parsers.unshift(validator);
|
|
|
|
ctrl.$formatters.unshift(validator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
2014-05-07 15:04:36 -07:00
|
|
|
.directive('enoughAmount', ['$rootScope',
|
|
|
|
function($rootScope) {
|
2014-06-12 07:09:18 -07:00
|
|
|
var bitcore = require('bitcore');
|
2014-06-25 11:44:19 -07:00
|
|
|
var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT);
|
2014-05-07 15:04:36 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, element, attrs, ctrl) {
|
|
|
|
var val = function(value) {
|
2014-06-25 11:44:19 -07:00
|
|
|
var availableBalanceNum = Number(($rootScope.availableBalance * config.unitToSatoshi).toFixed(0));
|
2014-06-16 08:44:18 -07:00
|
|
|
var vNum = Number((value * config.unitToSatoshi).toFixed(0)) + feeSat;
|
2014-05-07 15:04:36 -07:00
|
|
|
if (typeof vNum == "number" && vNum > 0) {
|
2014-06-12 07:09:18 -07:00
|
|
|
if (availableBalanceNum < vNum) {
|
2014-05-07 15:04:36 -07:00
|
|
|
ctrl.$setValidity('enoughAmount', false);
|
2014-06-12 07:09:18 -07:00
|
|
|
scope.notEnoughAmount = true;
|
2014-05-07 15:04:36 -07:00
|
|
|
} else {
|
|
|
|
ctrl.$setValidity('enoughAmount', true);
|
|
|
|
scope.notEnoughAmount = null;
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-23 10:41:27 -07:00
|
|
|
scope.notEnoughAmount = null;
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
return value;
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
ctrl.$parsers.unshift(val);
|
|
|
|
ctrl.$formatters.unshift(val);
|
2014-04-23 09:16:20 -07:00
|
|
|
}
|
2014-05-07 15:04:36 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
])
|
2014-05-14 14:24:24 -07:00
|
|
|
.directive('walletSecret', ['walletFactory',
|
|
|
|
function(walletFactory) {
|
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
var validator = function(value) {
|
2014-06-16 08:44:18 -07:00
|
|
|
ctrl.$setValidity('walletSecret', Boolean(walletFactory.decodeSecret(value)));
|
2014-05-14 14:24:24 -07:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
ctrl.$parsers.unshift(validator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
])
|
2014-05-07 15:04:36 -07:00
|
|
|
.directive('loading', function() {
|
2014-04-24 18:43:19 -07:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2014-05-16 14:33:06 -07:00
|
|
|
link: function($scope, element, attr) {
|
2014-04-24 18:43:19 -07:00
|
|
|
var a = element.html();
|
|
|
|
var text = attr.loading;
|
2014-05-16 14:33:06 -07:00
|
|
|
element.on('click', function() {
|
2014-06-16 08:44:18 -07:00
|
|
|
element.html('<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> ' + text + '...');
|
2014-05-16 14:33:06 -07:00
|
|
|
});
|
|
|
|
$scope.$watch('loading', function(val) {
|
|
|
|
if (!val) {
|
2014-04-24 18:43:19 -07:00
|
|
|
element.html(a);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2014-05-07 15:04:36 -07:00
|
|
|
.directive('ngFileSelect', function() {
|
2014-04-25 15:11:56 -07:00
|
|
|
return {
|
|
|
|
link: function($scope, el) {
|
|
|
|
el.bind('change', function(e) {
|
|
|
|
$scope.file = (e.srcElement || e.target).files[0];
|
|
|
|
$scope.getFile();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 11:03:39 -07:00
|
|
|
})
|
|
|
|
.directive('avatar', function($rootScope, controllerUtils) {
|
2014-05-07 15:04:36 -07:00
|
|
|
return {
|
|
|
|
link: function(scope, element, attrs) {
|
2014-05-08 11:48:00 -07:00
|
|
|
var peer = JSON.parse(attrs.peer)
|
|
|
|
var peerId = peer.peerId;
|
|
|
|
var nick = peer.nick;
|
|
|
|
element.addClass('video-small');
|
2014-05-16 14:33:06 -07:00
|
|
|
var muted = controllerUtils.getVideoMutedStatus(peerId);
|
2014-06-06 11:16:11 -07:00
|
|
|
if (true || muted) { // mute everyone for now
|
2014-05-07 15:04:36 -07:00
|
|
|
element.attr("muted", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 11:03:39 -07:00
|
|
|
})
|
2014-06-23 13:34:54 -07:00
|
|
|
.directive('contact', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
2014-06-24 08:36:32 -07:00
|
|
|
link: function(scope, element, attrs) {
|
2014-06-23 13:34:54 -07:00
|
|
|
if (!scope.wallet) return;
|
|
|
|
|
|
|
|
var address = attrs.address;
|
|
|
|
var contact = scope.wallet.addressBook[address];
|
2014-07-06 22:21:29 -07:00
|
|
|
if (contact && !contact.hidden) {
|
2014-06-23 13:34:54 -07:00
|
|
|
element.append(contact.label);
|
|
|
|
attrs['tooltip'] = attrs.address;
|
|
|
|
} else {
|
|
|
|
element.append(address);
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 08:36:32 -07:00
|
|
|
};
|
2014-06-23 13:34:54 -07:00
|
|
|
})
|
2014-06-11 13:49:14 -07:00
|
|
|
.directive('highlightOnChange', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function(scope, element, attrs) {
|
2014-06-16 08:44:18 -07:00
|
|
|
scope.$watch(attrs.highlightOnChange, function(newValue, oldValue) {
|
2014-06-11 13:49:14 -07:00
|
|
|
element.addClass('highlight');
|
2014-06-16 08:44:18 -07:00
|
|
|
setTimeout(function() {
|
|
|
|
element.removeClass('highlight');
|
|
|
|
}, 500);
|
2014-06-11 13:49:14 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2014-05-26 11:03:39 -07:00
|
|
|
.directive('checkStrength', function() {
|
|
|
|
return {
|
|
|
|
replace: false,
|
|
|
|
restrict: 'EACM',
|
2014-06-02 10:39:12 -07:00
|
|
|
require: 'ngModel',
|
2014-05-26 11:03:39 -07:00
|
|
|
link: function(scope, element, attrs) {
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
var MIN_LENGTH = 8;
|
|
|
|
var MESSAGES = ['Very Weak', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'];
|
|
|
|
var COLOR = ['#dd514c', '#dd514c', '#faa732', '#faa732', '#5eb95e', '#5eb95e'];
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
function evaluateMeter(password) {
|
|
|
|
var passwordStrength = 0;
|
|
|
|
var text;
|
|
|
|
if (password.length > 0) passwordStrength = 1;
|
|
|
|
if (password.length >= MIN_LENGTH) {
|
|
|
|
if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) {
|
|
|
|
passwordStrength++;
|
|
|
|
} else {
|
|
|
|
text = ', add mixed case';
|
|
|
|
}
|
|
|
|
if (password.match(/\d+/)) {
|
|
|
|
passwordStrength++;
|
|
|
|
} else {
|
|
|
|
if (!text) text = ', add numerals';
|
|
|
|
}
|
|
|
|
if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) {
|
|
|
|
passwordStrength++;
|
|
|
|
} else {
|
|
|
|
if (!text) text = ', add punctuation';
|
|
|
|
}
|
|
|
|
if (password.length > 12) {
|
|
|
|
passwordStrength++;
|
2014-06-16 08:44:18 -07:00
|
|
|
} else {
|
2014-07-14 07:59:47 -07:00
|
|
|
if (!text) text = ', add characters';
|
2014-06-16 08:44:18 -07:00
|
|
|
}
|
2014-07-14 07:59:47 -07:00
|
|
|
} else {
|
|
|
|
text = ', that\'s short';
|
|
|
|
}
|
|
|
|
if (!text) text = '';
|
2014-06-16 08:44:18 -07:00
|
|
|
|
2014-07-14 07:59:47 -07:00
|
|
|
return {
|
|
|
|
strength: passwordStrength,
|
|
|
|
message: MESSAGES[passwordStrength] + text,
|
|
|
|
color: COLOR[passwordStrength]
|
2014-05-26 11:03:39 -07:00
|
|
|
}
|
2014-07-14 07:59:47 -07:00
|
|
|
}
|
2014-06-02 10:39:12 -07:00
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
|
2014-05-26 11:03:39 -07:00
|
|
|
if (newValue && newValue !== '') {
|
2014-07-14 07:59:47 -07:00
|
|
|
var info = evaluateMeter(newValue);
|
2014-06-16 08:44:18 -07:00
|
|
|
element.css({
|
2014-07-14 07:59:47 -07:00
|
|
|
'border-color': info.color
|
2014-06-16 08:44:18 -07:00
|
|
|
});
|
2014-07-14 07:59:47 -07:00
|
|
|
scope[attrs.checkStrength] = info.message;
|
2014-05-26 11:03:39 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-06-16 11:51:19 -07:00
|
|
|
})
|
2014-07-31 10:06:55 -07:00
|
|
|
.directive('openExternal', function() {
|
2014-06-16 11:51:19 -07:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function(scope, element, attrs) {
|
|
|
|
element.bind('click', function() {
|
2014-08-13 15:07:57 -07:00
|
|
|
window.open('bitcoin:' + attrs.address, '_blank');
|
2014-06-16 11:51:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 10:06:55 -07:00
|
|
|
})
|
2014-08-13 15:07:57 -07:00
|
|
|
// From https://gist.github.com/asafge/7430497
|
|
|
|
.directive('ngReallyClick', [
|
|
|
|
|
|
|
|
function() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function(scope, element, attrs) {
|
|
|
|
element.bind('click', function() {
|
|
|
|
var message = attrs.ngReallyMessage;
|
|
|
|
if (message && confirm(message)) {
|
|
|
|
scope.$apply(attrs.ngReallyClick);
|
|
|
|
}
|
|
|
|
});
|
2014-07-31 10:06:55 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 15:07:57 -07:00
|
|
|
}
|
|
|
|
])
|
|
|
|
.directive('match', function() {
|
2014-07-18 13:56:50 -07:00
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
restrict: 'A',
|
|
|
|
scope: {
|
2014-08-13 15:07:57 -07:00
|
|
|
match: '='
|
2014-07-18 13:56:50 -07:00
|
|
|
},
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
2014-07-18 14:17:45 -07:00
|
|
|
scope.$watch(function() {
|
|
|
|
return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || scope.match === ctrl.$modelValue;
|
|
|
|
}, function(currentValue) {
|
|
|
|
ctrl.$setValidity('match', currentValue);
|
|
|
|
});
|
2014-07-18 13:56:50 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
2014-07-31 12:34:21 -07:00
|
|
|
.directive('clipCopy', function() {
|
|
|
|
ZeroClipboard.config({
|
|
|
|
moviePath: '/lib/zeroclipboard/dist/ZeroClipboard.swf',
|
|
|
|
trustedDomains: ['*'],
|
|
|
|
allowScriptAccess: 'always',
|
|
|
|
forceHandCursor: true
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
restric: 'A',
|
2014-08-13 15:07:57 -07:00
|
|
|
scope: {
|
|
|
|
clipCopy: '=clipCopy'
|
|
|
|
},
|
2014-07-31 12:34:21 -07:00
|
|
|
link: function(scope, elm) {
|
|
|
|
var client = new ZeroClipboard(elm);
|
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
client.on('ready', function(event) {
|
|
|
|
client.on('copy', function(event) {
|
2014-07-31 12:34:21 -07:00
|
|
|
event.clipboardData.setData('text/plain', scope.clipCopy);
|
|
|
|
});
|
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
client.on('aftercopy', function(event) {
|
2014-07-31 12:34:21 -07:00
|
|
|
elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!');
|
|
|
|
setTimeout(function() {
|
|
|
|
elm.addClass('btn-copy').removeClass('btn-copied').html('');
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-13 15:07:57 -07:00
|
|
|
client.on('error', function(event) {
|
|
|
|
console.log('ZeroClipboard error of type "' + event.name + '": ' + event.message);
|
2014-07-31 12:34:21 -07:00
|
|
|
ZeroClipboard.destroy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-07-31 10:06:55 -07:00
|
|
|
});
|