import _Promise from 'babel-runtime/core-js/promise';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import Personal from 'web3-eth-personal';

import isRequired from '../utils/isRequired';
import { UNABLE_TO_SIGN_TOKEN } from '../constants/error';

var Auth = function Auth() {
  var web3Wrapper = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('web3Wrapper');
  var storeProviderInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : isRequired('storeProviderInstance');

  _classCallCheck(this, Auth);

  _initialiseProps.call(this);

  this._Web3Wrapper = web3Wrapper;
  this._StoreProviderInstance = storeProviderInstance;
}

/**
 * Set store provider instance.
 * @param {object} storeProviderInstance - instance of store provider wrapper.
 */


/**
 * Set an auth token in the Store Provider. Call this instead of validateNewAuthToken
 * if you have a signed token saved.
 * @param {string} token - Hex representation of signed token.
 */


/**
 * Validate a new auth token. Note if you validate a new token old signed tokens
 * will not longer be valid regardless of their expiration time.
 * @param {string} userAddress - Address of the user profile
 * @returns {string} Signed token for future use.
 */


/**
 * Sign a message with your private key. Uses web3 1.0 personal sign
 * @param {string} userAddress - The address with which we want to sign the message
 * @param {string} data - Hex encoded data to sign
 * @returns {string} signed data
 */


/**
 * Validate an auth token.
 * @param {string} userAddress - The address of the user.
 * @param {string} authToken - Token to check.
 * @returns {Promise} resolves to True if token is valid.
 */
;

var _initialiseProps = function _initialiseProps() {
  var _this = this;

  this.setStoreProviderInstance = function (storeProviderInstance) {
    _this._StoreProviderInstance = storeProviderInstance;
  };

  this.setAuthToken = function (token) {
    _this._StoreProviderInstance.setAuthToken(token);
  };

  this.getNewAuthToken = function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(userAddress) {
      var unsignedToken, signedToken;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this._StoreProviderInstance.newAuthToken(userAddress);

            case 2:
              unsignedToken = _context.sent.unsignedToken;
              _context.next = 5;
              return _this.signMessage(userAddress, unsignedToken);

            case 5:
              signedToken = _context.sent;
              _context.next = 8;
              return _this.validateAuthToken(userAddress, signedToken);

            case 8:
              if (_context.sent) {
                _context.next = 10;
                break;
              }

              throw new Error(UNABLE_TO_SIGN_TOKEN);

            case 10:
              return _context.abrupt('return', signedToken);

            case 11:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }));

    return function (_x3) {
      return _ref.apply(this, arguments);
    };
  }();

  this.signMessage = function (userAddress, data) {
    var ethPersonal = new Personal(_this._Web3Wrapper.getProvider());
    return new _Promise(function (resolve, reject) {
      ethPersonal.sign(data, userAddress, function (error, result) {
        if (error) reject(error);

        resolve(result);
      });
    });
  };

  this.validateAuthToken = function (userAddress, authToken) {
    return _this._StoreProviderInstance.isTokenValid(userAddress, authToken);
  };
};

export default Auth;
//# sourceMappingURL=Auth.js.map