@foreverrbum/ethsign
Version:
This package will allow you to electronically sign documents within your application
872 lines (714 loc) • 30.1 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useUserInfo = exports.useSignNow = exports.useSameUser = exports.useHasEmail = exports.useEmail = exports.useClearEmailLocalStorage = exports.useCheckEmail = exports.setEmailLocalData = exports.saveEmail = exports.getEthAddress = exports.getEmailLocalData = exports.getEmail = exports.checkEmail = exports.checkCanPopup = exports.EMAIL_NOTIFICATION = void 0;
var _react = _interopRequireWildcard(require("react"));
var _dashboard = require("./dashboard");
var _reactIntl = require("react-intl");
var EmailValidator = _interopRequireWildcard(require("email-validator"));
var _reactRouter = require("react-router");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var endpoint = 'https://auxiliary.ethsign.xyz';
var EMAIL_NOTIFICATION = 'email-notification';
/**
*
* @param {function} provider
* @returns {string, function} email handleEmail
* @description a hook for operation email address
*/
exports.EMAIL_NOTIFICATION = EMAIL_NOTIFICATION;
var useEmail = function useEmail(provider) {
var _useState = (0, _react.useState)(''),
_useState2 = _slicedToArray(_useState, 2),
email = _useState2[0],
handleEmail = _useState2[1];
var _useLocation = (0, _reactRouter.useLocation)(),
pathname = _useLocation.pathname;
var _useSameUser = useSameUser(provider),
_useSameUser2 = _slicedToArray(_useSameUser, 2),
sameUser = _useSameUser2[0],
handleSameUser = _useSameUser2[1];
var _useIntl = (0, _reactIntl.useIntl)(),
formatMessage = _useIntl.formatMessage;
(0, _react.useEffect)(function () {
if (!sameUser) {
useClearEmailLocalStorage();
handleEmail('');
}
if (sameUser) {
checkEmail(provider, formatMessage).then(function (hasEmail) {
if (hasEmail && pathname === '/manage-profile') {
getEmail(provider, formatMessage).then(function (_ref) {
var email = _ref.email,
rejectSign = _ref.rejectSign;
handleEmail(email);
});
}
});
}
}, [sameUser, pathname]);
return [email, handleEmail];
};
exports.useEmail = useEmail;
var useHasEmail = function useHasEmail(provider) {
var _useState3 = (0, _react.useState)(false),
_useState4 = _slicedToArray(_useState3, 2),
hasEmail = _useState4[0],
handleHasEmail = _useState4[1];
var _useIntl2 = (0, _reactIntl.useIntl)(),
formatMessage = _useIntl2.formatMessage;
(0, _react.useEffect)(function () {
checkEmail(provider, formatMessage).then(function (hasEmail) {
handleHasEmail(hasEmail);
});
}, [provider]);
return {
hasEmail: hasEmail
};
};
exports.useHasEmail = useHasEmail;
var useSignNow = function useSignNow() {
var _useState5 = (0, _react.useState)(function () {
var _JSON$parse = JSON.parse(localStorage.getItem(EMAIL_NOTIFICATION) || '{}'),
email = _JSON$parse.email,
hasEmail = _JSON$parse.hasEmail;
if (!hasEmail && !email || hasEmail && !email) {
return false;
}
return true;
}),
_useState6 = _slicedToArray(_useState5, 2),
signNow = _useState6[0],
handleSignNow = _useState6[1];
return [signNow, handleSignNow];
};
exports.useSignNow = useSignNow;
var useCheckEmail = function useCheckEmail(provider, handleOpen) {
var _useLocation2 = (0, _reactRouter.useLocation)(),
pathname = _useLocation2.pathname;
var _useSameUser3 = useSameUser(provider),
_useSameUser4 = _slicedToArray(_useSameUser3, 1),
sameUser = _useSameUser4[0];
var _useIntl3 = (0, _reactIntl.useIntl)(),
formatMessage = _useIntl3.formatMessage;
(0, _react.useEffect)(function () {
if (!sameUser && pathname !== '/manage-profile') {
useClearEmailLocalStorage();
}
checkEmail(provider, formatMessage).then(function (hasEmail) {
var _getEmailLocalData = getEmailLocalData(),
skip = _getEmailLocalData.skip;
if (pathname === "/contracts") {
if (checkCanPopup(skip, hasEmail)) {
handleOpen(true);
} else {
handleOpen(false);
}
}
});
}, [pathname, sameUser]);
};
/**
*
* @returns {object, object} ethAddress, ethSignature
* @description a hook for operation user information
*/
exports.useCheckEmail = useCheckEmail;
var useUserInfo = function useUserInfo(provider) {
var _useState7 = (0, _react.useState)(),
_useState8 = _slicedToArray(_useState7, 2),
ethAddress = _useState8[0],
handleEthAddress = _useState8[1];
var _useSameUser5 = useSameUser(provider),
_useSameUser6 = _slicedToArray(_useSameUser5, 2),
sameUser = _useSameUser6[0],
handleSameUser = _useSameUser6[1];
(0, _react.useEffect)(function () {
var localData = JSON.parse(localStorage.getItem(EMAIL_NOTIFICATION) || '{}');
if (localData !== null && localData !== void 0 && localData.ethAddress) {
handleEthAddress(localData === null || localData === void 0 ? void 0 : localData.ethAddress);
} else {
_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var signer, ethAddress;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
signer = provider.getSigner();
_context.next = 3;
return signer.getAddress();
case 3:
ethAddress = _context.sent;
handleEthAddress(ethAddress);
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}))(provider);
}
}, [sameUser, provider]);
return {
ethAddress: ethAddress
};
};
exports.useUserInfo = useUserInfo;
var useClearEmailLocalStorage = function useClearEmailLocalStorage() {
localStorage.setItem(EMAIL_NOTIFICATION, JSON.stringify({
skip: false
}));
};
/**
*
* @param {function} provider
* @param {function} formatMessage i18n
* @returns {Promise} email info
* @description for get email info
*/
exports.useClearEmailLocalStorage = useClearEmailLocalStorage;
var getEmail = /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(provider, formatMessage) {
var _getEmailLocalData2, email, timeStamp, ethAddress, rejectSign, sameUser, _yield$getSignerInfo, _ethAddress, ethSignature, _rejectSign;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
// get data from local storage
_getEmailLocalData2 = getEmailLocalData(), email = _getEmailLocalData2.email, timeStamp = _getEmailLocalData2.timeStamp, ethAddress = _getEmailLocalData2.ethAddress, rejectSign = _getEmailLocalData2.rejectSign; // check timeStamp is expired
// if not isExpired and the same user , return the local data
// else return the backend data
_context2.next = 3;
return isSameUser(provider, ethAddress);
case 3:
sameUser = _context2.sent;
if (!(!isExpired(timeStamp, 24) && sameUser && !rejectSign)) {
_context2.next = 8;
break;
}
return _context2.abrupt("return", Promise.resolve({
email: email,
rejectSign: rejectSign
}));
case 8:
_context2.next = 10;
return getSignerInfo(provider, formatMessage);
case 10:
_yield$getSignerInfo = _context2.sent;
_ethAddress = _yield$getSignerInfo.ethAddress;
ethSignature = _yield$getSignerInfo.ethSignature;
_rejectSign = _yield$getSignerInfo.rejectSign;
if (!_rejectSign) {
_context2.next = 19;
break;
}
setEmailLocalData({
skip: false,
email: ''
});
return _context2.abrupt("return", Promise.resolve({
email: '',
rejectSign: _rejectSign
}));
case 19:
_context2.next = 21;
return fetch("".concat(endpoint, "/getEmail"), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
ethAddress: _ethAddress,
ethSignature: ethSignature
})
}).then(function (res) {
return res.json();
}).then(function (_ref4) {
var email = _ref4.email;
setEmailLocalData({
timeStamp: new Date().getTime(),
email: email
});
return {
email: email,
rejectSign: _rejectSign
};
}).catch(function (error) {
(0, _dashboard.storeNotif)(formatMessage({
id: 'BAD_REQUEST'
}), formatMessage({
id: 'REQUEST_EMAIL_ADDRESS_FAILED'
}), 'danger');
});
case 21:
return _context2.abrupt("return", _context2.sent);
case 22:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return function getEmail(_x, _x2) {
return _ref3.apply(this, arguments);
};
}();
/**
*
* @param {function} provider
* @param {string} email
* @param {function} formatMessage i18n
* @returns {Promise} save result
* @description a function for save email
*/
exports.getEmail = getEmail;
var saveEmail = /*#__PURE__*/function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(provider, email, formatMessage) {
var validateResult, _yield$getSignerInfo2, ethAddress, ethSignature;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!email) {
_context3.next = 7;
break;
}
//validate email address
validateResult = EmailValidator.validate(email);
if (validateResult) {
_context3.next = 5;
break;
}
(0, _dashboard.storeNotif)(formatMessage({
id: 'INVALID_EMAIL'
}), formatMessage({
id: 'INVALID_EMAIL_MESSAGE'
}), 'danger');
return _context3.abrupt("return", Promise.resolve({
success: false
}));
case 5:
_context3.next = 8;
break;
case 7:
if (email === '') {
email = null;
}
case 8:
_context3.next = 10;
return getSignerInfo(provider, formatMessage);
case 10:
_yield$getSignerInfo2 = _context3.sent;
ethAddress = _yield$getSignerInfo2.ethAddress;
ethSignature = _yield$getSignerInfo2.ethSignature;
_context3.next = 15;
return fetch("".concat(endpoint, "/setEmail"), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
ethAddress: ethAddress,
ethSignature: ethSignature,
email: email
})
}).then(function (res) {
return res.json();
}).then(function (data) {
// update hasEmial value when save email success
var hasEmail = true;
if (email === null) {
hasEmail = false;
} // update email address
// update email address
setEmailLocalData({
hasEmail: hasEmail,
email: email
});
(0, _dashboard.storeNotif)(formatMessage({
id: 'SAVE_SUCCESS'
}), formatMessage({
id: 'EMAIL_SAVE_SUCCESS'
}), 'success');
return data;
}).catch(function (error) {
(0, _dashboard.storeNotif)(formatMessage({
id: 'BAD_REQUEST'
}), formatMessage({
id: 'REQUEST_EMAIL_ADDRESS_FAILED'
}), 'danger');
return Promise.resolve({
success: false
});
});
case 15:
return _context3.abrupt("return", _context3.sent);
case 16:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return function saveEmail(_x3, _x4, _x5) {
return _ref5.apply(this, arguments);
};
}();
exports.saveEmail = saveEmail;
var checkEmail = /*#__PURE__*/function () {
var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(provider, formatMessage) {
var _JSON$parse2, hasEmail, ethAddress, timeStamp, sameUser, _ethAddress2;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_JSON$parse2 = JSON.parse(localStorage.getItem(EMAIL_NOTIFICATION) || '{}'), hasEmail = _JSON$parse2.hasEmail, ethAddress = _JSON$parse2.ethAddress, timeStamp = _JSON$parse2.timeStamp;
_context4.next = 3;
return isSameUser(provider, ethAddress);
case 3:
sameUser = _context4.sent;
if (!(!isExpired(timeStamp, 24) && sameUser)) {
_context4.next = 8;
break;
}
return _context4.abrupt("return", Promise.resolve(hasEmail));
case 8:
_context4.next = 10;
return getEthAddress(provider);
case 10:
_ethAddress2 = _context4.sent;
if (_ethAddress2) {
_context4.next = 13;
break;
}
return _context4.abrupt("return", true);
case 13:
_context4.next = 15;
return fetch("".concat(endpoint, "/checkEmail"), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
ethAddresses: [_ethAddress2]
})
}).then(function (res) {
return res.json();
}).then(function (res) {
var hasEmail = false;
var findItem = res.find(function (r) {
return Object.keys(r)[0] === _ethAddress2;
});
if (findItem) {
hasEmail = Object.values(findItem)[0]; // update hasEmail on localstorage
// update hasEmail on localstorage
setEmailLocalData({
hasEmail: hasEmail
});
}
return hasEmail;
}).catch(function (e) {
(0, _dashboard.storeNotif)(formatMessage({
id: 'BAD_REQUEST'
}), formatMessage({
id: 'CHECK_EMAIL_ADDRESS_FAILED'
}, {
error: e.toString()
}), 'danger');
});
case 15:
return _context4.abrupt("return", _context4.sent);
case 16:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return function checkEmail(_x6, _x7) {
return _ref6.apply(this, arguments);
};
}();
exports.checkEmail = checkEmail;
var getEthAddress = /*#__PURE__*/function () {
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(provider) {
var signer;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
signer = provider.getSigner();
_context5.prev = 1;
_context5.next = 4;
return signer.getAddress();
case 4:
return _context5.abrupt("return", _context5.sent);
case 7:
_context5.prev = 7;
_context5.t0 = _context5["catch"](1);
return _context5.abrupt("return", null);
case 10:
case "end":
return _context5.stop();
}
}
}, _callee5, null, [[1, 7]]);
}));
return function getEthAddress(_x8) {
return _ref7.apply(this, arguments);
};
}();
/**
*
* @param {function} provider
* @param {function} formatMessage i18n
* @returns {object, object} ethAddress, ethSignature
*/
exports.getEthAddress = getEthAddress;
var getSignerInfo = /*#__PURE__*/function () {
var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(provider, formatMessage) {
var rejectSign, localData, message, timeStamp, ethAddress, ethSignature, sameUser, resData, signer;
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
rejectSign = false; // get info from local first
localData = getEmailLocalData();
message = localData === null || localData === void 0 ? void 0 : localData.message;
timeStamp = localData === null || localData === void 0 ? void 0 : localData.timeStamp;
ethAddress = localData === null || localData === void 0 ? void 0 : localData.ethAddress;
ethSignature = localData === null || localData === void 0 ? void 0 : localData.ethSignature; // check timeStamp is expired
// if isExpired (24 h) and rpc user is not equal the local user, request data from backend
// else response directly local data
_context6.next = 8;
return isSameUser(provider, ethAddress);
case 8:
sameUser = _context6.sent;
if (!(isExpired(timeStamp, 24) || !ethSignature)) {
_context6.next = 30;
break;
}
_context6.next = 12;
return getSignMessage(formatMessage);
case 12:
resData = _context6.sent;
message = resData === null || resData === void 0 ? void 0 : resData.message;
signer = provider.getSigner();
_context6.next = 17;
return signer.getAddress();
case 17:
ethAddress = _context6.sent;
_context6.prev = 18;
_context6.next = 21;
return signer.signMessage(message);
case 21:
ethSignature = _context6.sent;
_context6.next = 29;
break;
case 24:
_context6.prev = 24;
_context6.t0 = _context6["catch"](18);
rejectSign = true;
ethSignature = undefined;
message = undefined;
case 29:
setEmailLocalData({
timeStamp: new Date().getTime(),
message: message,
ethAddress: ethAddress,
ethSignature: ethSignature,
rejectSign: rejectSign
});
case 30:
return _context6.abrupt("return", {
ethAddress: ethAddress,
ethSignature: ethSignature,
rejectSign: rejectSign
});
case 31:
case "end":
return _context6.stop();
}
}
}, _callee6, null, [[18, 24]]);
}));
return function getSignerInfo(_x9, _x10) {
return _ref8.apply(this, arguments);
};
}();
/**
*
* @param {function} provider
* @param {String} ethAddressFromLocal
* @returns {boolean}
* @description check the ethAddressFromLocal is equal with ethAddressFromRpc
*/
var isSameUser = /*#__PURE__*/function () {
var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(provider, ethAddressFromLocal) {
var signer, ethAddressFromRpc;
return regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.prev = 0;
// if the ethAddressFromLocal equal undefined, it is to show this is a new user, default the new user is a sigle user, should return true
signer = provider.getSigner();
_context7.next = 4;
return signer.getAddress();
case 4:
ethAddressFromRpc = _context7.sent;
if (!(ethAddressFromLocal === undefined)) {
_context7.next = 8;
break;
}
setEmailLocalData({
ethAddress: ethAddressFromRpc
});
return _context7.abrupt("return", true);
case 8:
;
return _context7.abrupt("return", ethAddressFromLocal === ethAddressFromRpc);
case 12:
_context7.prev = 12;
_context7.t0 = _context7["catch"](0);
return _context7.abrupt("return", false);
case 15:
case "end":
return _context7.stop();
}
}
}, _callee7, null, [[0, 12]]);
}));
return function isSameUser(_x11, _x12) {
return _ref9.apply(this, arguments);
};
}();
/**
*
* @param {function} formatMessage i18n
* @returns {Promise} validate message
*/
var getSignMessage = /*#__PURE__*/function () {
var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(formatMessage) {
return regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return fetch("".concat(endpoint, "/getMessage")).then(function (res) {
return res.json();
}).then(function (res) {
return res;
}).catch(function (error) {
(0, _dashboard.storeNotif)(formatMessage({
id: 'REQUEST_MESSAGE_FAILDED'
}), formatMessage({
id: 'REQUEST_EMAIL_ADDRESS_FAILED'
}), 'danger');
});
case 2:
return _context8.abrupt("return", _context8.sent);
case 3:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
return function getSignMessage(_x13) {
return _ref10.apply(this, arguments);
};
}();
/**
*
* @param {millisecond} time
* @param {hours} expiredTime
* @returns {boolean}
*/
var isExpired = function isExpired() {
var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var expiredTime = arguments.length > 1 ? arguments[1] : undefined;
var currentTime = new Date().getTime();
return (currentTime - time) / (1000 * 60 * 60) > expiredTime;
};
/**
*
* @param {*} provider
* @returns [boolean, function]
* @description check the user have change
*/
var useSameUser = function useSameUser(provider) {
var _useState9 = (0, _react.useState)(true),
_useState10 = _slicedToArray(_useState9, 2),
sameUser = _useState10[0],
handleSameUser = _useState10[1];
(0, _react.useEffect)(function () {
_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
var localData;
return regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
localData = JSON.parse(localStorage.getItem(EMAIL_NOTIFICATION) || '{}');
_context9.t0 = handleSameUser;
_context9.next = 4;
return isSameUser(provider, localData === null || localData === void 0 ? void 0 : localData.ethAddress);
case 4:
_context9.t1 = _context9.sent;
(0, _context9.t0)(_context9.t1);
case 6:
case "end":
return _context9.stop();
}
}
}, _callee9);
}))();
});
return [sameUser, handleSameUser];
};
/**
*
* @param {boolean} skip
* @param {boolean} hasEmail the use have email set before
* @returns {boolean} decide popup the email notification
*/
exports.useSameUser = useSameUser;
var checkCanPopup = function checkCanPopup(skip, hasEmail) {
if (!skip && !hasEmail) {
return true;
}
return false;
};
/**
*
* @returns the local storage data about EMAIL_NOTIFICATION
*/
exports.checkCanPopup = checkCanPopup;
var getEmailLocalData = function getEmailLocalData() {
return JSON.parse(localStorage.getItem(EMAIL_NOTIFICATION) || '{}');
};
/**
*
* @param {Object} value
* @description update the local storage data about EMAIL_NOTIFICATION
*/
exports.getEmailLocalData = getEmailLocalData;
var setEmailLocalData = function setEmailLocalData(value) {
var localData = getEmailLocalData();
localStorage.setItem(EMAIL_NOTIFICATION, JSON.stringify(_objectSpread(_objectSpread({}, localData), value)));
};
exports.setEmailLocalData = setEmailLocalData;