Commit 3a34382b authored by farhan-shahid's avatar farhan-shahid
Browse files

refactoring

parent 68d6a34f
......@@ -67,10 +67,10 @@ Here we are configuring the Access control and port on which ipfs daemon would b
1. Have Ganache running: `ganache-cli -d -m "raven shock define wish brown cloth twin win weasel stable tone share"`
2. In MetaMask restore from seed pharase wih: raven shock define wish brown cloth twin win weasel stable tone share
3. In MetaMask click on top left and switch network to `Localhost 8545`
4. In a seperate console run `sh truffle-script.sh` to compile the smart contracts and migrate to test network
4. In a seperate console run `sh truffle_script.sh` to compile the smart contracts and migrate to test network
5. To run the server, in a new console type `yarn run start`
6. Go to **localhost:3000**
7. You can make changes to frontend without doing all these steps again, just have ganache-cli and your server running (server may need to be restarted for certain changes)
7. You can make changes to frontend without doing all these steps again, just have ganache-cli and your server running (server may need to be restarted for certain changes)
# Ganache Available Accounts
......
......@@ -41,4 +41,80 @@ contract Federation {
polices[_police] = true;
}
//modifier to check if an employer has been registered with the federation
modifier onlyEmployer(address _employer) {
bool found = false;
if (employers[_employer]) {
found = true;
}
require(found == true);
_;
}
modifier onlyBank(address _bank) {
bool found = false;
if(checkBank(_bank) == true)
found = true;
require(found == true);
_;
}
modifier onlyHousingAuthority(address _housingAuthority) {
bool found = false;
if(checkHousingAuthority(_housingAuthority) == true)
found = true;
require(found == true);
_;
}
modifier onlyInsuranceProvider(address _insuranceProvider) {
bool found = false;
if(checkInsuranceProvider(_insuranceProvider) == true)
found = true;
require(found == true);
_;
}
modifier onlyPolice(address _police) {
bool found = false;
if(checkPolice(_police) == true)
found = true;
require(found == true);
_;
}
function checkEmployer(address _employer) public view returns (bool) {
if (employers[_employer]) {
return true;
}
return false;
}
function checkBank(address _bank) public view returns (bool) {
if (banks[_bank]) {
return true;
}
return false;
}
function checkHousingAuthority(address _housingAuthority) public view returns (bool) {
if (housingAuthorities[_housingAuthority]) {
return true;
}
return false;
}
function checkInsuranceProvider(address _insuranceProvider) public view returns (bool) {
if (insuranceProviders[_insuranceProvider]) {
return true;
}
return false;
}
function checkPolice(address _police) public view returns (bool) {
if (polices[_police]) {
return true;
}
return false;
}
}
\ No newline at end of file
pragma solidity ^0.4.4;
import './ProofOfEmploymentHelper.sol';
import "./Federation.sol";
contract ProofOfEmployment is ProofOfEmploymentHelper {
contract ProofOfEmployment is Federation {
struct contractItem {
address landlord;
string ipfsFileHashId;
bool status;
string ipfsFileHashId;
bool status;
}
mapping(address => contractItem) public contracts;
......@@ -27,7 +26,7 @@ contract ProofOfEmployment is ProofOfEmploymentHelper {
//event triggered when landlords updates housing authority
event updateHA(address indexed housingAuthority, address indexed landlord, string landlordName, address indexed employee, string employeeName, string fileHash);
event verificationRequest(string encryptedEmployee, string encryptedRequirement, string persona);
event verificationRequest(string encryptedEmployee, string encryptedRequirement, string persona, address thirdParty);
event verificationResponse(string encryptedEmployee, string encryptedResponse);
......@@ -36,18 +35,22 @@ contract ProofOfEmployment is ProofOfEmploymentHelper {
federation = msg.sender;
}
function VerifyRequirement(string encryptedEmployee, string encryptedRequirement, string persona) public {
if (keccak256(persona)==keccak256("employer")){
} else if (keccak256(persona)==keccak256("bank")){
}zelse if (keccak256(persona)==keccak256("housingAuthority")){}
else if (keccak256(persona)==keccak256("insuranceProvider")){}
else if (keccak256(persona)==keccak256("police")){}
else{
}
function VerifyRequirement(string encryptedEmployee, string encryptedRequirement, string persona, address thirdParty) public {
if (compareStrings(persona, "employer")) {
require(checkEmployer(thirdParty));
} else if (compareStrings(persona, "bank")) {
require(checkBank(thirdParty));
} else if (compareStrings(persona, "housingAuthority")) {
require(checkHousingAuthority(thirdParty));
} else if (compareStrings(persona, "insuranceProvider")) {
require(checkInsuranceProvider(thirdParty));
} else if (compareStrings(persona, "police")) {
require(checkPolice(thirdParty));
} else {
revert();
}
emit verificationRequest(encryptedEmployee, encryptedRequirement, persona, thirdParty);
}
......@@ -77,15 +80,15 @@ contract ProofOfEmployment is ProofOfEmploymentHelper {
}
function compareStrings (string a, string b) pure returns (bool){
return keccak256(a) == keccak256(b);
}
function updateHousingAuthority(string _name, address _employeeAddress, string _employeeName, address _housingAuthority, string _fileHash) public {
require(housingAuthorities[_housingAuthority] == true);
require(contracts[_employeeAddress].status == true);
require(compareStrings(contracts[_employeeAddress].ipfsFileHashId, _fileHash));
require(contracts[_employeeAddress].landlord == msg.sender);
emit updateHA(_housingAuthority, msg.sender, _name, _employeeAddress, _employeeName, _fileHash);
}
return keccak256(a) == keccak256(b);
}
function updateHousingAuthority(string _name, address _employeeAddress, string _employeeName, address _housingAuthority, string _fileHash) public {
require(housingAuthorities[_housingAuthority] == true);
require(contracts[_employeeAddress].status == true);
require(compareStrings(contracts[_employeeAddress].ipfsFileHashId, _fileHash));
require(contracts[_employeeAddress].landlord == msg.sender);
emit updateHA(_housingAuthority, msg.sender, _name, _employeeAddress, _employeeName, _fileHash);
}
}
\ No newline at end of file
pragma solidity ^0.4.4;
import './Federation.sol';
contract ProofOfEmploymentHelper is Federation {
//modifier to check if an employer has been registered with the federation
modifier onlyEmployer(address _employer) {
bool found = false;
if (employers[_employer]) {
found = true;
}
require(found == true);
_;
}
modifier onlyBank(address _bank) {
bool found = false;
if(checkBank(_bank) == true)
found = true;
require(found == true);
_;
}
modifier onlyHousingAuthority(address _housingAuthority) {
bool found = false;
if(checkHousingAuthority(_housingAuthority) == true)
found = true;
require(found == true);
_;
}
modifier onlyInsuranceProvider(address _insuranceProvider) {
bool found = false;
if(checkInsuranceProvider(_insuranceProvider) == true)
found = true;
require(found == true);
_;
}
modifier onlyPolice(address _police) {
bool found = false;
if(checkPolice(_police) == true)
found = true;
require(found == true);
_;
}
function checkEmployer(address _employer) public view returns (bool) {
if (employers[_employer]) {
return true;
}
return false;
}
function checkBank(address _bank) public view returns (bool) {
if (banks[_bank]) {
return true;
}
return false;
}
function checkHousingAuthority(address _housingAuthority) public view returns (bool) {
if (housingAuthorities[_housingAuthority]) {
return true;
}
return false;
}
function checkInsuranceProvider(address _insuranceProvider) public view returns (bool) {
if (insuranceProviders[_insuranceProvider]) {
return true;
}
return false;
}
function checkPolice(address _police) public view returns (bool) {
if (polices[_police]) {
return true;
}
return false;
}
}
\ No newline at end of file
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
class AddBank extends Component {
constructor(props) {
......@@ -16,7 +15,7 @@ class AddBank extends Component {
onClick = (e) => {
//alert('Going to add this: ' + this.state.value);
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
var bankAddr = this.state.value;
......@@ -24,7 +23,8 @@ class AddBank extends Component {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
return instance.addBank(bankAddr, { from: account });
})
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
class AddEmployee extends Component {
constructor(props) {
......@@ -23,7 +22,7 @@ class AddEmployee extends Component {
}
onClick = (e) => {
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
var employeeAddr = this.state.address;
......@@ -34,7 +33,8 @@ class AddEmployee extends Component {
var scope = this;
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
return instance.registerEmployee(employeeAddr, parseInt(scope.state.minsalary, 10), parseInt(scope.state.maxsalary, 10), { from: account });
})
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import ProofOfEmployment from './../../../build/contracts/ProofOfEmployment.json';
class AddEmployer extends Component {
constructor(props) {
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
class AddHousingAuthority extends Component {
constructor(props) {
......@@ -15,7 +14,7 @@ class AddHousingAuthority extends Component {
}
onClick = (e) => {
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
var housingAuthorityAddr = this.state.value;
......@@ -23,7 +22,8 @@ class AddHousingAuthority extends Component {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
return instance.addHousingAuthority(housingAuthorityAddr, { from: account });
})
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
class AddInsuranceProvider extends Component {
constructor(props) {
......@@ -16,7 +15,7 @@ class AddInsuranceProvider extends Component {
onClick = (e) => {
//alert('Going to add this: ' + this.state.value);
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
var insuranceProviderAddr = this.state.value;
......@@ -24,7 +23,8 @@ class AddInsuranceProvider extends Component {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
return instance.addInsuranceProvider(insuranceProviderAddr, { from: account });
})
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
class AddPolice extends Component {
constructor(props) {
......@@ -16,7 +15,7 @@ class AddPolice extends Component {
onClick = (e) => {
//alert('Going to add this: ' + this.state.value);
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
var policeAddr = this.state.value;
......@@ -25,7 +24,8 @@ class AddPolice extends Component {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
return instance.addPolice(policeAddr, { from: account });
})
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Blockies from 'react-blockies';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { keyMappings } from './../../../const.js';
import { UploadContract } from '../../components/UploadContract/UploadContract.js';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import ProofOfEmployment from './../../../build/contracts/ProofOfEmployment.json';
import { keyMappings } from './../../const.js';
import { req } from './../../util/MyContracts.js';
const util = require('ethereumjs-util');
const ecies = require('eth-ecies');
......@@ -159,17 +157,18 @@ class Events extends Component {
// }
handleApprove = (landlord) => (e) => {
var val= new req(this.context.drizzle.web3);
var val = new req(this.context.drizzle.web3);
var account = '';
val.w3v.eth.getAccounts(function (error, result) {
val.w3v.eth.getAccounts(function(error, result) {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
let self = this;
val.myContract.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function (instance) {
val.myContract
.at(this.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
var pubKey = util.privateToPublic(keyMappings[landlord.toLowerCase()]);
var data = {
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProofOfEmployment from './../../../../build/contracts/ProofOfEmployment.json';
import { req } from '../../components/ReusableComponents/MyContracts.js';
import { req } from './../../util/MyContracts.js';
const ipfsAPI = require('ipfs-api');
const pdfjsLib = require('pdfjs-dist');
......@@ -11,7 +10,7 @@ class UploadContract extends Component {
super(props);
this.state = {
added_file_hash: null,
value : ''
value: ''
};
this.ipfsApi = ipfsAPI('localhost', '4432');
......@@ -49,7 +48,6 @@ class UploadContract extends Component {
console.log(ipfsId);
this.setState({ added_file_hash: ipfsId });
//this.getFileContent();
})
.catch((err) => {
console.error(err);
......@@ -58,7 +56,7 @@ class UploadContract extends Component {
getFileContent() {
const self = this;
this.ipfsApi.cat(this.state.added_file_hash, function (err, file) {
this.ipfsApi.cat(this.state.added_file_hash, function(err, file) {
if (err) {
throw err;
}
......@@ -71,31 +69,30 @@ class UploadContract extends Component {
onClick = (e) => {
const self = this;
var val= new req(this.context.drizzle.web3);
var account = '';
var employeeAddr = self.state.value;
var ipfsId = self.state.added_file_hash;
console.log(employeeAddr);
console.log(ipfsId);
val.w3v.eth.getAccounts(function (error, result) {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract.at(self.context.drizzle.contracts.ProofOfEmployment.address)
.then(function (instance) {
console.log("Sending filehash to contract")
return instance.uploadContract(employeeAddr, ipfsId, { from: account });
})
.then(function (result) {
alert('Contract Uploaded Successfully! Transaction Hash: ' + result.tx + '\nIpfs File Hash: '+ ipfsId);
console.log(result);
})
.catch(function (err) {
console.log(err.message);
});
}
var val = new req(this.context.drizzle.web3);
var account = '';
var employeeAddr = self.state.value;
var ipfsId = self.state.added_file_hash;
console.log(employeeAddr);
console.log(ipfsId);
val.w3v.eth.getAccounts(function(error, result) {
if (error != null) console.log("Couldn't get accounts");
account = result[0];
});
val.myContract
.at(self.context.drizzle.contracts.ProofOfEmployment.address)
.then(function(instance) {
console.log('Sending filehash to contract');
return instance.uploadContract(employeeAddr, ipfsId, { from: account });
})
.then(function(result) {
alert('Contract Uploaded Successfully! Transaction Hash: ' + result.tx + '\nIpfs File Hash: ' + ipfsId);
console.log(result);
})
.catch(function(err) {
console.log(err.message);
});
};
handleSubmit(event) {
event.preventDefault();
......@@ -125,12 +122,12 @@ class UploadContract extends Component {
// Using DocumentInitParameters object to load binary data.
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
loadingTask.promise.then(
function (pdf) {
function(pdf) {
console.log('PDF loaded');
// Fetch the first page
var pageNumber = 1;
pdf.getPage(pageNumber).then(function (page) {
pdf.getPage(pageNumber).then(function(page) {
console.log('Page loaded');
var scale = 1.5;
......@@ -148,12 +145,12 @@ class UploadContract extends Component {
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.then(function () {
renderTask.then(function() {
console.log('Page rendered');
});
});
},
function (reason) {
function(reason) {
// PDF loading error
console.error(reason);
}
......@@ -166,7 +163,7 @@ class UploadContract extends Component {
<form id="captureMedia" onSubmit={this.handleSubmit}>
<input type="file" onChange={this.captureFile} />
</form>
<input type="