-
Notifications
You must be signed in to change notification settings - Fork 51
/
send.js
225 lines (172 loc) · 7.53 KB
/
send.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
$(document).ready(function() {
//
// Instantiate IOTA
//
var iota = new IOTA({
'host': 'http://localhost',
'port': 14265
});
var seed;
var balance = 0;
var address;
function toggleSidebar() {
$(".button").toggleClass("active");
$("main").toggleClass("move-to-left");
$(".sidebar-item").toggleClass("active");
$(".sidebar").toggleClass("donotdisplay");
}
//
// Properly formats the seed, replacing all non-latin chars with 9's
// And extending it to length 81
//
function setSeed(value) {
seed = "";
value = value.toUpperCase();
for (var i = 0; i < value.length; i++) {
if (("9ABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(value.charAt(i)) < 0) {
seed += "9";
} else {
seed += value.charAt(i);
}
}
}
//
// Gets the addresses and transactions of an account
// As well as the current balance
// Automatically updates the HTML on the site
//
function getAccountInfo() {
// Command to be sent to the IOTA API
// Gets the latest transfers for the specified seed
iota.api.getAccountData(seed, function(e, accountData) {
console.log("Account data", accountData);
// Update address
if (!address && accountData.addresses[0]) {
address = iota.utils.addChecksum(accountData.addresses[accountData.addresses.length - 1]);
updateAddressHTML(address);
}
balance = accountData.balance;
// Update total balance
updateBalanceHTML(balance);
})
}
//
// Generate address function
// Automatically updates the HTML on the site
//
function genAddress() {
console.log("Generating an address");
// Deterministically Generates a new address with checksum for the specified seed
iota.api.getNewAddress(seed, {'checksum': true}, function(e,address) {
if (!e) {
console.log("NEW ADDRESS GENERATED: ", address)
address = address;
// Update the HTML on the site
updateAddressHTML(address)
}
})
}
//
// Makes a new transfer for the specified seed
// Includes message and value
//
function sendTransfer(address, value, messageTrytes) {
var transfer = [{
'address': address,
'value': parseInt(value),
'message': messageTrytes
}]
console.log("Sending Transfer", transfer);
// We send the transfer from this seed, with depth 4 and minWeightMagnitude 18
iota.api.sendTransfer(seed, 4, 9, transfer, function(e) {
if (e){
var html = '<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>ERROR!</strong>' + e + '.</div>'
$("#send__success").html(JSON.stringify());
$("#submit").toggleClass("disabled");
$("#send__waiting").css("display", "none");
} else {
var html = '<div class="alert alert-info alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>Success!</strong> You have successfully sent your transaction. If you want to make another one make sure that this transaction is confirmed first (check in your client).</div>'
$("#send__success").html(html);
$("#submit").toggleClass("disabled");
$("#send__waiting").css("display", "none");
balance = balance - value;
updateBalanceHTML(balance);
}
})
}
//
// Menu Open/Close
//
$(".button").on("click tap", function() {
toggleSidebar();
});
//
// Set seed
//
$("#seedSubmit").on("click", function() {
// We modify the entered seed to fit the criteria of 81 chars, all uppercase and only latin letters
setSeed($("#userSeed").val());
// Then we remove the input
$("#enterSeed").html('<div class="alert alert-success" role="alert">Successfully saved your seed. You can generate an address now.</div>');
// We fetch the latest transactions every 90 seconds
getAccountInfo();
setInterval(getAccountInfo, 90000);
});
//
// Generate a new address
//
$("#genAddress").on("click", function() {
if (!seed)
return
genAddress();
})
//
// Send a new message
//
$("#submit").on("click", function() {
if (!seed) {
var html = '<div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>No Seed!</strong> You have not entered your seed yet. Do so on the Menu on the right.</div>'
$("#send__success").html(html);
return
}
if (!balance || balance === 0) {
var html = '<div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>No Tokens!</strong> You do not have enough IOTA tokens. Make sure you have enough confirmed tokens.</div>'
$("#send__success").html(html);
return
}
var name = $("#name").val();
var value = parseInt($("#value").val());
var address = $("#address").val();
var message = $("#message").val();
if (!name || !value || !message)
return
if (value > balance) {
var html = '<div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>Value too high!</strong> You have specified a too high value.</div>'
$("#send__success").html(html);
return
}
// the message which we will send with the transaction
var messageToSend = {
'name': name,
'message': message
}
// Convert the user message into trytes
// In case the user supplied non-ASCII characters we throw an error
try {
console.log("Sending Message: ", messageToSend);
var messageTrytes = iota.utils.toTrytes(JSON.stringify(messageToSend));
console.log("Converted Message into trytes: ", messageTrytes);
// We display the loading screen
$("#send__waiting").css("display", "block");
$("#submit").toggleClass("disabled");
// If there was any previous error message, we remove it
$("#send__success").html();
// call send transfer
sendTransfer(address, value, messageTrytes);
} catch (e) {
console.log(e);
var html = '<div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><strong>Wrong Format!</strong> Your message contains an illegal character. Make sure you only enter valid ASCII characters.</div>'
$("#send__success").html(html);
}
})
});