@foreverrbum/ethsign
Version:
This package will allow you to electronically sign documents within your application
208 lines (180 loc) • 9.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleNewProposal = exports.default = void 0;
var _bs = _interopRequireDefault(require("bs58"));
var _reactNotificationsComponent = require("react-notifications-component");
var _cryptoJs = _interopRequireDefault(require("crypto-js"));
var _fleekStorageJs = _interopRequireDefault(require("@fleekhq/fleek-storage-js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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); }); }; }
var FLEEK_SECRET = "hu1j3Lr1jZbpLF1L96HgKtjbtQCvHgw+J+Ww4dwIXHI=";
var FLEEK_KEY = "9mvRajFjGL6fws3M2BHJvA==";
var handleNewProposal = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(contract, ethAccount, web3, data, file, handleSubmitButton, documentKey, close, handleData, formatMessage) {
var fromAscii, _contract$methods, newProposal, voteProposal, proposalComment, reader;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
fromAscii = web3.utils.fromAscii;
_contract$methods = contract.methods, newProposal = _contract$methods.newProposal, voteProposal = _contract$methods.voteProposal;
proposalComment = fromAscii(data.comment); // File read
handleSubmitButton(formatMessage({
id: "READING_AND_PROCESSING_FILE"
}));
reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var buffer, encryptedString, uploadedFile, bytes32;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
// File encryption
handleSubmitButton(formatMessage({
id: "ENCRYPTING_FILE"
}));
buffer = Buffer(reader.result);
encryptedString = _cryptoJs.default.AES.encrypt(JSON.stringify(buffer), data.password).toString(); // File upload
handleSubmitButton(formatMessage({
id: "UPLOADING_TO_FLEEK_TAKE_A_WHILE"
}));
_context2.next = 6;
return _fleekStorageJs.default.upload({
apiKey: FLEEK_KEY,
apiSecret: FLEEK_SECRET,
key: documentKey,
data: encryptedString
});
case 6:
uploadedFile = _context2.sent;
bytes32 = '0x' + _bs.default.decode(uploadedFile.hashV0).slice(2).toString('hex'); // Add to smart contract
handleSubmitButton(formatMessage({
id: "INTERACTING_WITH_ETHSIGN_SMART_CONTRACT"
}));
_context2.prev = 9;
_context2.next = 12;
return newProposal(documentKey, bytes32, proposalComment).call({
from: ethAccount
});
case 12:
newProposal(documentKey, bytes32, proposalComment).send({
from: ethAccount
}).once('transactionHash', function (txHash) {
_reactNotificationsComponent.store.addNotification({
title: 'Transaction information',
message: "The network has accepted your new proposal transaction. TxHash ".concat(txHash),
type: 'info',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 10000,
onScreen: true
}
});
handleData(documentKey);
}).once('confirmation', /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_, receipt) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!data.autoApprove) {
_context.next = 5;
break;
}
_context.next = 3;
return voteProposal(documentKey, true).call({
from: ethAccount
});
case 3:
voteProposal(documentKey, true).send({
from: ethAccount
}).once('transactionHash', function (txHash) {
_reactNotificationsComponent.store.addNotification({
title: 'Transaction information',
message: "The network has accepted your voting transaction. TxHash ".concat(txHash),
type: 'info',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 10000,
onScreen: true
}
});
}).once('confirmation', function (_, receipt) {
_reactNotificationsComponent.store.addNotification({
title: 'Transaction success',
message: 'Your voting transaction has been confirmed by the network!',
type: 'success',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 10000,
onScreen: true
}
});
});
return _context.abrupt("return");
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x11, _x12) {
return _ref3.apply(this, arguments);
};
}());
close();
_context2.next = 21;
break;
case 16:
_context2.prev = 16;
_context2.t0 = _context2["catch"](9);
_reactNotificationsComponent.store.addNotification({
title: 'Smart contract error',
message: _context2.t0,
type: 'danger',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut']
});
handleSubmitButton(formatMessage({
id: "SUBMIT"
}));
return _context2.abrupt("return", false);
case 21:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[9, 16]]);
}));
case 7:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return function handleNewProposal(_x, _x2, _x3, _x4, _x5, _x6, _x7, _x8, _x9, _x10) {
return _ref.apply(this, arguments);
};
}();
exports.handleNewProposal = handleNewProposal;
var _default = {
handleNewProposal: handleNewProposal
};
exports.default = _default;