UNPKG

@foreverrbum/ethsign

Version:

This package will allow you to electronically sign documents within your application

431 lines (361 loc) 19.8 kB
"use strict"; 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.default = void 0; var _react = _interopRequireWildcard(require("react")); var _newdocument = require("../../helpers/newdocument"); var _ProgressBarIndicator = _interopRequireDefault(require("../Dashboard/ProgressBarIndicator")); var _dashboard = require("../../helpers/dashboard"); var _reactTypeformEmbed = require("react-typeform-embed"); var _reactRouter = require("react-router"); var _reactIntl = require("react-intl"); var _pdf = require("../../helpers/pdf"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 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 Sending = function Sending(props) { var data = props.data, contract = props.contract, web3 = props.web3, webviewer = props.webviewer, close = props.close, handleData = props.handleData, createProgress = props.createProgress, handleCreateProgress = props.handleCreateProgress, storageHash = props.storageHash, handleStorageHash = props.handleStorageHash, documentKey = props.documentKey, selfSignOnSubmit = props.selfSignOnSubmit, indicatorProgress = props.indicatorProgress, handleIndicatorProgress = props.handleIndicatorProgress, shouldResubmit = props.shouldResubmit, filter = props.filter, idx = props.idx, annotList = props.annotList, storageProvider = props.storageProvider, handleStorageProvider = props.handleStorageProvider, updateStoredData = props.updateStoredData; var _useState = (0, _react.useState)('Submitting'), _useState2 = _slicedToArray(_useState, 2), submitButton = _useState2[0], handleSubmitButton = _useState2[1]; var typeform = (0, _react.useRef)(null); var _useIntl = (0, _reactIntl.useIntl)(), formatMessage = _useIntl.formatMessage; var _useState3 = (0, _react.useState)(null), _useState4 = _slicedToArray(_useState3, 2), signers = _useState4[0], handleSigners = _useState4[1]; var _useState5 = (0, _react.useState)(null), _useState6 = _slicedToArray(_useState5, 2), signerFieldCount = _useState6[0], handleSignerFieldCount = _useState6[1]; var _useState7 = (0, _react.useState)(null), _useState8 = _slicedToArray(_useState7, 2), sigFields = _useState8[0], handleSigFields = _useState8[1]; var _useState9 = (0, _react.useState)(null), _useState10 = _slicedToArray(_useState9, 2), annotations = _useState10[0], handleAnnotations = _useState10[1]; var _useState11 = (0, _react.useState)(false), _useState12 = _slicedToArray(_useState11, 2), initialized = _useState12[0], handleInitialized = _useState12[1]; /** * progress bar: * max: 3 + 2*(# signers) + (1 if self-signing) * * progress (non-aggregated): * 1 - newBasicDocument created * 2 - uploaded to storage platforms * 3 - added fileHash to document * 4 - added signers to document * 5 - added signature fields to document * 6 - (optional) completed self-signing * * progress (aggregated): * 1 - aggregateHandleDocumentUpload completed * 2 - aggregateNewBasicDocumentAndSetStorage completed * 3 - aggregateAddSignersAndNumOfSigFieldsForDocument completed * 4 - (optional) completed self-signing * 5 - Feedback survey completed * 6 - Notification completed for successful creation */ (0, _react.useEffect)(function () { _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var _yield$getxfdfString, sigFieldsXFDF, annotationsXFDF, addresses, count; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(webviewer && annotList.length > 0 && !initialized)) { _context.next = 14; break; } _context.next = 3; return (0, _pdf.getxfdfString)(webviewer, annotList); case 3: _yield$getxfdfString = _context.sent; sigFieldsXFDF = _yield$getxfdfString.sigFieldsXFDF; annotationsXFDF = _yield$getxfdfString.annotationsXFDF; addresses = _yield$getxfdfString.addresses; count = _yield$getxfdfString.count; handleSigFields(sigFieldsXFDF); handleAnnotations(annotationsXFDF); handleSigners(addresses); handleSignerFieldCount(count); handleInitialized(true); updateStoredData(); case 14: case "end": return _context.stop(); } } }, _callee); }))(); }, [webviewer, annotList]); (0, _react.useEffect)(function () { _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var progress, _progress, signaturesFile, annotationsFile, encryptedFile, encryptedSigFields, encryptedAnnotations; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: if (!signerFieldCount) { _context2.next = 32; break; } if (!shouldResubmit) { _context2.next = 11; break; } progress = createProgress; if (!(progress == 0)) { _context2.next = 8; break; } _context2.t0 = progress; _context2.next = 7; return (0, _newdocument.handleDocumentReupload)(storageProvider, handleSubmitButton, data.password, documentKey, data.file, storageHash, handleStorageHash, createProgress, handleCreateProgress, indicatorProgress, handleIndicatorProgress, continueResubmitCallback, close, formatMessage); case 7: progress = _context2.t0 += _context2.sent; case 8: if (progress > 0) { continueResubmitCallback(progress, indicatorProgress, storageHash, storageProvider); } _context2.next = 32; break; case 11: // TODO: upload a blank file if file is null _progress = createProgress; // New way using aggregated signers: if (!(_progress == 0)) { _context2.next = 31; break; } // Upload document // File encryption signaturesFile = new Blob([sigFields], { type: 'text/plain' }); annotationsFile = new Blob([annotations], { type: 'text/plain' }); handleSubmitButton(formatMessage({ id: 'ENCRYPTING_FILE' })); _context2.next = 18; return (0, _newdocument.getEncryptedStringFromFile)(data.file, data.password); case 18: encryptedFile = _context2.sent; _context2.next = 21; return (0, _newdocument.getEncryptedStringFromFile)(signaturesFile, data.password); case 21: encryptedSigFields = _context2.sent; _context2.next = 24; return (0, _newdocument.getEncryptedStringFromFile)(annotationsFile, data.password); case 24: encryptedAnnotations = _context2.sent; _context2.t1 = _progress; _context2.next = 28; return (0, _newdocument.aggregateHandleDocumentUpload)(web3, contract, storageProvider, handleStorageProvider, handleSubmitButton, documentKey, encryptedFile, handleStorageHash, encryptedSigFields, encryptedAnnotations, handleCreateProgress, indicatorProgress, handleIndicatorProgress, continueCallback, close, formatMessage); case 28: _progress = _context2.t1 += _context2.sent; _context2.next = 32; break; case 31: if (_progress > 0) { continueCallback(_progress, indicatorProgress, storageHash, storageProvider); } case 32: case "end": return _context2.stop(); } } }, _callee2); }))(); }, [signerFieldCount]); var continueResubmitCallback = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(progress, indicatorProgress, docuStorageHash, storageProvider) { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: if (progress == 1) { // We just need to update the storage at this stage (0, _newdocument.setDocStorageForDocument)(contract, web3, documentKey, storageProvider, docuStorageHash, indicatorProgress, handleIndicatorProgress, handleCreateProgress, handleSubmitButton, continueResubmitCallback, close, formatMessage); } if (progress == 2) { handleSubmitButton(formatMessage({ id: 'CONTRACT_SUCCESFULLY_CREATED' })); (0, _dashboard.storeNotif)(formatMessage({ id: 'CONTRACT_SUCCESFULLY_CREATED' }), '', 'info'); if (handleData) { handleData(documentKey, filter ? filter : 'original', idx); } } case 2: case "end": return _context3.stop(); } } }, _callee3); })); return function continueResubmitCallback(_x, _x2, _x3, _x4) { return _ref3.apply(this, arguments); }; }(); var continueCallback = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(progress, indicatorProgress, docuStorageHash, storageProvider) { return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: if (progress == 1) { // New basic document and set storage (0, _newdocument.aggregateNewBasicDocumentAndSetStorage)(web3, contract, documentKey, data.filename.name + data.filename.ext, data.expiryBlock, storageProvider, docuStorageHash, signers, signerFieldCount, handleSubmitButton, formatMessage, /*#__PURE__*/function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(tx) { var networkId; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return web3.getNetwork(); case 2: networkId = _context4.sent.chainId; _context4.next = 5; return tx.wait(networkId == 1287 ? 2 : 1).then(function (receipt) { handleCreateProgress(2); indicatorProgress += 1; handleIndicatorProgress(indicatorProgress); continueCallback(2, indicatorProgress); }); case 5: case "end": return _context4.stop(); } } }, _callee4); })); return function (_x9) { return _ref5.apply(this, arguments); }; }(), function () { close(); }, formatMessage({ id: "SUBMIT" })); } else if (progress == 2 && selfSignOnSubmit) { // Add signers and number of signer fields (0, _newdocument.autoSelfSign)(contract, web3, documentKey, createProgress, handleCreateProgress, indicatorProgress, handleIndicatorProgress, handleSubmitButton, continueCallback, close, formatMessage); } else if (progress == 3 || progress == 2 && !selfSignOnSubmit) { // Self-sign the document if the checkbox is checked handleSubmitButton(formatMessage({ id: 'CONTRACT_SUCCESFULLY_CREATED' })); (0, _dashboard.storeNotif)(formatMessage({ id: 'CONTRACT_SUCCESFULLY_CREATED' }), '', 'info'); if (handleData) { handleData(documentKey); } handleCreateProgress(3); } case 1: case "end": return _context5.stop(); } } }, _callee5); })); return function continueCallback(_x5, _x6, _x7, _x8) { return _ref4.apply(this, arguments); }; }(); return /*#__PURE__*/_react.default.createElement("div", { className: "max-w-7xl mx-auto bg-white w-11/12 sm:w-7/12 border-t-4 border-orange-500" }, /*#__PURE__*/_react.default.createElement("div", { className: "py-5 px-5 flex flex-col justify-center w-full" }, /*#__PURE__*/_react.default.createElement("div", { className: "pt-3 pb-1 mx-auto text-center" }, shouldResubmit && createProgress == 2 || createProgress == 3 ? formatMessage({ id: 'HELP_US_IMPROVE_AND_RATE_YOUR_EXPERIENCE' }) : submitButton), !(shouldResubmit && createProgress == 2 || createProgress == 3) && /*#__PURE__*/_react.default.createElement(_ProgressBarIndicator.default, { value: indicatorProgress, max: shouldResubmit ? 2 : 2 + (selfSignOnSubmit ? 1 : 0) }), /*#__PURE__*/_react.default.createElement("div", { className: "-z-10 relative" }, /*#__PURE__*/_react.default.createElement(_reactTypeformEmbed.ReactTypeformEmbed, { url: "https://dygdc65xyp5.typeform.com/to/lC3Sns44" // style={"display:inline-block;text-decoration:none;background-color:#E98234;color:white;cursor:pointer;font-family:Helvetica,Arial,sans-serif;font-size:20px;line-height:50px;text-align:center;margin:0;height:50px;padding:0px 33px;border-radius:25px;max-width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:bold;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;"} , popup: true, ref: typeform, autoOpen: false, autoClose: 0, onSubmit: function onSubmit() { (0, _dashboard.storeNotif)("", formatMessage({ id: 'WE_APPRECITE_AND_VALUE_YOU_FEEDBACK' }), "info"); if (handleData) { props.history.push({ pathname: '/contracts' }); } } })), (shouldResubmit && createProgress == 2 || createProgress == 3) && /*#__PURE__*/_react.default.createElement("div", { className: "flex flex-wrap text-white justify-center my-5" }, /*#__PURE__*/_react.default.createElement("div", { className: "cursor-pointer bg-gray-300 px-3 py-2 hover:bg-gray-600 rounded-md mx-3", onClick: function onClick() { if (handleData) { props.history.push({ pathname: '/contracts' }); } } }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, { id: "NOT_NOW" })), /*#__PURE__*/_react.default.createElement("div", { className: " cursor-pointer bg-orange-500 px-3 py-2 rounded-md hover:bg-orange-600 mx-3", onClick: function onClick() { typeform.current.typeform.open(); } }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, { id: "GIVE_FEEDBACK" }))))); }; var _default = (0, _reactRouter.withRouter)(Sending); exports.default = _default;