<commit_before>/**
 * DOMAIN ALIAS
 *
 * The domain alias service resolves ripple address to domains.
 *
 * In the AccountRoot entry of any ripple account users can provide a reference
 * to a domain they own. Ownership of the domain is verified via the ripple.txt
 * magic file.
 *
 * This service provides both the lookup in the ledger and the subsequent
 * verification via ripple.txt.
 */

var module = angular.module('domainalias', ['network', 'rippletxt']);

module.factory('rpDomainAlias', ['$q', '$rootScope', 'rpNetwork', 'rpRippleTxt',
                                 function ($q, $scope, net, txt)
{
  var aliases = {};

  /**
   * Validates a domain against an object parsed from ripple.txt data.
   *
   * @private
   */
  function validateDomain(domain, address, data)
  {
    // Validate domain
    if (!data.domain ||
        data.domain.length !== 1 ||
        data.domain[0] !== domain) {
      return false;
    }

    // Validate address
    if (!data.accounts) {
      return false;
    }
    for (var i = 0, l = data.accounts.length; i < l; i++) {
      if (data.accounts[i] === address) {
        return true;
      }
    }

    return false;
  }

  function getAliasForAddress(address) {
    var aliasPromise = $q.defer();

    net.remote.request_account_info(address)
      .on('success', function (data) {
        if (data.account_data.Domain) {
          $scope.$apply(function () {
            var domain = sjcl.codec.utf8String.fromBits(sjcl.codec.hex.toBits(data.account_data.Domain));

            var txtData = txt.get(domain);
            txtData.then(function (data) {
              validateDomain(domain, address, data)
                ? aliasPromise.resolve(domain)
                : aliasPromise.reject(new Error("Invalid domain"));

            }, function (error) {

            });
          });
        }
      })
      .on('error', function () {
        aliasPromise.reject(new Error("No domain found"));
      })
      .request();

    console.log(jQuery.extend(true, {}, aliasPromise.promise));
    return aliasPromise.promise;
  }

  return {
    getAliasForAddress: getAliasForAddress
  };
}]);<commit_msg>Domainalias: Remember alias lookup results.<commit_after>/**
 * DOMAIN ALIAS
 *
 * The domain alias service resolves ripple address to domains.
 *
 * In the AccountRoot entry of any ripple account users can provide a reference
 * to a domain they own. Ownership of the domain is verified via the ripple.txt
 * magic file.
 *
 * This service provides both the lookup in the ledger and the subsequent
 * verification via ripple.txt.
 */

var module = angular.module('domainalias', ['network', 'rippletxt']);

module.factory('rpDomainAlias', ['$q', '$rootScope', 'rpNetwork', 'rpRippleTxt',
                                 function ($q, $scope, net, txt)
{
  var aliases = {};

  /**
   * Validates a domain against an object parsed from ripple.txt data.
   *
   * @private
   */
  function validateDomain(domain, address, data)
  {
    // Validate domain
    if (!data.domain ||
        data.domain.length !== 1 ||
        data.domain[0] !== domain) {
      return false;
    }

    // Validate address
    if (!data.accounts) {
      return false;
    }
    for (var i = 0, l = data.accounts.length; i < l; i++) {
      if (data.accounts[i] === address) {
        return true;
      }
    }

    return false;
  }

  function getAliasForAddress(address) {
    var aliasPromise = $q.defer();

    if (aliases[address] && aliases[address].checked) {
      if (aliases[address].domain) {
        aliasPromise.resolve(aliases[address].domain);
      }
      else {
        aliasPromise.reject(new Error("Invalid domain"));
      }

      return aliasPromise.promise;
    }

    net.remote.request_account_info(address)
      .on('success', function (data) {
        if (data.account_data.Domain) {
          $scope.$apply(function () {
            var domain = sjcl.codec.utf8String.fromBits(sjcl.codec.hex.toBits(data.account_data.Domain));

            var txtData = txt.get(domain);
            txtData.then(
              function (data) {
                if(validateDomain(domain, address, data)) {
                  aliases[address] = {
                    checked: true,
                    domain: domain
                  };
                  aliasPromise.resolve(domain);
                }
                else {
                  aliases[address] = {
                    checked: true,
                    domain: false
                  };
                  aliasPromise.reject(new Error("Invalid domain"));
                }
              },
              function (error) {
                aliases[address] = {
                  checked: true,
                  domain: false
                };
                aliasPromise.reject(new Error(error));
              }
            );
          });
        }
        else {
          aliases[address] = {
            checked: true,
            domain: false
          };
          aliasPromise.reject(new Error("No domain found"));
        }
      })
      .on('error', function () {
        aliasPromise.reject(new Error("No domain found"));
      })
      .request();

    return aliasPromise.promise;
  }

  return {
    getAliasForAddress: getAliasForAddress
  };
}]);
{'fraction_non_alphanumeric': 0.12192351382052254, 'fraction_numerical': 0.0015145778114350624, 'mean_word_length': 1.8947945205479453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7635221', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1451, 'n_words': 369}