From c93ea4e6ef43dd6716652e9554259d0ae3ac417d Mon Sep 17 00:00:00 2001 From: Robert Martin-Legene <robert@nic.ar> Date: Mon, 16 Sep 2019 17:36:53 -0300 Subject: [PATCH] Whitespace --- api/src/index.js | 118 ++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/api/src/index.js b/api/src/index.js index f728ff9..598d3ce 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -1,21 +1,20 @@ import 'dotenv/config' -import Web3 from 'web3' -import express from 'express' -import cors from 'cors' -import bodyParser from 'body-parser' -import basicAuth from 'express-basic-auth' -import Stamper from './StamperWrapper' -import fs from 'fs' +import Web3 from 'web3' +import express from 'express' +import cors from 'cors' +import bodyParser from 'body-parser' +import basicAuth from 'express-basic-auth' +import Stamper from './StamperWrapper' +import fs from 'fs' /***************************************************/ // Conexión al provider /***************************************************/ -const providerHost = process.env.GETH_HOST || 'http://localhost:7545' -// const providerHost = process.env.GETH_HOST || 'http://localhost:8545' -const accountIsSet = process.env.GETH_ACCOUNT_JSON || false +const providerHost = process.env.GETH_HOST || 'http://localhost:8545' +const accountIsSet = process.env.GETH_ACCOUNT_JSON || false -var web3 = web3 +var web3 = web3 if (typeof web3 !== 'undefined') { console.log('Cargando web3 provider desde el entorno') // Use Mist/MetaMask's provider @@ -26,41 +25,41 @@ if (typeof web3 !== 'undefined') { setupWeb3() } -let contractAbi; -let contractAddress; -let walletAccount; +let contractAbi; +let contractAddress; +let walletAccount; async function setupWeb3() { try { - let netIsListening = await web3.eth.net.isListening() - let netId = await web3.eth.net.getId() + let netIsListening = await web3.eth.net.isListening() + let netId = await web3.eth.net.getId() if (accountIsSet) { - let rawKeyJsonV3 = fs.readFileSync(process.env.GETH_ACCOUNT_JSON) - let keyJsonV3 = JSON.parse(rawKeyJsonV3) - let key = web3.eth.accounts.decrypt(keyJsonV3, process.env.GETH_ACCOUNT_PASSWORD) - walletAccount = web3.eth.accounts.wallet.add(key) - web3.eth.defaultAccount = (await web3.eth.getAccounts())[0] + let rawKeyJsonV3 = fs.readFileSync(process.env.GETH_ACCOUNT_JSON) + let keyJsonV3 = JSON.parse(rawKeyJsonV3) + let key = web3.eth.accounts.decrypt(keyJsonV3, process.env.GETH_ACCOUNT_PASSWORD) + walletAccount = web3.eth.accounts.wallet.add(key) + web3.eth.defaultAccount = (await web3.eth.getAccounts())[0] } else { // se trata de utilizar una que haya abierta - web3.eth.defaultAccount = (await web3.eth.getAccounts())[0] + web3.eth.defaultAccount = (await web3.eth.getAccounts())[0] } /***************************************************/ // Carga de contrato /***************************************************/ if (process.env.CONTRACT_ABI_PATH) { - contractAbi = require(process.env.CONTRACT_ABI_PATH) - if (!process.env.CONTRACT_ADDRESS) { + 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 + contractAddress = process.env.CONTRACT_ADDRESS } else { - let path = '../../contract/build/contracts/Stamper.json' + let path = '../../contract/build/contracts/Stamper.json' console.log(`Intentando cargar ${path} netId: ${netId}`) - let data = require(path) - contractAbi = data.abi - contractAddress = data.networks[netId].address + let data = require(path) + contractAbi = data.abi + contractAddress = data.networks[netId].address } console.log(`Conectado exitosamente: @@ -79,17 +78,17 @@ async function setupWeb3() { /***************************************************/ // Setup CORS y Auth /***************************************************/ -const app = express() +const app = express() // USE_CORS=0 para disablear -const useCors = process.env.USE_CORS || 1 -if (useCors) app.use(cors()) - +const useCors = process.env.USE_CORS || 1 +if (useCors) + app.use(cors()) app.use(bodyParser.json()) -if (process.env.API_USER && process.env.API_PASS) { - var users = {} - users[process.env.API_USER] = process.env.API_PASS +if ( process.env.API_USER && process.env.API_PASS ) { + var users = {} + users[process.env.API_USER] = process.env.API_PASS app.use(basicAuth({ users: users, @@ -103,14 +102,14 @@ if (process.env.API_USER && process.env.API_PASS) { // API Endpoints /***************************************************/ app.get('/wait1block', async (req, res) => { - let ss = new Stamper(web3, contractAbi, contractAddress) + let ss = new Stamper(web3, contractAbi, contractAddress) try { - let blockno = await ss.wait1block() + let blockno = await ss.wait1block() return res.json( { - success: true, - blocknumber: blockno + success: true, + blocknumber: blockno } ) } catch (e) { @@ -120,32 +119,36 @@ app.get('/wait1block', async (req, res) => { } }) app.post('/stamp', async (req, res) => { - let ss = new Stamper(web3, contractAbi, contractAddress) + let ss = new Stamper(web3, contractAbi, contractAddress) - if (!("hashes" in req.body)) { + if ( ! ("hashes" in req.body) ) + { res.status(422) res.send('No se incluyó la clave hashes en el cuerpo del POST') return } - let hashes = req.body.hashes - if (! Array.isArray(hashes)) { + let hashes = req.body.hashes + if ( ! Array.isArray(hashes)) + { res.status(422) res.send('La clave hashes debe ser un array') return } - for (let i=0; i < hashes.length; i++) { - let hash = hashes[i] - if (! hash.startsWith('0x')) { - hash = '0x' + hash + for ( let i=0; i < hashes.length; i++ ) + { + let hash = hashes[i] + if ( ! hash.startsWith('0x') ) { + hash = '0x' + hash } - hashes[i] = hash + hashes[i] = hash } - try { - let txHash = await ss.stamp(hashes, walletAccount) - //let fullUrl = req.protocol + '://' + req.get('host') + try + { + let txHash = await ss.stamp(hashes, walletAccount) + //let fullUrl = req.protocol + '://' + req.get('host') res.status(200).send('success') } catch (e) { console.error(e) @@ -155,15 +158,14 @@ app.post('/stamp', async (req, res) => { }) app.get('/verify/:hash', async (req, res) => { - let ss = new Stamper(web3, contractAbi, contractAddress) + let ss = new Stamper(web3, contractAbi, contractAddress) - var value = req.params.hash - if (! value.startsWith('0x')) { + var value = req.params.hash + if ( ! value.startsWith('0x') ) value = '0x' + value - } try { - let info = await ss.verify(value) + let info = await ss.verify(value) return res.json(info) } catch (e) { console.error(e) @@ -186,7 +188,7 @@ app.get('/verify/:hash', async (req, res) => { // }) -let port = (process.env.PORT) ? process.env.PORT : 3000 +let port = (process.env.PORT) ? process.env.PORT : 3000 app.listen(port, () => console.log(`TSA Api corriendo en ${port}!`), -- GitLab