-
Notifications
You must be signed in to change notification settings - Fork 0
/
XEMpay.js
256 lines (222 loc) · 6.51 KB
/
XEMpay.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
console.log("\n");
console.log("==============================================================");
console.log("======== XEMpay - Automated Multisignature Initiator ========");
console.log("==============================================================");
console.log("\n");
var fs = require('fs');
//Check access.json
if (fs.existsSync('./SENDaccess.json')) {
console.log("Enter a password to encrypt SENDaccess.json:");
var SecureConf = require('secure-conf');
var sconf = new SecureConf();
//Encryption
sconf.encryptFile(
"./SENDaccess.json",
"./SENDaccess.json.enc",
function(err, f, ef, ec) {
if (err) {
console.log("failed to encrypt %s, error is %s", f, err);
} else {
console.log("\n");
console.log("encrypt %s to %s complete.", f, ef);
console.log("encrypted contents are %s", ec);
console.log("\n");
console.log("NOW DELETE SENDaccess.json, EMPTY YOUR BIN AND RESTART XEMpay");
console.log("\n");
}
}
);
}
else{
console.log("Enter your password to start XEMpay:");
var SecureConf = require('secure-conf');
var sconf = new SecureConf();
var ef = "./SENDaccess.json.enc";
var express = require('express');
var app = express();
//Decryption
sconf.decryptFile(ef, function(err, file, content) {
if (err) {
console.log("Wrong password !"); //Not showing up... But big error log. Program stop.
// console.log('Failed to decrypt %s, error is %s', file, err);
} else {
console.log("\n");
console.log("decrypt %s complete.", file);
}
//Parsing decrypted content
var auth = JSON.parse(content);
var _DBhost = auth.DBhost; //
var _DBpassword = auth.DBpassword; //
var _DBuser = auth.DBuser; //
var _database = auth.database; //
var _wallet = auth.wallet; //
var _password = auth.walletPassword; //
var _type = auth.type; //
var _account = auth.account; //
var _multisigAccount = auth.multisigAccount; //
var _hoursDue = auth.hoursDue; //
var _amount = auth.amount; //
var _fee = auth.fee; //
var _hexMessage = auth.hexMessage; //
var _multisigFee = auth.multisigFee; //
var _encrypt = auth.encrypt; //
var _timer = auth.timer;
var _dailyTimer = auth.timer;
var _maxAmount = auth.maxAmount;
var _maxDayliAmount = auth.maxDayliAmount;
// include the required class
NEM = require('./NEM.js');
// create an instance using default configuration options
var nem = new NEM();
//Setting pull interval
var the_interval = _timer * 60 * 1000;
console.log("Success...");
console.log("\n");
console.log("Starting first check in", _timer, "minutes");
//starting with 0
var dayliAmount = 0;
var time = 0;
setInterval(function() {
//We want to check for funds
var fundsCheck = {
account: _multisigAccount
};
var fundsBalance;
//output a pretty formated JSON text
var toPrettyJson2 = function(fundsCheck) {
var h = JSON.stringify(fundsCheck,null,4);
//console.log(h);
//parsing Json
obj2 = JSON.parse(h);
//Get multisig account balance
fundsBalance = obj2.balance;
};
//request multisig account balance
nem.nccPost('/account/find',fundsCheck
,function(err) {
console.log(err);
}
,toPrettyJson2
);
//If 24h => reset dailyAmount
if (time > _dailyTimer * 60 * 1000)
{
dailyAmount = 0;
}
//DB connection
var mysql = require('mysql');
var pool = mysql.createPool({
host : _DBhost,
user : _DBuser,
password : _DBpassword,
database : _database
});
pool.getConnection(function(err,connection){
if (err) {
connection.release();
console.log('Error connecting database...');
return;
}
console.log('connected as id ' + connection.threadId);
//Select addresses with balance == _amount (/1000000 because the _amount value is in the smallest possible NEM fraction, that means that 1000000 means 1.000000 NEM.)
connection.query('SELECT * from yourTable WHERE columnBalance=? LIMIT 10', [_amount/1000000], function(err, rows, fields) {
//connection.release();
if (rows.length == 0)
{
console.log('No transaction to initiate, waiting...');
console.log("\n");
time += _timer * 60 * 1000;
} else{
var i;
for(i=0; i < rows.length; i++) //Batch Tx
{
//Cleaning "-" from addresses if any
var cleanAddress = [];
cleanAddress[i] = rows[i].XEMaddress.replace(/[^a-z\d\s]+/gi, "");
//address from DB
var address = [];
address[i] = rows[i].XEMaddress;
//address balance from DB
var balance = [];
balance[i] = rows[i].balance;
console.log('Transaction to: ', cleanAddress[i]);
// TransfertSendRequest (contain your decrypted informations and transaction parameters)
var transac = [];
transac[i] = {
wallet: _wallet,
type: _type,
multisigAccount: _multisigAccount,
account: _account,
recipient: cleanAddress[i],
amount: _amount,
hexMessage: _hexMessage,
encrypt: _encrypt,
hoursDue: _hoursDue,
password: _password,
fee: _fee,
multisigFee: _multisigFee
};
//output a pretty formated JSON text
var toPrettyJson = function(transac) {
var e = JSON.stringify(transac,null,4);
//console.log(e);
};
//Maximal Amount is _maxAmount XEM per tx
if (rows[i].balance > _maxAmount)
{
console.log("There is a problem maximum amount per transaction exceeded");
console.log("Following Transaction cause problems:");
console.log(rows[i].id);
throw new Error('Something goes wrong !');
}
//Maximum dayliAmount is _maxDayliAmount XEM
else if (dayliAmount < _maxDayliAmount)
{
if (fundsBalance < rows.length*(_amount+_fee+_multisigFee))
{
console.log("\n");
console.log("INSUFFICIENT FUNDS");
console.log("Funds balance:");
console.log(fundsBalance);
time += _timer * 60 * 1000;
}else{
//Initiate signature request
nem.nccPost('/wallet/account/transaction/send',transac[i]
,function(err) {
console.log(err);
}
,toPrettyJson
);
dayliAmount += balance[i];
time += _timer * 60 * 1000;
//We reset addresses balance
connection.query('UPDATE yourTable SET columBalance=0 WHERE columnAddress=?', [address[i]], function (err, result) {
if (err) throw err;
});
}//endelse check funds
}//endif dayli amount
else{
console.log("MAXIMAL AMOUNT REACHED !");
console.log("Waiting...");
time += _timer * 60 * 1000;
}
}//endfor
//Batch report
console.log("\n");
console.log("Total transactions: ");
console.log(rows.length);
console.log("Total amount: ");
console.log(dayliAmount);
console.log("Done, waiting...");
console.log("\n");
connection.release();
}
});
connection.on('error', function(err) {
console.log('Error connecting database...');
return;
});
});
}, the_interval);
});
}