-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrack_key.py
More file actions
99 lines (71 loc) · 3.38 KB
/
Copy pathcrack_key.py
File metadata and controls
99 lines (71 loc) · 3.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
import hashlib
from typing import Iterable, Optional
import web3
from bip_utils import (
Bip39MnemonicValidator,
Bip39SeedGenerator, Bip32Slip10Secp256k1, Bip32KeyIndex, Bech32Encoder,
)
from typos import Typos
class Wallet(object):
def __init__(self, mnemonic: str):
Bip39MnemonicValidator().Validate(mnemonic)
self.seed_generator = Bip39SeedGenerator(mnemonic)
def eth_key(self, passphrase: str):
seed_bytes = self.seed_generator.Generate(passphrase)
master = Bip32Slip10Secp256k1.FromSeed(seed_bytes)
# m/44'/60'/0'/0/0
child = master.ChildKey(Bip32KeyIndex.HardenIndex(44)) \
.ChildKey(Bip32KeyIndex.HardenIndex(60)) \
.ChildKey(Bip32KeyIndex.HardenIndex(0)) \
.ChildKey(0) \
.ChildKey(0)
return child
def avax_key(self, passphrase: str):
seed_bytes = self.seed_generator.Generate(passphrase)
master = Bip32Slip10Secp256k1.FromSeed(seed_bytes)
# m/44'/9000'/0'
child = master.ChildKey(Bip32KeyIndex.HardenIndex(44)) \
.ChildKey(Bip32KeyIndex.HardenIndex(9000)) \
.ChildKey(Bip32KeyIndex.HardenIndex(0)) \
.ChildKey(0) \
.ChildKey(0)
return child
def child_to_eth_address(child) -> str:
eth_account = web3.Account.from_key(child.PrivateKey().Raw().ToHex())
return eth_account.address
def guess_eth_address(inputs: Iterable[str], wallet: Wallet, target_address: str) -> Optional[str]:
for test_passphrase in inputs:
child = wallet.eth_key(test_passphrase)
computed_address = child_to_eth_address(child)
if computed_address == target_address:
return test_passphrase
def child_to_avaxp_address(child) -> str:
m = hashlib.sha256()
m.update(child.PublicKey().RawCompressed().ToBytes())
n = hashlib.new('ripemd160')
n.update(m.digest())
b32_encoded = Bech32Encoder().Encode('avax', n.digest())
return f'P-{b32_encoded}'
def guess_avaxp_address(inputs: Iterable[str], wallet: Wallet, target_address: str) -> Optional[str]:
for test_passphrase in inputs:
child = wallet.avax_key(test_passphrase)
computed_address = child_to_avaxp_address(child)
if computed_address == target_address:
return test_passphrase
if __name__ == "__main__":
mnemonic = "mixed snap before near whale there silent behave inform sight output keep ability bind target engage chief month axis belt bicycle timber slam glow"
# myfakephrase is the actual password.
best_guess = "myfakephraze"
expected_eth_address = '0xdc22f43BEFeb27E5B8185Ae6d336b41295998526'
expected_avaxp_address = 'P-avax1kp9uggwxkqhygljkpr0cls378sqwrxfw55qcka'
wallet = Wallet(mnemonic)
# Can take this and import into https://wallet.avax.network to get the corresponding
# c/p chain addresses with the 25th word.
# Can also use https://iancoleman.io/bip39/ but it only supports eth and not avax.
# print(wallet.eth_key('myfakephrase').PrivateKey().Raw().ToHex())
# print(wallet.avax_key('myfakephrase').PrivateKey().Raw().ToHex())
# If false, uses uppercase instead of lowercase letters
lowercase = True
typos = Typos(best_guess, max_edit_distance=2, lowercase=lowercase)
# print(guess_eth_address(typos, wallet, expected_eth_address))
print(guess_avaxp_address(typos, wallet, expected_avaxp_address))