-
Notifications
You must be signed in to change notification settings - Fork 18
/
sample.html
269 lines (227 loc) · 8.65 KB
/
sample.html
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
<!DOCTYPE html>
<html>
<head>
<title>RaiBlocksJS sample</title>
<meta charset="utf-8">
<link rel="stylesheet" media="all" href="sample.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<!-- JS BigNumber library https://github.com/MikeMcl/bignumber.js -->
<script type="text/javascript" src="bignumber.min.js"></script>
<!-- JS Blake library https://github.com/dcposch/blakejs -->
<script type="text/javascript" src="blake2b.js"></script>
<script type="text/javascript" src="rai.rpc.js"></script>
<script type="text/javascript" src="rai.extended.js"></script>
<script type="text/javascript" src="rai.community.js"></script>
<script type="text/javascript">
// Validate numbers input
function validate_numbers(input) {
var regexp = /^[0-9]*\.?[0-9]*$/;
return input.match(regexp);
}
// Hide and Show history
$(document).ready(function() {
$('.line').click(function() {
if ($('#hide_'+$(this).attr('id')).is(":visible")) { $('#hide_'+$(this).attr('id')).hide(); }
else { $('.hide').hide(); $('#hide_'+$(this).attr('id')).show(); }
});
});
// Send max from account
$(document).ready(function() {
$("#send_max").click(function() {
if ($("#send_max").is(":checked")) {
$("#send_amount").prop("readonly", true);
$("#send_amount").addClass("readonly");
} else {
$("#send_amount").prop("readonly", false);
$("#send_amount").removeClass("readonly");
}
});
});
</script>
</head>
<body>
<div class="counters" id="version"></div>
<div class="counters" id="block_count">Block count: </div>
<div class="counters" id="frontier_count">Frontiers count: </div>
<div class="counters" id="total_balance">Wallet total balance: </div>
<div class="counters" id="pending_balance">Wallet pending balance: </div>
<div class="counters" id="password">
<div id="password_text"></div>
<div id="password_input">
Enter password
<form name="password_input">
<input id="password_input_text" name="input" value="password">
</form>
<div id="password_input_click" class="button">Unlock</div>
</div>
</div>
<div class="accounts">
<div class="line">
<div class="account head">Account</div>
<div class="balance head">Balance</div>
</div>
</div>
<form name="send">
Send from: <select name="send_from" id="send_from"></select><br />
Send to: <input id="send_to" name="send_to" size="60"><br />
Amount: <input id="send_amount" name="send_amount" value="1000">
<select name="send_unit" id="send_unit">
<option value="rai">rai</option>
<option value="krai" selected>krai</option>
<option value="Mrai">Mrai</option>
</select>
<input type="checkbox" name="send_max" id="send_max">Send max
</form>
<div id="send" class="button">Send</div>
<!-- General page scripts -->
<script type="text/javascript">
var wallet = prompt( " Enter wallet from config.json "); // replace promt with your wallet actual id
var host = "http://localhost:7076"; // default host
var rai = new Rai(host); // connection
var hash_explorer = "https://raiblocks.net/explorer/index.php?h=";
var acc_explorer = "https://raiblocks.net/account/index.php?acc=";
// Initialization global variables
rai.initialize();
// Version
$('#version').append(rai.node_vendor());
// Sync Block Count Sample
/*
var block_count = RaiBlocks.block_count;
// Check block count from Community website
var community = new RaiCommunity();
var comm_summary = community.summary(); // Community summary initialization
var block_count_check = community.block_count;
$('#block_count').append(block_count);
if (block_count != block_count_check) {
$('#block_count').css("color", "red"); // if block counts from node and community doesn't match
}
*/
// async Block Count
var block_count;
var community = new RaiCommunity();
function check_blocks_callback(response) {
var block_count_check = response.blocks;
if (Math.abs(parseInt(block_count) - parseInt(block_count_check)) > 15 ) {
$('#block_count').css("color", "red"); // if block counts from node and community doesn't match
}
}
function blocks_callback(response) {
block_count = response.count;
$('#block_count').append(block_count);
community.json("https://raiblocks.net/page/summary.php?json=1", JSON.stringify({}), check_blocks_callback);
}
rai.rpc(JSON.stringify({"action":"block_count"}), blocks_callback);
// Frontiers count
$('#frontier_count').append(RaiBlocks.frontier_count);
// Total balance & Pending Balance
var wallet_balance = rai.wallet_balance_total(wallet, 'rai');
$('#total_balance').append(wallet_balance.balance + ' rai');
$('#pending_balance').append(wallet_balance.pending + ' rai');
// Peers
var peers = RaiBlocks.peers;
console.log("Peers list");
console.log(peers);
// Wallet password
var password_valid = rai.password_valid(wallet);
if (password_valid == 1) {
$('#password_text').append("Password is empty (or already unlocked)");
$('#password_text').css("color", "blue");
$('#password_input').hide();
}
else {
$('#password_text').append("Wallet is locked");
$('#password_text').css("color", "red");
}
// Password enter check
$( "#password_input_click" ).click(function() {
var password = $('#password_input_text')[0].value;
var password_enter = rai.password_enter(wallet, password);
if (password_enter == 1) {
$('#password_text').replaceWith("Wallet unlocked");
$('#password_text').css("color", "black");
$('#password_input').hide();
}
else {
alert( "Password is invalid" );
}
});
// Accounts
var accounts = rai.wallet_accounts_info(wallet); // Accounts Array + balances
// Sorting accounts
accounts.sort(function(a, b) {
return b.balance - a.balance;
});
console.log("Accounts array");
console.log(accounts);
// Parse HTML
$.each(accounts, function(){
var key = this.key;
$(".accounts").append("<div id=\"" + key + "\" class=\"line\"><div class=\"account\">" + key + "</div><div class=\"remove\">X</div><div class=\"balance\">" + this.balance + "</div></div>");
$('#send_from').append($("<option></option>").val(key).html(key));
$('#send_to').val(key);
// History for each account
$(".accounts").append("<div class=\"hide\" id=\"hide_" + this.key + "\"><table class=\"tab_" + key + " history\"><tbody></tbody></table></div>");
$(".tab_"+key).append("<tr><th>Type</th><th>Account</th><th>Amount</th><th>Hash</th></tr>");
$.each(this.history, function(){
$(".tab_"+key).append("<tr><td class=\"hist_type\">" + this.type + "</td><td class=\"hist_account\"><a href=\"" + acc_explorer + this.account + "\">" + this.account + "</a></td><td class=\"hist_amount\">" + rai.unit(this.amount, 'raw', 'rai') + "</td><td class=\"hist_hash\"><a href=\"" + hash_explorer + this.hash + "\">" + this.hash + "</a></td></tr>");
});
});
if (accounts.length > 1) $('#send_from').append($("<option></option>").val("everything").html("Everything from all accounts"));
// Send sample
$( "#send" ).click(function() {
var send_from = $('#send_from')[0].value;
var send_to = $('#send_to')[0].value;
var send_amount = $('#send_amount')[0].value;
var send_unit = $('#send_unit')[0].value;
var send_max = $('#send_max')[0].checked;
// Check for digits only
var validate_amount = validate_numbers(send_amount);
if (!validate_amount) {
alert( "Amount contains non digit characters" );
return false;
}
// Check if trying to send more than balance
var raw_amount = rai.unit(send_amount, send_unit);
var breaking = false;
$.each(accounts, function(){
// Send_max for 1 account
if ((this.key == send_from) && (send_max == true)) {
let confirmation = confirm("WARNING! You are trying to send all RaiBlocks from account " + send_from + " to account " + send_to);
send_amount = this.raw_balance;
send_unit = "raw";
if (confirmation == false) breaking = true;
return false;
}
else if ((this.key == send_from) && (parseInt(this.raw_balance) < parseInt(raw_amount))) {
alert( "Account " + send_from + " does not contain enough funds");
breaking = true;
return false;
}
// Send all
else if (send_from == "everything") {
if (!breaking) breaking = confirm("WARNING! You are trying to send all RaiBlocks from EVERY account in this wallet to account " + send_to);
if ((breaking == true) && (parseInt(this.raw_balance) !== 0)) rai.send(wallet, this.key, send_to, this.raw_balance, "raw");
}
});
// Checking account send to
if (!rai.account_validate(send_to)) {
breaking = true;
return false;
}
if (!breaking) {
let sending = rai.send(wallet, send_from, send_to, send_amount, send_unit);
prompt( " Send hash ", sending);
}
});
// Remove account sample
$('.remove').click(function() {
var account = $(this).parent().attr('id');
var confirmation = confirm("WARNING! You are trying to delete account " + account + "\nIt will be removed forever");
if (confirmation == true) {
rai.account_remove(wallet, account);
location.reload();
}
});
</script>
</body>
</html>