From b267446aac3f15e110458be1efd29b1c134dc5e3 Mon Sep 17 00:00:00 2001
From: Renzo <rontivero@ultimamillasa.com.ar>
Date: Tue, 28 Apr 2020 09:31:14 -0300
Subject: [PATCH] Avances Renzo

---
 api/controllers/BlockchainController.js | 14 ++++++--
 api/helpers/get-block.js                | 47 +++++++++++++++++++++++++
 config/custom.js                        | 15 ++++++--
 config/security.js                      | 15 ++++----
 4 files changed, 80 insertions(+), 11 deletions(-)
 create mode 100755 api/helpers/get-block.js

diff --git a/api/controllers/BlockchainController.js b/api/controllers/BlockchainController.js
index 54ca6aa..35f5023 100755
--- a/api/controllers/BlockchainController.js
+++ b/api/controllers/BlockchainController.js
@@ -21,7 +21,7 @@ const contract = new web3.eth.Contract(contractABI, contractAddress);
 module.exports = {
   
   verify : async function (req, res){  
-	  	
+	  	sails.log("Verify("+req.params.ots+", "+req.params.file_hash+")");
 	  	const file_hash = req.params.file_hash;
 	  	const base64_ots = req.params.ots;
 
@@ -53,6 +53,7 @@ module.exports = {
 					tx_hash : tx_hash,					
 					file_hash : file_hash,
 					ots : ots,
+					msg : 'La Transacción aún no es incluida en un Bloque. Intente nuevamente en unos minutos.'
 				});			
 			}
 
@@ -66,6 +67,9 @@ module.exports = {
 			if(result_verify){
 
 				const block_number = await sails.helpers.getBlockNumber(ots);
+
+				// Tengo que obtener el bloque entero, para sacar su timestamp
+				const block = await sails.helpers.getBlock(block_number);
 				
 				return res.json({
 					status : 'success',
@@ -73,14 +77,17 @@ module.exports = {
 					block_number : block_number,
 					file_hash : file_hash,
 					ots : ots,
+					contract_address: contractAddress,
+					timestamp : block.timestamp,
+					block_hash : block.hash
 				});
 			} else {
 							
-				var new_tx_hash = await sails.helpers.getHash(ots);
+				var file_hash_by_ots = await sails.helpers.getHash(ots);
 				
 				return res.json({
 					status : 'fail',
-					file_hash_by_ots : new_tx_hash,
+					file_hash_by_ots : file_hash_by_ots,
 					file_hash_send : file_hash,
 					tx_hash : tx_hash,					
 					ots : ots,
@@ -95,6 +102,7 @@ module.exports = {
   },
 
   stamp : async function (req, res){
+  		sails.log("Stamp("+req.body.file_hash+")");
 	  	const file_hash = req.body.file_hash;
 	  	// A partir del Hash recibido, genero el OpenTimeStamp (OTS)
 	  	const ots = await sails.helpers.getOts(file_hash);
diff --git a/api/helpers/get-block.js b/api/helpers/get-block.js
new file mode 100755
index 0000000..b821ed4
--- /dev/null
+++ b/api/helpers/get-block.js
@@ -0,0 +1,47 @@
+const Web3 = require('web3');
+const url = sails.config.custom.urlRpc;
+const web3 = new Web3(url);
+
+module.exports = {
+
+
+  friendlyName: 'Get block',
+
+
+  description: '',
+
+
+  inputs: {
+    block_number : {
+      type: 'number',
+      require: true,
+    },
+  },
+
+
+  exits: {
+
+    success: {
+      outputFriendlyName: 'Block',
+    },
+
+  },
+
+
+  fn: async function (inputs) {
+
+    var result = await web3.eth.getBlock(inputs.block_number, (err, result) => {
+      if(err){
+        return err.toString();
+      }
+
+      return result;
+    });
+
+    return result;
+
+  }
+
+
+};
+
diff --git a/config/custom.js b/config/custom.js
index 7cc2791..d168a0e 100755
--- a/config/custom.js
+++ b/config/custom.js
@@ -19,10 +19,21 @@ module.exports.custom = {
   // mailgunSecret: 'key-testkeyb183848139913858e8abd9a3',
   // stripeSecret: 'sk_test_Zzd814nldl91104qor5911gjald',
   // …
+
+  //Datos para Ropsten 
+  urlRpc: 'https://ropsten.infura.io/v3/a59f70f1e62e4db4babb69284e37672f',
+  accountAddress : '0x59060CF376DeB6729da45C729EbecC171C2c16b9',
+  contractABI : [{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getHash","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"verify","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"selfDestroy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"stamp","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"hash","type":"string"},{"indexed":true,"name":"ots","type":"string"}],"name":"Stamped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"}],"name":"Deploy","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"}],"name":"SelfDestroy","type":"event"}],
+  contractAddress: '0x96a716f91da961639f685100b12d13ef30ba89dc',
+  privateKey: 'D51E9F04E45381E1CC3C450CB296E73BBD0BDFCA7EE22FF2533D71631B9F536C',  
+  
+
+
+  /* Datos para localhost
   urlRpc: 'http://localhost:8545',
   accountAddress : '0xDBF0C927F9E92dFE7C31e045e0Ba1067Ee205f73',
   contractABI : [{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getHash","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"verify","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"selfDestroy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"stamp","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"accountAddress"},{"indexed":true,"name":"hash","type":"string"},{"indexed":true,"name":"ots","type":"string"}],"name":"Stamped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"accountAddress"}],"name":"Deploy","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"accountAddress"}],"name":"SelfDestroy","type":"event"}],
   contractAddress: '0xBD89a34041190439d43ec391486819eF5CBfBDBe',
-  privateKey: '6671485e4250881473c639465464148b5a0285461f136b585623333f22f7ca3f',  
-
+  privateKey: '6671485e4250881473c639465464148b5a0285461f136b585623333f22f7ca3f', 
+  */
 };
diff --git a/config/security.js b/config/security.js
index bd57631..1cc2d93 100755
--- a/config/security.js
+++ b/config/security.js
@@ -28,11 +28,14 @@ module.exports.security = {
   *                                                                          *
   ***************************************************************************/
 
-  // cors: {
-  //   allRoutes: false,
-  //   allowOrigins: '*',
-  //   allowCredentials: false,
-  // },
+   cors: {
+     allRoutes: true,
+    allowOrigins: '*',
+    allowCredentials: false,
+    allowRequestHeaders: 'content-type,accept,authorization',
+    allowRequestMethods: 'GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD',
+    allowAnyOriginWithCredentialsUnsafe: true
+   },
 
 
   /****************************************************************************
@@ -49,6 +52,6 @@ module.exports.security = {
   *                                                                           *
   ****************************************************************************/
 
-  // csrf: false
+  csrf: false
 
 };
-- 
GitLab