-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathsendOffline.html
441 lines (402 loc) · 18.8 KB
/
sendOffline.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<link rel="icon shortcut" href=img/logo.png type=image/png>
<link rel=apple-touch-icon href=img/logo.png>
<link rel=stylesheet href=lib/bootstrap-4.0.0-dist/css/bootstrap.min.css>
<link rel=stylesheet href=css/base.css>
<link rel=stylesheet href=css/ui-block.css>
<meta name=viewport content="width=device-width">
<style>
.transaction {
display: none;
}
#qrcode {
text-align: center;
}
</style>
<title>NEBULAS</title>
</head>
<body>
<div class=logo-main></div>
<div class=header></div>
<div id=step1 class="container wallet">
<div id=generate_infomation>
<h1 data-i18n=send-offline/step1>Step 1: Generate Information (Online Computer)</h1>
<div class=form-group>
<div data-i18n=send-nas/from-address>From Address</div>
<div class="from icon-address"></div>
</div>
<button id=generate-infomation class="btn btn-block" data-i18n=send-offline/gen-info>Inquire Information</button>
<div class="form-group row" id=information style=display:none;>
<div class=col>
<label for=fa_gasprice data-i18n=gas-price>Gas Price:</label>
<input type=text id=fa_gasprice class=form-control>
</div>
<div class=col>
<label for=fa_nonce data-i18n=send-offline/nonce>Nonce: (next transaction nonce)</label>
<input type=text id=fa_nonce class=form-control>
</div>
</div>
</div>
</div>
<div id=step2 class="container wallet">
<div id=generate_transaction>
<h1 data-i18n=send-offline/step2>Step 2: Generate Transaction (Offline Computer)</h1>
<div class="form-group row">
<div class=col>
<label data-i18n=send-nas/from-address>From Address</label>
<div class="icon-address from" data-disabled></div>
</div>
<div class=col>
<label data-i18n=send-nas/to-address>To Address</label>
<div class="icon-address to"></div>
</div>
</div>
<div class="form-group row">
<div class=col>
<label data-i18n=send-nas/amount>Value / Amount to Send</label>
<div class=number-comma data-data-i18n=amount data-id=amount data-unit=currency data-validate="required number eqgt0" data-currency-selector></div>
</div>
<div class=col>
<label>Nonce</label>
<input type=text class=form-control id=nonce data-validate-order-matters="required number gt0">
</div>
</div>
<div class="form-group row">
<div class=col>
<label>
<span data-i18n=gas-limit>Gas Limit</span>
</label>
<div class=number-comma data-data-i18n=amount data-id=limit data-validate="required number gt0" data-value=200000></div>
</div>
<div class=col>
<label>
<span data-i18n=gas-price>Gas Price</span>
<i>( 1 NAS = 1EWei = 10
<sup>18</sup> Wei )</i>
</label>
<div class=number-comma data-data-i18n=amount data-id=price data-unit=wei data-validate="required number gt0" data-value=20000000000></div>
</div>
</div>
</div>
</div>
<div class="container select-wallet-file"></div>
<div class="container transaction">
<button id=generate class="btn btn-block" data-i18n=send-nas/button/generate>Generate Transaction</button>
<div class=row>
<div class=col>
<div class=form-group>
<label for=raw data-i18n=send-nas/raw>Raw Transaction</label>
<textarea class=form-control rows=8 id=raw disabled></textarea>
</div>
</div>
<div class=col>
<div class=form-group>
<label for=signed data-i18n=send-nas/signed>Signed Transaction</label>
<textarea class=form-control rows=8 id=signed disabled></textarea>
</div>
</div>
</div>
</div>
<div id=step3 class="container wallet">
<h1 data-i18n=send-offline/step3>Step 3: Send / Publish Transaction (Online Computer)</h1>
<div class="form-group row">
<div class=col>
<label for=signedoffline data-i18n=send-nas/signed>Signed Transaction</label>
<textarea class=form-control rows=8 id=signedoffline></textarea>
</div>
<div class=col>
<div id=qrcode style=text-align:center;>
</div>
</div>
</div>
<div>
<button data-toggle=modal data-target=#exampleModal class="btn btn-block send" data-i18n=send-nas/send>Send Transaction</button>
</div>
</div>
<div class="container transaction" id=receipt_div>
<span data-i18n=contract/txhash></span>
<i data-i18n=contract/clickToCheck></i>↓
<a id=receipt></a>
<div class=form-group>
<label for=signed data-i18n=send-nas/receipt>receipt :</label>
<textarea id=receipt_state rows=6 class=form-control disabled></textarea>
</div>
</div>
<div class="fade modal" id=exampleModal tabindex=-1 role=dialog aria-labelledby=exampleModalLabel aria-hidden=true>
<div class=modal-dialog role=document>
<div class=modal-content>
<div class=modal-header>
<h5 class=modal-title id=exampleModalLabel data-i18n=send-nas/transfer_msg>transfer message :</h5>
<button type=button class=close data-dismiss=modal aria-label=Close>
<span aria-hidden=true>×</span>
</button>
</div>
<div class=modal-body>
<div>
<label data-i18n=send-nas/from-address>From Address</label>
<input type=text class=form-control id=for_addr disabled>
</div>
<div>
<label data-i18n=send-nas/to-address>To Address</label>
<input type=text class=form-control id=to_addr disabled>
</div>
<div>
<label for=amount data-i18n=send-nas/amount>Value / Amount to Send</label>
<div id=value_container class=number-comma data-data-i18n=amount data-id=value data-unit=currency data-disabled></div>
</div>
<!-- <div class=data>
<label>data</label>
<input type=text class=data id=data disabled style="width: 100%;">
</div> -->
</div>
<div class=modal-footer>
<button class="btn btn-secondary" data-dismiss=modal data-i18n=send-nas/close>Close</button>
<button class="btn btn-primary send" data-dismiss=modal data-i18n=send-nas/check></button>
</div>
</div>
</div>
</div>
<div class="fade modal" tabindex=-1 id=info role=dialog>
<div class=modal-dialog role=document>
<div class=modal-content>
<div class=modal-header>
<button type=button class=close data-dismiss=modal aria-label=Close>
<span aria-hidden=true>×</span>
</button>
<h4 class=modal-title>info</h4>
</div>
<div class=modal-body>
<p>unlocking…</p>
</div>
</div>
</div>
</div>
<div class=footer></div>
<script src=lib/jquery-3.3.1.min.js></script>
<script src=lib/bootstrap-4.0.0-dist/js/bootstrap.bundle.min.js data-depends=jquery.slim></script>
<script src=lib/bootbox.min.js data-depends="bootstrap jquery.slim"></script>
<script src=lib/jquery.qrcode.min.js data-depends=jquery></script>
<script src=lib/blockies.min.js></script>
<script src=lib/nebulas.js></script>
<script src=js/1-localSave.js></script>
<script src=js/home.v1.js></script>
<script src=js/i18n.js data-depends=jquery.slim></script>
<script src=js/ui-block.js data-depends="bootbox blockies jquery.slim i18n.js nebulas.js"></script>
<script>
"use strict";
var nebulas = require("nebulas"),
Account = nebulas.Account,
Neb = nebulas.Neb,
Transaction = nebulas.Transaction,
utils = nebulas.Utils,
unit = nebulas.Unit,
neb = new Neb(),
gAccount = new Account(),
gLastGenerateInfo = {},
validateStep1 = uiBlock.validate("#step1"),
validateStep2 = uiBlock.validate("#step2"),
gFromState;
neb.setRequest(new nebulas.HttpRequest(localSave.getItem("apiPrefix") || "https://testnet.nebulas.io/"));
$("#exampleModal .send").on("click", onClickExampleModalSend);
$("#generate").on("click", onClickGenerate);
$("#generate-infomation").on("click", onClickGenerateInfomation);
$("#signedoffline").on({
focus: onFocusSignedOffline,
input: onInputSignedOffline
});
$("#step3 .send").on("click", onClickStep3Send);
uiBlock.insert({
footer: ".footer",
header: ".header",
iconAddress: ".icon-address",
logoMain: ".logo-main",
numberComma: ".number-comma",
selectWalletFile: [".select-wallet-file", onUnlockFile]
});
function onUnlockFile(swf, fileJson, account, password) {
var address;
// test11
try {
account.fromKey(fileJson, password);
address = account.getAddressString();
gAccount = account;
$("#step2 .icon-address.from input").val(address).trigger("input");
$(".transaction").show();
} catch (e) {
bootbox.dialog({
backdrop: true,
onEscape: true,
message: localSave.getItem("lang") == "en" ? e : "keystore 文件错误, 或者密码错误",
size: "large",
title: "Error"
});
}
}
function onClickStep3Send() {
var val = $("#signedoffline").val(), tx;
if (val) try {
tx = Transaction.prototype.fromProto(val);
var to = tx.to.getAddressString();
var value = tx.value;
var currency = "NAS"
if (tx.data.payload) {
var data = eval("var d = " + nebulas.CryptoUtils.toBuffer(tx.data.payload).toString() + "; d");
var args = eval("var args = " + data["Args"]+"; args");
currency = uiBlock.getCurrencyByContractAddr(tx.to.getAddressString());
to = args[0];
value = args[1]
}
$("#value_container").attr("data-unit", currency);
$("#for_addr").val(tx.from.getAddressString());
$("#to_addr").val(to);
var balance;
if(uiBlock.currency === "NAX") {
balance = unit.fromBasic(value, "gwei").toString();
} else {
balance = unit.fromBasic(value, "nas").toString(10);
}
$("#value").val(balance).trigger("input");
} catch (err) {
bootbox.dialog({
backdrop: true,
onEscape: true,
message: localSave.getItem("lang") == "en" ? err.message : "无效的签名交易",
size: "large",
title: "Error"
});
}
}
function onClickGenerate() {
var fromaddress, toaddress, amount, nonce, gaslimit, gasprice, toaccount, tx;
if (validateStep2()) {
fromaddress = $(".icon-address.from input").val();
toaddress = $(".icon-address.to input").val().trim();
amount = $("#amount").val();
nonce = $("#nonce").val();
gaslimit = $("#limit").val();
gasprice = $("#price").val();
var contract = null;
toaccount = Account.fromAddress(toaddress);
if (uiBlock.currency != "NAS") {
var transfer_amount;
if(uiBlock.currency === "NAX") {
transfer_amount = unit.toBasic(amount, "gwei").toString();
} else {
transfer_amount = unit.nasToBasic(amount).toString(10);
}
contract = {"source": "", "sourceType": "js", "function": "transfer", "args": "[\"" + toaccount.getAddressString() + "\", \"" + transfer_amount + "\"]", "binary": "", "type": "call"};
}
if (gLastGenerateInfo.fromaddress != fromaddress ||
gLastGenerateInfo.toaddress != toaddress ||
gLastGenerateInfo.amount != amount ||
gLastGenerateInfo.nonce != nonce ||
gLastGenerateInfo.gaslimit != gaslimit ||
gLastGenerateInfo.gasprice != gasprice ||
gLastGenerateInfo.contract != contract) try {
var a = amount.split(".");
var amountValid = a.length == 1 || a[1].length <= 18;
if (uiBlock.currency == "NAS") {
if (!amountValid) throw new Error("Invalid value! The minimum unit is wei (1^-18nas) ");
tx = new Transaction(parseInt(localSave.getItem("chainId")), gAccount, toaccount.getAddressString(), unit.nasToBasic(amount), parseInt(nonce), gasprice, gaslimit);
} else {
if (!amountValid) throw new Error("Invalid value! The minimum unit is wei (1^-18" + uiBlock.currency.toLowerCase() + ") ");
tx = new Transaction(parseInt(localSave.getItem("chainId")), gAccount, uiBlock.getContractAddr(uiBlock.currency), unit.nasToBasic("0"), parseInt(nonce), gasprice, gaslimit, contract);
}
tx.signTransaction();
$("#raw").val(tx.toString());
$("#signed").val(tx.toProtoString());
$("#signedoffline").val(tx.toProtoString());
// $("#qrcode").qrcode(tx.toProtoString());
$("<div id=qrcode style=text-align:center;></div>")
.qrcode(tx.toProtoString())
.replaceAll('#qrcode');
$(".transaction").show();
gLastGenerateInfo.fromaddress = fromaddress;
gLastGenerateInfo.toaddress = toaddress;
gLastGenerateInfo.amount = amount;
gLastGenerateInfo.nonce = nonce;
gLastGenerateInfo.gaslimit = gaslimit;
gLastGenerateInfo.gasprice = gasprice;
gLastGenerateInfo.contract = contract;
} catch (e) {
bootbox.dialog({
backdrop: true,
onEscape: true,
message: e,
size: "large",
title: "Error"
});
}
}
}
function onClickGenerateInfomation() {
var fromaddress = $(".icon-address.from input").val();
if (validateStep1())
try {
neb.api.gasPrice()
.then(function (resp) {
$("#fa_gasprice").val(resp.gas_price);
$("#price").val(resp.gas_price);
return neb.api.getAccountState(fromaddress);
})
.then(function (resp) {
gFromState = {
balance: resp.balance,
nonce: resp.nonce
};
$("#fa_nonce").val(parseInt(resp.nonce) + 1);
$("#nonce").val(parseInt(resp.nonce) + 1);
$("#information").show();
})
.catch(function (e) {
// https://stackoverflow.com/questions/30715367/why-can-i-not-throw-inside-a-promise-catch-handler
bootbox.dialog({
backdrop: true,
onEscape: true,
message: e,
size: "large",
title: "Error"
});
});
} catch (e) {
bootbox.dialog({
backdrop: true,
onEscape: true,
message: e,
size: "large",
title: "Error"
});
}
}
function onFocusSignedOffline() {
$("[data-validate-order-matters]").removeClass("invalid").popover("hide");
}
function onInputSignedOffline() {
$("<div id=qrcode></div>")
.qrcode($("#signedoffline").val())
.replaceAll('#qrcode');
}
function onClickExampleModalSend() {
neb.api.sendRawTransaction($("#signedoffline").val())
.then(function (resp) {
return neb.api.getTransactionReceipt(resp.txhash);
}).then(function (resp) {
$("#receipt_state").val(JSON.stringify(resp));
$("#receipt").text(resp.hash).prop("href", "check.html?" + resp.hash);
$("#receipt_div").show();
}).catch(function (o) {
bootbox.dialog({
backdrop: true,
onEscape: true,
message: i18n.apiErrorToText(o.message),
size: "large",
title: "Error"
});
});
}
</script>
</body>
</html>