Repository navigation
Expand file tree
/
Copy pathReadTransaction.js
More file actions
75 lines (59 loc) · 3.76 KB
/
Copy pathReadTransaction.js
File metadata and controls
75 lines (59 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* File JavaScript per la lettura di transazioni su blockchain */
// includiamo il modulo web3; questo ci servirà per creare una connessione web3
const Web3 = require('web3')
// includiamo il modulo readline-sync che ci permette di gestire un processo sincrono (cioè inserire un input da tastiera)
var readlineSync = require('readline-sync');
//dichiariamo le variabili che andranno, corrispettivamente, ad ospitare l'indirizzo del nodo e l'indirizzo dell'account
var rpcURL
var account
//inseriamo un do while per non permettere ad utenti malevoli o sbadati di creare errori durante l'esecuzione delle operazioni
var s=0;
do {
// Specifichiamo con quale nodo (e quindi con quale account) vogliamo andare a leggere dalla blockchain
var response = readlineSync.question('PLEASE, ENTER THE NODE WITH WHICH YOU WANT TO READ FROM BLOCKCHAIN: ');
if(response!='1' && response!='2' && response!='3'){
console.log("WRONG ENTRY!");
s=1;
}
else{s=0}
}
while (s);
switch (response) {
case '1':
{
// NODO 1
rpcURL = "http://localhost:22000";
account = "0xed9d02e382b34818e88b88a309c7fe71e65f419d";
break;
}
case '2':
{
// NODO 2
rpcURL = "http://localhost:22001";
account = "0xca843569e3427144cead5e4d5999a3d0ccf92b8e";
break;
}
case '3':
{
// NODO 3
rpcURL = "http://localhost:22002";
account = "0x0fbdc686b912d7722dc86510934589e0aaf3b55a";
break;
}
}
// creiamo un'istanza di una connessione web3; questo ci servirà per dialogare con la blockchain
const web3 = new Web3(rpcURL)
// Collegamento al contratto
var abi = [{"constant":false,"inputs":[{"name":"_data_ora","type":"string"},{"name":"_hash_immagine","type":"string"},{"name":"_hash_misure","type":"string"},{"name":"_account_sender","type":"string"}],"name":"addFunction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"packCount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"image_hash","type":"string"}],"name":"getIDByImageHash","outputs":[{"name":"result","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"measures_hash","type":"string"}],"name":"getIDByMeasuresHash","outputs":[{"name":"result","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ID","type":"uint256"}],"name":"getAllByID","outputs":[{"name":"result","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"packs","outputs":[{"name":"_id","type":"uint256"},{"name":"_data_ora","type":"string"},{"name":"_hash_immagine","type":"string"},{"name":"_hash_misure","type":"string"},{"name":"_account_sender","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
// Inseriamo l'address del contratto utilizzando un processo sincrono di immissione di input da tastiera
var address = readlineSync.question('PLEASE, ENTER CONTRACT ADDRESS: ');
// Inseriamo l'ID per identificare a quali hash di immagine e misure si riferisce; quest'input viene fatto attraverso processo sincrono
var id = readlineSync.question('PLEASE, ENTER THE ID: ');
// creiamo l'oggetto contract; in questo modo possiamo interagire con lo smart contract come se fosse un oggetto JavaScript
const contract = new web3.eth.Contract(abi, address)
console.log('')
/*
LETTURA TRANSAZIONE
Chiamiamo la funzione getAllByID() contenuta nello smart contract; questo metodo ha come parametro l'indice della transazione che si vuole visualizzare.
*/
contract.methods.getAllByID(id).call((err, result) => {console.log(result)})