forked from chainstacklabs/quorum-iot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate-externalSign.js
115 lines (98 loc) · 2.79 KB
/
private-externalSign.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { compileContract } = require('./utils/compiler.js');
const { serializeAndSign } = require('./utils/helper.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 deployed at address: ${contractAddress}`);
const statusUnAuthorized = await setTemp({
to: contractAddress,
node: node3,
privateFor: [node1.TM_PK],
temp: 8,
});
console.log(`Set Temp status from unauthorized node: ${statusUnAuthorized}`);
const resultUnAuthorized = await getTemp({
contractAddress,
node: node3,
});
console.log(`Contract temperature: ${resultUnAuthorized}`);
const status = await setTemp({
to: contractAddress,
node: node2,
privateFor: [node1.TM_PK],
temp: 22,
});
console.log(`Set temp status from authorized node: ${status}`);
const result = await getTemp({
contractAddress,
node: node2,
});
console.log(`Contract temperature after update: ${result}`);
};
async function deployContract( {node, privateFor }) {
// encode contract
const contract = new node.web3.eth.Contract(temperatureMonitor.interface);
const encodedABI = contract
.deploy({
data: temperatureMonitor.bytecode,
})
.encodeABI();
// store the bytecode in tessera using the storeRawRequest API
const rawTxHash = await node.txManager.storeRawRequest(
encodedABI,
node.TM_PK,
);
const privateSignedTxHex = await serializeAndSign(node, {
to: null,
data: `0x${rawTxHash}`,
});
return node.txManager
.sendRawRequest(privateSignedTxHex, privateFor)
.then(tx => {
return tx.contractAddress;
})
.catch(error => error.message);
}
async function setTemp({ to, node, privateFor, temp}) {
const encodedABI = node.web3.eth.abi.encodeFunctionCall(
temperatureMonitor.interface.find(x => x.name === 'set'),
[temp],
);
const rawTxHash = await node.txManager.storeRawRequest(encodedABI, node.TM_PK);
const privateSignedTxHex = await serializeAndSign(node, {
to,
data: `0x${rawTxHash}`,
});
return node.txManager
.sendRawRequest(privateSignedTxHex, privateFor)
.then(tx => {
return `${tx.status} - ${tx.blockHash}`;
})
.catch(error => error.message);
}
async function getTemp({ contractAddress, node }) {
const contract = new node.web3.eth.Contract(
temperatureMonitor.interface,
contractAddress,
);
return contract.methods
.get().call({
from: node.WALLET_ADDRESS,
})
.then(data => data)
.catch(error => error.message);
}
main();