-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanity.js
102 lines (91 loc) · 3.12 KB
/
vanity.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
/* eslint-env worker */
const secp256k1 = require('secp256k1');
const keccak = require('keccak');
const randomBytes = require('randombytes');
const fs = require('fs');
/**
* Transform a private key into an address
*/
const privateToAddress = (privateKey) => {
const pub = secp256k1.publicKeyCreate(privateKey, false).slice(1);
return keccak('keccak256').update(pub).digest().slice(-20).toString('hex');
};
/**
* Create a wallet from a random private key
* @returns {{address: string, privKey: string}}
*/
const getRandomWallet = () => {
const randbytes = randomBytes(32);
return {
address: privateToAddress(randbytes).toString('hex'),
privKey: randbytes.toString('hex')
};
};
/**
* Check if a wallet respects the input constraints
* @param address
* @param input
* @param isChecksum
* @param isSuffix
* @returns {boolean}
*/
const isValidVanityAddress = (address, input, isChecksum, isSuffix) => {
const subStr = isSuffix ? address.substr(40 - input.length) : address.substr(0, input.length);
if (!isChecksum) {
return input === subStr;
}
if (input.toLowerCase() !== subStr) {
return false;
}
return isValidChecksum(address, input, isSuffix);
};
const isValidChecksum = (address, input, isSuffix) => {
const hash = keccak('keccak256').update(address).digest().toString('hex');
const shift = isSuffix ? 40 - input.length : 0;
for (let i = 0; i < input.length; i++) {
const j = i + shift;
if (input[i] !== (parseInt(hash[j], 16) >= 8 ? address[j].toUpperCase() : address[j])) {
return false;
}
}
return true;
};
const toChecksumAddress = (address) => {
const hash = keccak('keccak256').update(address).digest().toString('hex');
let ret = '';
for (let i = 0; i < address.length; i++) {
ret += parseInt(hash[i], 16) >= 8 ? address[i].toUpperCase() : address[i];
}
return ret;
};
/**
* Generate a lot of wallets until one satisfies the input constraints
* @param input - String chosen by the user
* @param isChecksum - Is the input case-sensitive
* @param isSuffix - Is it a suffix, or a prefix
* @param cb - Callback called after x attempts, or when an address if found
* @returns
*/
const getVanityWallet = async (input, isChecksum, isSuffix, minLength = 3, cb) => {
input = isChecksum ? input : input.toLowerCase();
let wallet = getRandomWallet();
let attempts = 1;
for(;;){
for(let j = 0; j < input.length-minLength; j++) {
if(isValidVanityAddress(wallet.address, input.substr(0, input.length-j), isChecksum, isSuffix)){
// Write to file
fs.appendFileSync('wallet'+input.substr(0, input.length-j)+'.txt', "Address: 0x"+ toChecksumAddress(wallet.address) + '\n' + "Private Key: "+ wallet.privKey + '\n\n');
cb(`Found 0x${toChecksumAddress(wallet.address)} after ${attempts} attempts`);
attempts = 0;
break;
}
}
wallet = getRandomWallet();
attempts++;
}
};
module.exports = {
getVanityWallet,
isValidVanityAddress,
toChecksumAddress
};