From f8b9767b6b8f3281b0a432851d2a773ef14c6b2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9s=20Blanco?= <ablanco@siu.edu.ar>
Date: Wed, 8 May 2019 15:12:55 -0300
Subject: [PATCH] =?UTF-8?q?Mejoras=20en=20parametrizaci=C3=B3n=20de=20cont?=
 =?UTF-8?q?rato?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 api/src/StamperWrapper.js | 12 +++++++-----
 api/src/index.js          | 29 +++++++++++++++++++++++++----
 ui/.env                   |  1 -
 3 files changed, 32 insertions(+), 10 deletions(-)
 delete mode 100644 ui/.env

diff --git a/api/src/StamperWrapper.js b/api/src/StamperWrapper.js
index a5b2cda..1d1e2f6 100644
--- a/api/src/StamperWrapper.js
+++ b/api/src/StamperWrapper.js
@@ -1,10 +1,9 @@
-const stamperInterface = require('../../contract/build/contracts/Stamper.json')
+//const stamperInterface = require('../../contract/build/contracts/Stamper.json')
 
 class Stamper {
-    constructor(web3, netId) {
+    constructor(web3, contractAbi, contractAddress) {
         this.web3 = web3
-        let address = stamperInterface.networks[netId].address
-        this.contract = new web3.eth.Contract(stamperInterface.abi, address)
+        this.contract = new web3.eth.Contract(contractAbi, contractAddress)
     }
 
     setSender(fromAddress) {
@@ -32,7 +31,8 @@ class Stamper {
         })
 
         txPromise.then((receipt) => {
-            console.log(`${hash}: stamped (bloque: ${receipt.blockNumber})`)
+            console.log(`> hashes stampeados en bloque: ${receipt.blockNumber}`)
+            console.log(hashesToStamp)
         }).catch((error) => {
             console.error(error)
         })
@@ -51,6 +51,7 @@ class Stamper {
         try {
             let count = await this.contract.methods.getObjectCount(hash).call()
             if (count == 0) {
+                console.log(`fallo verificación ${hash}`)
                 return { stamped: false, stamps: [] }
             }
 
@@ -62,6 +63,7 @@ class Stamper {
                 stamps.push({ stamper: stamp[1], block: stamp[2].toString() })
             }
 
+            console.log(`exito verificación ${hash}`)
             return { stamped: true, stamps: stamps }
         } catch (e) {
             console.error(e)
diff --git a/api/src/index.js b/api/src/index.js
index 9535319..ce3adc7 100644
--- a/api/src/index.js
+++ b/api/src/index.js
@@ -16,6 +16,7 @@ const accountIsSet = process.env.GETH_ACCOUNT || false
 // si no se seteó una account se usa esta const como el indice de accounts de ganache
 const account = (accountIsSet) ? process.env.GETH_ACCOUNT : 1
 
+
 var web3 = web3
 if (typeof web3 !== 'undefined') {
     console.log('Cargando web3 provider desde el entorno')
@@ -45,6 +46,28 @@ async function setupWeb3() {
     }
 }
 
+/***************************************************/
+// Carga de contrato
+/***************************************************/
+let contractAbi;
+let contractAddress;
+
+if (process.env.CONTRACT_ABI_PATH) {
+    contractAbi = require(process.env.CONTRACT_ABI_PATH)
+    if (!process.env.CONTRACT_ADDRESS) {
+        console.error('Si se especifica el path de un abi, debe proveerse un address con la env CONTRACT_ADDRESS')
+        process.exit(1)
+    }
+    contractAddress = process.env.CONTRACT_ADDRESS
+} else {
+    let path = '../../contract/build/contracts/Stamper.json'
+    console.log(`Intentando cargar ${path}`)
+    let data = require(path)
+    contractAbi = data.abi
+    web3.eth.net.getId().then(function(netId) {
+        contractAddress = data.networks[netId].address
+    })
+}
 /***************************************************/
 // Setup API
 /***************************************************/
@@ -72,8 +95,7 @@ if (process.env.API_USER && process.env.API_PASS) {
 // API Endpoints
 /***************************************************/
 app.post('/stamp', async (req, res) => {
-    let netId = await web3.eth.net.getId()
-    let ss = new Stamper(web3, netId)
+    let ss = new Stamper(web3, contractAbi, contractAddress)
     ss.setSender(web3.eth.defaultAccount)
 
     if (!("hashes" in req.body)) {
@@ -109,8 +131,7 @@ app.post('/stamp', async (req, res) => {
 })
 
 app.get('/verify/:hash', async (req, res) => {
-    let netId = await web3.eth.net.getId()
-    let ss = new Stamper(web3, netId)
+    let ss = new Stamper(web3, contractAbi, contractAddress)
     ss.setSender(web3.eth.defaultAccount)
 
     var value = req.params.hash
diff --git a/ui/.env b/ui/.env
deleted file mode 100644
index a4b630a..0000000
--- a/ui/.env
+++ /dev/null
@@ -1 +0,0 @@
-VUE_APP_API_URL=http://localhost:3000
\ No newline at end of file
-- 
GitLab