@foreverrbum/ethsign
Version:
This package will allow you to electronically sign documents within your application
104 lines (89 loc) • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTimestampOfFutureBlock = exports.default = exports.changeEstimateExpiration = void 0;
var _networkinfo = require("./networkinfo");
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); }); }; }
var getTimestampOfFutureBlock = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(block, currentBlock, provider) {
var expectedDate;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
expectedDate = new Date(Date.now());
_context.next = 4;
return provider.getNetwork().then(function (network) {
var network_info = _networkinfo.networkinfo.get(network.chainId);
if (network_info) {
expectedDate.setSeconds(expectedDate.getSeconds() + network_info.blockSeconds * (block - currentBlock));
}
});
case 4:
_context.next = 10;
break;
case 6:
_context.prev = 6;
_context.t0 = _context["catch"](0);
console.log("Error: Couldn't find network info for chainId " + network.chainId);
return _context.abrupt("return", null);
case 10:
return _context.abrupt("return", expectedDate);
case 11:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 6]]);
}));
return function getTimestampOfFutureBlock(_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}();
exports.getTimestampOfFutureBlock = getTimestampOfFutureBlock;
var changeEstimateExpiration = function changeEstimateExpiration(useBlock, userInput, currentBlock, provider, handleEstimatedExpirationDate, handleEstimatedExpirationBlock) {
if (useBlock) {
// Calculate the estimated expiration date using the block in the expiry input
var expectedDate = new Date(Date.now());
provider.getNetwork().then(function (network) {
try {
if (userInput < currentBlock) {
handleEstimatedExpirationDate(null);
} else {
expectedDate.setSeconds(expectedDate.getSeconds() + _networkinfo.networkinfo.get(network.chainId).blockSeconds * (userInput - currentBlock));
handleEstimatedExpirationDate(expectedDate);
}
} catch (error) {
console.log("Error: Couldn't find network info for chainId " + network.chainId);
}
});
} else {
// Calculate the estimated expiration block using the date provided in the expiry input
var inputDate = new Date(userInput); // The above date gets added in GMT-0, but when we print it, it switches to local timezone, altering the date.
// Add back the timezone offset to make sure it interprets the date input as local time.
// 1440 minutes in a day. We want this to expire at end of day, so add 1439 to expire at 11:59PM.
inputDate.setMinutes(inputDate.getMinutes() + inputDate.getTimezoneOffset() + 1439);
var expectedBlock = currentBlock;
var seconds = (inputDate - new Date(Date.now())) / 1000;
provider.getNetwork().then(function (network) {
try {
if (seconds < 0) {
handleEstimatedExpirationBlock(null);
} else {
expectedBlock += seconds / _networkinfo.networkinfo.get(network.chainId).blockSeconds;
handleEstimatedExpirationBlock(Math.ceil(expectedBlock));
}
} catch (error) {
console.log("Error: Couldn't find network info for chainId " + network.chainId);
}
});
}
};
exports.changeEstimateExpiration = changeEstimateExpiration;
var _default = {
changeEstimateExpiration: changeEstimateExpiration
};
exports.default = _default;