-
Notifications
You must be signed in to change notification settings - Fork 24
/
renderer.js
305 lines (284 loc) · 12.1 KB
/
renderer.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const {ipcRenderer} = require('electron');
var tableify = require('tableify');
var txs = [];
var privTxs = [];
var memos = [];
var memosFlag = false; // when true update the memo table
var tmpSelect = document.createElement('select');
var genHistory = {'transparent': false, 'private': false};
Array.prototype.getRandom = function () {
return this[Math.floor(Math.random() * this.length)];
};
function hexToString(s) {
let str = '';
for (let i = 0; i < s.length; i++) {
let charCode = parseInt(s[(i*2)] + s[(i*2)+1], 16);
str += String.fromCharCode(charCode);
}
return str;
}
function generateQuery (method, params) {
let jsonObject;
jsonObject = {'jsonrpc': '1.0', 'id': method, 'method': method, 'params': params};
ipcRenderer.send('jsonQuery-request', jsonObject);
return(jsonObject);
}
function generateQuerySync (method, params) {
let jsonObject;
jsonObject = {'jsonrpc': '1.0', 'id': method, 'method': method, 'params': params};
return ipcRenderer.sendSync('jsonQuery-request-sync', jsonObject);
}
function showTxDetails(txid) {
let res = generateQuerySync('gettransaction', [txid]);
let datetime = new Date(res.result.time * 1000);
datetime = datetime.toLocaleTimeString() + " - " + datetime.toLocaleDateString();
let category = (res.result.amount < 0.0) ? 'send' : 'receive';
let obj = { amount: res.result.amount, blockhash: res.result.blockhash, category: category, confirmations: res.result.confirmations, fee: res.result.fee, txid: res.result.txid, time: datetime };
let alertText = `Amount: ${obj.amount}\n` +
`Blockhash: ${obj.blockhash}\n` +
`Confirmations: ${obj.confirmations}\n` +
`Fee: ${obj.fee}\n` +
`Time: ${obj.time}\n` +
`TXID: ${obj.txid}\n`;
alert(alertText);
}
function generateMemoTable (memos) {
if (memosFlag === true) {
memosFlag = false;
memos.sort(function(a, b){ // sort table by date
return b.time - a.time;
});
for (let i = 0; i < memos.length; i++) {
memos[i]["details"] = '<a href="javascript:void(0)" onclick="renderer.showTxDetails(\'' + memos[i].txid + '\')">click</a>';
let datetime = new Date(memos[i]["time"] * 1000);
memos[i]["time"] = datetime.toLocaleTimeString() + " - " + datetime.toLocaleDateString();
delete memos[i].txid;
}
// build empty table if no results
if (memos.length < 1) {
memos[0] = {'amount': '', 'address': '', 'memo': '', 'time': '', 'details': ''};
}
let tableElement = tableify(memos);
let div = document.createElement('div');
div.innerHTML = tableElement;
div.firstElementChild.className += ' w3-table-all w3-tiny';
document.getElementById("memoPage").innerHTML = div.innerHTML;
}
}
function generateHistoryTable (txs, privTxs) {
let combinedTxs = [].concat(txs, privTxs);
combinedTxs.sort(function(a, b){ // sort table by date
return b.time - a.time;
});
for (let i = 0; i < privTxs.length; i++) {
privTxs[i]["address"] = privTxs[i].address.substr(0, 16) + '......' + privTxs[i].address.substr(-16);
}
for (let i = 0; i < combinedTxs.length; i++) {
if (combinedTxs[i].memo) {
memos.push({
amount: combinedTxs[i].amount,
address: combinedTxs[i].address,
txid: combinedTxs[i].txid,
memo: hexToString(combinedTxs[i].memo),
time: combinedTxs[i].time
});
}
let datetime = new Date(combinedTxs[i]["time"] * 1000);
combinedTxs[i]["time"] = datetime.toLocaleTimeString() + " - " + datetime.toLocaleDateString();
combinedTxs[i]["details"] = '<a href="javascript:void(0)" onclick="renderer.showTxDetails(\'' + combinedTxs[i].txid + '\')">click</a>';
delete combinedTxs[i].memo;
delete combinedTxs[i].txid;
}
memosFlag = true;
// build empty table if no results
if (combinedTxs.length < 1) {
combinedTxs[0] = {'address': 'No received transactions found', 'amount': 0, 'category': '', 'confirmations': '', 'time': '', 'details': ''};
}
let tableElement = tableify(combinedTxs);
let div = document.createElement('div');
div.innerHTML = tableElement;
div.firstElementChild.className += 'w3-table-all w3-tiny';
for (let i = 0; i < div.getElementsByClassName("number").length; i++) {
div.getElementsByClassName("number")[i].className += ' w3-right';
}
document.getElementById("transactionTransparentSpan").innerHTML = div.innerHTML;
}
ipcRenderer.on('jsonQuery-reply', (event, arg) => {
if (arg.error && arg.error.code === -28) {
document.getElementById("alertSpan").innerHTML = '<h2>' + arg.error.message + '</h2>' + '<br />© 2017 zdeveloper.org (a Zclassic project) - Licensed under the Common Public Attribution License';
while (true) {
let res = generateQuerySync('getinfo', []);
if (!res.result.error) {
refreshUI();
break;
}
}
}
document.getElementById("alertSpan").innerHTML = '';
if (arg.id === 'getnetworkinfo') {
document.getElementById("connectionsValue").innerHTML = arg.result.connections;
}
else if (arg.id === 'z_gettotalbalance') {
document.getElementById("currentBalanceValue").innerHTML = arg.result.total;
document.getElementById("transparentBalanceValue").innerHTML = arg.result.transparent;
document.getElementById("transparentAvailableValue").innerHTML = arg.result.transparent;
document.getElementById("privateBalanceValue").innerHTML = arg.result.private;
}
else if (arg.id === 'listtransactions') {
let table = arg.result;
for (let i = 0; i < table.length; i++) {
delete table[i]["account"];
delete table[i]["blockhash"];
delete table[i]["blockindex"];
delete table[i]["blocktime"];
delete table[i]["fee"];
delete table[i]["size"];
delete table[i]["timereceived"];
delete table[i]["vjoinsplit"];
delete table[i]["vout"];
delete table[i]["walletconflicts"];
txs.push(table[i]);
}
genHistory.transparent = true;
}
if (arg.id === 'listaddressgroupings') {
let table = [];
let ctr = 0;
for (let i = 0; i < arg.result.length; i++) {
for (let n = 0; n < arg.result[i].length; n++) {
table[ctr] = { 'transparent address': arg.result[i][n][0], 'amount': arg.result[i][n][1] };
ctr += 1;
let option = document.createElement("option");
option.text = arg.result[i][n][0] + ' (' + arg.result[i][n][1] + ')';
option.value = arg.result[i][n][0];
document.getElementById("privateFromSelect").add(option);
}
}
// build empty table if no results
if (arg.result.length < 1) {
table[0] = {'transparent address': 'No addresses with received balances found', 'amount': 0};
}
let tableElement = tableify(table);
let div = document.createElement('div');
div.innerHTML = tableElement;
div.firstElementChild.className += ' w3-table-all w3-tiny';
for (let i = 0; i < div.getElementsByClassName("number").length; i++) {
div.getElementsByClassName("number")[i].className += ' w3-right';
}
document.getElementById("addressTransparentSpan").innerHTML = div.innerHTML;
}
else if (arg.id === 'z_listaddresses') {
let table = [];
let ctr = 0;
for (let i = 0; i < arg.result.length; i++) {
let res = generateQuerySync('z_getbalance', [arg.result[i], 0]);
table[ctr] = { 'private address': arg.result[i], 'amount': res.result };
ctr += 1;
if (res.result > 0) {
let option = document.createElement("option");
option.text = arg.result[i] + ' (' + res.result + ')';
option.value = arg.result[i];
document.getElementById("privateFromSelect").add(option);
}
}
// build empty table if no results
if (arg.result.length < 1) {
table[0] = {'privateaddress': 'No addresses with received balances found', 'amount': 0};
}
let tableElement = tableify(table);
let div = document.createElement('div');
div.innerHTML = tableElement;
div.firstElementChild.className += ' w3-table-all w3-tiny';
for (let i = 0; i < div.getElementsByClassName("number").length; i++) {
div.getElementsByClassName("number")[i].className += ' w3-right';
}
document.getElementById("addressPrivateSpan").innerHTML = div.innerHTML;
// gather a list of TXIDs associated with z_addresses
for (let i = 0; i < arg.result.length; i++) {
let res = generateQuerySync('z_listreceivedbyaddress', [arg.result[i], 0]);
for (let n = 0; n < res.result.length; n++) {
let tx = generateQuerySync('gettransaction', [res.result[n].txid]);
privTxs.push({ address: arg.result[i], txid: tx.result.txid, amount: res.result[n].amount,
memo: res.result[n].memo, category: 'receive', time: tx.result.time, confirmations: tx.result.confirmations });
}
}
genHistory.private = true;
}
else if (arg.id === 'listreceivedbyaddress') {
let unusedAddresses = [];
for (let i = 0; i < arg.result.length; i++) {
if (arg.result[i].amount === 0) {
unusedAddresses.push(arg.result[i].address);
}
if (unusedAddresses.length === 0) {
document.getElementById("newTransparentAddress").click();
}
}
document.getElementById("receivingAddressValue").value = unusedAddresses.getRandom();
}
else if (arg.id === 'sendmany') {
if (arg.result === null) {
window.alert('There was an error:\n\n' + arg.error.message);
}
else {
window.alert('Successfully transmitted transaction.\n\nTXID: ' + arg.result);
}
}
else if (arg.id === 'z_sendmany') {
if (arg.result === null) {
window.alert('There was an error:\n\n' + arg.error.message);
}
else {
window.alert('Successfully initiated private transaction.\n\nTXID: ' + arg.result);
}
}
});
ipcRenderer.on('coin-reply', (event, arg) => {
document.getElementById("coin").innerHTML = arg;
});
ipcRenderer.on('wallet-auth-error', (event, arg) => {
document.getElementById("alertSpan").innerHTML = arg;
});
function refreshUI() {
// reset
document.getElementById("privateFromSelect").innerHTML = '';
// for receivePage
generateQuery('listreceivedbyaddress', [0, true]);
// for historyPage
generateQuery('listtransactions', []);
// for addressesPage
generateQuery('listaddressgroupings', []);
generateQuery('z_listaddresses', []);
// for general use
generateQuery('getnetworkinfo', []);
generateQuery('getinfo', []);
generateQuery('z_gettotalbalance', [0]);
}
function pollUI() {
if (genHistory.transparent === true && genHistory.private === true) {
generateHistoryTable(txs, privTxs);
txs = [];
privTxs = [];
genHistory.transparent = false;
genHistory.private = false;
generateMemoTable(memos);
}
}
refreshUI();
setInterval(refreshUI, 16000);
setInterval(pollUI, 400);
ipcRenderer.send('coin-request'); // get abbreviation for coin
module.exports = {
generateQuerySync: function(method, params) {
return generateQuerySync(method, params);
},
generateQuery: function(method, params) {
return generateQuery(method, params);
},
showTxDetails: function(txid) {
return showTxDetails(txid);
}
};