-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegalheir.sol
178 lines (142 loc) · 7.37 KB
/
legalheir.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
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
// SPDX-License-Identifier: MI
pragma solidity ^0.8.1;
// officer
// 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2
// 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db
contract LegalHeir{
// STEP 1 : DEATH CERTIFICATE
struct DeathCertificate {
// AID = Aadhar ID
string deceasedAID;
string applicantAID;
bool isNaturalDeath;
string firID;
string stationID;
string[] elibigleHeirs;
bool hasHeirApplication; // default: false
string[] heirAIDs;
bool hasLegalHeir;
}
DeathCertificate[] certificates;
mapping(string => DeathCertificate) deathCertificate;
function applyDeathCert(string memory _decAID, string memory _appAID, bool _naturalDeath, string memory _firID, string memory _stationID) public {
// verify if person already has deathCertificate
require(bytes(deathCertificate[_decAID].deceasedAID).length == 0, "death certificate already exists");
DeathCertificate memory cert;
cert.deceasedAID = _decAID;
cert.applicantAID = _appAID;
cert.isNaturalDeath = _naturalDeath;
if(_naturalDeath == false){
require(bytes(_firID).length != 0, "provide firID for unnatural death");
require(bytes(_stationID).length != 0, "provide stationID for unnatural death");
cert.firID = _firID;
cert.stationID = _stationID;
}
certificates.push(cert);
deathCertificate[_decAID] = certificates[certificates.length-1];
}
// helper for STEP 1
function viewCert(string memory _decAID) public view returns (string memory applicantAID, bool isNaturalDeath, string memory firID) {
return(deathCertificate[_decAID].applicantAID,deathCertificate[_decAID].isNaturalDeath,deathCertificate[_decAID].firID);
}
// STEP 2 : LIST OF ELIGIBLE PEOPLE
address[] officerAddresses = [0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db];
function isOfficer(address address1) private view returns(bool officer){
for(uint i = 0; i < officerAddresses.length; ++i){
if(officerAddresses[i] == address1){
return true;
}
}
return false;
}
string[] validRelations = ["mother", "father", "brother", "sister", "son", "daughter", "grandchild"];
function addHeirList(string memory _decAID, string[] memory _listHeirAID, string[] memory _listHeirRelation) public {
// check for authorized officer
require(isOfficer(msg.sender) == true, "address not authorized");
// officer should be able to add to list till legal heir not assigned
// check if legal heir assigned already
require(deathCertificate[_decAID].hasLegalHeir != true, "legal heir assigned already");
for (uint i = 0; i < _listHeirAID.length; i++) {
// avoid duplicates in eligibleHeirs
// iterate array or use mapping
bool alreadyExists = false;
for(uint j = 0; j < deathCertificate[_decAID].elibigleHeirs.length; j++){
if (keccak256(abi.encodePacked(deathCertificate[_decAID].elibigleHeirs[j])) == keccak256(abi.encodePacked(_listHeirAID[i]))) {
alreadyExists = true;
}
}
if(alreadyExists == false){
// check for valid relation
bool isValidRelation = false;
for(uint j=0; j<validRelations.length; j++){
if(keccak256(abi.encodePacked(_listHeirRelation[i])) == keccak256(abi.encodePacked(validRelations[j]))){
isValidRelation = true;
}
}
require(isValidRelation == true, "one or more legal heir do not have valid relation");
// check if legal heir not already deceased
require(bytes(deathCertificate[_listHeirAID[i]].deceasedAID).length == 0, "one or more legal heir already deceased");
deathCertificate[_decAID].elibigleHeirs.push(_listHeirAID[i]);
}
}
}
// helper for STEP 2
function viewEligibleList(string memory _decAID) public view returns(string[] memory listHeirAID){
return(deathCertificate[_decAID].elibigleHeirs);
}
// STEP 3 : APPLY LEGAL HEIR
function applyHeirApplication(string memory _decAID, string[] memory _heirAIDs) public {
// check if death certificate exists
require(bytes(deathCertificate[_decAID].deceasedAID).length != 0, "no death certificate");
// check if legal heir already assigned
require(deathCertificate[_decAID].hasLegalHeir != true, "legal heir already assigned");
// check if no one has applied before
require(deathCertificate[_decAID].hasHeirApplication != true, "legal heir application already under review");
// check if array not empty
require(bytes(_heirAIDs[0]).length != 0, "provide atleast one heir");
// check if all applying heirs are in the list
bool isValidApplication = true;
for(uint j = 0; j < _heirAIDs.length; j++){
bool isValidHeir = false;
for (uint i = 0; i < deathCertificate[_decAID].elibigleHeirs.length; i++) {
if (keccak256(abi.encodePacked(deathCertificate[_decAID].elibigleHeirs[i])) == keccak256(abi.encodePacked(_heirAIDs[j]))) {
isValidHeir = true;
}
}
if(isValidHeir == false){
isValidApplication = false;
}
}
require(isValidApplication == true, "one or more applicant(s) is not eligible for legal heir");
deathCertificate[_decAID].hasHeirApplication = true;
for(uint i = 0; i < _heirAIDs.length; i++){
deathCertificate[_decAID].heirAIDs.push(_heirAIDs[i]);
}
// is done in step4
// // update legalHeir in deathCertificate
// deathCertificate[_decAID].hasLegalHeir = true;
// deathCertificate[_decAID].heirAID = _heirAID;
}
// helper for STEP 3
function viewHeirApplication(string memory _decAID) public view returns (bool hasHeirApplication, bool hasLegalHeir, string[] memory heirAIDs){
return(deathCertificate[_decAID].hasHeirApplication, deathCertificate[_decAID].hasLegalHeir, deathCertificate[_decAID].heirAIDs);
}
// alloted heirAIDs
function viewHeirList(string memory _decAID) public view returns(string[] memory listHeirAID){
return(deathCertificate[_decAID].heirAIDs);
}
// STEP 4
function approveApplication(string memory _decAID) public {
// check for authorized officer
require(isOfficer(msg.sender) == true, "address not authorized");
deathCertificate[_decAID].hasLegalHeir = true;
}
function declineApplication(string memory _decAID) public {
// check for authorized officer
require(isOfficer(msg.sender) == true, "address not authorized");
deathCertificate[_decAID].hasHeirApplication = false;
for(uint i = 0; i < deathCertificate[_decAID].heirAIDs.length; i++){
deathCertificate[_decAID].heirAIDs.pop();
}
}
}