forked from gurnec/btcrecover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseedrecover.py
1516 lines (1281 loc) · 76.5 KB
/
seedrecover.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# seedrecover.py -- Bitcoin mnemonic sentence recovery tool
# Copyright (C) 2015 Christopher Gurnee
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License version 2 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# If you find this program helpful, please consider a small
# donation to the developer at the following Bitcoin address:
#
# 17LGpN2z62zp7RS825jXwYtE7zZ19Mxxu8
#
# Thank You!
# PYTHON_ARGCOMPLETE_OK - enables optional bash tab completion
# TODO: finish pythonizing comments/documentation
# (all futures as of 2.6 and 2.7 except unicode_literals)
from __future__ import print_function, absolute_import, division, \
generators, nested_scopes, with_statement
__version__ = "0.4.1"
import btcrecover as btcr
import sys, os, io, base64, hashlib, hmac, difflib, itertools, \
unicodedata, collections, struct, glob, atexit, re
# Try to add the Armory libraries to the path for various platforms
if sys.platform == "win32":
progfiles_path = os.environ.get("ProgramFiles", r"C:\Program Files") # default is for XP
armory_path = progfiles_path + r"\Armory"
sys.path.extend((armory_path, armory_path + r"\library.zip"))
# 64-bit Armory might install into the 32-bit directory; if this is 64-bit Python look in both
if struct.calcsize('P') * 8 == 64: # calcsize('P') is a pointer's size in bytes
assert not progfiles_path.endswith("(x86)"), "ProgramFiles doesn't end with '(x86)' on x64 Python"
progfiles_path += " (x86)"
armory_path = progfiles_path + r"\Armory"
sys.path.extend((armory_path, armory_path + r"\library.zip"))
elif sys.platform.startswith("linux"):
sys.path.append("/usr/lib/armory")
elif sys.platform == "darwin": # untested
sys.path.append("/Applications/Armory.app/Contents/MacOS/py/usr/lib/armory")
#
from CppBlockUtils import CryptoECDSA, SecureBinaryData
# Order of the base point generator, from SEC 2
GENERATOR_ORDER = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141L
################################### Utility Functions ###################################
def bytes_to_int(bytes_rep):
"""convert a string of bytes (in big-endian order) to a long integer
:param bytes_rep: the raw bytes
:type bytes_rep: str
:return: the unsigned integer
:rtype: long
"""
return long(base64.b16encode(bytes_rep), 16)
def int_to_bytes(int_rep, min_length = 0):
"""convert an unsigned integer to a string of bytes (in big-endian order)
:param int_rep: a non-negative integer
:type int_rep: long or int
:param min_length: the minimum output length
:type min_length: int
:return: the raw bytes, zero-padded (at the beginning) if necessary
:rtype: str
"""
assert int_rep >= 0
hex_rep = "{:X}".format(int_rep)
if len(hex_rep) % 2 == 1: # The hex decoder below requires
hex_rep = "0" + hex_rep # exactly 2 chars per byte.
return base64.b16decode(hex_rep).rjust(min_length, "\0")
dec_digit_to_base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base58_digit_to_dec = { b58:dec for dec,b58 in enumerate(dec_digit_to_base58) }
def base58check_to_bytes(base58_rep, expected_size):
"""decode a base58check string to its raw bytes
:param base58_rep: check-code appended base58-encoded string
:type base58_rep: str
:param expected_size: the expected number of decoded bytes (excluding the check code)
:type expected_size: int
:return: the base58-decoded bytes
:rtype: str
"""
base58_stripped = base58_rep.lstrip("1")
int_rep = 0
for base58_digit in base58_stripped:
int_rep *= 58
int_rep += base58_digit_to_dec[base58_digit]
# Convert int to raw bytes
all_bytes = int_to_bytes(int_rep, expected_size + 4)
zero_count = next(zeros for zeros,byte in enumerate(all_bytes) if byte != "\0")
if len(base58_rep) - len(base58_stripped) != zero_count:
raise ValueError("prepended zeros mismatch")
if hashlib.sha256(hashlib.sha256(all_bytes[:-4]).digest()).digest()[:4] != all_bytes[-4:]:
raise ValueError("base58 check code mismatch")
return all_bytes[:-4]
def base58check_to_hash160(base58_rep):
"""convert from a base58check address to its hash160 form
:param base58_rep: check-code appended base58-encoded address
:type base58_rep: str
:return: the ripemd160(sha256()) hash of the pubkey/redeemScript, then the version byte
:rtype: (str, str)
"""
decoded_bytes = base58check_to_bytes(base58_rep, 1 + 20)
return decoded_bytes[1:], decoded_bytes[0]
BIP32ExtendedKey = collections.namedtuple("BIP32ExtendedKey",
"version depth fingerprint child_number chaincode key")
#
def base58check_to_bip32(base58_rep):
"""decode a bip32-serialized extended key from its base58check form
:param base58_rep: check-code appended base58-encoded bip32 extended key
:type base58_rep: str
:return: a namedtuple containing: version depth fingerprint child_number chaincode key
:rtype: BIP32ExtendedKey
"""
decoded_bytes = base58check_to_bytes(base58_rep, 4 + 1 + 4 + 4 + 32 + 33)
return BIP32ExtendedKey(decoded_bytes[0:4], ord(decoded_bytes[ 4:5]), decoded_bytes[ 5:9],
struct.unpack(">I", decoded_bytes[9:13])[0], decoded_bytes[13:45], decoded_bytes[45:])
def pubkey_to_hash160(pubkey_bytes):
"""convert from a raw public key to its a hash160 form
:param pubkey_bytes: SEC 1 EllipticCurvePoint OctetString
:type pubkey_bytes: str
:return: ripemd160(sha256(pubkey_bytes))
:rtype: str
"""
assert len(pubkey_bytes) == 65 and pubkey_bytes[0] == "\x04" or \
len(pubkey_bytes) == 33 and pubkey_bytes[0] in "\x02\x03"
return hashlib.new("ripemd160", hashlib.sha256(pubkey_bytes).digest()).digest()
def compress_pubkey(uncompressed_pubkey):
"""convert an uncompressed public key into a compressed public key
:param uncompressed_pubkey: the uncompressed public key
:type uncompressed_pubkey: str
:return: the compressed public key
:rtype: str
"""
assert len(uncompressed_pubkey) == 65 and uncompressed_pubkey[0] == "\x04"
return chr((ord(uncompressed_pubkey[-1]) & 1) + 2) + uncompressed_pubkey[1:33]
print = btcr.safe_print # use btcr's print which never dies from printing Unicode
################################### Wallets ###################################
# A class decorator which adds a wallet class to a registered
# list that can later be selected by a user in GUI mode
selectable_wallet_classes = []
def register_selectable_wallet_class(description):
def _register_selectable_wallet_class(cls):
selectable_wallet_classes.append((cls, description))
return cls
return _register_selectable_wallet_class
# Loads a wordlist from a file into a list of Python unicodes. Note that the
# unicodes are normalized in NFC format, which is not what BIP39 requires (NFKD).
wordlists_dir = os.path.join(os.path.dirname(__file__), "wordlists")
def load_wordlist(name, lang):
filename = os.path.join(wordlists_dir, "{}-{}.txt".format(name, lang))
with io.open(filename, encoding="utf_8_sig") as wordlist_file:
wordlist = []
for word in wordlist_file:
word = word.strip()
if word and not word.startswith(u"#"):
wordlist.append(unicodedata.normalize("NFC", word))
return wordlist
btcr.clear_registered_wallets() # clears btcr's default wallet file auto-detection list
############### Electrum1 ###############
@register_selectable_wallet_class("Electrum 1.x (including wallets upgraded to 2.x)")
@btcr.register_wallet_class # enables wallet type auto-detection via is_wallet_file()
class WalletElectrum1(object):
_words = None
@classmethod
def _load_wordlist(cls):
if not cls._words:
cls._words = tuple(map(str, load_wordlist("electrum1", "en"))) # also converts to ASCII
cls._word_to_id = { word:id for id,word in enumerate(cls._words) }
@property
def word_ids(self): return xrange(len(self._words))
@classmethod
def id_to_word(cls, id): return cls._words[id]
@staticmethod
def is_wallet_file(wallet_file):
wallet_file.seek(0)
# returns "maybe yes" or "definitely no"
return None if wallet_file.read(2) == b"{'" else False
def __init__(self, loading = False):
assert loading, "use load_from_filename or create_from_params to create a " + self.__class__.__name__
self._master_pubkey = None
self._load_wordlist()
self._num_words = len(self._words) # needs to be an instance variable so it can be pickled
def __getstate__(self):
# Convert unpicklable Armory library object to a standard binary string
state = self.__dict__.copy()
if self._master_pubkey:
state["_master_pubkey"] = self._master_pubkey.toBinStr()
return state
def __setstate__(self, state):
# Restore unpicklable Armory library object
if state["_master_pubkey"]:
state["_master_pubkey"] = SecureBinaryData(state["_master_pubkey"])
self.__dict__ = state
@staticmethod
def passwords_per_seconds(seconds):
return max(int(round(8 * seconds)), 1)
# Load an Electrum1 wallet file (the part of it we need, just the master public key)
@classmethod
def load_from_filename(cls, wallet_filename):
from ast import literal_eval
with open(wallet_filename) as wallet_file:
wallet = literal_eval(wallet_file.read(1048576)) # up to 1M, typical size is a few k
return cls._load_from_dict(wallet)
@classmethod
def _load_from_dict(cls, wallet):
seed_version = wallet.get("seed_version")
if seed_version is None: raise ValueError("Unrecognized wallet format (Electrum1 seed_version not found)")
if seed_version != 4: raise NotImplementedError("Unsupported Electrum1 seed version " + seed_version)
if not wallet.get("use_encryption"): raise ValueError("Electrum1 wallet is not encrypted")
master_pubkey = base64.b16decode(wallet["master_public_key"], casefold=True)
if len(master_pubkey) != 64: raise ValueError("Electrum1 master public key is not 64 bytes long")
self = cls(loading=True)
self._master_pubkey = SecureBinaryData("\x04" + master_pubkey) # prepend the uncompressed tag
return self
# Creates a wallet instance from either an mpk or an address and address_limit.
# If neither an mpk nor address is supplied, prompts the user for one or the other.
@classmethod
def create_from_params(cls, mpk = None, address = None, address_limit = None, is_performance = False):
self = cls(loading=True)
# Process the mpk (master public key) argument
if mpk:
if len(mpk) != 128:
raise ValueError("an Electrum 1.x master public key must be exactly 128 hex digits long")
try:
mpk = base64.b16decode(mpk, casefold=True)
# (it's assigned to the self._master_pubkey later)
except TypeError as e:
raise ValueError(e) # consistently raise ValueError for any bad inputs
# Process the address argument
if address:
if mpk:
print("warning: address is ignored when an mpk is provided", file=sys.stderr)
else:
self._known_hash160, version_byte = base58check_to_hash160(address)
if ord(version_byte) != 0:
raise ValueError("the address must be a P2PKH address")
# Process the address_limit argument
if address_limit:
if mpk:
print("warning: address limit is ignored when an mpk is provided", file=sys.stderr)
else:
address_limit = int(address_limit)
if address_limit <= 0:
raise ValueError("the address limit must be > 0")
# (it's assigned to self._addrs_to_generate later)
# If neither mpk nor address arguments were provided, prompt the user for an mpk first
if not mpk and not address:
init_gui()
while True:
mpk = tkSimpleDialog.askstring("Electrum 1.x master public key",
"Please enter your master public key if you have it, or click Cancel to search by an address instead:",
initialvalue="c79b02697b32d9af63f7d2bd882f4c8198d04f0e4dfc5c232ca0c18a87ccc64ae8829404fdc48eec7111b99bda72a7196f9eb8eb42e92514a758f5122b6b5fea"
if is_performance else None)
if not mpk:
break # if they pressed Cancel, stop prompting for an mpk
mpk = mpk.strip()
try:
if len(mpk) != 128:
raise TypeError()
mpk = base64.b16decode(mpk, casefold=True) # raises TypeError() on failure
break
except TypeError:
tkMessageBox.showerror("Master public key", "The entered Electrum 1.x key is not exactly 128 hex digits long")
# If an mpk has been provided (in the function call or from a user), convert it to the needed format
if mpk:
assert len(mpk) == 64, "mpk is 64 bytes long (after decoding from hex)"
self._master_pubkey = SecureBinaryData("\x04" + mpk) # prepend the uncompressed tag
# If an mpk wasn't provided (at all), and an address also wasn't provided
# (in the original function call), prompt the user for an address.
else:
if not address:
# init_gui() was already called above
while True:
address = tkSimpleDialog.askstring("Bitcoin address",
"Please enter an address from your wallet, preferably one created early in your wallet's lifetime:",
initialvalue="17LGpN2z62zp7RS825jXwYtE7zZ19Mxxu8" if is_performance else None)
if not address:
sys.exit("canceled")
address = address.strip()
try:
# (raises ValueError() on failure):
self._known_hash160, version_byte = base58check_to_hash160(address)
if ord(version_byte) != 0:
raise ValueError("not a Bitcoin P2PKH address; version byte is {:#04x}".format(ord(version_byte)))
break
except ValueError as e:
tkMessageBox.showerror("Bitcoin address", "The entered address is invalid ({})".format(e))
if not address_limit:
init_gui() # might not have been called yet
address_limit = tkSimpleDialog.askinteger("Address limit",
"Please enter the address generation limit. Smaller will\n"
"be faster, but it must be equal to at least the number\n"
"of addresses created before the one you just entered:", minvalue=1)
if not address_limit:
sys.exit("canceled")
self._addrs_to_generate = address_limit
return self
# Performs basic checks so that clearly invalid mnemonic_ids can be completely skipped
@staticmethod
def verify_mnemonic_syntax(mnemonic_ids):
return len(mnemonic_ids) == 12 and None not in mnemonic_ids
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a mnemonic
# is correct return it, else return False for item 0; return a count of mnemonics checked for item 1
def return_verified_password_or_false(self, mnemonic_ids_list):
# Copy some vars into local for a small speed boost
l_sha256 = hashlib.sha256
num_words = self._num_words
num_words2 = num_words * num_words
crypto_ecdsa = CryptoECDSA()
for count, mnemonic_ids in enumerate(mnemonic_ids_list, 1):
# Compute the binary seed from the word list the Electrum1 way
seed = ""
for i in xrange(0, 12, 3):
seed += "{:08x}".format( mnemonic_ids[i ]
+ num_words * ( (mnemonic_ids[i + 1] - mnemonic_ids[i ]) % num_words )
+ num_words2 * ( (mnemonic_ids[i + 2] - mnemonic_ids[i + 1]) % num_words ))
#
unstretched_seed = seed
for i in xrange(100000): # Electrum1's seed stretching
seed = l_sha256(seed + unstretched_seed).digest()
# If a master public key was provided, check the pubkey derived from the seed against it
if self._master_pubkey:
if crypto_ecdsa.CheckPubPrivKeyMatch(SecureBinaryData(seed), self._master_pubkey):
return mnemonic_ids, count # found it
# Else derive addrs_to_generate addresses from the seed, searching for a match with known_hash160
else:
master_privkey = bytes_to_int(seed)
master_pubkey_bytes = crypto_ecdsa.ComputePublicKey(SecureBinaryData(seed)).toBinStr()
assert master_pubkey_bytes[0] == "\x04", "ComputePublicKey() returns an uncompressed pubkey"
master_pubkey_bytes = master_pubkey_bytes[1:] # remove the uncompressed tag byte
for seq_num in xrange(self._addrs_to_generate):
# Compute the next deterministic private/public key pair the Electrum1 way.
# FYI we derive a privkey first, and then a pubkey from that because it's
# likely faster than deriving a pubkey directly from the base point and
# seed -- it means doing a simple modular addition instead of a point
# addition (plus a scalar point multiplication which is needed for both).
d_offset = bytes_to_int( l_sha256(l_sha256(
"{}:0:{}".format(seq_num, master_pubkey_bytes) # 0 means: not a change address
).digest()).digest() )
d_privkey = int_to_bytes((master_privkey + d_offset) % GENERATOR_ORDER, 32)
d_pubkey = crypto_ecdsa.ComputePublicKey(SecureBinaryData(d_privkey))
if pubkey_to_hash160(d_pubkey.toBinStr()) == self._known_hash160: # assumes uncompressed
return mnemonic_ids, count # found it
return False, count
# Configures the values of four globals used later in config_btcrecover():
# mnemonic_ids_guess, close_mnemonic_ids, num_inserts, and num_deletes
@classmethod
def config_mnemonic(cls, mnemonic_guess = None, closematch_cutoff = 0.65):
# If a mnemonic guess wasn't provided, prompt the user for one
if not mnemonic_guess:
init_gui()
mnemonic_guess = tkSimpleDialog.askstring("Electrum seed",
"Please enter your best guess for your Electrum seed:")
if not mnemonic_guess:
sys.exit("canceled")
mnemonic_guess = str(mnemonic_guess) # ensures it's ASCII
# Convert the mnemonic words into numeric ids and pre-calculate similar mnemonic words
global mnemonic_ids_guess, close_mnemonic_ids
mnemonic_ids_guess = ()
# close_mnemonic_ids is a dict; each dict key is a mnemonic_id (int), and each
# dict value is a tuple containing length 1 tuples, and finally each of the
# length 1 tuples contains a single mnemonic_id which is similar to the dict's key
close_mnemonic_ids = {}
for word in mnemonic_guess.lower().split():
close_words = difflib.get_close_matches(word, cls._words, sys.maxint, closematch_cutoff)
if close_words:
if close_words[0] != word:
print("'{}' was in your guess, but it's not a valid Electrum seed word;\n"
" trying '{}' instead.".format(word, close_words[0]))
mnemonic_ids_guess += cls._word_to_id[close_words[0]],
close_mnemonic_ids[mnemonic_ids_guess[-1]] = tuple( (cls._word_to_id[w],) for w in close_words[1:] )
else:
print("'{}' was in your guess, but there is no similar Electrum seed word;\n"
" trying all possible seed words here instead.".format(word))
mnemonic_ids_guess += None,
global num_inserts, num_deletes
num_inserts = max(12 - len(mnemonic_ids_guess), 0)
num_deletes = max(len(mnemonic_ids_guess) - 12, 0)
if num_inserts:
print("Seed sentence was too short, inserting {} word{} into each guess."
.format(num_inserts, "s" if num_inserts > 1 else ""))
if num_deletes:
print("Seed sentence was too long, deleting {} word{} from each guess."
.format(num_deletes, "s" if num_deletes > 1 else ""))
# Produces an infinite stream of differing mnemonic_ids guesses (for testing)
@staticmethod
def performance_iterator():
return itertools.product(xrange(len(WalletElectrum1._words)), repeat = 12)
############### BIP32 ###############
class WalletBIP32(object):
def __init__(self, path = None, loading = False):
assert loading, "use load_from_filename or create_from_params to create a " + self.__class__.__name__
self._chaincode = None
# Split the BIP32 key derivation path into its constituent indexes
# (doesn't support the last path element for the address as hardened)
if not path: # Defaults to BIP44
path = "m/44'/0'/0'/"
# Append the internal/external (change) index to the path in create_from_params()
self._append_last_index = True
else:
self._append_last_index = False
path_indexes = path.split('/')
if path_indexes[0] == "m" or path_indexes[0] == "":
del path_indexes[0] # the optional leading "m/"
assert path_indexes[-1] != "'", "the last path element is not hardened"
if path_indexes[-1] == "":
del path_indexes[-1] # the optional trailing "/"
self._path_indexes = ()
for path_index in path_indexes:
if path_index.endswith("'"):
self._path_indexes += int(path_index[:-1]) + 2**31,
else:
self._path_indexes += int(path_index),
# Creates a wallet instance from either an mpk or an address and address_limit.
# If neither an mpk nor address is supplied, prompts the user for one or the other.
# (the BIP32 key derivation path is by default BIP44's account 0)
@classmethod
def create_from_params(cls, mpk = None, address = None, address_limit = None, path = None, is_performance=False):
self = cls(path, loading=True)
# Process the mpk (master public key) argument
if mpk:
if not mpk.startswith("xpub"):
raise ValueError("the BIP32 extended public key must begin with 'xpub'")
mpk = base58check_to_bip32(mpk)
# (it's processed more later)
# Process the address argument
if address:
if mpk:
print("warning: address is ignored when an mpk is provided", file=sys.stderr)
else:
self._known_hash160, version_byte = base58check_to_hash160(address)
if ord(version_byte) != 0:
raise ValueError("the address must be a P2PKH address")
# Process the address_limit argument
if address_limit:
if mpk:
print("warning: address limit is ignored when an mpk is provided", file=sys.stderr)
else:
address_limit = int(address_limit)
if address_limit <= 0:
raise ValueError("the address limit must be > 0")
# (it's assigned to self._addrs_to_generate later)
# If neither mpk nor address arguments were provided, prompt the user for an mpk first
if not mpk and not address:
init_gui()
while True:
mpk = tkSimpleDialog.askstring("Master extended public key",
"Please enter your master extended public key (xpub) if you "
"have it, or click Cancel to search by an address instead",
initialvalue=self._performance_xpub() if is_performance else None)
if not mpk:
break # if they pressed Cancel, stop prompting for an mpk
mpk = mpk.strip()
try:
if not mpk.startswith("xpub"):
raise ValueError("not a BIP32 extended public key (doesn't start with 'xpub')")
mpk = base58check_to_bip32(mpk)
break
except ValueError as e:
tkMessageBox.showerror("Master extended public key", "The entered key is invalid ({})".format(e))
# If an mpk has been provided (in the function call or from a user), extract the
# required chaincode and adjust the path to match the mpk's depth and child number
if mpk:
if mpk.depth == 0:
print("xpub depth: 0")
assert mpk.child_number == 0, "child_number == 0 when depth == 0"
else:
if mpk.child_number < 2**31:
child_num = mpk.child_number
else:
child_num = str(mpk.child_number - 2**31) + "'"
print("xpub depth: {}\n"
"xpub fingerprint: {}\n"
"xpub child #: {}"
.format(mpk.depth, base64.b16encode(mpk.fingerprint), child_num))
self._chaincode = mpk.chaincode
if mpk.depth <= len(self._path_indexes): # if this, ensure the path
self._path_indexes = self._path_indexes[:mpk.depth] # length matches the depth
if self._path_indexes and self._path_indexes[-1] != mpk.child_number:
raise ValueError("the extended public key's child # doesn't match "
"the corresponding index of this wallet's path")
elif mpk.depth == 1 + len(self._path_indexes) and self._append_last_index:
self._path_indexes += mpk.child_number,
else:
raise ValueError(
"the extended public key's depth exceeds the length of this wallet's path ({})"
.format(len(self._path_indexes)))
else: # else if not mpk
# If we don't have an mpk but need to append the last
# index, assume it's the external (non-change) chain
if self._append_last_index:
self._path_indexes += 0,
# If an mpk wasn't provided (at all), and an address also wasn't provided
# (in the original function call), prompt the user for an address.
if not address:
# init_gui() was already called above
while True:
address = tkSimpleDialog.askstring("Bitcoin address",
"Please enter an address from the first account in your wallet,\n"
"preferably one created early in the account's lifetime:",
initialvalue="17LGpN2z62zp7RS825jXwYtE7zZ19Mxxu8" if is_performance else None)
if not address:
sys.exit("canceled")
address = address.strip()
try:
# (raises ValueError() on failure):
self._known_hash160, version_byte = base58check_to_hash160(address)
if ord(version_byte) != 0:
raise ValueError("not a Bitcoin P2PKH address; version byte is {:#04x}".format(ord(version_byte)))
break
except ValueError as e:
tkMessageBox.showerror("Bitcoin address", "The entered address is invalid ({})".format(e))
if not address_limit:
init_gui() # might not have been called yet
address_limit = tkSimpleDialog.askinteger("Address limit",
"Please enter the address generation limit. Smaller will\n"
"be faster, but it must be equal to at least the number\n"
"of addresses created before the one you just entered:", minvalue=1)
if not address_limit:
sys.exit("canceled")
self._addrs_to_generate = address_limit
return self
# Performs basic checks so that clearly invalid mnemonic_ids can be completely skipped
@staticmethod
def verify_mnemonic_syntax(mnemonic_ids):
# Length must be divisible by 3 and all ids must be present
return len(mnemonic_ids) % 3 == 0 and None not in mnemonic_ids
# This is the time-consuming function executed by worker thread(s). It returns a tuple: if a mnemonic
# is correct return it, else return False for item 0; return a count of mnemonics checked for item 1
def return_verified_password_or_false(self, mnemonic_ids_list):
hardened_min = 2**31 # BIP32 path indexes >= this are hardened/private
crypto_ecdsa = CryptoECDSA()
for count, mnemonic_ids in enumerate(mnemonic_ids_list, 1):
# Check the (BIP39 or Electrum2) checksum; most guesses will fail this test
if not self._verify_checksum(mnemonic_ids):
continue
# Convert the mnemonic sentence to seed bytes (according to BIP39 or Electrum2)
seed_bytes = hmac.new("Bitcoin seed", self._derive_seed(mnemonic_ids), hashlib.sha512).digest()
# Derive the chain of private keys for the specified path as per BIP32
privkey_bytes = seed_bytes[:32]
chaincode_bytes = seed_bytes[32:]
for i in self._path_indexes:
if i < hardened_min: # if it's a normal child key
data_to_hmac = compress_pubkey( # derive the compressed public key
crypto_ecdsa.ComputePublicKey(SecureBinaryData(privkey_bytes)).toBinStr())
else: # else it's a hardened child key
data_to_hmac = "\0" + privkey_bytes # prepended "\0" as per BIP32
data_to_hmac += struct.pack(">I", i) # append the index (big-endian) as per BIP32
seed_bytes = hmac.new(chaincode_bytes, data_to_hmac, hashlib.sha512).digest()
# The child private key is the parent one + the first half of the seed_bytes (mod n)
privkey_bytes = int_to_bytes((bytes_to_int(seed_bytes[:32]) +
bytes_to_int(privkey_bytes)) % GENERATOR_ORDER)
chaincode_bytes = seed_bytes[32:]
# If an extended public key was provided, check the derived chain code against it
if self._chaincode:
if seed_bytes[32:] == self._chaincode:
return mnemonic_ids, count # found it
else:
# (note: the rest doesn't support the last path element being hardened)
# Derive the final public keys, searching for a match with known_hash160
# (the first step below is a loop invariant)
data_to_hmac = compress_pubkey( # derive the parent's compressed public key
crypto_ecdsa.ComputePublicKey(SecureBinaryData(privkey_bytes)).toBinStr())
#
for i in xrange(self._addrs_to_generate):
seed_bytes = hmac.new(chaincode_bytes,
data_to_hmac + struct.pack(">I", i), hashlib.sha512).digest()
# The final derived private key is the parent one + the first half of the seed_bytes
d_privkey_bytes = int_to_bytes((bytes_to_int(seed_bytes[:32]) +
bytes_to_int(privkey_bytes)) % GENERATOR_ORDER)
d_pubkey = compress_pubkey( # a compressed public key as per BIP32
crypto_ecdsa.ComputePublicKey(SecureBinaryData(d_privkey_bytes)).toBinStr())
if pubkey_to_hash160(d_pubkey) == self._known_hash160:
return mnemonic_ids, count # found it
return False, count
# Returns a dummy xpub for performance testing purposes
@staticmethod
def _performance_xpub():
# an xpub at path m/44'/0'/0', as Mycelium for Android would export
return "xpub6BgCDhMefYxRS1gbVbxyokYzQji65v1eGJXGEiGdoobvFBShcNeJt97zoJBkNtbASLyTPYXJHRvkb3ahxaVVGEtC1AD4LyuBXULZcfCjBZx"
############### BIP39 ###############
@register_selectable_wallet_class("Generic BIP39/BIP44 (Mycelium, TREZOR)")
class WalletBIP39(WalletBIP32):
# Load the wordlists for all languages (actual one to use is selected in config_mnemonic() )
_language_words = {}
@classmethod
def _load_wordlists(cls, name = "bip39"):
for filename in glob.iglob(os.path.join(wordlists_dir, name + "-??*.txt")):
wordlist_lang = os.path.basename(filename)[len(name)+1:-4] # e.g. "en", or "zh-hant"
wordlist = load_wordlist(name, wordlist_lang)
assert len(wordlist) == 2048 or cls is not WalletBIP39, "BIP39 wordlist has 2048 words"
cls._language_words[wordlist_lang] = wordlist
@property
def word_ids(self): return self._words
@staticmethod
def id_to_word(id): return id # returns a UTF-8 encoded bytestring
def __init__(self, path = None, loading = False):
super(WalletBIP39, self).__init__(path, loading)
if not self._language_words:
self._load_wordlists()
btcr.load_pbkdf2_library() # btcr's pbkdf2 library is used in _derive_seed()
def __setstate__(self, state):
self.__dict__ = state
# (Re)load the pbkdf2 library if necessary
btcr.load_pbkdf2_library()
def passwords_per_seconds(self, seconds):
# (experimentally determined; doesn't have to be perfect, just decent)
#
# number of 3-word groups in excess of the base (of 12); they speed things up:
extra_word_groups = (len(mnemonic_ids_guess) + num_inserts - num_deletes - 12) // 3
#
# number of priv-to-pubkey derivations per checksum-validated seed; they slow things down:
derivation_count = 0 if self._chaincode else len(self._path_indexes) + self._addrs_to_generate
#
est = 3500.0 * 1.6**extra_word_groups * (3.0 / max(derivation_count, 6.0) if derivation_count else 1.0)
return max(int(round(est * seconds)), 1)
# Converts a mnemonic word from a Python unicode (as produced by load_wordlist())
# into a bytestring (of type str) in the format required by BIP39
@staticmethod
def _unicode_to_bytes(word):
assert isinstance(word, unicode)
return intern(unicodedata.normalize("NFKD", word).encode("utf_8"))
# Configures the values of four globals used later in config_btcrecover():
# mnemonic_ids_guess, close_mnemonic_ids, num_inserts, and num_deletes;
# also selects the appropriate wordlist language to use
def config_mnemonic(self, mnemonic_guess = None, lang = None, passphrase = u"", expected_len = None, closematch_cutoff = 0.65):
if expected_len:
if expected_len < 12:
raise ValueError("minimum BIP39 sentence length is 12 words")
if expected_len > 24:
raise ValueError("maximum BIP39 sentence length is 24 words")
if expected_len % 3 != 0:
raise ValueError("BIP39 sentence length must be evenly divisible by 3")
# The pbkdf2 salt as per BIP39 (needed by _derive_seed())
self._derivation_salt = "mnemonic" + self._unicode_to_bytes(passphrase)
# Do most of the work in this function:
self._config_mnemonic(mnemonic_guess, lang, expected_len, closematch_cutoff)
# Calculate each word's index in binary (needed by _verify_checksum())
self._word_to_binary = { word : "{:011b}".format(i) for i,word in enumerate(self._words) }
#
def _config_mnemonic(self, mnemonic_guess, lang, expected_len, closematch_cutoff):
# If a mnemonic guess wasn't provided, prompt the user for one
if not mnemonic_guess:
init_gui()
mnemonic_guess = tkSimpleDialog.askstring("Seed",
"Please enter your best guess for your seed (mnemonic):")
if not mnemonic_guess:
sys.exit("canceled")
# Note: this is not in BIP39's preferred encoded form yet, instead it's
# in the same format as load_wordlist creates (NFC normalized Unicode)
mnemonic_guess = unicodedata.normalize("NFC", unicode(mnemonic_guess).lower()).split()
if len(mnemonic_guess) == 1: # assume it's a logographic script (no spaces, e.g. Chinese)
mnemonic_guess = tuple(mnemonic_guess)
# Select the appropriate wordlist language to use
if not lang:
language_word_hits = {} # maps a language id to the # of words found in that language
for word in mnemonic_guess:
for lang, one_languages_words in self._language_words.iteritems():
if word in one_languages_words:
language_word_hits.setdefault(lang, 0)
language_word_hits[lang] += 1
if len(language_word_hits) == 0:
raise ValueError("unable to determine wordlist language: 0 word hits")
sorted_hits = language_word_hits.items()
sorted_hits.sort(key=lambda x: x[1]) # sort based on hit count
if len(sorted_hits) > 1 and sorted_hits[-1][1] == sorted_hits[-2][1]:
raise ValueError("unable to determine wordlist language: top best guesses ({}, {}) have equal hits ({})"
.format(sorted_hits[-1][0], sorted_hits[-2][0], sorted_hits[-1][1]))
lang = sorted_hits[-1][0] # (probably) found it
#
try:
words = self._language_words[lang]
except KeyError: # consistently raise ValueError for any bad inputs
raise ValueError("can't find wordlist for language code '{}'".format(lang))
self._lang = lang
print("Using the '{}' wordlist.".format(lang))
# Build the mnemonic_ids_guess and pre-calculate similar mnemonic words
global mnemonic_ids_guess, close_mnemonic_ids
mnemonic_ids_guess = ()
# close_mnemonic_ids is a dict; each dict key is a mnemonic_id (a string), and
# each dict value is a tuple containing length 1 tuples, and finally each of the
# length 1 tuples contains a single mnemonic_id which is similar to the dict's key;
# e.g.: { "a-word" : ( ("a-ward", ), ("a-work",) ), "other-word" : ... }
close_mnemonic_ids = {}
for word in mnemonic_guess:
close_words = difflib.get_close_matches(word, words, sys.maxint, closematch_cutoff)
if close_words:
if close_words[0] != word:
print(u"'{}' was in your guess, but it's not a valid seed word;\n"
u" trying '{}' instead.".format(word, close_words[0]))
mnemonic_ids_guess += self._unicode_to_bytes(close_words[0]), # *now* convert to BIP39's format
close_mnemonic_ids[mnemonic_ids_guess[-1]] = \
tuple( (self._unicode_to_bytes(w),) for w in close_words[1:] )
else:
print(u"'{}' was in your guess, but there is no similar seed word;\n"
" trying all possible seed words here instead.".format(word))
mnemonic_ids_guess += None,
guess_len = len(mnemonic_ids_guess)
if not expected_len:
if guess_len < 12:
expected_len = 12
elif guess_len > 24:
expected_len = 24
else:
off_by = guess_len % 3
if off_by == 1:
expected_len = guess_len - 1
elif off_by == 2:
expected_len = guess_len + 1
else:
expected_len = guess_len
global num_inserts, num_deletes
num_inserts = max(expected_len - guess_len, 0)
num_deletes = max(guess_len - expected_len, 0)
if num_inserts:
print("Seed sentence was too short, inserting {} word{} into each guess."
.format(num_inserts, "s" if num_inserts > 1 else ""))
if num_deletes:
print("Seed sentence was too long, deleting {} word{} from each guess."
.format(num_deletes, "s" if num_deletes > 1 else ""))
# Now that we're done with the words in Unicode format,
# convert them to BIP39's encoding and save for future reference
self._words = tuple(map(self._unicode_to_bytes, words))
# Called by WalletBIP32.return_verified_password_or_false() to verify a BIP39 checksum
def _verify_checksum(self, mnemonic_words):
# Convert from the mnemonic_words (ids) back to the entropy bytes + checksum
bit_string = "".join(self._word_to_binary[w] for w in mnemonic_words)
cksum_len_in_bits = len(mnemonic_words) // 3 # as per BIP39
entropy_bytes = bytearray()
for i in xrange(0, len(bit_string) - cksum_len_in_bits, 8):
entropy_bytes.append(int(bit_string[i:i+8], 2))
cksum_int = int(bit_string[-cksum_len_in_bits:], 2)
#
# Calculate and verify the checksum
return ord(hashlib.sha256(entropy_bytes).digest()[:1]) >> 8-cksum_len_in_bits \
== cksum_int
# Called by WalletBIP32.return_verified_password_or_false() to create a binary seed
def _derive_seed(self, mnemonic_words):
# Note: the words are already in BIP39's normalized form
return btcr.pbkdf2_hmac("sha512", " ".join(mnemonic_words), self._derivation_salt, 2048)
# Produces an infinite stream of differing mnemonic_ids guesses (for testing)
# (uses mnemonic_ids_guess, num_inserts, and num_deletes globals as set by config_mnemonic())
def performance_iterator(self):
return itertools.product(self._words, repeat= len(mnemonic_ids_guess) + num_inserts - num_deletes)
############### bitcoinj ###############
@register_selectable_wallet_class("Bitcoinj compatible (MultiBit HD (Beta 8+), Bitcoin Wallet for Android, Hive, breadwallet)")
@btcr.register_wallet_class # enables wallet type auto-detection via is_wallet_file()
class WalletBitcoinj(WalletBIP39):
def __init__(self, path = None, loading = False):
# Just calls WalletBIP39.__init__() with a hardcoded path
if path: raise ValueError("can't specify a BIP32 path with Bitcoinj wallets")
super(WalletBitcoinj, self).__init__("m/0'/0/", loading)
@staticmethod
def is_wallet_file(wallet_file):
wallet_file.seek(0)
if wallet_file.read(1) == b"\x0a": # protobuf field number 1 of type length-delimited
network_identifier_len = ord(wallet_file.read(1))
if 1 <= network_identifier_len < 128:
wallet_file.seek(2 + network_identifier_len)
if wallet_file.read(1) in b"\x12\x1a": # field number 2 or 3 of type length-delimited
return True
return False
# Load a bitcoinj wallet file (the part of it we need, just the chaincode)
@classmethod
def load_from_filename(cls, wallet_filename):
import wallet_pb2
pb_wallet = wallet_pb2.Wallet()
with open(wallet_filename, "rb") as wallet_file:
pb_wallet.ParseFromString(wallet_file.read(1048576)) # up to 1M, typical size is a few k
if pb_wallet.encryption_type == wallet_pb2.Wallet.UNENCRYPTED:
raise ValueError("this bitcoinj wallet is not encrypted")
# Search for the (one and only) master public extended key (whose path length is 0)
self = None
for key in pb_wallet.key:
if key.HasField("deterministic_key") and len(key.deterministic_key.path) == 0:
assert not self, "only one master public extended key is in the wallet file"
assert len(key.deterministic_key.chain_code) == 32, "chaincode length is 32 bytes"
self = cls(loading=True)
self._chaincode = key.deterministic_key.chain_code
# Because it's the *master* xpub, it has an empty path
self._path_indexes = ()
if not self:
raise ValueError("No master public extended key was found in this bitcoinj wallet file")
return self
# Returns a dummy xpub for performance testing purposes
@staticmethod
def _performance_xpub():
# an xpub at path m/0', as Bitcoin Wallet for Android would export
return "xpub67tjk7ug7iNivs1f1pmDswDDbk6kRCe4U1AXSiYLbtp6a2GaodSUovt3kNrDJ2q18TBX65aJZ7VqRBpnVJsaVQaBY2SANYw6kgZf4QLCpPu"
############### Electrum2 ###############
@register_selectable_wallet_class("Electrum 2.x (initially created with 2.x)")
@btcr.register_wallet_class # enables wallet type auto-detection via is_wallet_file()
class WalletElectrum2(WalletBIP39):
# Load the wordlists for all languages (actual one to use is selected in config_mnemonic() )
@classmethod
def _load_wordlists(cls):
super(WalletElectrum2, cls)._load_wordlists() # the default bip39 word lists
super(WalletElectrum2, cls)._load_wordlists("electrum2") # any additional Electrum2 word lists
assert all(len(w) >= 1411 for w in cls._language_words.values()), \
"Electrum2 wordlists are at least 1411 words long" # because we assume a max mnemonic length of 13
def __init__(self, path = None, loading = False):
# Just calls WalletBIP39.__init__() with a hardcoded path
if path: raise ValueError("can't specify a BIP32 path with Electrum 2.x wallets")
super(WalletElectrum2, self).__init__("m/0/", loading)
def passwords_per_seconds(self, seconds):
# (experimentally determined; doesn't have to be perfect, just decent)
#
# number of priv-to-pubkey derivations per checksum-validated seed; they slow things down:
derivation_count = 0 if self._chaincode else self._addrs_to_generate
#
est = 25000.0 * (3.0 / max(derivation_count, 6.0) if derivation_count else 1.0)
return max(int(round(est * seconds)), 1)
@staticmethod
def is_wallet_file(wallet_file):
wallet_file.seek(0)
data = wallet_file.read(20).split()
return len(data) >= 2 and data[0] == '{' and data[1] == '"accounts":'
# Load an Electrum2 wallet file (the part of it we need, just the master public key)
@classmethod
def load_from_filename(cls, wallet_filename):
import json
with open(wallet_filename) as wallet_file:
wallet = json.load(wallet_file)
wallet_type = wallet.get("wallet_type")
if not wallet_type: raise ValueError("Electrum2 wallet_type not found")
if wallet_type == "old": # if it's a converted Electrum1 wallet, return a WalletElectrum1 object
return WalletElectrum1._load_from_dict(wallet)
seed_version = wallet.get("seed_version")
if seed_version is None: raise ValueError("Unrecognized wallet format (Electrum2 seed_version not found)")
if seed_version != 11: raise NotImplementedError("Unsupported Electrum2 seed version " + seed_version)
if not wallet.get("use_encryption"): raise ValueError("Electrum2 wallet is not encrypted")
if wallet.get("wallet_type") != "standard":
raise NotImplementedError("Unsupported Electrum2 wallet type: " + wallet.get("wallet_type"))
mpks = wallet.get("master_public_keys", ())
if len(mpks) == 0: raise ValueError("No master public keys found in Electrum2 wallet")
if len(mpks) > 1: raise ValueError("Multiple master public keys found in Electrum2 wallet")
return cls.create_from_params(mpks.values()[0])
# Converts a mnemonic word from a Python unicode (as produced by load_wordlist())
# into a bytestring (of type str) in the method as Electrum 2.x
@staticmethod
def _unicode_to_bytes(word):