File size: 1,739 Bytes
74f2b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract RustVitalAudit {
    struct Record {
        bytes32 cidHash;
        bytes32 proofHash;
        uint256 attestations;
        address submitter;
        uint256 timestamp;
    }

    address public owner;
    mapping(bytes32 => Record) public records;
    mapping(bytes32 => mapping(address => bool)) public attested;

    event RecordAnchored(bytes32 indexed recordId, bytes32 cidHash, bytes32 proofHash, uint256 attestations, address submitter);
    event Attested(bytes32 indexed recordId, address indexed hospital, bytes32 attestationHash);

    modifier onlyOwner() {
        require(msg.sender == owner, "not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function anchorRecord(bytes32 recordId, bytes32 cidHash, bytes32 proofHash) external {
        require(records[recordId].timestamp == 0, "already anchored");
        records[recordId] = Record({
            cidHash: cidHash,
            proofHash: proofHash,
            attestations: 0,
            submitter: msg.sender,
            timestamp: block.timestamp
        });
        emit RecordAnchored(recordId, cidHash, proofHash, 0, msg.sender);
    }

    function attest(bytes32 recordId) external {
        Record storage record = records[recordId];
        require(record.timestamp != 0, "unknown record");
        require(!attested[recordId][msg.sender], "already attested");
        attested[recordId][msg.sender] = true;
        record.attestations += 1;
        bytes32 attestationHash = keccak256(abi.encodePacked(recordId, msg.sender, record.cidHash, record.proofHash, block.timestamp));
        emit Attested(recordId, msg.sender, attestationHash);
    }
}