-
Notifications
You must be signed in to change notification settings - Fork 28
/
ProofOfExistence2.sol
61 lines (54 loc) · 1.23 KB
/
ProofOfExistence2.sol
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract ProofOfExistence2 {
// state
bytes32[] private proofs;
// store a proof of existence in the contract state
// *transactional function*
function storeProof(bytes32 proof)
public
{
proofs.push(proof);
}
// calculate and store the proof for a document
// *transactional function*
function notarize(string calldata document)
external
{
bytes32 proof = proofFor(document);
storeProof(proof);
}
// helper function to get a document's sha256
// *read-only function*
function proofFor(string memory document)
pure
public
returns (bytes32)
{
return sha256(abi.encodePacked(document));
}
// check if a document has been notarized
// *read-only function*
function checkDocument(string memory document)
public
view
returns (bool)
{
bytes32 proof = proofFor(document);
return hasProof(proof);
}
// returns true if proof is stored
// *read-only function*
function hasProof(bytes32 proof)
internal
view
returns (bool)
{
for (uint256 i = 0; i < proofs.length; i++) {
if (proofs[i] == proof) {
return true;
}
}
return false;
}
}