Skip to content

Commit

Permalink
adds ipfs support and refactors code
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroAsantos committed Apr 17, 2021
1 parent a3a22d7 commit 406cd36
Show file tree
Hide file tree
Showing 8 changed files with 12,086 additions and 4,150 deletions.
16,067 changes: 11,956 additions & 4,111 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"truffle": "5.3.2"
},
"dependencies": {
"@truffle/contract": "^4.3.13"
"@truffle/contract": "^4.3.13",
"ipfs": "^0.54.4"
}
}
18 changes: 18 additions & 0 deletions src/Auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Auth = (function() {
function checkPermissions(auth){
var authObj = JSON.parse(auth);
console.log(authObj);

return true;

}

function getClientURLPhoto(auth){
return JSON.parse(auth)["photoUrl"];
}

return {
checkPermissions: checkPermissions,
getClientURLPhoto: getClientURLPhoto
}
})();
32 changes: 32 additions & 0 deletions src/IpfsModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

const IpfsModule = (function(){
//const IPFS_ADDRESS = "https://ipfs.io/ipfs/";
const IPFS_ADDRESS = "http://localhost:8080/ipfs/";
function upload(elementId, callback) {
const reader = new FileReader();
reader.onloadend = function() {
const ipfs = window.IpfsApi('localhost', 5001) // Connect to IPFS
const buf = buffer.Buffer(reader.result) // Convert data into buffer
ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
if(err) {
console.error(err)
return
}
let url = IPFS_ADDRESS + result[0].hash
console.log(`Url --> ${url}`)
/*document.getElementById("url").innerHTML= url
document.getElementById("url").href= url
document.getElementById("output").src = url*/
callback(url);
})

}
const photo = document.getElementById(elementId);
reader.readAsArrayBuffer(photo.files[0]); // Read Provided File

}

return {
upload: upload
};
})();
9 changes: 9 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Utils = (function (){
function date2timestamp(myDate) {
return new Date(myDate).getTime()
}

return {
date2timestamp: date2timestamp
}
})();
74 changes: 37 additions & 37 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
App = {
providerAddress: '0x54D492CeE3bF82B735498b40a818834d9c786C5e',
clientAddress: '0x986E4874D0fFaB15480801db3BdDe64FE886bD9A',
contracts: {},

load: async () => {
Expand All @@ -16,7 +18,7 @@ App = {
App.contracts.LetMeIn = TruffleContract(contract)
const provider = new Web3.providers.HttpProvider("http://localhost:7545");
App.contracts.LetMeIn.setProvider(provider)
App.contracts.LetMeIn.defaults({ from: '0x1688d0d48C1E45C711f36B4fb03d3FA8975C4203' })
App.contracts.LetMeIn.defaults({ from: App.providerAddress })
App.contract = await App.contracts.LetMeIn.deployed()
},

Expand All @@ -25,54 +27,52 @@ App = {
console.log('providerCount', providerCount)
},

createAccess: async (address, expiry) => {
await App.contract.createAccess('0x76313a8170daA3C2B68517937c757Cb4505411c1', JSON.stringify({ expiry }))
createAccess: async (address, expiry, photoUrl) => {
console.log("asd: "+JSON.stringify({ expiry:expiry, photoUrl:photoUrl }))
await App.contract.createAccess(App.clientAddress, JSON.stringify({ expiry:expiry, photoUrl:photoUrl }))
console.log('Created access...')
await App.authorization()
},

authorization: async () => {
const authorization = await App.contract.authorization('0x76313a8170daA3C2B68517937c757Cb4505411c1')
console.log('authorization', authorization)
const authorization = await App.contract.authorization(App.clientAddress)
return authorization;
},
};

function date2timestamp(myDate) {
return new Date(myDate).getTime()
}

function upload() {
const reader = new FileReader();
reader.onloadend = function() {
const ipfs = window.IpfsApi('localhost', 5001) // Connect to IPFS
const buf = buffer.Buffer(reader.result) // Convert data into buffer
ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
if(err) {
console.error(err)
return
}
let url = `https://ipfs.io/ipfs/${result[0].hash}`
console.log(`Url --> ${url}`)
document.getElementById("url").innerHTML= url
document.getElementById("url").href= url
document.getElementById("output").src = url
(function() {
App.load()
if( document.getElementById("submit")) {
document.getElementById("submit").addEventListener('click', async () => {
const address = document.getElementById('address').value
const expiry = Utils.date2timestamp(document.getElementById('expiry').value)


if(expiry && address){
const photoUrl = IpfsModule.upload("photo", async (photoUrl)=> {
console.log("photoUrl: ", photoUrl);
await App.createAccess(address, expiry, photoUrl)
})

}
const photo = document.getElementById("photo");
reader.readAsArrayBuffer(photo.files[0]); // Read Provided File
}
else alert('invalid inputs')
})
}
if(document.getElementById("submitVerification")) {
document.getElementById("submitVerification").addEventListener('click', async () => {

(function() {
App.load()
var auth = await App.authorization();

if(Auth.checkPermissions(auth)){
var photoElement = document.getElementById("photo");
photo.src = Auth.getClientURLPhoto(auth);
photo.display = "block" ;
}else {
alert("User not Authorized");
}

document.getElementById("submit").addEventListener('click', async () => {
const address = document.getElementById('address').value
const expiry = date2timestamp(document.getElementById('expiry').value)
upload()
})
}

//if(expiry && address) await App.createAccess(address, expiry)
//else alert('invalid inputs')
})
const photo = document.getElementById("photo");
reader.readAsArrayBuffer(photo.files[0]);
})();
25 changes: 25 additions & 0 deletions src/authorize_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://wzrd.in/standalone/buffer"></script>
<script src="https://unpkg.com/[email protected]/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>
<title>Let Me In</title>
</head>
<body>
<label for="address">Client Address:</label>
<input type="text" id="address">
<button type="button" id="submitVerification">Verify</button>

<img style="display: none, width: 200, length: 400" id="photo" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="vendor/@truffle/contract/dist/truffle-contract.min.js"></script>
<script src="vendor/web3/dist/web3.min.js"></script>
<script src="IpfsModule.js"></script>
<script src="Auth.js"></script>
<script src="app.js"></script>
</body>
</html>
8 changes: 7 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://wzrd.in/standalone/buffer"></script>
<script src="https://unpkg.com/[email protected]/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>
<title>Let Me In</title>
</head>
<body>
Expand All @@ -12,10 +16,12 @@
<input type="date" id="expiry">
<input type="file" name="photo" id="photo">
<input type="submit" id="submit">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="vendor/@truffle/contract/dist/truffle-contract.min.js"></script>
<script src="vendor/web3/dist/web3.min.js"></script>
<script src="Utils.js"></script>
<script src="IpfsModule.js"></script>
<script src="app.js"></script>
</body>
</html>

0 comments on commit 406cd36

Please sign in to comment.