Skip to content
Snippets Groups Projects
Commit 55efae6e authored by Mariano Absatz (git)'s avatar Mariano Absatz (git)
Browse files

Agrego comentarios y aclaraciones. El código de la API en sí es idéntico

parent a7027dc8
No related branches found
No related tags found
No related merge requests found
import { sign } from "crypto"; import { sign } from "crypto";
//const stamperInterface = require('../../contract/build/contracts/Stamper.json')
class Stamper { class Stamper {
constructor(web3, contractAbi, contractAddress) { constructor(web3, contractAbi, contractAddress) {
this.web3 = web3 this.web3 = web3
...@@ -9,21 +7,23 @@ class Stamper { ...@@ -9,21 +7,23 @@ class Stamper {
this.contract = new web3.eth.Contract(contractAbi, contractAddress) this.contract = new web3.eth.Contract(contractAbi, contractAddress)
} }
// stampea un conjunto de hashes. // stampea un conjunto de objects (hashes) recibido como array
// si walletAccount es undefined trata de usar la account de web3.eth.defaultAccount // utiliza la cuenta walletAccount para enviar la transaccion
async stamp(hashes, walletAccount) { // (o defaultAccount si no se especifica)
console.log(`stamping ${hashes}`) async stamp(objects, walletAccount) {
console.log(`stamping ${objects}`)
// si walletAccount es undefined trata de usar la account de web3.eth.defaultAccount
let defaultAccount = (walletAccount) ? walletAccount.address : this.web3.eth.defaultAccount let defaultAccount = (walletAccount) ? walletAccount.address : this.web3.eth.defaultAccount
let hashesToStamp = [] let objectsToStamp = []
for (let i=0; i < hashes.length; i++) { for (let i=0; i < objects.length; i++) {
let blockNo = await this.contract.methods.getBlockNo(hashes[i], defaultAccount).call() let blockNo = await this.contract.methods.getBlockNo(objects[i], defaultAccount).call()
if (blockNo == 0) hashesToStamp.push(hashes[i]) if (blockNo == 0) objectsToStamp.push(objects[i])
} }
if (hashesToStamp.length == 0) return new Promise( (resolve) => { if (objectsToStamp.length == 0) return new Promise( (resolve) => {
console.log(`Los hashes enviados ya están stampeados`) console.log(`Los objects enviados ya están stampeados`)
resolve() resolve()
}) })
...@@ -31,7 +31,7 @@ class Stamper { ...@@ -31,7 +31,7 @@ class Stamper {
let gasLimit = 2000000 let gasLimit = 2000000
if (walletAccount) { if (walletAccount) {
let methodPut = this.contract.methods.put(hashesToStamp) let methodPut = this.contract.methods.put(objectsToStamp)
let encodedABI = methodPut.encodeABI() let encodedABI = methodPut.encodeABI()
let tx = { let tx = {
...@@ -52,15 +52,15 @@ class Stamper { ...@@ -52,15 +52,15 @@ class Stamper {
// txPromise = this.web3.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex')) // txPromise = this.web3.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex'))
txPromise = this.web3.eth.sendSignedTransaction(signedTx.rawTransaction) txPromise = this.web3.eth.sendSignedTransaction(signedTx.rawTransaction)
} else { } else {
txPromise = this.contract.methods.put(hashesToStamp).send({ txPromise = this.contract.methods.put(objectsToStamp).send({
from: defaultAccount, from: defaultAccount,
gasLimit: gasLimit gasLimit: gasLimit
}) })
} }
txPromise.then((receipt) => { txPromise.then((receipt) => {
console.log(`> hashes stampeados en bloque: ${receipt.blockNumber}`) console.log(`> objects stampeados en bloque: ${receipt.blockNumber}`)
console.log(hashesToStamp) console.log(objectsToStamp)
}).catch((error) => { }).catch((error) => {
console.error(error) console.error(error)
}) })
...@@ -77,34 +77,34 @@ class Stamper { ...@@ -77,34 +77,34 @@ class Stamper {
async wait1block() { async wait1block() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let max = 10; let max = 10;
let web3 = this.web3; let web3 = this.web3;
web3.eth.getBlockNumber() web3.eth.getBlockNumber()
.then( .then(
(startnum) => { (startnum) => {
setTimeout( setTimeout(
function nextblock() function nextblock()
{ {
web3.eth.getBlockNumber() web3.eth.getBlockNumber()
.then( .then(
(nownum) => { (nownum) => {
if ( nownum != startnum ) if ( nownum != startnum )
resolve( nownum ); resolve( nownum );
else else
if ( max-- > 0 ) if ( max-- > 0 )
setTimeout( nextblock, 500 ) setTimeout( nextblock, 500 )
else else
reject( 'Timeout. Tal vez no esta sincronizado el nodo local' ) reject( 'Timeout. Tal vez no esta sincronizado el nodo local' )
}, },
reject reject
) )
}, },
500 500
) )
}, },
(errtxt) => { reject('No conseguimos el blockNumber.\n'+errtxt) } (errtxt) => { reject('No conseguimos el blockNumber.\n'+errtxt) }
) )
}) })
} }
async verify(hash) { async verify(hash) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment