-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.js
122 lines (98 loc) · 4.48 KB
/
permissions.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
116
117
118
119
120
121
const Web3 = require("web3");
const HttpHeaderProvider = require('httpheaderprovider');
const Web3Quorum = require("web3js-quorum");
// Node1 is a node managed account
const node1 = {
privateKey: '0xc5148ef48a47a72575b234e25486edc2f4f3f184ef7c879a31a4b300ff52d3e7',
address: '0x339a6e3a8d47882c5be9b0e901360649600d1868',
name: 'node1'
}
// Wallet1 is a self managed account (eg: generated by metamask)
const wallet1 = {
privateKey: '87f8c55e037ddb6dbbe454c7500f7c3d4232892aa19405a47bc75786f0882ff9',
address: '0x50b09ec167697863f5f8fbc124022d8c893b73c5',
name: 'wallet1'
}
const wallet2 = {
privateKey: 'ebcb6cddfbb2c5b6293de38a92a7dbe974888ce0061863d4e4784d6bdc3561c7',
address: '0x1fE7E194edB898864FF7B6b09b91a93076FaAF4B',
name: 'wallet2'
}
const usage = async () => {
console.error(`Invalid arguments!
usage:
${process.argv[0]} ${process.argv[1]} <node1 | wallet1 | wallet2> bearerToken
`);
process.exit(-1);
}
if (process.argv.length != 4) {
usage();
}
const from = process.argv[2];
const TOKEN = process.argv[3];
let ACCOUNT_FROM;
let ACCOUNT_TO;
if (from === 'wallet1') {
ACCOUNT_FROM = wallet1;
} else if (from === 'wallet2') {
ACCOUNT_FROM = wallet2;
} else if (from === 'node1') {
ACCOUNT_FROM = node1;
} else {
usage();
}
// Ignore TLS errors from self-signed certificates
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
console.log(`
From: ${ACCOUNT_FROM.name} ${ACCOUNT_FROM.address}
`);
// obtain the preauthenticated bearer token
// by authenticating with the authorization server
const headers = { "Authorization": `Bearer ${TOKEN}` };
const provider = new HttpHeaderProvider('https://localhost:20000?PSI=PSA', headers);
// https://consensys.net/docs/goquorum/en/22.4.0/configure-and-manage/manage/json-rpc-api-security/
const web3 = new Web3Quorum(new Web3(provider));
const transact = async ({ account }) => {
const abi = [{ "constant": true, "inputs": [], "name": "storedData", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [{ "name": "retVal", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "x", "type": "uint256" }], "name": "inc", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "name": "initVal", "type": "uint256" }], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": false, "name": "value", "type": "uint256" }], "name": "IncEvent", "type": "event" }];
const contractAddress = "0xdd79a0ef8250ebc98af4890745147a554990bfc6";
const simpleContract = new web3.eth.Contract(abi, contractAddress);
const tm2 = "wsfcqtINO3linaBlh4Md19rMlwmXT7GDEuvnPjz9PiY=";
let val = await simpleContract.methods.get().call();
console.log(`before is ${val}`);
// const workingTx = await simpleContract.methods.inc(1).send({
// to: node1.address,
// gas: 40000,
// from: account.address,
// privateFor: [tm2]
// });
// console.log(`workingTx`, workingTx)
const incrementTx = simpleContract.methods.inc(1);
const gas = await incrementTx.estimateGas();
console.log(`gas is ${gas}`);
const txObj = {
to: node1.address,
gas: 40000,
from: account.address,
data: incrementTx.encodeABI(),
privateFor: [tm2]
}
await incrementTx.send(txObj);
// Sign tx with PK
// const createTransaction = await account.signTransaction(txObj);
const createTransaction = await web3.eth.accounts.signTransaction(txObj, account.privateKey)
console.log(`signed trx is `, createTransaction);
const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction);
console.log(`Transaction successful with hash: ${createReceipt.transactionHash}`);
val = await simpleContract.methods.get().call();
console.log(`after is ${val}`);
}
const main = async () => {
// const psi = await web3.eth.getPSI();
// console.log(`psi: ${psi}`);
// Make sure accounts exists and have a balance
const acc = await web3.eth.accounts.privateKeyToAccount(ACCOUNT_FROM.privateKey);
const balance = web3.utils.fromWei(await web3.eth.getBalance(acc.address), 'ether');
console.log(`using account ${acc.address} -- ${balance} ETH`)
await transact({ account: acc })
}
main();