-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
97 lines (86 loc) · 2.89 KB
/
generator.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
//https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract
var fs = require('fs');
const contract_data = JSON.parse(
fs.readFileSync('../0.contracts/build/contracts/Products.json')
);
if (typeof(contract_data.abi) !== 'object') {
throw new Error('not abi');
}
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider(
'https://ropsten.infura.io/2m0g6KR1H1ImBwBz2zVz'
));
var manufacturer_address = '0x7eC536287F36fAfE585E0e8201F363caD10Ca308';
var manufacturer_private = '726f290e97aeb7a9ac181fb4c0e10be474e09eace154416403d3d579c14ebc73';
var manufacturer_contract;
var contract_address = '0xc1bd41c8f5c801ffe1b7f48e052ea4f8c4264341';
var manufacturer_account = web3.eth.accounts.privateKeyToAccount(manufacturer_private);
if (manufacturer_account.address != manufacturer_address) {
throw new Error('wtf with private key');
}
web3.eth.accounts.wallet.add(manufacturer_private)
generateKeys = function () {
var secret_key = web3.utils.randomHex(32);
console.log('--generating--');
console.log(secret_key);
console.log('->');
manufacturer_contract.methods.getPublicForSecretFor(secret_key).call()
.then(function (public_key) {
console.log(public_key);
saveKey(secret_key, public_key);
});
}
saveKey = function (secret_key, public_key) {
console.log('--saving--');
console.log('->');
manufacturer_contract.methods.addItem(public_key).send({
from : manufacturer_address,
gas: 150000
})
.on('confirmation', function(confirmationNumber, receipt){
console.log('confirmation', confirmationNumber);
})
.then(function (done) {
console.log(done);
console.log('--done--');
fs.writeFileSync('toPrint.txt', ' secret : ' + secret_key + "\n public : " + public_key);
});
}
if (contract_address) {
manufacturer_contract = new web3.eth.Contract(contract_data.abi, contract_address, {
from: manufacturer_address, // default from address
gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});
generateKeys();
} else {
manufacturer_contract = new web3.eth.Contract(contract_data.abi);
manufacturer_contract.deploy({
data: contract_data.unlinked_binary,
arguments: []
})
.send({
from: manufacturer_address,
gas: 150000000,
gasPrice: '30000000'
}, function(error, transactionHash){
})
.on('error', function(error){
console.log('error', error);
})
.on('transactionHash', function(transactionHash){
console.log('transactionHash', transactionHash);
})
.on('receipt', function(receipt){
console.log('receipt', receipt.contractAddress);
})
.on('confirmation', function(confirmationNumber, receipt){
console.log('confirmation', confirmationNumber);
})
.then(function(new_contract){
console.log('--deployed--');
manufacturer_contract.defaultAccount = manufacturer_address;
manufacturer_contract = new_contract;
console.log(new_contract.options.address)
generateKeys();
});
}