forked from chainstacklabs/quorum-iot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate.js
97 lines (81 loc) · 2.48 KB
/
private.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { compileContract } = require('./utils/compiler.js');
const {
node1,
node2,
node3,
} = require('./utils/environment.js');
let temperatureMonitor = {};
const main = async () => {
const {interface, bytecode} = compileContract('temperatureMonitor.sol');
temperatureMonitor = {
interface,
bytecode,
};
const contractAddress = await deployContract({
node: node1,
privateFor: [node2.TM_PK],
});
console.log(`Contract address after deployment: ${contractAddress}`);
const unauthorizedStatus = await setTemperature({
contractAddress,
node: node3,
privateFor: [node1.TM_PK],
temp: 3,
});
console.log(`Unauthorized - Transaction status: ${unauthorizedStatus}`);
const unauthorizedTemp = await getTemperature({
contractAddress,
node: node3,
});
console.log('Unauthorized - Retrieved contract Temperature', unauthorizedTemp);
const authorizedStatus = await setTemperature({
contractAddress,
node: node2,
privateFor: [node1.TM_PK],
temp: 18,
});
console.log(`Authorized - Transaction status: ${authorizedStatus}`);
const authorizedTemp = await getTemperature({
node: node2,
contractAddress,
});
console.log('Authorized - Retrieved contract Temperature', authorizedTemp);
}
function getContract(web3, contractAddress) {
return new web3.eth.Contract(temperatureMonitor.interface, contractAddress);
}
async function deployContract({ node, privateFor }) {
await node.web3.eth.personal.unlockAccount(node.WALLET_ADDRESS, '', 1000);
const contract = new node.web3.eth.Contract(temperatureMonitor.interface);
return contract.deploy({
data: temperatureMonitor.bytecode,
})
.send({
from: node.WALLET_ADDRESS,
gasPrice: 0,
gasLimit: 4300000,
privateFor,
value: 0,
})
.on('error', console.error)
.then((newContractInstance) => {
return newContractInstance.options.address;
});
}
async function setTemperature({ node, contractAddress, privateFor, temp }) {
await node.web3.eth.personal.unlockAccount(node.WALLET_ADDRESS, '', 1000);
const myContract = getContract(node.web3, contractAddress);
return myContract.methods.set(temp).send({
from: node.WALLET_ADDRESS,
privateFor,
})
.on('error', console.error)
.then((receipt) => {
return receipt.status;
});
}
async function getTemperature({ node, contractAddress }) {
const myContract = getContract(node.web3, contractAddress);
return myContract.methods.get().call().then(result => result);
}
main()