@foreverrbum/ethsign
Version:
This package will allow you to electronically sign documents within your application
137 lines (131 loc) • 6.12 kB
JavaScript
import bs58 from 'bs58';
import { store } from 'react-notifications-component';
import CryptoJS from 'crypto-js';
const FLEEK_SECRET = "hu1j3Lr1jZbpLF1L96HgKtjbtQCvHgw+J+Ww4dwIXHI=";
const FLEEK_KEY = "9mvRajFjGL6fws3M2BHJvA==";
import fleekStorage from '@fleekhq/fleek-storage-js'
export const handleNewProposal = async(contract, ethAccount, web3, data, file, handleSubmitButton, documentKey, close, handleData, formatMessage) => {
const { fromAscii } = web3.utils;
const {
newProposal,
voteProposal,
} = contract.methods;
const proposalComment = fromAscii(data.comment);
// File read
handleSubmitButton(formatMessage({id: "READING_AND_PROCESSING_FILE"}))
const reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = async () => {
// File encryption
handleSubmitButton(formatMessage({id: "ENCRYPTING_FILE"}))
const buffer = Buffer(reader.result);
const encryptedString = CryptoJS.AES.encrypt(
JSON.stringify(buffer),
data.password
).toString();
// File upload
handleSubmitButton(formatMessage({id: "UPLOADING_TO_FLEEK_TAKE_A_WHILE"}));
const uploadedFile = await fleekStorage.upload({
apiKey: FLEEK_KEY,
apiSecret: FLEEK_SECRET,
key: documentKey,
data: encryptedString,
});
const bytes32 =
'0x' + bs58.decode(uploadedFile.hashV0).slice(2).toString('hex');
// Add to smart contract
handleSubmitButton(formatMessage({id: "INTERACTING_WITH_ETHSIGN_SMART_CONTRACT"}))
try {
await newProposal(documentKey, bytes32, proposalComment).call({
from: ethAccount,
});
newProposal(documentKey, bytes32, proposalComment)
.send({ from: ethAccount })
.once('transactionHash', (txHash) => {
store.addNotification({
title: 'Transaction information',
message: `The network has accepted your new proposal transaction. TxHash ${txHash}`,
type: 'info',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 10000,
onScreen: true,
},
});
handleData(documentKey);
})
.once('confirmation', async (_, receipt) => {
// store.addNotification({
// title: 'Transaction success',
// message:
// 'Your new proposal 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,
// },
// });
if (data.autoApprove) {
await voteProposal(documentKey, true).call({
from: ethAccount,
});
voteProposal(documentKey, true)
.send({ from: ethAccount })
.once('transactionHash', (txHash) => {
store.addNotification({
title: 'Transaction information',
message: `The network has accepted your voting transaction. TxHash ${txHash}`,
type: 'info',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 10000,
onScreen: true,
},
});
})
.once('confirmation', (_, receipt) => {
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;
}
});
close();
} catch (error) {
store.addNotification({
title: 'Smart contract error',
message: error,
type: 'danger',
insert: 'top',
container: 'bottom-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
});
handleSubmitButton(formatMessage({id: "SUBMIT"}))
return false;
}
};
}
export default { handleNewProposal }