-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
217 lines (180 loc) · 6.38 KB
/
Copy pathindex.js
File metadata and controls
217 lines (180 loc) · 6.38 KB
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
require("dotenv").config()
const lpad = require("lpad")
const bip39 = require("bip39")
const { hdkey } = require("ethereumjs-wallet")
const Web3 = require("web3")
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.ETHEREUM_PROVIDER_URL))
const fs = require("fs")
const money = []
const problems = []
const getBalance = async address => {
const balancePromise = address => {
return new Promise((resolve, reject) => {
web3.eth.getBalance(address, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
}
const wei = await balancePromise(address)
const balance = web3.utils.fromWei(wei, "ether")
return balance
}
const getAddress = (hdWallet, path) => {
const wallet = hdWallet.derivePath(path).getWallet()
const address = wallet.getAddressString()
return address
}
const getAddresses = async (mnemonic, paths, gap = 1) => {
const seed = await bip39.mnemonicToSeed(mnemonic)
const hdWallet = hdkey.fromMasterSeed(seed)
const data = []
for (let path of paths.split(",")) {
let address = ""
if (path.includes("x")) {
for (let i = 0; i < gap; i++) {
const realPath = path.replace("x", i)
address = getAddress(hdWallet, realPath)
data.push({ address, path: realPath })
}
} else {
address = getAddress(hdWallet, path)
data.push({ address, path })
}
}
return data
}
const checkValidity = async (seed, paths, gap, longestWord, updateIteration) => {
const mnemonic = seed.join(" ")
if (!bip39.validateMnemonic(mnemonic)) return;
updateIteration()
const addressesPaths = await getAddresses(mnemonic, paths, gap)
const balances = []
for (const addressPath of addressesPaths) {
let balance = 0
try {
balance = await getBalance(addressPath.address)
balances.push({ path: addressPath.path, address: addressPath.address, balance })
} catch (error) {
problems.push({ mnemonic, path: addressPath.path, address: addressPath.address })
continue
}
}
console.log(`${new Date().toISOString()}:`, mnemonic.split(" ").map(word => lpad(word, [...Array(longestWord + 1 - word.length)].join(" "))).join(" "))
for (const balance of balances) {
if (balance.balance > 0) {
money.push({ mnemonic, ...balance })
}
}
if (money.length > 0) {
console.log(`found ${money.length} addresses with a balance so far`)
console.log(money)
}
if (problems.length > 0) {
console.log("there was a problem looking up the balance for the following addresses")
console.log(problems)
}
}
const iterateWords = async (seedArray, wordlist, wordIndex, progress, progressIndex, callback) => {
let currentProgress = progress.indexes[progressIndex]
for (let i = currentProgress.wordListIndex; i < wordlist.length; i++) {
currentProgress.wordListIndex = i
seedArray[wordIndex] = wordlist[i]
await callback(seedArray, () => progressFile(progress))
}
currentProgress.wordListIndex = 0
progressFile(progress)
}
const getWrongWord = async (seedArray, otherIndex, progress, progressIndex, callback) => {
let currentProgress = progress.indexes[progressIndex]
if (!currentProgress) {
progress.indexes[progressIndex] = { seedIndex: 0, wordListIndex: 0 }
currentProgress = progress.indexes[progressIndex]
progressFile(progress)
}
for (let i = currentProgress.seedIndex; i < seedArray.length; i++) {
if (otherIndex >= i) continue
currentProgress.seedIndex = i
progressFile(progress)
const badWord = seedArray[i]
await callback(i, seedArray)
seedArray[i] = badWord
}
currentProgress.seedIndex = 0
progressFile(progress)
}
const progressFile = progress => {
const progressPath = "./PROGRESS.json"
const write = (path, progress) => fs.writeFileSync(path, JSON.stringify(progress, null, "\t"))
if (progress) return write(progressPath, progress)
if (fs.existsSync(progressPath)) return require(progressPath)
const defaultData = { indexes: [] }
write(progressPath, defaultData)
return require(progressPath)
}
const maxCounts = (progress, index, seedLength, wordListLength) => {
let currentProgress = progress.indexes[index]
currentProgress.seedIndex = seedLength - 1
currentProgress.wordListIndex = wordListLength - 1
progressFile(progress)
}
const findLongestWord = wordlist => {
return wordlist.reduce((currentLength, word) => {
if (word.length > currentLength) return word.length
return currentLength
}, 0)
}
const findMoney = async (badSeed, paths, gap, wordlist) => {
const progress = progressFile()
const longestWord = findLongestWord(wordlist)
const seedArray = badSeed.split(" ")
// single word replacement
await getWrongWord(seedArray, -1, progress, 0, async (wordOneIndex, seedArray) => {
await iterateWords(seedArray, wordlist, wordOneIndex, progress, 0, async (seedArray, updateIteration) => {
await checkValidity(seedArray, paths, gap, longestWord, updateIteration)
})
})
maxCounts(progress, 0, seedArray.length, wordlist.length)
// double word replacement
await getWrongWord(seedArray, -1, progress, 1, async (wordOneIndex, seedArray) => {
await getWrongWord(seedArray, wordOneIndex, progress, 2, async (wordTwoIndex, seedArray) => {
await iterateWords(seedArray, wordlist, wordOneIndex, progress, 1, async seedArray => {
await iterateWords(seedArray, wordlist, wordTwoIndex, progress, 2, async (seedArray, updateIteration) => {
await checkValidity(seedArray, paths, gap, longestWord, updateIteration)
})
})
})
})
maxCounts(progress, 1, seedArray.length, wordlist.length)
maxCounts(progress, 2, seedArray.length, wordlist.length)
}
const go = async (badSeed, paths, gap, wordlist) => {
await findMoney(badSeed, paths, gap, wordlist)
done(money, problems)
}
const done = (money, problems) => {
if (money.length > 0) {
console.log("")
console.log("we have a winner!")
console.log(money)
console.log("")
} else {
console.log("")
console.log(":(")
console.log("")
}
if (problems.length > 0) {
console.log("")
console.log("there were problems checking the balance of the following addresses")
console.log(problems)
console.log("")
}
process.exit()
}
process.on("SIGINT", () => done(money, problems))
go(
process.env.BAD_SEED,
process.env.DERIVATION_PATHS,
process.env.DERIVATION_PATH_GAP,
eval(`bip39.wordlists.${process.env.WORD_LIST_LANGUAGE}`)
)