-
Notifications
You must be signed in to change notification settings - Fork 10
/
generate_wallets.py
91 lines (72 loc) · 2.94 KB
/
generate_wallets.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
#!/usr/bin/env/python
# Generate bitcoin private keys on an airgapped machine
# See blog post before using
import csv
from datetime import datetime
import getpass
# Download pycoin (https://github.com/richardkiss/pycoin)
# and put this file in the root directory so that these imports work
from pycoin.wallet import Wallet
from pycoin.encoding import is_valid_wif
# Set the number of keys you want to generate here:
NUM_KEYS_TO_GENERATE = 100000
def check_entropy(entropy, num_chars, num_unique_chars):
"""
Check to be sure entropy:
- Is longer than num_chars, AND
- Has more unique characters than num_unique_chars
Returns True iff both conditions are met
"""
if len(entropy) <= num_chars:
return False
if len(set(entropy)) <= num_unique_chars:
return False
return True
def devrandom_entropy():
return open("/dev/random", "rb").read(64)
def generate_random_wallet():
entropy = bytearray(devrandom_entropy())
return Wallet.from_master_secret(bytes(entropy), is_test=False)
if __name__ == '__main__':
START_TIME = datetime.now()
FILE_NAME = '/media/wipeout/btc_keys_%s.csv' % START_TIME.strftime("%Y%m%d_%H%M")
HEADERS = ['born_at', 'wif', 'public_bitcoin_address']
all_wifs = set()
all_public_addresses = set()
# for feeding entropy to /dev/random
MSG = 'Please type at least 100 random characters, of which 25 of them must be unique. Feel free to type more: '
entropy = getpass.getpass(MSG)
assert check_entropy(entropy, 100, 25), 'Not enough entropy, please try again!'
print 'Starting wallet generation at %s...' % START_TIME
print 'Saving results to %s...' % FILE_NAME
with open(FILE_NAME, 'w') as f:
myWriter = csv.DictWriter(f, HEADERS)
# For some reason this doesn't work on tails' python installation:
# myWriter.writeheader()
# Hack to write header row anyway:
HEADER_DICT = {}
for header in HEADERS:
HEADER_DICT[header]=header
myWriter.writerow(HEADER_DICT)
for key_cnt in range(NUM_KEYS_TO_GENERATE):
wallet = generate_random_wallet()
public_address = wallet.bitcoin_address()
wif = wallet.wif()
wallet_dict = {
'born_at': datetime.now(),
'wif': wif,
'public_bitcoin_address': public_address,
}
myWriter.writerow(wallet_dict)
# weak safety checks
assert is_valid_wif(wif)
assert wif not in all_wifs
assert public_address not in all_public_addresses
# add wallet to set
all_wifs.add(wif)
all_public_addresses.add(public_address)
# print out progress
if key_cnt % 500 == 0:
print key_cnt, datetime.now()
END_TIME = datetime.now()
print 'Finished generating %s private keys at %s (%s run time)' % (key_cnt+1, END_TIME, END_TIME-START_TIME)