· 8 years ago · May 24, 2018, 08:12 PM
1import itertools
2import sqlite3
3
4import binascii
5import more_itertools as mit
6import requests
7from bip32utils import BIP32Key
8from mnemonic import Mnemonic
9from electrum import bitcoin, mnemonic, util, keystore, Network
10import argparse
11import sys
12from electrum.util import json_encode, print_msg
13import json
14n = Network()
15n.start()
16
17conn = sqlite3.connect('btc.db')
18c = conn.cursor()
19
20c.execute('''
21CREATE TABLE IF NOT EXISTS phrases (phrase text primary key, valid integer)
22''')
23
24###### SETUP HERE #######
25m = Mnemonic('english')
26target = '3CcxyPhyvyc3S9UuPfu42GNZLvVVV11Uk8'
27target2 = '37XTVuaWt1zyUPRgDDpsnoo5ioHk2Da6Fs'
28password = ""
29valid = 0
30invalid = 0
31nonemptywalletsfound = 0
32WORDS = 24
33
34def xpub2btc(xpub):
35 _xtype, _depth, _fp, _cn, _c, K = bitcoin.deserialize_xpub(xpub)
36 return bitcoin.pubkey_to_address("p2wpkh-p2sh", util.bh2u(K))
37
38with open('words.txt') as f:
39 words = f.read().split('\n')
40
41i = 0
42while True:
43 combi = list(mit.random_permutation(words, 24))
44 # combi = list(['fiscal','iron','trust','crawl','afford','middle','amused','cereal','catalog','walnut','melt','satisfy','inspire','prevent','keep','royal','juice','pilot','chuckle','amateur','essence','amazing','artefact','labor'])
45 phrase = ','.join(combi)
46 #print('Valid perms: ', valid, '/', invalid, ' /// ', i)
47
48 conn = sqlite3.connect('btc.db')
49 cursor = conn.cursor()
50 cursor.execute('SELECT * FROM phrases WHERE phrase=\'%s\'' % phrase)
51 if not cursor.fetchone():
52 cursor.execute('INSERT INTO phrases VALUES (\'%s\', %d)' % (phrase, 0))
53 conn.commit()
54 i += 1
55 try:
56 (checksum_ok, wordlist_ok) = keystore.bip39_is_checksum_valid(' '.join(combi))
57 #print("CHK: ", ' '.join(combi))
58 if not wordlist_ok:
59 #print("INVALID! words", i)
60 invalid += 1
61 cursor.execute('UPDATE phrases SET valid=1 WHERE phrase=\'%s\'' % phrase)
62 conn.commit()
63 if not checksum_ok:
64 #print('Invalid checksum')
65 invalid += 1
66 cursor.execute('UPDATE phrases SET valid=1 WHERE phrase=\'%s\'' % phrase)
67 conn.commit()
68 else:
69 valid += 1
70 line = ' '.join(combi)
71 s=util.bh2u(keystore.bip39_to_seed(line, password))
72 s=util.bfh(s)
73 #print('ok1')
74 xprv, _xpub = bitcoin.bip32_root(s, "standard")
75 #print('ok2')
76 xprv, _xpub = bitcoin.bip32_private_derivation(xprv, "", "49'")
77 #print('ok3')
78 xprv, _xpub = bitcoin.bip32_private_derivation(xprv, "", "0'")
79 #print('ok4')
80 xprv, _xpub = bitcoin.bip32_private_derivation(xprv, "", "0'")
81 #print('ok5')
82 xprv, _xpub = bitcoin.bip32_private_derivation(xprv, "", "0")
83 #print('ok6')
84 #_xtype, _depth, _fp, _cn, _c, K = bitcoin.deserialize_xpub(_xpub)
85 xprv, _xpub = bitcoin.bip32_private_derivation(xprv, "", "0")
86 #print('ok6')
87 btc_addr = xpub2btc(_xpub)
88 addr = btc_addr
89# addr = bitcoin.pubkey_to_address("p2wpkh-p2sh", util.bh2u(K))
90 #print('ok6')
91 #print('addr: ', addr, 'phrase: ', ' '.join(combi))
92 h = n.synchronous_get(('blockchain.address.get_balance',[addr]))
93 bal = h.get('confirmed')
94 print('Status: ', valid, 'valid /', invalid, ' invalid /// permutes tested: ', i, ' /// NonEmpty Wallets Found:', nonemptywalletsfound)
95 if bal > 0:
96 print('*** BALANCE FOUND: ', bal, 'addr: ', addr, 'bal: ', bal, 'phrase: ', ' '.join(combi))
97 nonemptywalletsfound += 1
98 else:
99 print('Empty addr: ', addr, 'bal: ', bal, 'phrase: ', ' '.join(combi))
100 print('\n')
101 address = addr
102 #print(address, i, combi)
103
104
105 cursor.execute('UPDATE phrases SET valid=1 WHERE phrase=\'%s\'' % phrase)
106 conn.commit()
107
108 if address.lower() != target.lower() and address.lower() != target2.lower():
109 #print('NOT A MATCH to A1 or A2!')
110 continue
111 print("MATCH FOUND!")
112 exit()
113
114 except Exception:
115 pass
116 finally:
117 conn.close()