-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
149 lines (138 loc) · 3.52 KB
/
app.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
(function ($) {
var CHARS = '0123456789abcdefghijklmnopqrstuvwxysABCDEFGHIJKLMNOPQRSTUVWXYS$_'.split('');
var CHAR_MAP = {};
CHARS.forEach(function (c, index) {
CHAR_MAP[c] = index;
});
var CHAR_CODES = CHARS.map(function (c) {
return c.charCodeAt(0);
});
var CHAR_CODE_MAP = {};
CHARS.forEach(function (c, index) {
CHAR_CODE_MAP[index] = c.charCodeAt(0);
});
var data = { blocks: [], s: [] };
function save(hash) {
data.reset = hash.reset;
data.block = hash.block;
data.start = hash.start;
data.finalized = hash.finalized;
data.lastByteIndex = hash.lastByteIndex;
for (var i = 0; i < hash.blocks.length; ++i) {
data.blocks[i] = hash.blocks[i];
}
for (var i = 0; i < hash.s.length; ++i) {
data.s[i] = hash.s[i];
}
}
function restore(hash) {
hash.reset = data.reset;
hash.block = data.block;
hash.start = data.start;
hash.finalized = data.finalized;
hash.lastByteIndex = data.lastByteIndex;
for (var i = 0; i < data.blocks.length; ++i) {
hash.blocks[i] = data.blocks[i];
}
for (var i = 0; i < data.s.length; ++i) {
hash.s[i] = data.s[i];
}
}
function toBytes(str) {
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}
function parseSignature(signature) {
if (signature.charAt(signature.length - 1) != ')' || signature.indexOf(' ') !== -1) {
return false;
}
var parts = signature.split('(');
if (parts.length == 2) {
return {
name: parts[0],
args: '(' + parts[1]
};
} else {
return false;
}
}
function increase(bytes) {
bytes[0] += 1;
for (var i = 0; i < bytes.length; ++i) {
if (bytes[i] === 64) {
bytes[i] = 0;
if (i == bytes.length - 1) {
bytes[i + 1] = 1;
} else {
bytes[i + 1] += 1;
}
} else {
break;
}
}
return bytes;
}
function toChars(bytes) {
var str = '';
for (var i = 0; i < bytes.length; ++i) {
str += CHARS[bytes[i]];
}
return str;
}
function toCharCodes(bytes) {
var codes = [];
for (var i = 0; i < bytes.length; ++i) {
codes.push(CHAR_CODE_MAP[bytes[i]]);
}
return codes;
}
function find(obj) {
var sig = obj.name + obj.args;
var args = toBytes(obj.args);
var bytes = [0];
var index = 0;
var prefix = toBytes(obj.name + '_');
var hash = keccak256.create();
hash.update(prefix);
save(hash);
var char, methodId = keccak256.array(sig);
while (methodId[0] || methodId[1]) {
if (index >= CHARS.length) {
increase(bytes);
hash = keccak256.create();
hash.update(prefix);
hash.update(toCharCodes(bytes));
save(hash);
index = 0;
}
char = CHARS[index];
hash.update(char);
hash.update(args);
methodId = hash.array();
restore(hash);
++index;
}
if (index) {
sig = obj.name + '_' + toChars(bytes) + char + obj.args;
}
return [sig, keccak256(sig).substr(0, 8)];
}
$(document).ready(function () {
$('#optimize').click(function () {
var input = $('#input').val();
$('#error').hide();;
var data = parseSignature(input);
if (!data) {
$('#error').show();;
return;
}
console.time('a');
var result = find(data);
console.timeEnd('a')
$('#output').append('<p>' + result[0] + ': 0x' + result[1] + '</p>')
});
});
})(jQuery);