#!/usr/bin/env python3 ''' MIT License Copyright (c) 2018 Luis Teixeira Copyright (c) 2019 Niklas Baumstark Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import binascii, hashlib, hmac, struct from ecdsa.curves import SECP256k1 from eth_utils import to_checksum_address, keccak as eth_utils_keccak BIP39_PBKDF2_ROUNDS = 2048 BIP39_SALT_MODIFIER = "mnemonic" BIP32_PRIVDEV = 0x80000000 BIP32_CURVE = SECP256k1 BIP32_SEED_MODIFIER = b'Bitcoin seed' ETH_DERIVATION_PATH = "m/44'/60'/0'/0" TARGET_PUBLIC_KEY = '0x9C2F44EFAd0c1E852a09dF9939e6DaF061140CaF' class PublicKey: def __init__(self, private_key): self.point = int.from_bytes(private_key, byteorder='big') * BIP32_CURVE.generator def __bytes__(self): xstr = self.point.x().to_bytes(32, byteorder='big') parity = self.point.y() & 1 return (2 + parity).to_bytes(1, byteorder='big') + xstr def address(self): x = self.point.x() y = self.point.y() s = x.to_bytes(32, 'big') + y.to_bytes(32, 'big') return to_checksum_address(eth_utils_keccak(s)[12:]) def mnemonic_to_bip39seed(mnemonic, passphrase): mnemonic = bytes(mnemonic, 'utf8') salt = bytes(BIP39_SALT_MODIFIER + passphrase, 'utf8') return hashlib.pbkdf2_hmac('sha512', mnemonic, salt, BIP39_PBKDF2_ROUNDS) def bip39seed_to_bip32masternode(seed): k = seed h = hmac.new(BIP32_SEED_MODIFIER, seed, hashlib.sha512).digest() key, chain_code = h[:32], h[32:] return key, chain_code def derive_bip32childkey(parent_key, parent_chain_code, i): assert len(parent_key) == 32 assert len(parent_chain_code) == 32 k = parent_chain_code if (i & BIP32_PRIVDEV) != 0: key = b'\x00' + parent_key else: key = bytes(PublicKey(parent_key)) d = key + struct.pack('>L', i) while True: h = hmac.new(k, d, hashlib.sha512).digest() key, chain_code = h[:32], h[32:] a = int.from_bytes(key, byteorder='big') b = int.from_bytes(parent_key, byteorder='big') key = (a + b) % BIP32_CURVE.order if a < BIP32_CURVE.order and key != 0: key = key.to_bytes(32, byteorder='big') break d = b'\x01' + h[32:] + struct.pack('>L', i) return key, chain_code def parse_derivation_path(str_derivation_path): path = [] if str_derivation_path[0:2] != 'm/': raise ValueError("Can't recognize derivation path. It should look like \"m/44'/60/0'/0\".") for i in str_derivation_path.lstrip('m/').split('/'): if "'" in i: path.append(BIP32_PRIVDEV + int(i[:-1])) else: path.append(int(i)) return path def mnemonic_to_private_key(mnemonic, derivation_path, passphrase=""): bip39seed = mnemonic_to_bip39seed(mnemonic, passphrase) master_private_key, master_chain_code = bip39seed_to_bip32masternode(bip39seed) private_key, chain_code = master_private_key, master_chain_code for i in derivation_path: private_key, chain_code = derive_bip32childkey(private_key, chain_code, i) return private_key def check(mnemonic_list): #try: idx = map(word_to_code, mnemonic_list) b = "".join(idx) #except ValueError: # # print(mnemonic_list) # return False l = len(b) # noqa: E741 d = b[: l // 33 * 32] h = b[-l // 33 :] nd = int(d, 2).to_bytes(l // 33 * 4, byteorder="big") nh = bin(int(hashlib.sha256(nd).hexdigest(), 16))[2:].zfill(256)[: l // 33] return h == nh from mnemonic import Mnemonic mnemo = Mnemonic("english") index_memo = {} def word_to_code(w): r = index_memo.get(w) if r is not None: return r r = bin(mnemo.wordlist.index(w))[2:].zfill(11) index_memo[w] = r return r import itertools, random import time def prepare_words(text): words = [x.strip().lower() for x in text.split()] result = set() for w in words: try: word_to_code(w) result.add(w) except: pass known = set(['fog', 'parrot', 'dutch', 'fiber']) cnt = 6 for w in list(result): if w in known: cnt -= 1 result.remove(w) result = list(result) random.shuffle(result) return lambda: itertools.combinations(result, cnt) # 14 4 1001 # 16 4 240240 # 40320 POST_WORDS = prepare_words(""" Round dutch cattle in the forest and eating wood Only because there is a lot of healthy fiber Hunter like the rib roast dinner fresh hidden witch """) VIDEO_WORDS = prepare_words(""" expect anything easy there will be dark fog on the lake Also It is not impossible Do you think its more likely for parrot can sing a song then for a goat to whistle """) def mk_words(): for w1 in POST_WORDS(): for w2 in VIDEO_WORDS(): wc = w1 + w2 if len(set(wc)) != 8: continue for w in itertools.permutations(wc): yield w if __name__ == '__main__': derivation_path = parse_derivation_path(f'{ETH_DERIVATION_PATH}/0') mnemonic=None start_time = time.time() iteration = 0 checked = 0 for rest in mk_words(): assert len(rest) == 8 iteration += 1 if iteration % 1000 == 0: elapsed = time.time() - start_time print(f'it={iteration} checked={checked} rate={iteration / elapsed:.1f} last={mnemonic}') mnemonic = [None] * 12 mnemonic[0] = 'dutch' mnemonic[3] = 'fiber' mnemonic[4] = 'fog' mnemonic[11] = 'parrot' i = 0 for w in rest: while mnemonic[i] is not None: i += 1 mnemonic[i] = w valid = check(mnemonic) mnemonic = ' '.join(mnemonic) if not valid: continue checked += 1 private_key = mnemonic_to_private_key(mnemonic, derivation_path=derivation_path) public_key = PublicKey(private_key) if TARGET_PUBLIC_KEY.lower() == public_key.address().lower(): print(mnemonic) print(f'privkey: {binascii.hexlify(private_key).decode("utf-8")}') print(f'pubkey: {binascii.hexlify(bytes(public_key)).decode("utf-8")}') print(f'address: {public_key.address()}') break