Cambios API

parent 005b50d1
847e7d6ea18a417496518dc90b547438bf1b3d05.json
api/nohup.out
www
......@@ -75,6 +75,38 @@ class Stamper {
})
}
async wait1block() {
return new Promise((resolve, reject) => {
let max = 10;
let web3 = this.web3;
web3.eth.getBlockNumber()
.then(
(startnum) => {
setTimeout(
function nextblock()
{
web3.eth.getBlockNumber()
.then(
(nownum) => {
if ( nownum != startnum )
resolve( nownum );
else
if ( max-- > 0 )
setTimeout( nextblock, 500 )
else
reject( 'Timeout. Tal vez no esta sincronizado el nodo local' )
},
reject
)
},
500
)
},
(errtxt) => { reject('No conseguimos el blockNumber.\n'+errtxt) }
)
})
}
async verify(hash) {
try {
let count = await this.contract.methods.getObjectCount(hash).call()
......@@ -85,10 +117,16 @@ class Stamper {
var stamps = []
for (var i = 0; i < count; i++) {
let stampPos = await this.contract.methods.getObjectPos(hash, i).call()
let stamp = await this.contract.methods.getStamplistPos(stampPos).call()
stamps.push({ stamper: stamp[1], block: stamp[2].toString() })
let stampPos = await this.contract.methods.getObjectPos(hash, i).call()
let stamp = await this.contract.methods.getStamplistPos(stampPos).call()
let whostamped = stamp[1];
let blockno = stamp[2];
let block = await this.web3.eth.getBlock( blockno );
stamps.push({
whostamped: whostamped,
blocknumber: blockno.toString(),
blocktimestamp: block.timestamp
});
}
console.log(`exito verificación ${hash}`)
......@@ -100,4 +138,4 @@ class Stamper {
}
}
export default Stamper
\ No newline at end of file
export default Stamper
......@@ -102,6 +102,23 @@ if (process.env.API_USER && process.env.API_PASS) {
/***************************************************/
// API Endpoints
/***************************************************/
app.get('/wait1block', async (req, res) => {
let ss = new Stamper(web3, contractAbi, contractAddress)
try {
let blockno = await ss.wait1block()
return res.json(
{
success: true,
blocknumber: blockno
}
)
} catch (e) {
console.error( e )
res.status( 500 )
res.send( e )
}
})
app.post('/stamp', async (req, res) => {
let ss = new Stamper(web3, contractAbi, contractAddress)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment