forked from jatchili/minimalist-ripple-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.html
1593 lines (1365 loc) · 42.6 KB
/
dev.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>minimalist ripple client</title>
<style>
.hidden { visibility: hidden; }
.displaynone { display: none !important; }
#mysecret[disabled] { border: none; }
#myaddress:empty:before { content: "(no identity set)"; }
table:empty { display: none; }
td.centered { text-align: center; }
input[type="text"] { width: 240px; }
input[type="text"].wide { width: 280px; }
input[type="text"].narrow { width: 120px; }
button:not(.offline) {
/* buttons that query the network */
cursor: pointer;
font-weight: bold;
}
.editflagbutton { width: 60px; }
#sendbutton, .complexsendbutton, .cancelbutton { width: 72px; }
#trustbutton { width: 84px; }
#startpathfindingbutton, #rekeybutton { width: 100px; }
.tilde { width: 30px; }
.halfoffset {
position: relative;
top: 9px;
}
.pathsJSON div { display: none; }
.pathsJSON:hover div {
display: block;
position: absolute;
word-break: break-all;
width: 200px;
border: 1px solid black;
padding: 8px;
background-color: white;
}
.pathsJSON:after {
content: "hover to view";
font-style: italic;
}
.pathsJSON:after:hover { content: ""; }
</style>
</head>
<body>
<noscript><h2>you need to enable javascript to use this!</h2><hr/></noscript>
<h1>minimalist ripple client</h1>
<h2>identity</h2>
<span id="myaddress"></span><br/>
<button class="offline" onclick="toggleSecret()">show/hide secret</button>
<input id="mysecret" type="text" placeholder="secret" /><br/>
<button class="offline" onclick="pressSetIdentity()">set identity</button>
<button class="offline" onclick="pressClearIdentity()">clear identity</button>
<button class="offline" onclick="pressGenerateIdentity()">generate identity</button>
<button id="rekeybutton" onclick="pressRekey()">rekey account</button><br/>
<span class="displaynone" id="encryptshow">
<button class="offline" onclick="showEncryptor()">encrypt secret</button>
</span>
<input class="displaynone" id="passphrase" type="password" placeholder="passphrase" />
<input class="displaynone" id="confirmpassphrase" type="password" placeholder="confirm passphrase" />
<span class="displaynone" id="decryptbutton">
<button class="offline" onclick="pressDecrypt()">decrypt</button>
</span>
<span class="displaynone" id="encryptbutton">
<button class="offline" onclick="pressEncryptSecret()">encrypt</button>
</span>
<span class="hidden"> <!-- space-filler -->
<button>_</button><input type="password"/>
</span>
<h2>connect</h2>
<button onclick="connectToRipple()">connect to ripple</button>
<button onclick="disconnectFromRipple()">disconnect</button><br/><br/>
<input id="nameurl" type="text" value="id.ripple.com/v1/authinfo?username=" /><br/>
<label><input id="useripplenames" type="checkbox"/> resolve ripple names with ~ button</label>
<h2>account details</h2>
<button class="offline" onclick="useCurrentIdentity()">use current identity</button>
<button class="offline" onclick="clearAllInfo()">clear all</button><br/>
<input id="infoaccount" type="text" placeholder="account" />
<button tabindex="-1" class="tilde" onclick="pressResolve(this,['infoaccount'])">~</button>
<br/>
<button onclick="pressInfo()">view info</button><br/>
<table id="infotable" border="1"></table>
<button onclick="pressLines()">view trustlines</button><br/>
<table id="linestable" border="1"></table>
<button onclick="pressOffers()">view/edit offers</button><br/>
<table id="offerstable" border="1"></table>
<button onclick="pressFlags()">view/edit flags</button><br/>
<table border="1" id="flagstable"></table>
<h2>send payment</h2>
<input type="text" id="sendamount" placeholder="amount" /><br/>
<input type="text" id="sendcurrency" class="narrow" placeholder="currency" />
<label>
<input type="checkbox" id="directxrp" onclick="toggleDirectXRP(this)"/>direct xrp→xrp
</label><br/>
<input type="text" id="senddestination" placeholder="destination" />
<button tabindex="-1" class="tilde" onclick="pressResolve(this,['senddestination'])">~</button>
<br/>
<label><input id="useoptional" onclick="toggleOptionalFields(this)" type="checkbox"/>use optional fields</label><br/>
<div id="optionalfields" class="displaynone">
<input type="text" id="destinationtag" placeholder="destination tag (integer)" /><br/>
<input type="text" id="sourcetag" placeholder="source tag (integer)" /><br/>
<input type="text" id="invoiceid" placeholder="invoice id (at most 64 hex chars)" /><br/>
</div>
<div id="directsender" class="displaynone">
<button id="sendbutton" onclick="pressSend()">send</button>
<table border="1" class="hidden"></table> <!-- space-filler -->
</div>
<div id="complexsender">
<button onclick="pressPathfind()" id="startpathfindingbutton">find paths</button>
<button onclick="stopPathfinding()" id="stoppathfindingbutton" disabled="disabled">stop</button><br/>
<table border="1" id="pathstable"></table>
</div>
<h2>set trust</h2>
<input id="trustamount" type="text" placeholder="amount" /><br/>
<input id="trustcurrency" type="text" placeholder="currency" /><br/>
<input id="trustissuer" type="text" placeholder="issuer" />
<button tabindex="-1" class="tilde" onclick="pressResolve(this,['trustissuer'])">~</button><br/>
<label><input id="trustnoripple" type="checkbox" / >no ripple</label><br/>
<button id="trustbutton" onclick="pressTrust()">set trust</button>
<h2>trade</h2>
<input type="text" class="wide" id="curissuer1" placeholder="CUR/issuer #1" />
<button tabindex="-1" class="tilde halfoffset" onclick="pressResolve(this,['curissuer1','curissuer2'])">~</button><br/>
<input type="text" class="wide" id="curissuer2" placeholder="CUR/issuer #2" /><br/>
<button onclick="pressBook()">view order book</button>
<button class="offline" onclick="clearBooks()">clear</button><br/>
<table border="1" class="booktable" id="bidtable"></table>
<table border="1" class="booktable" id="asktable"></table><br/>
<input type="text" class="wide" id="tpamount" placeholder="TakerPays amount" /><br/>
<input type="text" class="wide" id="tpcurissuer" placeholder="TakerPays CUR/issuer" />
<button tabindex="-1" class="tilde halfoffset" onclick="pressResolve(this,['tpcurissuer','tgcurissuer'])">~</button><br/>
<input type="text" class="wide" id="tgamount" placeholder="TakerGets amount" /><br/>
<input type="text" class="wide" id="tgcurissuer" placeholder="TakerGets CUR/issuer" /><br/>
<button onclick="pressOffer()" id="offerbutton">submit offer</button>
<h2>disclaimer</h2>
<p>this product is provided "as is" without warranty of any kind and is not endorsed by ripple labs inc.</p>
<script src="ripple-0.12.5-rc2-min.js"></script>
<script>
// Opening setup
// --------------------------------
var MY_ADDRESS;
var MASTER_SECRET;
var remote = new ripple.Remote({
servers: [
"wss://s-west.ripple.com:443",
"wss://s-east.ripple.com:443"
]
});
document.getElementById("directxrp").checked = false;
document.getElementById("useoptional").checked = false;
document.getElementById("mysecret").value = "";
clearAllInfo();
var VER_REKEYED = 220;
var VER_ENCRYPTED_ORDINARY = 160;
var VER_ENCRYPTED_REKEYED = 250;
// Helper functions
// --------------------------------
// State/input validation
var validate = {
number: function(x) {
return x!=="" && !isNaN(x);
},
address: function(x) {
return ripple.UInt160.is_valid(x);
},
currency: function(x) {
return x.length===3 && x.toUpperCase()===x;
},
integer: function(x) {
// Allow x==="" because "integer" is only used on optional fields
return x==="" || (!isNaN(x) && parseInt(x)===parseFloat(x));
},
"hex string": function(x) {
// Allow x==="" because "hex string" is only used on optional fields
return /^[0-9A-Fa-f]*$/.test(x) && x.length<=64;
},
secret: function(x) {
return !isNaN(ripple.Base.decode_check(ripple.Base.VER_FAMILY_SEED, x));
},
encryptedSecret: function(x) {
return !isNaN(ripple.Base.decode_check(VER_ENCRYPTED_ORDINARY, x)) ||
!isNaN(ripple.Base.decode_check(VER_ENCRYPTED_REKEYED, x))
},
"currency/issuer pair": function(x) {
var s = x.split("/");
if (s.length === 1 && s[0] === "XRP") {
return true;
} else {
return (s.length === 2 &&
validate.currency(s[0]) &&
validate.address(s[1])
);
}
},
ALLOW_ALL: function(x) {
return true;
}
};
function mustConnect(r) {
if (remote.isConnected()) {
return r;
} else {
return {error: "you must connect first"};
}
}
function mustIdentify(r) {
if (MASTER_SECRET) {
return r;
} else {
return {error: "you must set an identity first"};
}
}
function mustBeValid(values, types) {
if (values.length !== types.length) {
throw "values and types must be the same length";
}
return function(r) {
var returnValues = [];
for (var i=0; i<values.length; i++) {
var value = eval(values[i]);
var type = types[i];
if (!validate[type](value)) {
return {error: '"'+value+'" is not a valid '+type};
} else {
returnValues.push(value);
}
}
return {values: returnValues};
};
}
function guard(guardians, target) {
return function(){
var result = {}
for (var i=0; i<guardians.length; i++) {
var guardian = guardians[i];
result = guardian(result);
if (result.error) {
alert(result.error);
return;
}
}
target.apply(this, result.values);
};
}
// Ripple names
function httpGet(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(null, xhr.responseText);
}
}
xhr.onerror = function() {
callback(true);
}
xhr.open("GET", url, true);
xhr.send(null);
}
var rnCache = {};
function resolveRippleName(name, callback) {
var nameURL = document.getElementById("nameurl").value;
if (!rnCache[nameURL]) {
rnCache[nameURL] = {};
}
if ("undefined" !== typeof rnCache[nameURL][name]) {
callback(null, rnCache[nameURL][name]);
} else {
httpGet("https://"+nameURL+name, function(err, res){
var result;
if (!err) {
var parsed = JSON.parse(res);
var result = parsed.exists && parsed.address;
rnCache[nameURL][name] = result;
}
callback(err, result);
});
}
}
function pressResolve(pressedButton, inputs) {
if (!document.getElementById("useripplenames").checked) {
alert("you must enable the ~ button");
return;
}
pressedButton.disabled = "disabled";
pressedButton.innerHTML = "...";
var numberOfResponses = 0;
function concludeResponse() {
numberOfResponses++;
if (numberOfResponses === inputs.length) {
pressedButton.disabled = "";
pressedButton.innerHTML = "~";
}
}
var alreadyAlerted = false;
for (var i=0; i<inputs.length; i++) {
var input = inputs[i];
var value = document.getElementById(input).value;
var ci = value.split("/");
var currency = false;
if (ci.length === 2) {
currency = ci[0];
value = ci[1];
}
if (!value || inputs.length > 1 && !currency && value === "XRP" || validate.address(value)) {
concludeResponse();
continue;
}
if (value.charAt(0) !== "~") {
alreadyAlerted || alert("ripple name must begin with ~");
alreadyAlerted = true;
concludeResponse();
} else {
resolveRippleName(value, (function(){
var inp = input;
var cur = currency;
return function(err, result) {
concludeResponse();
if (err) {
alreadyAlerted || alert("could not reach ripple name resolver");
alreadyAlerted = true;
} else if (!result) {
alreadyAlerted || alert("could not resolve ripple name "+value);
alreadyAlerted = true;
} else {
document.getElementById(inp).value = cur ?
cur+"/"+result : result;
}
}
})());
}
}
}
// Miscellaneous
function notifyOutcome(err, res) {
var outcome = "transaction finalized with " +
(err ? "error" : "result") + ":";
prompt(outcome, JSON.stringify(err||res));
}
// This is a really kludgy workaround to deal with the fact that
// ripple-lib doesn't currently support signing transactions with
// a RegularKey.
var NORMAL_SIGNING_FUNCTION = ripple.Transaction.prototype.sign;
function redefineSigningToAlwaysUseSecret(secret) {
if (!secret) {
ripple.Transaction.prototype.sign = NORMAL_SIGNING_FUNCTION;
} else {
ripple.Transaction.prototype.sign = function(testnet) {
this.setSecret(secret);
this.tx_json.SigningPubKey = ripple.Seed.from_json(this._secret).get_key().to_hex_pub();
var seed = ripple.Seed.from_json(this._secret);
var prev_sig = this.tx_json.TxnSignature;
delete this.tx_json.TxnSignature;
var hash = this.signingHash(testnet);
// If the hash is the same, we can re-use the previous signature
if (prev_sig && hash === this.previousSigningHash) {
this.tx_json.TxnSignature = prev_sig;
return this;
}
var key = seed.get_key();
var sig = key.sign(hash);
var hex = ripple.sjcl.codec.hex.fromBits(sig).toUpperCase();
this.tx_json.TxnSignature = hex;
this.previousSigningHash = hash;
return this;
};
}
}
// Identity
// --------------------------------
function pressSetIdentity() {
var secret = document.getElementById("mysecret").value;
if (document.getElementById("myaddress").innerHTML) {
alert("you must clear the old identity first");
} else if (secret) {
if (secret.charAt(0) === "s" && setOrdinarySecret(secret)) {
// pass
} else if (secret.charAt(0) === "3" && setRekeyedSecret(secret)) {
// pass
} else if (secret.charAt(0) === "h" && setEncryptedSecret(secret)) {
// pass
} else {
alert('"'+secret+'" is not a valid secret');
}
} else {
alert("you must enter a secret");
}
}
function setOrdinarySecret(secret) {
if (!validate.secret(secret)) {
return false;
} else {
var result = generateAddress(secret);
setIdentity(result.address, result.secret, result.secret);
remote.setSecret(result.address, result.secret);
redefineSigningToAlwaysUseSecret(false);
return true;
}
}
function setRekeyedSecret(secret) {
// This uses a custom format that is specific to this application:
// ripple.Base.encode_check(VER_REKEYED, regular_key_secret concat master_secret)
if (secret.length < 2) {
return false;
}
var b = ripple.Base.decode_check(VER_REKEYED, secret);
if (isNaN(b)) {
return false;
}
var y = ripple.sjcl.codec.bytes.fromBits(b.toBits());
if (y.length !== 32) {
return false;
}
var rks = ripple.Base.encode_check(ripple.Base.VER_FAMILY_SEED, y.slice(0,16));
var ms = ripple.Base.encode_check(ripple.Base.VER_FAMILY_SEED, y.slice(16));
var ra = generateAddress(ms).address;
setIdentity(ra, secret, ms);
remote.setSecret(ra, ms);
remote.setSecret(generateAddress(rks).address, rks);
redefineSigningToAlwaysUseSecret(rks);
return true;
}
function setEncryptedSecret(encryptedSecret) {
if (!validate.encryptedSecret(encryptedSecret)) {
return false;
}
showDecryptor();
return true;
}
function pressDecrypt() {
var es = document.getElementById("mysecret").value;
var passphrase = document.getElementById("passphrase").value;
if (es.charAt(0) !== "h" || !validate.encryptedSecret(es)) {
alert('invalid encrypted secret');
return;
}
if (!passphrase) {
alert('you must enter a passphrase');
return;
}
var secret;
try {
secret = decryptSecret(passphrase, es);
} catch(err) {
alert('invalid passphrase or encrypted secret');
document.getElementById("passphrase").value = "";
return;
}
if (secret.charAt(0) === "s" && setOrdinarySecret(secret)) {
hideDecryptor();
} else if (secret.charAt(0) === "3" && setRekeyedSecret(secret)) {
hideDecryptor();
} else {
alert('invalid passphrase or encrypted secret');
document.getElementById("passphrase").value = "";
}
}
function showDecryptor() {
document.getElementById("passphrase").className = "";
document.getElementById("decryptbutton").className = "";
document.getElementById("encryptshow").className = "displaynone";
}
function hideDecryptor() {
var pp = document.getElementById("passphrase");
pp.value = "";
pp.className = "displaynone";
document.getElementById("decryptbutton").className = "displaynone";
}
function showEncryptorShower() {
document.getElementById("encryptshow").className = "";
}
function showEncryptor() {
document.getElementById("passphrase").className = "";
document.getElementById("confirmpassphrase").className = "";
document.getElementById("encryptshow").className = "displaynone";
document.getElementById("encryptbutton").className = "";
}
function hideEncryptor() {
var pp = document.getElementById("passphrase")
pp.className = "displaynone";
pp.value = "";
var pp2 = document.getElementById("confirmpassphrase")
pp2.className = "displaynone";
pp2.value = "";
document.getElementById("encryptshow").className = "";
document.getElementById("encryptbutton").className = "displaynone";
}
function pressEncryptSecret() {
var pp = document.getElementById("passphrase").value;
var pp2 = document.getElementById("confirmpassphrase").value;
if (!pp || !pp2) {
alert("you must enter something");
} else if (pp !== pp2) {
alert("passphrases must match");
} else {
var es = encryptSecret(pp, document.getElementById("mysecret").value);
prompt("your encrypted secret is:", es);
hideEncryptor();
}
}
function encryptSecret(passphrase, secret) {
var payloadBytes = ripple.Base.decode(secret);
var version = payloadBytes[0]===ripple.Base.VER_FAMILY_SEED ?
VER_ENCRYPTED_ORDINARY : VER_ENCRYPTED_REKEYED;
var salt = ripple.sjcl.random.randomWords(2); // 8 bytes of salt
var streamBytes = ripple.sjcl.codec.bytes.fromBits(
ripple.sjcl.misc.pbkdf2(passphrase, salt, void(0), 8*payloadBytes.length)
);
var encryptedBytes = streamBytes.map(function(x,i) {
return x ^ payloadBytes[i];
});
var saltBytes = ripple.sjcl.codec.bytes.fromBits(salt);
return ripple.Base.encode_check(version, saltBytes.concat(encryptedBytes));
}
function decryptSecret(passphrase, es) {
var ao = ripple.Base.decode_check(VER_ENCRYPTED_ORDINARY, es);
var ar = ripple.Base.decode_check(VER_ENCRYPTED_REKEYED, es);
var a = isNaN(ao) ? ar : ao;
ripple.sjcl.codec.bytes.fromBits(a.toBits());
var b = ripple.sjcl.codec.bytes.fromBits(a.toBits());
var saltBytes = b.slice(0,8)
var encryptedBytes = b.slice(8)
var streamBytes = ripple.sjcl.codec.bytes.fromBits(
ripple.sjcl.misc.pbkdf2(passphrase, ripple.sjcl.codec.bytes.toBits(saltBytes), void(0), 8*encryptedBytes.length)
);
var payloadBytes = streamBytes.map(function(x,i) {
return x ^ encryptedBytes[i];
});
var result = ripple.Base.encode(payloadBytes);
return result;
}
function setIdentity(address, displayedSecret, masterSecret) {
MY_ADDRESS = address;
MASTER_SECRET = masterSecret;
document.getElementById("mysecret").value = displayedSecret;
document.getElementById("myaddress").innerHTML = address;
document.getElementById("mysecret").disabled="disabled";
document.getElementById("mysecret").className="hidden";
showEncryptorShower();
}
function pressGenerateIdentity() {
if (document.getElementById("myaddress").innerHTML) {
alert("you must clear the old identity first");
} else {
var result = generateAddress();
setIdentity(result.address, result.secret, result.secret);
remote.setSecret(result.address, result.secret);
alert("save your secret before you close the window!");
}
}
function pressClearIdentity() {
if (confirm("are you sure? the secret will be lost!")) {
document.getElementById("mysecret").value = "";
document.getElementById("myaddress").innerHTML = "";
setIdentity("", "");
document.getElementById("mysecret").disabled=null;
document.getElementById("mysecret").className="";
hideEncryptor();
hideDecryptor();
document.getElementById("encryptshow").className = "displaynone";
}
}
function toggleSecret() {
var s = document.getElementById("mysecret");
if (s.className === "hidden") {
s.className = "";
} else {
s.className = "hidden";
}
}
function generateAddress(secret) {
if (secret) {
secret = ripple.Seed.from_json(secret);
} else {
secret = ripple.Seed.from_bits(ripple.sjcl.random.randomWords(4));
}
return {
secret: secret.to_json(),
address: secret.get_key().get_address().to_json()
};
}
function generateRekeyedKey() {
var keypair = generateAddress();
var a1 = ripple.sjcl.codec.bytes.fromBits(ripple.Seed.from_json(keypair.secret).to_bits());
var a2 = ripple.sjcl.codec.bytes.fromBits(ripple.Seed.from_json(MASTER_SECRET).to_bits());
var rks = ripple.Base.encode_check(VER_REKEYED, a1.concat(a2));
return {
regularKey: keypair.address,
rekeyedSecret: rks,
actualSecret: keypair.secret
};
}
var pressRekey = guard([
mustConnect,
mustIdentify,
], function() {
var rekeyedKey = generateRekeyedKey();
if (null!==prompt(
"This will submit a transaction to invalidate your current " +
"secret and replace it with the new secret below, which you "+
"should save before continuing. Are you sure you want to do "+
"this?", rekeyedKey.rekeyedSecret)
) {
var rb = document.getElementById("rekeybutton");
rb.disabled = "disabled";
rb.innerHTML = "rekeying...";
rekeyAccount(rekeyedKey);
}
}
);
function rekeyAccount(rekeyedKey) {
remote.requestAccountInfo({
account: MY_ADDRESS,
ledger: "validated"
}, function(err, info) {
if (err) {
prompt("requestAccountInfo failed with error:", JSON.stringify(err));
reenableRB();
} else {
// Set regular key
var transaction = remote.createTransaction("SetRegularKey", {
account: MY_ADDRESS,
regular_key: rekeyedKey.regularKey
});
transaction.submit(function(err, res) {
if (err) {
prompt("SetRegularKey failed with error:", JSON.stringify(err));
reenableRB();
} else {
var dm = ripple.Remote.flags.account_root.DisableMaster;
if (info.account_data.Flags && (info.account_data.Flags & dm)) {
concludeRekeying(rekeyedKey);
} else { // We need to set the DisableMaster flag now.
var transaction2 = remote.createTransaction("AccountSet", {
account: MY_ADDRESS,
set_flag: ripple.Transaction.set_clear_flags.AccountSet.asfDisableMaster
});
transaction2.submit(function(err, res) {
if (err) {
prompt("DisableMaster transaction failed with error:", JSON.stringify(err));
}
concludeRekeying(rekeyedKey);
});
}
}
});
}
});
}
function concludeRekeying(rekeyedKey) {
remote.setSecret(rekeyedKey.regularKey, rekeyedKey.actualSecret);
redefineSigningToAlwaysUseSecret(rekeyedKey.actualSecret)
document.getElementById("mysecret").value = rekeyedKey.rekeyedSecret;
alert("rekeying complete");
reenableRB();
}
function reenableRB() {
var rb = document.getElementById("rekeybutton");
rb.disabled = "";
rb.innerHTML = "rekey account";
}
// Connect
// --------------------------------
function connectToRipple() {
remote.connect(function(err, res) {
res || (res = "success");
var outcome = "connected to ripple with " +
(err ? "error" : "result") + ": " +
JSON.stringify(err||res);
alert(outcome);
});
}
function disconnectFromRipple() {
remote.disconnect(function(){
alert("disconnected");
});
}
// Account Details
// --------------------------------
// Helper functions
function useCurrentIdentity() {
if (MY_ADDRESS && MY_ADDRESS !== "") {
document.getElementById("infoaccount").value = MY_ADDRESS;
clearAllInfo();
}
}
function clearAllInfo() {
document.getElementById("infotable").innerHTML = "";
document.getElementById("linestable").innerHTML = "";
document.getElementById("offerstable").innerHTML = "";
document.getElementById("flagstable").innerHTML = "";
var ia = document.getElementById("infoaccount");
ia.readOnly = false;
ia.disabled = "";
}
function accountInfoError(err, table) {
if (err.remote && err.remote.error === "actNotFound") {
table.innerHTML = "account not found";
} else {
prompt("request failed with error:", JSON.stringify(err));
}
}
// Button presses
function takeAddressAndDo(action) {
return guard([
mustConnect,
mustBeValid([
'document.getElementById("infoaccount").value'
],["address"])
], function(account){
var ia = document.getElementById("infoaccount");
ia.readOnly = true;
ia.disabled = "disabled";
action(account);
});
}
var pressInfo = takeAddressAndDo(getAccountInfo);
var pressLines = takeAddressAndDo(getAccountLines);
var pressOffers = takeAddressAndDo(getAccountOffers);
var pressFlags = takeAddressAndDo(loadFlagsTable);
// Query the network
function getAccountInfo(account) {
remote.requestAccountInfo({
account: account,
ledger: "validated"
}, function(err, res) {
var table = document.getElementById("infotable");
if (err) {
accountInfoError(err, table);
} else {
var content = "";
var data = res.account_data;
for (var key in data) {
content += ("<tr><th>"+key+"</th><td>"+data[key]+"</td></tr>");
}
table.innerHTML = content;
}
});
}
function getAccountLines(account) {
remote.requestAccountLines({
account: account,
ledger: "validated"
}, function(err, res) {
var table = document.getElementById("linestable");
if (err) {
accountInfoError(err, table);
} else {
var content = "";
var lines = res.lines;
if (!lines.length) {
table.innerHTML = "account has no trustlines";
} else {
for (var i=0; i<lines.length; i++) {
var data = lines[i];
var entry = "";
for (var key in data) {
entry += ("<tr><th>"+key+"</th><td>"+data[key]+"</td></tr>");
}
entry = "<table>"+entry+"</table>";
content += ("<tr><td>"+entry+"</td></tr>");
}
table.innerHTML = content;
}
}
});
}
function getAccountOffers(account) {
remote.requestAccountOffers({
account: account,
ledger: "validated"
}, function(err, res) {
var table = document.getElementById("offerstable");
if (err) {
accountInfoError(err, table);
} else {
var content = "";
if (!res.offers.length) {
content = "account has no offers";
} else {
content = "<tr>"+
"<th colspan=2>TakerGets</th>"+
"<th colspan=2>TakerPays</th>"+
"<th rowspan=2>cancel</th>"+
"</tr>"+
"<tr>"+
"<th>amount</th>"+
"<th>CUR/issuer</th>"+
"<th>amount</th>"+
"<th>CUR/issuer</th>"+
"</tr>";
for (var i=0; i<res.offers.length; i++) {
var offer = res.offers[i];
var tg = offer.taker_gets;
var tgc, tgv;
var tp = offer.taker_pays;
var tpc, tpv;
if (tg.currency) {
tgc = tg.currency+"/"+tg.issuer;
tgv = tg.value;
} else {
tgc = "XRP";
tgv = parseInt(tg) / 1000000;
}
if (tp.currency) {
tpc = tp.currency+"/"+tp.issuer;
tpv = tp.value;
} else {
tpc = "XRP";
tpv = parseInt(tp) / 1000000;
}
var args = '"' + res.account + '","' + offer.seq + '"';
content += ("<tr>"+
"<td>"+tgv+"</td>"+
"<td>"+tgc+"</td>"+
"<td>"+tpv+"</td>"+
"<td>"+tpc+"</td>"+
"<td><button class='cancelbutton' onclick='pressCancelOffer(this,"+args+
")'>cancel</button></td>"+
"</tr>");
}
}
table.innerHTML = content;
}
});
}
function loadFlagsTable(account) {
remote.requestAccountInfo({
account: account,
ledger: "validated"
}, function(err, res) {
var table = document.getElementById("flagstable");
if (err) {
accountInfoError(err, table);
} else {
var flags = parseInt(res.account_data.Flags);
var content = ("<tr>"+
"<th>flag</th>"+
"<th>value</th>"+
"<th>edit</th>"
);
for (var flagName in ripple.Remote.flags.account_root) {
var mask = ripple.Remote.flags.account_root[flagName];
var flagOn = (mask & flags);
content += ("<tr>"+