├── .gitignore ├── LICENSE.txt ├── README.md ├── app.js ├── index.html └── style.css /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 Chen, Yi-Cyuan 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solidity optimize name 2 | Find a optimized name for method. 3 | Please go to [here](http://emn178.github.io/solidity-optimize-name/). 4 | 5 | ## License 6 | The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). 7 | 8 | ## Contact 9 | The project's website is located at https://github.com/emn178/solidity-optimize-name 10 | Author: Chen, Yi-Cyuan (emn178@gmail.com) 11 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | var CHARS = '0123456789abcdefghijklmnopqrstuvwxysABCDEFGHIJKLMNOPQRSTUVWXYS$_'.split(''); 3 | var CHAR_MAP = {}; 4 | CHARS.forEach(function (c, index) { 5 | CHAR_MAP[c] = index; 6 | }); 7 | 8 | var CHAR_CODES = CHARS.map(function (c) { 9 | return c.charCodeAt(0); 10 | }); 11 | 12 | var CHAR_CODE_MAP = {}; 13 | CHARS.forEach(function (c, index) { 14 | CHAR_CODE_MAP[index] = c.charCodeAt(0); 15 | }); 16 | 17 | var data = { blocks: [], s: [] }; 18 | function save(hash) { 19 | data.reset = hash.reset; 20 | data.block = hash.block; 21 | data.start = hash.start; 22 | data.finalized = hash.finalized; 23 | data.lastByteIndex = hash.lastByteIndex; 24 | for (var i = 0; i < hash.blocks.length; ++i) { 25 | data.blocks[i] = hash.blocks[i]; 26 | } 27 | for (var i = 0; i < hash.s.length; ++i) { 28 | data.s[i] = hash.s[i]; 29 | } 30 | } 31 | 32 | function restore(hash) { 33 | hash.reset = data.reset; 34 | hash.block = data.block; 35 | hash.start = data.start; 36 | hash.finalized = data.finalized; 37 | hash.lastByteIndex = data.lastByteIndex; 38 | for (var i = 0; i < data.blocks.length; ++i) { 39 | hash.blocks[i] = data.blocks[i]; 40 | } 41 | for (var i = 0; i < data.s.length; ++i) { 42 | hash.s[i] = data.s[i]; 43 | } 44 | } 45 | 46 | function toBytes(str) { 47 | var bytes = []; 48 | for (var i = 0; i < str.length; ++i) { 49 | bytes.push(str.charCodeAt(i)); 50 | } 51 | return bytes; 52 | } 53 | 54 | function parseSignature(signature) { 55 | if (signature.charAt(signature.length - 1) != ')' || signature.indexOf(' ') !== -1) { 56 | return false; 57 | } 58 | var parts = signature.split('('); 59 | if (parts.length == 2) { 60 | return { 61 | name: parts[0], 62 | args: '(' + parts[1] 63 | }; 64 | } else { 65 | return false; 66 | } 67 | } 68 | 69 | function increase(bytes) { 70 | bytes[0] += 1; 71 | for (var i = 0; i < bytes.length; ++i) { 72 | if (bytes[i] === 64) { 73 | bytes[i] = 0; 74 | if (i == bytes.length - 1) { 75 | bytes[i + 1] = 1; 76 | } else { 77 | bytes[i + 1] += 1; 78 | } 79 | } else { 80 | break; 81 | } 82 | } 83 | return bytes; 84 | } 85 | 86 | function toChars(bytes) { 87 | var str = ''; 88 | for (var i = 0; i < bytes.length; ++i) { 89 | str += CHARS[bytes[i]]; 90 | } 91 | return str; 92 | } 93 | 94 | function toCharCodes(bytes) { 95 | var codes = []; 96 | for (var i = 0; i < bytes.length; ++i) { 97 | codes.push(CHAR_CODE_MAP[bytes[i]]); 98 | } 99 | return codes; 100 | } 101 | 102 | function find(obj) { 103 | var sig = obj.name + obj.args; 104 | var args = toBytes(obj.args); 105 | var bytes = [0]; 106 | var index = 0; 107 | var prefix = toBytes(obj.name + '_'); 108 | var hash = keccak256.create(); 109 | hash.update(prefix); 110 | save(hash); 111 | var char, methodId = keccak256.array(sig); 112 | while (methodId[0] || methodId[1]) { 113 | if (index >= CHARS.length) { 114 | increase(bytes); 115 | hash = keccak256.create(); 116 | hash.update(prefix); 117 | hash.update(toCharCodes(bytes)); 118 | save(hash); 119 | index = 0; 120 | } 121 | char = CHARS[index]; 122 | hash.update(char); 123 | hash.update(args); 124 | methodId = hash.array(); 125 | restore(hash); 126 | ++index; 127 | } 128 | if (index) { 129 | sig = obj.name + '_' + toChars(bytes) + char + obj.args; 130 | } 131 | return [sig, keccak256(sig).substr(0, 8)]; 132 | } 133 | 134 | $(document).ready(function () { 135 | $('#optimize').click(function () { 136 | var input = $('#input').val(); 137 | $('#error').hide();; 138 | var data = parseSignature(input); 139 | if (!data) { 140 | $('#error').show();; 141 | return; 142 | } 143 | console.time('a'); 144 | var result = find(data); 145 | console.timeEnd('a') 146 | $('#output').append('
' + result[0] + ': 0x' + result[1] + '
') 147 | }); 148 | }); 149 | })(jQuery); 150 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |