Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2354 from darkdarkdragon/develop-RT-2957-fix-3
Browse files Browse the repository at this point in the history
[FIX] Trust: fix not to trust unfunded (RT-2957)
  • Loading branch information
mrajvanshy committed May 12, 2015
2 parents 32223b4 + 880b2b8 commit 83f76cb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 106 deletions.
134 changes: 64 additions & 70 deletions src/js/directives/accountExists.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,90 @@
*/
angular.module('validators').directive('rpAccountExists', rpAccountExists);

rpAccountExists.$inject = ['$timeout', '$parse', 'rpNetwork'];
rpAccountExists.$inject = ['$timeout', '$parse', 'rpNetwork', '$q'];

function rpAccountExists($timeout, $parse, $network) {
function rpAccountExists($timeout, $parse, $network, $q) {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, elm, attr, ctrl) {
if (!ctrl) return;

var rpAccountExistsModelGetter;
var currentCheckTimeout = null,
currentDefer = null;

function showLoading(doShow) {
if (attr.rpAccountExistsLoading) {
var getterL = $parse(attr.rpAccountExistsLoading);
getterL.assign(scope, doShow);
}
}
ctrl.$asyncValidators.rpAccountExists = function(value) {

var validator = function(value) {
var valToCheck = rpAccountExistsModelGetter ? rpAccountExistsModelGetter(scope) : value;
var strippedValue = webutil.stripRippleAddress(value),
address = ripple.UInt160.from_json(strippedValue);

var strippedValue = webutil.stripRippleAddress(valToCheck);
var address = ripple.UInt160.from_json(strippedValue);
if (currentDefer) {
// another check is pending, but it is not needed
currentDefer.resolve(true);
currentDefer = null;
$timeout.cancel(currentCheckTimeout);
currentCheckTimeout = null;
}

function checkCurrentValue() {
var currentValue = (rpAccountExistsModelGetter) ? rpAccountExistsModelGetter(scope) : ctrl.$viewValue;
var currentAddress = ripple.UInt160.from_json(webutil.stripRippleAddress(currentValue));
if (strippedValue != currentValue && !currentAddress.is_valid()) {
scope.$apply(function() {
ctrl.$setValidity('rpAccountExists', true);
showLoading(false);
});
}
return strippedValue != currentValue;
function checkFunded(addressToCheckPromise) {
return addressToCheckPromise.then(function(addressToCheck) {
if (addressToCheck === false) {
// skip check
return $q.when(true);
}
var address2 = ripple.UInt160.from_json(addressToCheck);
if (address2.is_valid()) {
var defer = $q.defer();

$network.remote.requestAccountInfo({account: addressToCheck})
.on('success', function(m) {
defer.resolve(true);
})
.on('error', function(m) {
if (m && m.remote && m.remote.error === 'actNotFound') {
defer.reject(false);
} else {
console.log('There was an error', m);
defer.resolve(true);
}
})
.request();
return defer.promise;
} else {
return $q.when(true);
}
});
}

if (address.is_valid()) {
ctrl.$setValidity('rpAccountExists', false);
showLoading(true);

$network.remote.requestAccountInfo({account: strippedValue})
.on('success', function(m) {
if (checkCurrentValue()) {
return;
}
scope.$apply(function() {
ctrl.$setValidity('rpAccountExists', true);
showLoading(false);
});
})
.on('error', function(m) {
if (checkCurrentValue()) {
return;
return checkFunded($q.when(value));
} else if (scope.userBlob && webutil.getContact(scope.userBlob.data.contacts, strippedValue)) {
return checkFunded($q.when(webutil.getContact(scope.userBlob.data.contacts, strippedValue).address));
} else if (webutil.isRippleName(value)) {
var defer = $q.defer();
currentDefer = defer;

currentCheckTimeout = $timeout(function() {
currentCheckTimeout = null;
currentDefer = null;

rippleVaultClient.AuthInfo.get(Options.domain, value, function(err, info) {
if (info && info.exists && info.address) {
defer.resolve(info.address);
} else {
defer.resolve(false);
}
scope.$apply(function() {
showLoading(false);
if (m && m.remote && m.remote.error_code === 15) {
ctrl.$setValidity('rpAccountExists', false);
} else {
console.log('There was an error', m);
ctrl.$setValidity('rpAccountExists', true);
}
});
})
.request();
return value;
});
}, 500);

return checkFunded(defer.promise);
}

ctrl.$setValidity('rpAccountExists', true);
return value;
return $q.when(true);
};

if (attr.rpAccountExistsModel) {
rpAccountExistsModelGetter = $parse(attr.rpAccountExistsModel);
var watcher = scope.$watch(attr.rpAccountExistsModel, function() {
var address = rpAccountExistsModelGetter(scope);
validator(address);
});

scope.$on('$destroy', function() {
// Remove watcher
watcher();
});
}

ctrl.$formatters.push(validator);
ctrl.$parsers.unshift(validator);

attr.$observe('rpAccountExists', function() {
validator(ctrl.$viewValue);
ctrl.$validate();
});
}
};
Expand Down
69 changes: 37 additions & 32 deletions src/js/directives/validators.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,48 +110,57 @@ module.directive('rpDest', ['$q', '$timeout', '$parse', 'rpFederation', function
link: function (scope, elm, attr, ctrl) {
if (!ctrl) return;

var getter;
var currentCheckTimeout = null,
currentDefer = null,
getter;

function setDestModelValue(value) {
if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope, value);
}
}

ctrl.$asyncValidators.rpDest = function(value) {
var strippedValue = webutil.stripRippleAddress(value),
address = ripple.UInt160.from_json(strippedValue);

if (currentDefer) {
// another check is pending, but it is not needed
currentDefer.resolve(true);
currentDefer = null;
$timeout.cancel(currentCheckTimeout);
currentCheckTimeout = null;
}

ctrl.rpDestType = null;
if (attr.rpDestFederationModel) {
getter = $parse(attr.rpDestFederationModel);
getter.assign(scope,null);
getter.assign(scope, null);
}

// client-side validation
if (attr.rpDestAddress && address.is_valid()) {
ctrl.rpDestType = "address";

if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,value);
}
setDestModelValue(value);

return $q.when(true);
}

if (attr.rpDestContact && scope.userBlob &&
webutil.getContact(scope.userBlob.data.contacts,strippedValue)) {
webutil.getContact(scope.userBlob.data.contacts, strippedValue)) {
ctrl.rpDestType = "contact";

if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,webutil.getContact(scope.userBlob.data.contacts,strippedValue).address);
}
setDestModelValue(scope, webutil.getContact(scope.userBlob.data.contacts, strippedValue).address);

return $q.when(true);
}

if (attr.rpDestBitcoin && !isNaN(Base.decode_check([0, 5], strippedValue, 'bitcoin'))) {
ctrl.rpDestType = "bitcoin";

if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,value);
}
setDestModelValue(value);

