-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
96 lines (77 loc) · 3.07 KB
/
index.ts
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
import fs from "fs";
import { ethers, HDNodeWallet, Wallet, Mnemonic } from "ethers";
import {
MY_SEED_PHRASE_WITH_MISSING_WORDS,
MISSING_WORD_POSITION,
CUSTOM_PROVIDER,
} from "./config";
console.log("Finding matches...");
const incompleteSeed = MY_SEED_PHRASE_WITH_MISSING_WORDS.split(" ");
const incompleteSeedLength = incompleteSeed.length;
const filePath = "./bip-39-wordlist.txt";
const provider = CUSTOM_PROVIDER
? new ethers.JsonRpcProvider(CUSTOM_PROVIDER)
: ethers.getDefaultProvider("homestead");
const isSeedPhraseValid = (seedPhrase) => {
try {
const wallet = Wallet.fromPhrase(seedPhrase);
// If no error is thrown, the seed phrase is valid
return true;
} catch (error) {
// If an error is thrown, the seed phrase is not valid
return false;
}
};
const checkIfSeedPhraseHasTransactions = async (potentialSeedPhrase) => {
const mnemonic = Mnemonic.fromPhrase(potentialSeedPhrase);
// so ledger and metamask use different derivation methods for wallets from seed phrases. checking both.
// making the assumption here that the first wallet in a seed phrase has executed a transaction.
// these are the two standart derivation methods. any other wallet using non standard derivation methods please PR/create issue!!
// assuming the first wallet in the derivation method has an ETH Balance, or a tx history.
// NOTE: in the case of the wallet having zero ETH balance, or wallet having zero tx history, this will not detect
const ledgerFirstWalletPath = ethers.getAccountPath(0);
const metamaskFirstWalletPath = ethers.getIndexedAccountPath(0);
const ledgerWallet = HDNodeWallet.fromMnemonic(
mnemonic,
ledgerFirstWalletPath
);
const metamaskWallet = HDNodeWallet.fromMnemonic(
mnemonic,
metamaskFirstWalletPath
);
const ledgerBalance = await provider.getBalance(ledgerWallet.address);
const metamaskBalance = await provider.getBalance(metamaskWallet.address);
const ledgerTransactionCount = await provider.getTransactionCount(
ledgerWallet.address
);
const metamaskTransactionCount = await provider.getTransactionCount(
metamaskWallet.address
);
const walletHasZeroBalance = ledgerBalance === 0n && metamaskBalance === 0n;
const walletHasZeroTxHistory =
ledgerTransactionCount === 0 && metamaskTransactionCount === 0;
if (walletHasZeroBalance && walletHasZeroTxHistory) return;
console.log("Match found!", "Seed Phrase: ", potentialSeedPhrase);
};
const main = async () => {
try {
const data = fs.readFileSync(filePath, "utf8");
const lines = data.split("\n");
const seedWords = [];
lines.forEach((line, index) => {
seedWords.push(line.trim());
});
for (let i = 0; i < seedWords.length; i++) {
const seedWord = seedWords[i];
const potentialSeedPhrase = incompleteSeed
.toSpliced(MISSING_WORD_POSITION, 0, seedWord)
.join(" ");
if (isSeedPhraseValid(potentialSeedPhrase))
await checkIfSeedPhraseHasTransactions(potentialSeedPhrase);
}
console.log("Search complete.");
} catch (err) {
console.error(err);
}
};
main();