-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrc_common.js
209 lines (172 loc) · 6.37 KB
/
rc_common.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const rc_json = require('@realitio/realitio-contracts/truffle/build/contracts/Realitio.json');
const arb_json = require('@realitio/realitio-contracts/truffle/build/contracts/Arbitrator.json');
const wallet_json = require('./abi/MultiSigWallet.json');
const splitter_json = require('./abi/SplitterWallet.json');
try{
require('./config.json');
} catch(err) {
console.log('Error: Config file config.json is missing. Please create one based on config.example.json');
process.exit(1);
}
const config = require('./config.json');
const BigNumber = require('bignumber.js');
const fs = require('fs');
const contract = require('truffle-contract');
const Tx = require('ethereumjs-tx')
const createKeccakHash = require('keccak')
const secp256k1 = require('secp256k1')
const web3_utils = require('web3-utils');
const qr = require('qrcode-terminal');
const GWEI_TO_WEI = 1000000000;
exports.GWEI_TO_WEI = GWEI_TO_WEI;
exports.sanitizeNonce = function(nonce) {
var n = parseInt(nonce);
if (n > config.max_nonce) {
console.log('nonce higher than max ' + config.max_nonce);
process.exit(1);
}
if (n < config.min_nonce) {
console.log('nonce lower than min ' + config.min_nonce);
process.exit(1);
}
return n;
}
exports.sanitizeGasPrice = function(gas_price_in_gwei) {
var n = parseInt(gas_price_in_gwei);
if (n > config.max_gas_price_in_gwei) {
console.log('gas_price_in_gwei higher than max ' + config.max_gas_price_in_gwei);
process.exit(1);
}
if (n < config.min_gas_price_in_gwei) {
console.log('gas_price_in_gwei lower than min ' + config.min_gas_price_in_gwei);
process.exit(1);
}
return n;
}
exports.loadKey = function() {
const key_file = config.key_file;
const key_hex = fs.readFileSync(key_file, 'utf8').replace(/\n$/, "");
if (key_hex.length != 66) throw 'Private key was the wrong length';
if (key_hex.substring(0,2) != '0x') throw 'Private key did not begin with 0x';
return new Buffer(key_hex.substring(2,66), 'hex')
}
exports.rcContract = function() {
const rc_address = rc_json.networks[config.network_id].address;
const rc_contract = contract(rc_json);
return rc_contract.at(rc_address);
}
exports.arbContract = function() {
const arb_address = arb_json.networks[config.network_id].address;
const arb_contract = contract(arb_json);
return arb_contract.at(arb_address);
}
exports.walletContract = function() {
if (!config.multisig_wallet) {
return null;
}
const wallet_contract = contract(wallet_json);
return wallet_contract.at(config.multisig_wallet);
}
exports.splitterContract= function() {
if (!config.splitter_wallet) {
return null;
}
const wallet_contract = contract(splitter_json);
return wallet_contract.at(config.splitter_wallet);
}
exports.checkArgumentLength = function(num_args, usage_txt) {
if (process.argv.length != num_args) {
console.log(usage_txt);
process.exit(1);
}
}
exports.commonParams = function(argv) {
return {
'nonce': this.sanitizeNonce(argv[2]),
'gas_price_in_gwei': this.sanitizeGasPrice(argv[3])
}
}
exports.serializedValueTX = function(params, addr, val, no_multisig) {
var gas_limit = 22000;
var data = "0x";
// If there's a multi-sig wallet configured, use the data and address as arguments
// ...then switch out the wallet for the address and data.
if (!no_multisig && config.multisig_wallet) {
const wallet = this.walletContract();
const wallet_req = wallet.submitTransaction.request(addr, val, data);
data = wallet_req.params[0].data;
addr = config.multisig_wallet;
gas_limit = 1000000;
}
const key = this.loadKey();
const tra = {
gasPrice: web3_utils.toHex(params['gas_price_in_gwei'] * GWEI_TO_WEI),
gasLimit: web3_utils.toHex(gas_limit),
nonce: web3_utils.toHex(params['nonce']),
to: addr,
data: data,
value: web3_utils.toHex(val),
chainId: config.network_id
};
const tx = new Tx(tra);
tx.sign(key);
return tx.serialize();
}
exports.serializedTX = function(params, cntr, data, gas_limit, no_multisig) {
var address = cntr.address;
// If there's a multi-sig wallet configured, use the data and address as arguments
// ...then switch out the wallet for the address and data.
if (!no_multisig && config.multisig_wallet) {
const wallet = this.walletContract();
const wallet_req = wallet.submitTransaction.request(address, 0, data);
data = wallet_req.params[0].data;
address = config.multisig_wallet;
}
if (gas_limit == undefined) {
gas_limit = config.gas_limit;
}
const key = this.loadKey();
const tra = {
gasPrice: web3_utils.toHex(params['gas_price_in_gwei'] * GWEI_TO_WEI),
gasLimit: web3_utils.toHex(gas_limit),
data: data,
nonce: web3_utils.toHex(params['nonce']),
to: address,
value: '0x00',
data: data,
chainId: config.network_id
};
const tx = new Tx(tra);
tx.sign(key);
return tx.serialize();
}
exports.sanitizeBytes32 = function(item, name, allow_zero) {
if (item.length != 66) throw name + ' was not the expected length';
if (!allow_zero && new BigNumber(item).equals(0)) throw name + ' was 0';
var BYTES32_REGEX = /^0x[0-9a-f]{64}/i;
if (!BYTES32_REGEX.test(item)) throw name + ' hex string incorrectly formatted';
return item;
}
exports.sanitizeAddress = function(addr) {
if (addr.length != 42) throw 'Answerer address was not the expected length';
console.log(new BigNumber(addr));
if (new BigNumber(addr).equals(0)) throw 'Address was zero';
if (new BigNumber(addr).lt("0xffffffff")) throw 'Address was suspiciously low';
if (new BigNumber(addr).gt("0xffffffff00000000000000000000000000000000")) throw 'Address was suspiciously high';
var ADDRESS_REGEX = /^0x[0-9a-f]{40}/i;
if (!ADDRESS_REGEX.test(addr)) throw 'address hex string incorrectly formatted';
return addr
}
exports.configParam = function(n) {
return config[n];
}
exports.address = function() {
const privateKey = this.loadKey();
let pubKey = secp256k1.publicKeyCreate(privateKey, false).slice(1);
let address = createKeccakHash('keccak256').update(pubKey).digest().slice(-20).toString('hex');
return address;
}
exports.output = function(tx) {
console.log('0x' + tx.toString('hex'));
qr.generate('0x' + tx.toString('hex'), {small: true});
}