UNPKG

@foreverrbum/ethsign

Version:

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

639 lines (531 loc) 25.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.signDocumentAtIndex = exports.signAndSaveComments = exports.makeSignaturesReadOnly = exports.getDocumentSignatureAtIndex = exports.default = exports.createAndSign = exports.aggregateSetSigFieldAsSignerForDocument = void 0; var _ethers = require("ethers"); var _dashboard = require("./dashboard"); var _newdocument = require("./newdocument"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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 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 emptyHex = '0x0000000000000000000000000000000000000000000000000000000000000000'; var createAndSign = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(provider, ethAccount, instance, signaturesToSave, formatMessage, contract, handleSaveStatus, password, docName, initialSigners, bar, handleProgress, user, networkId, callback, errorCallback, handleSavedAnnotations, handleNewChanges) { var docViewer, key, storageHash, expiration, storageProvider, doc, fileData, buffer, blob, encryptedFile, _yield$docUpload, documentKey, hash, storage_provider, docuStorageHash, annotManager, x, signers, signerFieldCount, created, _doc; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: // 1-docUpload // 2-setDocStorage // 3-addSignersAndNumofSigFields // 4-signMessage // 5-saveSigsAndSaveComment docViewer = instance.docViewer; expiration = 0; storageProvider = "FL"; doc = docViewer.getDocument(); _context2.next = 6; return doc.getFileData(); case 6: fileData = _context2.sent; buffer = new Uint8Array(fileData); blob = new Blob([buffer], { type: 'application/pdf' }); _context2.next = 11; return (0, _newdocument.getEncryptedStringFromFile)(blob, password); case 11: encryptedFile = _context2.sent; _context2.prev = 12; _context2.next = 15; return (0, _newdocument.docUpload)(formatMessage, encryptedFile, contract, handleSaveStatus, storageProvider); case 15: _yield$docUpload = _context2.sent; documentKey = _yield$docUpload.documentKey; hash = _yield$docUpload.hash; storage_provider = _yield$docUpload.storage_provider; key = documentKey; storageHash = hash; storageProvider = storage_provider; _context2.next = 31; break; case 24: _context2.prev = 24; _context2.t0 = _context2["catch"](12); console.log(_context2.t0); (0, _dashboard.storeNotif)(formatMessage({ id: 'FILE_UPLOAD_ERROR' }), formatMessage({ id: 'FILE_UPLOAD_ERROR_MESSAGE' }, { button: bar.button2 }), 'danger'); handleSaveStatus(null); if (errorCallback) { errorCallback(); } return _context2.abrupt("return"); case 31: handleProgress(1); docuStorageHash = [storageHash, emptyHex, emptyHex, emptyHex, emptyHex, emptyHex]; annotManager = instance.annotManager; x = annotManager.getAnnotationsList().filter(function (a) { return a.Subject == "Widget" && a.Signer != null; }).length; signers = [ethAccount].concat(_toConsumableArray(initialSigners)); signerFieldCount = [x]; initialSigners.map(function (a) { signerFieldCount.push(1); }); _context2.next = 40; return (0, _newdocument.aggregateNewBasicDocumentAndSetStorage)(provider, contract, key, docName, expiration, storageProvider, docuStorageHash, signers, signerFieldCount, handleSaveStatus, formatMessage, /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(tx) { var networkId; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return provider.getNetwork(); case 2: networkId = _context.sent.chainId; _context.next = 5; return tx.wait(networkId == 1287 ? 2 : 1).then(function (receipt) { handleProgress(2); }); case 5: case "end": return _context.stop(); } } }, _callee); })); return function (_x19) { return _ref2.apply(this, arguments); }; }(), function () { handleSaveStatus(null); if (errorCallback) { errorCallback(); } }, bar.button2); case 40: created = _context2.sent; if (!created) { _context2.next = 45; break; } _doc = { documentKey: key, storageProvider: storageProvider }; _context2.next = 45; return signAndSaveComments(annotManager, signaturesToSave, provider, ethAccount, contract, _doc, password, user, callback, errorCallback, formatMessage, handleSaveStatus, handleSavedAnnotations, handleNewChanges, 2, handleProgress); case 45: case "end": return _context2.stop(); } } }, _callee2, null, [[12, 24]]); })); return function createAndSign(_x, _x2, _x3, _x4, _x5, _x6, _x7, _x8, _x9, _x10, _x11, _x12, _x13, _x14, _x15, _x16, _x17, _x18) { return _ref.apply(this, arguments); }; }(); exports.createAndSign = createAndSign; var signAndSaveComments = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(annotManager, signaturesToSave, provider, ethAccount, contract, doc, password, user, callback, errorCallback, formatMessage, handleSaveStatus, handleSavedAnnotations, handleNewChanges, progress, handleProgress) { var annotSignatures, signedIndices, ec_signatures, signatures, signer, instance, annotsToDelete, xfdfStringAnnotations, commentsToSave, annotationsFile, encryptedAnnotations, newChanges; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: annotSignatures = []; // exporting and setting custom Data signedIndices = []; ec_signatures = []; signatures = []; signer = provider.getSigner(); instance = contract.connect(signer); if (!(signaturesToSave.length > 0)) { _context7.next = 22; break; } handleSaveStatus(formatMessage({ id: 'REQUESTING_SIGNATURES' })); _context7.prev = 8; _context7.next = 11; return Promise.all(signaturesToSave.map( /*#__PURE__*/function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(annot) { var hash, hashArray, ec_signature; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: annot.annot.setCustomData('Signer', annot.Signer); annot.annot.setCustomData('Index', annot.Index); signedIndices.push(annot.Index); annotSignatures.push(annot.annot); // const hash = await contract.hashDocumentKey(doc.ipfsHash + doc.metaHash + `${annot.Index}`); _context3.next = 6; return instance.hashSaltedAddressWithIndexMappingKeyAsSigner(doc.documentKey, annot.Index); case 6: hash = _context3.sent; hashArray = _ethers.ethers.utils.arrayify(hash); _context3.next = 10; return signer.signMessage(hashArray); case 10: ec_signature = _context3.sent; ec_signatures.push(ec_signature); signatures.push(annot.annot); case 13: case "end": return _context3.stop(); } } }, _callee3); })); return function (_x36) { return _ref4.apply(this, arguments); }; }())); case 11: handleProgress(progress + 1); _context7.next = 22; break; case 14: _context7.prev = 14; _context7.t0 = _context7["catch"](8); signaturesToSave.map( /*#__PURE__*/function () { var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(annot) { return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: annot.annot.setCustomData('Signer', null); annot.annot.setCustomData('Index', null); case 2: case "end": return _context4.stop(); } } }, _callee4); })); return function (_x37) { return _ref5.apply(this, arguments); }; }()); console.log(_context7.t0); handleSaveStatus(null); if (errorCallback) { errorCallback(); } (0, _dashboard.storeNotif)(formatMessage({ id: 'TRANSACTION_ERROR' }), formatMessage({ id: 'YOU_REJECTED_TRANSACTION' }), "danger"); return _context7.abrupt("return"); case 22: handleSaveStatus(formatMessage({ id: 'ENCRYPTING_ANNOTATIONS' })); // deleting those Ethsign Signature Fields (orange) widgets that are not signed annotsToDelete = annotManager.getAnnotationsList().filter(function (annot) { return (annot.Subject == "SignatureField" || annot.Subject == "Widget") && annot.Signer != null && annot.annot == null; }); _context7.next = 26; return annotManager.deleteAnnotations(annotsToDelete, null, true); case 26: _context7.next = 28; return annotManager.exportAnnotations(); case 28: xfdfStringAnnotations = _context7.sent; commentsToSave = _toConsumableArray(annotManager.getAnnotationsList()); _context7.next = 32; return annotManager.addAnnotations(annotsToDelete); case 32: _context7.next = 34; return annotManager.drawAnnotationsFromList(annotsToDelete); case 34: annotationsFile = new Blob([xfdfStringAnnotations], { type: 'text/plain' }); _context7.next = 37; return (0, _newdocument.getEncryptedStringFromFile)(annotationsFile, password); case 37: encryptedAnnotations = _context7.sent; _context7.next = 40; return (0, _newdocument.setDocumentCommentsForSigner)(provider, contract, ethAccount, doc.storageProvider, doc.documentKey, signedIndices, ec_signatures, encryptedAnnotations, formatMessage, callback, errorCallback, handleSaveStatus, signaturesToSave.length); case 40: if (!_context7.sent) { _context7.next = 49; break; } handleProgress(progress + 2); signatures.map( /*#__PURE__*/function () { var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(annot) { return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: annot.ReadOnly = true; case 1: case "end": return _context5.stop(); } } }, _callee5); })); return function (_x38) { return _ref6.apply(this, arguments); }; }()); handleSavedAnnotations(commentsToSave); newChanges = annotManager.getAnnotationsList().filter(function (annot) { return annot.Subject != "SignatureField" && annot.Subject != "Widget" && annot.Author == user; }); _.pullAll(newChanges, commentsToSave); handleNewChanges(newChanges.length > 0); _context7.next = 50; break; case 49: signatures.map( /*#__PURE__*/function () { var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(annot) { return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: annot.setCustomData('Signer', null); annot.setCustomData('Index', null); case 2: case "end": return _context6.stop(); } } }, _callee6); })); return function (_x39) { return _ref7.apply(this, arguments); }; }()); case 50: return _context7.abrupt("return"); case 51: case "end": return _context7.stop(); } } }, _callee7, null, [[8, 14]]); })); return function signAndSaveComments(_x20, _x21, _x22, _x23, _x24, _x25, _x26, _x27, _x28, _x29, _x30, _x31, _x32, _x33, _x34, _x35) { return _ref3.apply(this, arguments); }; }(); exports.signAndSaveComments = signAndSaveComments; var makeSignaturesReadOnly = function makeSignaturesReadOnly(signatures) { signatures.map(function (annot) { annot.ReadOnly = true; }); }; exports.makeSignaturesReadOnly = makeSignaturesReadOnly; var aggregateSetSigFieldAsSignerForDocument = /*#__PURE__*/function () { var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(ethAccount, contract, provider, documentKey, indices, r, s, v, handleSaveStatus, commentsLength, formatMessage) { var signer, instance, saltedAddressMappingKey, saltedMetaDocumentMappingKey, tx, networkId; return regeneratorRuntime.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.prev = 0; signer = provider.getSigner(); _context8.next = 4; return contract.connect(signer); case 4: instance = _context8.sent; _context8.next = 7; return contract.hashSaltedAddressMappingKey(documentKey, ethAccount); case 7: saltedAddressMappingKey = _context8.sent; _context8.next = 10; return contract.hashSaltedMetaDocumentMappingKey(documentKey); case 10: saltedMetaDocumentMappingKey = _context8.sent; _context8.next = 13; return instance.aggregateSetSigFieldAsSignerForDocument(documentKey, indices, saltedAddressMappingKey, saltedMetaDocumentMappingKey, r, s, v); case 13: tx = _context8.sent; handleSaveStatus(formatMessage({ id: 'WAITING_FOR_CONFIRMATIONS_FROM_NEWWORK' })); _context8.next = 17; return provider.getNetwork(); case 17: networkId = _context8.sent.chainId; _context8.next = 20; return tx.wait(networkId == 1287 ? 2 : 1).then(function (receipt) { console.log(receipt); if (commentsLength == 0) { handleSaveStatus(null); (0, _dashboard.storeNotif)(formatMessage({ id: 'SIGNATURE_APPLIED' }), formatMessage({ id: 'SUCCESSFULLY_SIGNED_CONTRACT' }), 'success'); } }); case 20: _context8.next = 26; break; case 22: _context8.prev = 22; _context8.t0 = _context8["catch"](0); console.log(_context8.t0); return _context8.abrupt("return", false); case 26: return _context8.abrupt("return", true); case 27: case "end": return _context8.stop(); } } }, _callee8, null, [[0, 22]]); })); return function aggregateSetSigFieldAsSignerForDocument(_x40, _x41, _x42, _x43, _x44, _x45, _x46, _x47, _x48, _x49, _x50) { return _ref8.apply(this, arguments); }; }(); // Signing a particular field in a specific version of the document exports.aggregateSetSigFieldAsSignerForDocument = aggregateSetSigFieldAsSignerForDocument; var signDocumentAtIndex = /*#__PURE__*/function () { var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(contract, provider, documentKey, doc_storage_id, metadata_storage_id, index, handleSubmitButton, formatMessage) { var signer, instance, ethAccount, hash, hashArray, ec_signature, tx, networkId; return regeneratorRuntime.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.prev = 0; signer = provider.getSigner(); _context9.next = 4; return contract.connect(signer); case 4: instance = _context9.sent; _context9.next = 7; return signer.getAddress(); case 7: ethAccount = _context9.sent; _context9.next = 10; return instance.hashSaltedAddressWithMappingKeyAsSigner(documentKey, index); case 10: hash = _context9.sent; hashArray = _ethers.ethers.utils.arrayify(hash); _context9.next = 14; return signer.signMessage(hashArray); case 14: ec_signature = _context9.sent; _context9.next = 17; return instance.aggregateSetSigFieldForDocument(documentKey, ethAccount, [index], ec_signature); case 17: tx = _context9.sent; if (handleSubmitButton) { handleSubmitButton(formatMessage({ id: 'WAITING_FOR_CONFIRMATIONS_FROM_NEWWORK' })); } _context9.next = 21; return provider.getNetwork(); case 21: networkId = _context9.sent.chainId; _context9.next = 24; return tx.wait(networkId == 1287 ? 2 : 1).then(function (receipt) { console.log(receipt); (0, _dashboard.storeNotif)(formatMessage({ id: 'SIGNATURE_APPLIED' }), formatMessage({ id: 'SUCCESSFULLY_SIGNED_CONTRACT' }), 'success'); }); case 24: _context9.next = 30; break; case 26: _context9.prev = 26; _context9.t0 = _context9["catch"](0); console.log(_context9.t0); return _context9.abrupt("return", false); case 30: return _context9.abrupt("return", true); case 31: case "end": return _context9.stop(); } } }, _callee9, null, [[0, 26]]); })); return function signDocumentAtIndex(_x51, _x52, _x53, _x54, _x55, _x56, _x57, _x58) { return _ref9.apply(this, arguments); }; }(); exports.signDocumentAtIndex = signDocumentAtIndex; var getDocumentSignatureAtIndex = /*#__PURE__*/function () { var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(contract, doc, index, signerAddress) { var legit, signatureArray, ec_signature, hash, hashArray; return regeneratorRuntime.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: _context10.prev = 0; _context10.next = 3; return contract.getDocumentECSignatureForSignerAtIndex(signerAddress, doc.documentKey, index); case 3: signatureArray = _context10.sent; ec_signature = _ethers.ethers.utils.joinSignature({ "r": signatureArray.r, "s": signatureArray.s, "v": signatureArray.v }); _context10.next = 7; return contract.hashDocumentKey(doc.ipfsHash + doc.metaHash + "".concat(index)); case 7: hash = _context10.sent; hashArray = _ethers.ethers.utils.arrayify(hash); legit = _ethers.ethers.utils.verifyMessage(hashArray, ec_signature); _context10.next = 16; break; case 12: _context10.prev = 12; _context10.t0 = _context10["catch"](0); console.log(_context10.t0); return _context10.abrupt("return", false); case 16: if (!(legit.localeCompare(signerAddress, undefined, { sensitivity: 'accent' }) === 0)) { _context10.next = 20; break; } return _context10.abrupt("return", true); case 20: return _context10.abrupt("return", false); case 21: case "end": return _context10.stop(); } } }, _callee10, null, [[0, 12]]); })); return function getDocumentSignatureAtIndex(_x59, _x60, _x61, _x62) { return _ref10.apply(this, arguments); }; }(); exports.getDocumentSignatureAtIndex = getDocumentSignatureAtIndex; var _default = { getDocumentSignatureAtIndex: getDocumentSignatureAtIndex, signDocumentAtIndex: signDocumentAtIndex, signAndSaveComments: signAndSaveComments, makeSignaturesReadOnly: makeSignaturesReadOnly }; exports.default = _default;