Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit c481438

Browse files
Paste array of trytes.
1 parent 85727e5 commit c481438

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

ui/index.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,12 @@ <h1>Claim Process</h1>
166166
<h1>Paste Trytes</h1>
167167
<div class="contents">
168168
<div id="paste-trytes-group">
169-
<div class='input-group'><label>Trytes:</label>
170169
<textarea name='trytes' id='pasted-trytes' placeholder='Paste your Trytes here (Array or JSON representation)' style='width:98%;min-height:100px;max-height:300px;'></textarea>
171-
</div>
172170
<button id="verify-pasted-trytes-btn" type="button" class="btn" data-initial="Verify Trytes" data-loading="Verifying...">Verify Trytes</button>
173171
</div>
174172
<div id="process-trytes-group">
175-
<input type="hidden" name="trytes" id="process-trytes" value="" />
176-
<div id="verify-trytes"></div>
173+
<div id="process-trytes"></div>
174+
<input type="hidden" name="pasted_trytes_completed" id="process-pasted-trytes-completed" value="0" />
177175
<button id="process-pasted-trytes-btn" type="button" class="btn" data-initial="Process Trytes" data-loading="Processing...">Process Trytes</button>
178176
</div>
179177
</div>

ui/js/ui.pasteTrytes.js

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var UI = (function(UI, $, undefined) {
2+
var isProcessing = false;
3+
24
UI.showPasteTrytes = function(callback) {
35
console.log("UI.showPasteTrytes");
46

@@ -23,60 +25,121 @@ var UI = (function(UI, $, undefined) {
2325
modal.open();
2426
}
2527

26-
//999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999A9RGRKVGWMWMKOLVMDFWJUHNUNYWZTJADGGPZGXNLERLXYWJE9WQHWWBMCPZMVVMJUMWWBLZLNMLDCGDJ999999999999999999999999999999999999999999999999999999YGYQIVD99999999999999999999TXEFLKNPJRBYZPORHZU9CEMFIFVVQBUSTDGSJCZMBTZCDTTJVUFPTCCVHHORPMGCURKTH9VGJIXUQJVHK999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
27-
UI.handlePastingTrytes = function() {
28-
$("#verify-pasted-trytes-btn").on("click", function(e) {
29-
var trytes = $("#pasted-trytes").val();
28+
function parseTrytesToBundle(trytes) {
29+
var bundleTxs = [];
3030

31-
if (iota.validate.isTrytes(trytes)) {
32-
var transaction = iota.utils.transactionObject(trytes);
33-
} else {
34-
// We conver to trytes, then back to transaction so that no extra json key/value pairs are added..
35-
try {
36-
var transaction = JSON.parse($("#pasted-trytes").val());
37-
} catch (err) {
38-
transaction = null;
39-
}
40-
if (transaction) {
41-
trytes = iota.utils.transactionTrytes(transaction);
42-
transaction = iota.utils.transactionObject(trytes);
31+
if (iota.valid.isTrytes(trytes)) {
32+
var bundle = [trytes];
33+
} else {
34+
try {
35+
var bundle = JSON.parse(trytes);
36+
} catch (err) {
37+
bundle = null;
38+
}
39+
}
40+
41+
var trytesError = false;
42+
43+
if ($.isArray(bundle)) {
44+
$.each(bundle, function(index, trytes) {
45+
if (!iota.valid.isTrytes(trytes)) {
46+
trytesError = true;
47+
return false;
48+
} else {
49+
var transaction = iota.utils.transactionObject(trytes);
50+
if (transaction) {
51+
bundleTxs.push(transaction);
52+
} else {
53+
trytesError = true;
54+
return false;
55+
}
4356
}
57+
});
58+
}
59+
60+
if (trytesError || !bundleTxs || bundleTxs.length == 0) {
61+
return false;
62+
}
63+
64+
return bundleTxs;
65+
}
66+
67+
UI.handlePastingTrytes = function() {
68+
$(document).on("closed", "#paste-trytes-modal", function (e) {
69+
if (isProcessing) {
70+
isProcessing = false;
71+
$("#process-pasted-trytes-btn").loadingReset("Process Trytes");
72+
iota.api.interruptAttachingToTangle();
4473
}
74+
});
4575

46-
$("#process-trytes").val(trytes);
76+
$("#verify-pasted-trytes-btn").on("click", function(e) {
77+
var trytes = $("#pasted-trytes").val();
4778

48-
if (!transaction) {
49-
$("#verify-pasted-trytes-btn").loadingError("Invalid trytes", {"initial": "Verify Trytes"});
79+
var bundleTxs = parseTrytesToBundle($("#pasted-trytes").val());
80+
81+
if (!bundleTxs) {
82+
$("#verify-pasted-trytes-btn").loadingError("Invalid trytes or input", {"initial": "Verify Trytes"});
83+
return;
84+
} else if (!iota.utils.isBundle(bundleTxs)) {
85+
$("#verify-pasted-trytes-btn").loadingError("Invalid signature", {"initial": "Process Trytes"});
5086
return;
5187
}
52-
88+
5389
var html = "<div class='list'><ul>";
54-
$.each(transaction, function(key, value) {
55-
html += "<li><div class='details details-" + String(key).escapeHTML() + "' title='" + String(key).escapeHTML() + "'><div class='address'>" + String(key).escapeHTML() + "</div></div><div class='value value-" + String(key).escapeHTML() + "' title='" + String(value).escapeHTML() + "'>" + String(value).escapeHTML() + "</div></li>";
56-
});
5790

58-
html += "</ul></div>";
91+
for (var i=0; i<bundleTxs.length; i++) {
92+
html += "<li><div class='details'><div class='address'>" + UI.formatForClipboard(iota.utils.addChecksum(bundleTxs[i].address)) + "</div></div><div class='value'>" + UI.formatAmount(bundleTxs[i].value) + "</div></li>";
93+
}
5994

95+
html += "</ul></div>";
96+
97+
$("#process-trytes").html(html);
6098
$("#paste-trytes-modal h1").html("Verify Trytes");
61-
$("#verify-trytes").html(html);
6299
$("#process-trytes-group").show();
63100
$("#paste-trytes-group").hide();
101+
$("#process-pasted-trytes-completed").val(0);
102+
64103
$("#verify-pasted-trytes-btn").loadingReset("Verify Trytes");
65104
});
66105

67106
$("#process-pasted-trytes-btn").on("click", function(e) {
68-
iota.api.sendTrytes($("#process-trytes").val(), connection.depth, connection.minWeightMagnitude, function(error, transfers) {
107+
var bundleTxs = parseTrytesToBundle($("#pasted-trytes").val());
108+
109+
if ($("#process-pasted-trytes-completed").val() == 1) {
110+
$("#process-pasted-trytes-btn").loadingError("Already Processed", {"initial": "Process Trytes"});
111+
return;
112+
} else if (!bundleTxs) {
113+
$("#process-pasted-trytes-btn").loadingError("Invalid trytes or input", {"initial": "Process Trytes"});
114+
return;
115+
} else if (!iota.utils.isBundle(bundleTxs)) {
116+
$("#process-pasted-trytes-btn").loadingError("Invalid signature", {"initial": "Process Trytes"});
117+
return;
118+
}
119+
120+
var trytes = [];
121+
122+
$.each(bundleTxs, function(index, transaction) {
123+
trytes.push(iota.utils.transactionTrytes(transaction));
124+
});
125+
126+
trytes = trytes.reverse();
127+
128+
console.log(trytes);
129+
130+
isProcessing = true;
131+
132+
iota.api.sendTrytes(trytes, connection.depth, connection.minWeightMagnitude, function(error, transfers) {
69133
if (error) {
70134
console.log("Process Pasted Trytes: Error");
71135
console.log(error);
72-
73-
136+
$("#process-pasted-trytes-btn").loadingError(error);
74137
} else {
75138
console.log("Process Pasted Trytes: Success");
76139
$("#process-pasted-trytes-btn").loadingSuccess("Transaction Completed");
140+
$("#process-pasted-trytes-completed").val(1);
77141
UI.updateState(1000);
78142
}
79-
$stack.removeClass("loading");
80143
});
81144
});
82145
}

0 commit comments

Comments
 (0)