-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
335 lines (294 loc) · 10.3 KB
/
common.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
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
const fs = require('fs')
const rlp = require('rlp')
const util = require('ethereumjs-util')
const Web3 = require('web3')
const os = require('os')
const cluster = require('cluster')
const web3 = new Web3('https://non-exists-host.non-exists-tld'/* use a non-exists url for safety purpose */)
const minimumLengthToCheckVanityScore = 3
const maximumLengthToCheckVanityScore = 19
const giveBaseVanityScoreToPattern = (pattern) => {
if (typeof pattern !== 'string')
throw `Pattern must be string`
const patternLen = pattern.length
if (patternLen < 3) {
return 0
}
if (patternLen <= 5) {
return patternLen
}
const extraLen = patternLen - 5
let vscore = 5
for (let i = 0; i < extraLen; i++) {
vscore += (i + 2)
}
return vscore
}
const injectBaseVanityScore = (arr) => {
for (const idx in arr) {
const pattern = arr[idx]
arr[idx] = {
p: pattern,
s: giveBaseVanityScoreToPattern(pattern),
}
}
}
/*
* Generate something like 999999 based on selected character and length
* Eg: 999999 is result of generateContinousText(9, 6)
*/
const generateContinousText = (char, length) => {
let result = ''
for (let i = 0; i < length; i++) {
result = `${result}${char}`
}
return result
}
const continous = []
const initVanityScoreTable = function() {
for (let c = 0; c <= 9; c++) {
const cL = []
for (let i = 0; i < minimumLengthToCheckVanityScore; i++) {
cL.push(i)
}
for (let l = minimumLengthToCheckVanityScore; l <= maximumLengthToCheckVanityScore; l++) {
const pattern = generateContinousText(c, l)
cL.push({
p: pattern,
s: giveBaseVanityScoreToPattern(pattern),
})
}
continous.push(cL)
}
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f']
for (const idx in alphabet) {
const cL = []
for (let i = 0; i < minimumLengthToCheckVanityScore; i++) {
cL.push(i)
}
for (let l = minimumLengthToCheckVanityScore; l <= maximumLengthToCheckVanityScore; l++) {
const pattern = generateContinousText(alphabet[idx], l)
cL.push({
p: pattern,
s: giveBaseVanityScoreToPattern(pattern),
})
}
continous.push(cL)
}
}
const _rateAddress = (address, prefix) => {
let highestVanityScore = 0
for (let c = 15 /* 9 -> 0 */ /* a -> f */; c >= 0; c--) {
if (prefix) {
if (!address.startsWith(continous[c][minimumLengthToCheckVanityScore].p)) {
continue
}
} else {
if (!address.endsWith(continous[c][minimumLengthToCheckVanityScore].p)) {
continue
}
}
highestVanityScore = continous[c][minimumLengthToCheckVanityScore].s
for (let l = minimumLengthToCheckVanityScore + 1; l <= maximumLengthToCheckVanityScore; l++) {
if (prefix) {
if (address.startsWith(continous[c][l].p)) {
highestVanityScore = continous[c][l].s
} else {
return highestVanityScore
}
} else {
if (address.endsWith(continous[c][l].p)) {
highestVanityScore = continous[c][l].s
} else {
return highestVanityScore
}
}
}
}
return highestVanityScore
}
/*
* Give vanity score for an address
*/
const rateAddress = (address) => {
return _rateAddress(address, true) + _rateAddress(address, false)
}
const getNowMs = () => {
return new Date().getTime()
}
const appendFile = (logFile, logContent) => {
try {
fs.writeFileSync(logFile, '\n' + logContent, { flag: 'a+' })
//file written successfully
} catch (err) {
console.error(err)
console.error(`ERR: Failed to append content: '${logContent}'`)
console.error(` into file '${logFile}'`)
}
}
const passwordFileName = 'vanitye-encryption-password.txt'
const readPassword = (isDebug = false) => {
if (!fs.existsSync(passwordFileName)) {
try {
fs.writeFileSync(passwordFileName, '123456', { flag: 'a+' })
} catch (err) {
console.error(err)
console.error(`ERR: Unable to create default ${passwordFileName} file`)
console.error(`ERR: You may need to check permission, or manually create a file with name ${passwordFileName}, open it with a text editor and write your password you want to encrypt wallet info`)
return
}
}
const password = fs.readFileSync(passwordFileName, 'utf8').toString().trim()
if (password === '123456') {
if (isDebug === true) {
return password
}
console.error(`ERR: You have to open ${passwordFileName} file and replace the default password 123456 with yours strongly secured password. This password will be used to create json wallet files (V3 keystore)`)
return
} else {
const minimumPasswordLength = 6
if (password.length < minimumPasswordLength) {
console.error(`ERR: Password is too short, require minimum ${minimumPasswordLength} characters in length`)
return
}
if (cluster.isPrimary) {
console.log(`WARNING: remember to wipe content of ${passwordFileName} file after you done. It's dangerous to leave your secure password there`)
}
return password
}
}
const minimumPatternStringLength = 5
const parsePatterns = (argv) => {
const inputPatterns = argv.pattern
if (!inputPatterns) {
console.error('ERR: No pattern was provided, use flag --pattern to specify')
return
}
if (typeof inputPatterns == 'boolean') {
console.error('ERR: Wrong usage of flag --pattern, it requires parameter')
return
}
const patterns = Array.isArray(inputPatterns) ? inputPatterns : [`${inputPatterns}`]
if (patterns.length < 1) {
console.error('ERR: No pattern was provided, use flag --pattern to specify')
return
}
if (cluster.isPrimary) {
const re = /^[0-9aA-fF]+$/
for (const idx in patterns) {
const pattern = patterns[idx] = `${patterns[idx]}`.toLowerCase()
if (pattern.length < minimumPatternStringLength) {
console.error(`ERR: Pattern '${pattern}' is too short, minimum length is ${minimumPatternStringLength}`)
return
}
if (!re.test(pattern)) {
console.error(`ERR: Invalid pattern '${pattern}', only accept combination of 0-9 a-f (hex)`)
return
}
console.log(`Pattern '${pattern}' difficulty = ${Math.pow(16, pattern.length)}`)
}
} else {
for (const idx in patterns) {
patterns[idx] = `${patterns[idx]}`.toLowerCase()
}
}
patterns.sort(function(a, b) {
return b.length - a.length
})
return patterns
}
const parseNonce = (argv) => {
const nonce = argv.nonce
if (nonce === undefined) {
console.error('ERR: Missing flag --nonce')
return
}
if (typeof nonce != 'number') {
console.error(`ERR: Flag --nonce must be a number (${typeof nonce} found)`)
return
}
if (Math.floor(nonce) != nonce) {
console.error('ERR: Flag --nonce must be an integer')
console.error('ERR: Minimum nonce is 1 because you must test every generated wallets before use (so nonce 0 will be used for the very first transaction)')
return
}
return nonce
}
const numCPUs = os.cpus().length
const suggestedProcessesCount = Math.max(1, numCPUs - 1)
const parseChildrenProcessesCount = (argv) => {
const count = argv.cpu
if (count === undefined) {
return {
count: suggestedProcessesCount,
max: false,
single: suggestedProcessesCount === 1,
}
}
if (count < 1 || count > numCPUs) {
console.error(`ERR: Flag --threads must be a number between 1 and ${numCPUs}`)
return
}
if (count > suggestedProcessesCount) {
console.log(`NOTICE: It is recommended to use less than ${numCPUs} children processes on your machine to prevent machine freeze issue`)
}
return {
count: count,
max: count >= numCPUs,
single: count === 1,
}
}
const parseExit = (argv) => {
const exit = argv.exit
if (!exit) {
return
}
if (exit < 0) {
console.error(`ERR: Value of flag --exit can not be negative`)
return
}
if (exit == 0) {
return
}
return exit
}
const minimumWow = minimumPatternStringLength * 2 + 2
const rateVanityScoreOfAddress = (address, startsWith, endsWith, vscore, extraAddress, patternMatched) => {
let vanityScore = (startsWith ? vscore : 0) + (endsWith ? vscore : 0)
vanityScore += rateAddress(address)
if (extraAddress) {
vanityScore += (extraAddress.startsWith(patternMatched) ? vscore : 0) + (extraAddress.endsWith(patternMatched) ? vscore : 0)
vanityScore += rateAddress(extraAddress)
}
return vanityScore
}
const fromPrivateKeyToV3KeyStore = (privateKeyAsHex, password) => {
return web3.eth.accounts.encrypt(`0x${privateKeyAsHex}`, password)
}
const fromAddressToContractAddressAtGivenNonce = (bufferAddress, nonce) => {
return util.keccak256(util.arrToBufArr(rlp.encode([bufferAddress, nonce]))).slice(12)
}
const createDir = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}
module.exports = {
giveBaseVanityScoreToPattern: giveBaseVanityScoreToPattern,
injectBaseVanityScore: injectBaseVanityScore,
initVanityScoreTable: initVanityScoreTable,
parsePatterns: parsePatterns,
parseNonce: parseNonce,
parseChildrenProcessesCount: parseChildrenProcessesCount,
parseExit: parseExit,
rateAddress: rateAddress,
getNowMs: getNowMs,
appendFile: appendFile,
readPassword: readPassword,
minimumWow: minimumWow,
passwordFileName: passwordFileName,
rateVanityScoreOfAddress: rateVanityScoreOfAddress,
fromPrivateKeyToV3KeyStore: fromPrivateKeyToV3KeyStore,
fromAddressToContractAddressAtGivenNonce: fromAddressToContractAddressAtGivenNonce,
createDir: createDir,
}