Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TSA NG
Manage
Activity
Members
Labels
Plan
Issues
2
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
blockchain
TSA NG
Commits
c93ea4e6
Commit
c93ea4e6
authored
5 years ago
by
Robert Martin-Legene
Browse files
Options
Downloads
Patches
Plain Diff
Whitespace
parent
f7758553
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api/src/index.js
+60
-58
60 additions, 58 deletions
api/src/index.js
with
60 additions
and
58 deletions
api/src/index.js
+
60
−
58
View file @
c93ea4e6
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
}
!`
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment