-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
198 lines (181 loc) · 5.73 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
let web3;
async function initWeb3() {
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
let acc= await window.ethereum.request({ method: 'eth_requestAccounts' });
document.getElementById('connected').innerHTML ="Connected Acoount :"+ acc[0];
console.log('Connected to Ethereum');
} catch (error) {
console.error('User denied account access');
}
} else {
console.error('Web3 provider not found. Please install MetaMask.');
}
}
// Connect to the contract
async function connectToContract() {
const contractAddress = "0xDfEa244f08Ae4d1744c0154F632DC5a209e6F341";
const abi = [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "documentHash",
"type": "string"
},
{
"indexed": false,
"internalType": "address",
"name": "signer",
"type": "address"
}
],
"name": "DocumentSigned",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "_documentHash",
"type": "string"
}
],
"name": "signDocument",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_documentHash",
"type": "string"
}
],
"name": "signPersonal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_documentHash",
"type": "string"
},
{
"internalType": "address",
"name": "_signer",
"type": "address"
}
],
"name": "uploadDocument",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_documentHash",
"type": "string"
}
],
"name": "isDocumentSigned",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
];
return new web3.eth.Contract(abi, contractAddress);
}
// Calculate the hash of the file
async function calculateFileHash(file) {
const buffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
async function upload() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const signerAddress = document.getElementById('signer').value;
const contract = await connectToContract();
if (!file) {
document.getElementById('status').innerText = 'Please select a file.';
return;
}
try {
const accounts = await web3.eth.getAccounts();
const hash = await calculateFileHash(file);
await contract.methods.uploadDocument(hash, signerAddress).send({ from: accounts[0] });
document.getElementById('status').innerText = 'Document uploaded successfully.';
} catch (error) {
console.error('Error uploading document:', error);
document.getElementById('status').innerText = 'Error uploading document.';
}
}
// Sign a document
async function signDocument() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const contract = await connectToContract();
try {
const accounts = await web3.eth.getAccounts();
const hash = await calculateFileHash(file);
await contract.methods.signDocument(hash).send({ from: accounts[0] });
document.getElementById('status').innerText = 'Document signed successfully.';
} catch (error) {
console.error('Error signing document:', error);
document.getElementById('status').innerText = 'Error signing document.';
}
}
async function selfsign() {
try {
const contract = await connectToContract();
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const accounts = await web3.eth.getAccounts();
const hash = await calculateFileHash(file);
await contract.methods.signDocument(hash).send({ from: accounts[0] });
document.getElementById('status').innerText = 'Document signed successfully.';
} catch (error) {
console.error('Error signing document:', error);
document.getElementById('status').innerText = 'Document not signed (error).';
}
}
// Check if a document is signed
async function verify() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const contract = await connectToContract();
try {
const hash = await calculateFileHash(file);
const isSigned = await contract.methods.isDocumentSigned(hash).call();
if (isSigned) {
document.getElementById('status').innerText = 'Document is signed.';
} else {
document.getElementById('status').innerText = 'Document is not signed.';
}
} catch (error) {
console.error('Error checking document signed:', error);
document.getElementById('status').innerText = 'Error checking document signed.';
}
}
window.onload = async function() {
await initWeb3();
};