return $q.when(true);
}
Expand All @@ -165,42 +174,38 @@ module.directive('rpDest', ['$q', '$timeout', '$parse', 'rpFederation', function
// Check if this request is still current, exit if not
if (value != ctrl.$viewValue) return;

if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,value);
}
setDestModelValue(value);

if (attr.rpDestFederationModel) {
getter = $parse(attr.rpDestFederationModel);
getter.assign(scope,result);
getter.assign(scope, result);
}
return true;
}, function() {
return false;
});
} else {
if (attr.rpDestModel) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,value);
}
setDestModelValue(value);

return $q.when(true);
}
}

if (((attr.rpDestRippleName && webutil.isRippleName(value)) ||
(attr.rpDestRippleNameNoTilde && value && value[0] !== '~' && webutil.isRippleName('~'+value)))) { // TODO Don't do a client check in validators
(attr.rpDestRippleNameNoTilde && value && value[0] !== '~' && webutil.isRippleName('~' + value)))) { // TODO Don't do a client check in validators
ctrl.rpDestType = "rippleName";

var defer = $q.defer();
$timeout(function() {
rippleVaultClient.AuthInfo.get(Options.domain, value, function(err, info){
scope.$apply(function(){
if (attr.rpDestModel && info.exists) {
getter = $parse(attr.rpDestModel);
getter.assign(scope,info.address);
}
currentDefer = defer;

currentCheckTimeout = $timeout(function() {
currentCheckTimeout = null;
currentDefer = null;

rippleVaultClient.AuthInfo.get(Options.domain, value, function(err, info) {
scope.$apply(function(){
if (info.exists) {
setDestModelValue(info.address);
defer.resolve(info.exists);
} else {
defer.reject();
Expand Down
6 changes: 2 additions & 4 deletions src/templates/tabs/trust.jade
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ section.col-xs-12.content(ng-controller="TrustCtrl")
rp-combobox-value-as-ripple-name
ng-model='counterparty_view'
rp-account-exists
rp-account-exists-model="counterparty_address"
rp-account-exists-loading="counterparty_loading"
rp-dest, rp-dest-address, rp-dest-contact, rp-not-me, rp-dest-email
rp-dest-ripple-name, rp-dest-model="counterparty"
rp-dest-loading="counterparty_loading"
Expand Down Expand Up @@ -155,7 +153,7 @@ section.col-xs-12.content(ng-controller="TrustCtrl")
.row
.col-xs-8.col-sm-7.col-md-6
rp-popup(rp-popup-on-open="grant()")
a.btn.btn-block.btn-primary.submit(href="", rp-popup-link, ng-disabled='trustForm.$invalid || verifying', l10n) Save
a.btn.btn-block.btn-primary.submit(href="", rp-popup-link, ng-disabled='trustForm.$invalid || trustForm.$pending || verifying', l10n) Save
div.connectModal(rp-popup-content)
.modal-header
.navbar-brand.hidden-sm.modal-logo#logo
Expand Down Expand Up @@ -404,4 +402,4 @@ section.col-xs-12.content(ng-controller="TrustCtrl")
button.btn.btn-block.btn-danger.btn-xs.submit(type="button", ng-click="delete_account()", ng-show="isIncomingOnly()", disabled, l10n) Remove
.col-xs-3
a.btn.btn-block.btn-cancel(href="", ng-click="cancel()"
ng-disabled="trust.loading", l10n) cancel
ng-disabled="trust.loading", l10n) cancel

0 comments on commit 83f76cb

Please sign in to comment.