-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
1392 lines (1217 loc) · 49.9 KB
/
index.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 lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>LLEVM - EVM Chat</title>
<link rel="icon" type="image/png" href="bit.jpg">
<style>
@font-face {
font-family: 'Monaspace Argon';
src: url('/fonts/MonaspaceArgon-Regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
body,
html {
margin: 0;
padding: 0;
height: 100%;
color: #F8F8F2;
background-color: #282A36;
font-family: 'Monaspace Argon', monospace;
font-size: 14px;
line-height: 1.6;
}
.container {
display: flex;
height: 100%;
border: 1px solid #44475A;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.left-panel {
display: flex;
flex-direction: column;
width: 60%;
min-width: 200px;
border-right: 1px solid #44475A;
}
.search-container {
display: flex;
padding: 10px;
background-color: #44475A;
border-bottom: 1px solid #6272A4;
}
#contract-address {
flex-grow: 1;
margin-right: 10px;
background-color: #282A36;
color: #F8F8F2;
border: 1px solid #6272A4;
padding: 5px;
font-family: 'Monaspace Argon', monospace;
}
#search-btn {
background-color: #44475A;
color: #F8F8F2;
border: 1px solid #6272A4;
cursor: pointer;
font-family: 'Monaspace Argon', monospace;
padding: 5px 10px;
transition: background-color 0.3s;
}
#search-btn:hover {
background-color: #6272A4;
}
.tab-bar {
display: flex;
background-color: #44475A;
border-bottom: 1px solid #6272A4;
}
.tab {
padding: 5px 10px;
cursor: pointer;
border-right: 1px solid #6272A4;
transition: background-color 0.3s;
}
.tab:hover {
background-color: #6272A4;
}
.tab.active {
background-color: #282A36;
color: #FF79C6;
}
#bytecode-display,
#decompiled-display,
#opcodes-display,
#yul-display,
#instructions-display {
flex-grow: 1;
padding: 10px;
overflow-y: auto;
background-color: #282A36;
color: #F8F8F2;
font-family: 'Monaspace Argon', monospace;
line-height: 1.4;
white-space: pre-wrap;
word-wrap: break-word;
}
#instructions-display {
white-space: normal;
}
#instructions-display h2 {
color: #FF79C6;
margin-bottom: 10px;
font-size: 1.3em;
}
#instructions-display ol {
padding-left: 20px;
margin-top: 0;
margin-bottom: 10px;
}
#instructions-display li {
margin-bottom: 5px;
}
#instructions-display p {
margin-top: 0;
margin-bottom: 10px;
}
#instructions-display br {
display: none;
}
/* Remove any pre-existing styles that might affect spacing */
#instructions-display pre,
#instructions-display code {
white-space: normal;
margin: 0;
padding: 0;
}
/* Theme-specific styles */
.one-dark #instructions-display {
background-color: #282C34;
color: #ABB2BF;
}
.one-dark #instructions-display h2 {
color: #C678DD;
}
.catppuccin-mocha #instructions-display {
background-color: #1e1e2e;
color: #cdd6f4;
}
.catppuccin-mocha #instructions-display h2 {
color: #cba6f7;
}
.right-panel {
width: 40%;
min-width: 200px;
display: flex;
flex-direction: column;
}
#chat-history {
flex-grow: 1;
overflow-y: auto;
padding: 10px;
background-color: #282A36;
}
#chat-input {
border-top: 1px solid #44475A;
padding: 10px;
display: flex;
background-color: #44475A;
}
#chat-input input {
flex-grow: 1;
margin-right: 10px;
background-color: #282A36;
color: #F8F8F2;
border: 1px solid #6272A4;
padding: 5px;
font-family: 'Monaspace Argon', monospace;
}
.resizer-x {
width: 4px;
background: #44475A;
cursor: col-resize;
}
#send-chat {
background-color: #44475A;
color: #F8F8F2;
border: 1px solid #6272A4;
cursor: pointer;
font-family: 'Monaspace Argon', monospace;
padding: 5px 10px;
transition: background-color 0.3s;
}
#send-chat:hover {
background-color: #6272A4;
}
.chat-message {
padding: 5px;
margin: 5px 0;
border-radius: 3px;
}
.chat-message.user {
background-color: #44475A;
border-left: 3px solid #BD93F9;
}
.chat-message.assistant {
background-color: #383A59;
border-left: 3px solid #50FA7B;
}
.chat-message.error {
background-color: #44475A;
border-left: 3px solid #FF5555;
color: #FF5555;
}
/* Custom syntax highlighting theme */
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: #282A36;
color: #F8F8F2;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-section,
.hljs-link {
color: #FF79C6;
}
.hljs-function .hljs-keyword {
color: #8BE9FD;
}
.hljs-subst {
color: #F8F8F2;
}
.hljs-string,
.hljs-title,
.hljs-name,
.hljs-type,
.hljs-attribute,
.hljs-symbol,
.hljs-bullet,
.hljs-addition,
.hljs-variable,
.hljs-template-tag,
.hljs-template-variable {
color: #F1FA8C;
}
.hljs-comment,
.hljs-quote,
.hljs-deletion,
.hljs-meta {
color: #6272A4;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-title,
.hljs-section,
.hljs-doctag,
.hljs-type,
.hljs-name,
.hljs-strong {
font-weight: bold;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-number,
span.hljs-number,
span.hljs-literal,
span.hljs-params {
color: #BD93F9;
}
.hljs-regexp,
.hljs-built_in {
color: #50FA7B;
}
.hljs-tag {
color: #FF79C6;
}
.hljs-attr {
color: #50FA7B;
}
.hljs-symbol,
.hljs-bullet {
color: #FFB86C;
}
.hljs-code,
.hljs-title,
.hljs-section,
span.hljs-title {
color: #8BE9FD;
}
/* Ensure background color for all highlighted elements */
#bytecode-display pre,
#decompiled-display pre,
#opcodes-display pre,
#yul-display pre,
#bytecode-display code,
#decompiled-display code,
#opcodes-display code,
#yul-display code,
.hljs,
.language-solidity,
.language-plaintext,
.language-x86asm {
background-color: #282A36 !important;
}
#bytecode-display,
#decompiled-display,
#opcodes-display,
#yul-display,
#bytecode-display pre,
#decompiled-display pre,
#opcodes-display pre,
#yul-display pre,
#bytecode-display code,
#decompiled-display code,
#opcodes-display code,
#yul-display code {
color: #F8F8F2;
}
.opcode-tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.opcode-tooltip .tooltiptext {
visibility: hidden;
width: 200px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -100px;
opacity: 0;
transition: opacity 0.3s;
}
.opcode-tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
.scratch-space {
background-color: rgba(255, 121, 198, 0.2);
}
.free-memory-pointer {
background-color: rgba(189, 147, 249, 0.2);
}
.allocatable-memory {
background-color: rgba(80, 250, 123, 0.2);
}
.keyword {
color: #FF79C6;
}
.comment {
color: #6272A4;
}
.number {
color: #BD93F9;
}
.opcode-tooltip {
color: #50FA7B;
position: relative;
display: inline-block;
cursor: pointer;
}
.opcode-tooltip .tooltiptext {
visibility: hidden;
width: 200px;
background-color: #44475A;
color: #F8F8F2;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -100px;
opacity: 0;
transition: opacity 0.3s;
}
.opcode-tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
#theme-switcher {
position: absolute;
top: 10px;
right: 10px;
z-index: 1000;
}
#switch-theme {
background-color: #44475A;
color: #F8F8F2;
border: 1px solid #6272A4;
cursor: pointer;
font-family: 'Monaspace Argon', monospace;
padding: 5px 10px;
transition: background-color 0.3s;
}
#switch-theme:hover {
background-color: #6272A4;
}
/* One Dark theme */
body.one-dark,
.one-dark html {
color: #ABB2BF;
background-color: #282C34;
}
.one-dark .container {
border-color: #3E4451;
}
.one-dark .left-panel {
border-right-color: #3E4451;
}
.one-dark .search-container,
.one-dark .tab-bar {
background-color: #3E4451;
border-bottom-color: #5C6370;
}
.one-dark #contract-address,
.one-dark #chat-input input {
background-color: #282C34;
color: #ABB2BF;
border-color: #5C6370;
}
.one-dark #search-btn,
.one-dark #send-chat,
.one-dark #switch-theme {
background-color: #3E4451;
color: #ABB2BF;
border-color: #5C6370;
}
.one-dark #search-btn:hover,
.one-dark #send-chat:hover,
.one-dark #switch-theme:hover {
background-color: #5C6370;
}
.one-dark .tab.active {
background-color: #282C34;
color: #C678DD;
}
.one-dark #bytecode-display,
.one-dark #decompiled-display,
.one-dark #opcodes-display,
.one-dark #yul-display,
.one-dark #instructions-display,
.one-dark #chat-history {
background-color: #282C34;
}
.one-dark #chat-input {
background-color: #3E4451;
border-top-color: #5C6370;
}
.one-dark .chat-message.user {
background-color: #3E4451;
border-left-color: #C678DD;
}
.one-dark .chat-message.assistant {
background-color: #2C313A;
border-left-color: #98C379;
}
.one-dark .chat-message.error {
background-color: #3E4451;
border-left-color: #E06C75;
color: #E06C75;
}
.one-dark .keyword {
color: #C678DD;
}
.one-dark .comment {
color: #5C6370;
}
.one-dark .number {
color: #D19A66;
}
.one-dark .opcode-tooltip {
color: #98C379;
}
.one-dark .opcode-tooltip .tooltiptext {
background-color: #3E4451;
color: #ABB2BF;
}
.one-dark .scratch-space {
background-color: rgba(224, 108, 117, 0.2);
}
.one-dark .free-memory-pointer {
background-color: rgba(198, 120, 221, 0.2);
}
.one-dark .allocatable-memory {
background-color: rgba(152, 195, 121, 0.2);
}
.one-dark #instructions-display {
background-color: #282C34;
color: #ABB2BF;
}
.one-dark #instructions-display h2 {
color: #C678DD;
}
body.catppuccin-mocha,
.catppuccin-mocha html {
color: #cdd6f4;
background-color: #1e1e2e;
}
.catppuccin-mocha .container {
border-color: #313244;
}
.catppuccin-mocha .left-panel {
border-right-color: #313244;
}
.catppuccin-mocha .search-container,
.catppuccin-mocha .tab-bar {
background-color: #313244;
border-bottom-color: #45475a;
}
.catppuccin-mocha #contract-address,
.catppuccin-mocha #chat-input input {
background-color: #1e1e2e;
color: #cdd6f4;
border-color: #45475a;
}
.catppuccin-mocha #search-btn,
.catppuccin-mocha #send-chat,
.catppuccin-mocha #switch-theme {
background-color: #313244;
color: #cdd6f4;
border-color: #45475a;
}
.catppuccin-mocha #search-btn:hover,
.catppuccin-mocha #send-chat:hover,
.catppuccin-mocha #switch-theme:hover {
background-color: #45475a;
}
.catppuccin-mocha .tab.active {
background-color: #1e1e2e;
color: #cba6f7;
}
.catppuccin-mocha #bytecode-display,
.catppuccin-mocha #decompiled-display,
.catppuccin-mocha #opcodes-display,
.catppuccin-mocha #yul-display,
.catppuccin-mocha #instructions-display,
.catppuccin-mocha #chat-history {
background-color: #1e1e2e;
}
.catppuccin-mocha #chat-input {
background-color: #313244;
border-top-color: #45475a;
}
.catppuccin-mocha .chat-message.user {
background-color: #313244;
border-left-color: #cba6f7;
}
.catppuccin-mocha .chat-message.assistant {
background-color: #45475a;
border-left-color: #a6e3a1;
}
.catppuccin-mocha .chat-message.error {
background-color: #313244;
border-left-color: #f38ba8;
color: #f38ba8;
}
.catppuccin-mocha .keyword {
color: #cba6f7;
}
.catppuccin-mocha .comment {
color: #6c7086;
}
.catppuccin-mocha .number {
color: #fab387;
}
.catppuccin-mocha .opcode-tooltip {
color: #a6e3a1;
}
.catppuccin-mocha .opcode-tooltip .tooltiptext {
background-color: #313244;
color: #cdd6f4;
}
.catppuccin-mocha .scratch-space {
background-color: rgba(243, 139, 168, 0.2);
}
.catppuccin-mocha .free-memory-pointer {
background-color: rgba(203, 166, 247, 0.2);
}
.catppuccin-mocha .allocatable-memory {
background-color: rgba(166, 227, 161, 0.2);
}
.catppuccin-mocha #instructions-display {
background-color: #1e1e2e;
color: #cdd6f4;
}
.catppuccin-mocha #instructions-display h2 {
color: #cba6f7;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sevm.js"></script>
</head>
<body class="catppuccin-mocha">
<div id="theme-switcher">
<button id="switch-theme">Switch Theme</button>
</div>
<div class="container">
<div class="left-panel">
<div class="search-container">
<input type="text" id="contract-address" placeholder="Enter Ethereum contract address or bytecode" />
<button id="search-btn">Search</button>
</div>
<div class="tab-bar">
<div class="tab active" data-tab="bytecode">Bytecode</div>
<div class="tab" data-tab="decompiled">Decompiled</div>
<div class="tab" data-tab="opcodes">Opcodes</div>
<div class="tab" data-tab="yul">Yul</div>
<div class="tab" data-tab="instructions">Instructions</div>
</div>
<div id="bytecode-display"></div>
<div id="decompiled-display" style="display: none;"></div>
<div id="opcodes-display" style="display: none;"></div>
<div id="yul-display" style="display: none;"></div>
<div id="instructions-display" style="display: none;">
<h2>How to Use This Site</h2>
<ol>
<li>Enter an Ethereum contract address or bytecode in the search bar and click "Search" or press Enter.</li>
<li>The contract's bytecode will be displayed in the "Bytecode" tab.</li>
<li>Click on the "Decompiled" tab to view the decompiled Solidity-like code.</li>
<li>The "Opcodes" tab shows the low-level EVM opcodes of the contract.</li>
<li>Use the chat feature on the right to ask questions about the contract's code or functionality.</li>
</ol>
<p>This tool helps you analyze and understand Ethereum smart contracts. If you need assistance, feel free to ask in the chat. The LLM (Hermes-2-Pro-Llama-3-8B) runs entirely in your browser using WebGPU. Please note that the LLM's output may not always be accurate, so don't treat it as definitive.</p>
<p>For demonstration purposes only.</p>
</div>
</div>
<div class="resizer-x"></div>
<div class="right-panel">
<div id="chat-history"></div>
<div id="chat-input">
<input disabled type="text" id="chat-message" placeholder="Initializing..." />
<button disabled id="send-chat">Send</button>
</div>
</div>
</div>
<script type="module">
// Set this to false to disable WebLLM initialization during development
const ENABLE_WEBLLM = true;
import * as webllm from "https://esm.run/@mlc-ai/web-llm";
const contractAddressInput = document.getElementById("contract-address");
const searchButton = document.getElementById("search-btn");
const bytecodeDisplay = document.getElementById("bytecode-display");
const decompiledDisplay = document.getElementById("decompiled-display");
const opcodesDisplay = document.getElementById("opcodes-display");
const yulDisplay = document.getElementById("yul-display");
const instructionsDisplay = document.getElementById("instructions-display");
const chatHistory = document.getElementById("chat-history");
const chatInput = document.getElementById("chat-message");
const sendChatButton = document.getElementById("send-chat");
const tabs = document.querySelectorAll(".tab");
const themeSwitcher = document.getElementById("switch-theme");
let currentTheme = "catppuccin-mocha";
let bytecode = "";
let decompiled = "";
let opcodes = "";
let yul = "";
let activeTab = "bytecode";
let selectedText = "";
// Tab switching
tabs.forEach(tab => {
tab.addEventListener("click", () => {
tabs.forEach(t => t.classList.remove("active"));
tab.classList.add("active");
activeTab = tab.dataset.tab;
document.getElementById("bytecode-display").style.display = "none";
document.getElementById("decompiled-display").style.display = "none";
document.getElementById("opcodes-display").style.display = "none";
document.getElementById("yul-display").style.display = "none";
document.getElementById("instructions-display").style.display = "none";
document.getElementById(`${activeTab}-display`).style.display = "block";
if (activeTab === "decompiled" && !decompiled) {
decompileContract(bytecode);
} else if (activeTab === "opcodes" && !opcodes) {
displayOpcodes(bytecode);
} else if (activeTab === "yul" && !yul) {
displayYul(bytecode);
}
selectedText = "";
});
});
[bytecodeDisplay, decompiledDisplay, opcodesDisplay, yulDisplay, instructionsDisplay].forEach(display => {
display.addEventListener("mouseup", () => {
selectedText = window.getSelection().toString().trim();
});
});
async function patchContract(contract) {
const keys = Object.keys;
const hex = selector => '0x' + selector;
const functions = [...keys(contract.functions), ...keys(contract.reverts)].map(hex).join(',');
const events = keys(contract.events).map(hex).join(',');
let lookup = {};
const url = `https://api.openchain.xyz/signature-database/v1/lookup?function=${functions}&event=${events}`;
const resp = await fetch(url);
if (!resp.ok) {
throw new Error(`Failed to fetch signatures from api.openchain.xyz, url: ${url}`);
}
const { result } = await resp.json();
const mapHashes = hashes => Object.fromEntries(
Object.entries(hashes).map(([hash, matches]) => [hash, matches?.map(({ name }) => name) ?? []])
);
lookup.function = mapHashes(result.function);
lookup.event = mapHashes(result.event);
for (let [selector, fn] of Object.entries(contract.functions)) {
selector = '0x' + selector;
if (selector in lookup.function)
fn.label = lookup.function[selector][0];
}
for (let [topic, event] of Object.entries(contract.events)) {
topic = '0x' + topic;
if (topic in lookup.event)
event.sig = lookup.event[topic][0];
}
for (let [selector, decl] of Object.entries(contract.reverts)) {
selector = '0x' + selector;
if (selector in lookup.function)
decl.sig = lookup.function[selector][0];
}
return contract;
}
async function searchContract() {
const input = contractAddressInput.value.trim();
if (input) {
if (input.startsWith('0x') && input.length > 42) {
// Input is bytecode
bytecode = input;
displayBytecode(bytecode);
decompiled = "";
opcodes = "";
yul = "";
processNewBytecode();
} else {
// Input is an address
try {
const response = await fetch("https://eth.llamarpc.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "eth_getCode",
params: [input],
id: 0,
}),
});
const data = await response.json();
bytecode = data.result;
displayBytecode(bytecode);
decompiled = "";
opcodes = "";
yul = "";
processNewBytecode();
} catch (error) {
console.error("Error fetching bytecode:", error);
bytecodeDisplay.textContent = "Error fetching bytecode. Please try again.";
}
}
}
}
function processNewBytecode() {
if (activeTab === "decompiled") {
decompileContract(bytecode);
} else if (activeTab === "opcodes") {
displayOpcodes(bytecode);
} else if (activeTab === "yul") {
displayYul(bytecode);
}
}
function displayBytecode(code) {
const scratchSpaceEnd = 64 * 2; // 64 bytes * 2 characters per byte
const freeMemoryPointerEnd = 96 * 2; // 96 bytes * 2 characters per byte
const allocatableMemoryStart = 128 * 2; // 128 bytes * 2 characters per byte
let formattedCode = '';
for (let i = 0; i < code.length; i += 64) {
const line = code.slice(i, i + 64);
let coloredLine = '';
for (let j = 0; j < line.length; j += 2) {
const bytePosition = i + j;
const byte = line.slice(j, j + 2);
if (bytePosition < scratchSpaceEnd) {
coloredLine += `<span class="scratch-space">${byte}</span>`;
} else if (bytePosition < freeMemoryPointerEnd) {
coloredLine += `<span class="free-memory-pointer">${byte}</span>`;
} else if (bytePosition >= allocatableMemoryStart) {
coloredLine += `<span class="allocatable-memory">${byte}</span>`;
} else {
coloredLine += byte;
}
}
formattedCode += coloredLine + '\n';
}
bytecodeDisplay.innerHTML = `<pre>${formattedCode}</pre>`;
}
const opcodeExplanations = {
STOP: "Halts execution",
ADD: "Addition operation",
SUB: "Subtraction operation",
MUL: "Multiplication operation",
DIV: "Integer division operation",
SDIV: "Signed integer division operation",
MOD: "Modulo operation",
SMOD: "Signed modulo operation",
EXP: "Exponential operation",
NOT: "Bitwise NOT operation",
LT: "Less-than comparison",
GT: "Greater-than comparison",
SLT: "Signed less-than comparison",
SGT: "Signed greater-than comparison",
EQ: "Equality comparison",
ISZERO: "Simple not operator",
AND: "Bitwise AND operation",
OR: "Bitwise OR operation",
XOR: "Bitwise XOR operation",
BYTE: "Retrieve single byte from word",
SHL: "Bitwise left shift",
SHR: "Bitwise right shift",
SAR: "Bitwise right shift with sign extension",
ADDMOD: "Addition modulo operation",
MULMOD: "Multiplication modulo operation",
SIGNEXTEND: "Extend length of two's complement signed integer",
KECCAK256: "Compute Keccak-256 hash",
PC: "Get the value of the program counter",
POP: "Remove item from stack",
MLOAD: "Load word from memory",
MSTORE: "Save word to memory",
MSTORE8: "Save byte to memory",
SLOAD: "Load word from storage",
SSTORE: "Save word to storage",
MSIZE: "Get the size of the memory in bytes",
GAS: "Get the amount of available gas",
ADDRESS: "Get address of currently executing account",
BALANCE: "Get balance of the given account",
SELFBALANCE: "Get balance of the currently executing account",
CALLER: "Get caller address",
CALLVALUE: "Get deposited value by the instruction/transaction responsible for this execution",
CALLDATALOAD: "Get input data of current environment",
CALLDATASIZE: "Get size of input data in current environment",
CALLDATACOPY: "Copy input data in current environment to memory",
CODESIZE: "Get size of code running in current environment",
CODECOPY: "Copy code running in current environment to memory",
EXTCODESIZE: "Get size of an account's code",
EXTCODECOPY: "Copy an account's code to memory",
RETURNDATASIZE: "Get size of output data from the previous call from the current environment",
RETURNDATACOPY: "Copy output data from the previous call from the current environment to memory",
EXTCODEHASH: "Get hash of an account's code",
CREATE: "Create a new account with associated code",
CREATE2: "Create a new account with associated code using salt",
CALL: "Message-call into an account",
CALLCODE: "Message-call into this account with alternative account's code",
DELEGATECALL: "Message-call into this account with an alternative account's code, but persisting the current values for sender and value",
STATICCALL: "Static message-call into an account",