This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gen_cold_wallet
executable file
·165 lines (150 loc) · 4.76 KB
/
gen_cold_wallet
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
#!/usr/bin/python
# https://github.com/sowbug/cold-wallet-generator
#
# Generates a TeX or HTML file representing a nice-looking, scannable paper
# Bitcoin wallet suitable for cold storage.
#
# Accepts a series of addresses on stdin, where each line contains an
# Electrum-format <address:private_key> Bitcoin address.
#
# To generate a document with a lot of addresses, try something like this:
#
# hexdump -v -e '/1 "%02X"' -n 32 /dev/urandom | ./format_key "%a:%w"
import argparse
import datetime
from jinja2 import Template
import sys
TEX_TEMPLATE = r"""\documentclass[landscape, twocolumn]{book}
\setlength\columnsep{\dimexpr 1in/2\relax}
\setlength{\columnseprule}{0.4pt}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf}
\usepackage[margin=0.6in]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\fancyfoot[RE,LO]{Generated {{ today }}}
\begin{document}
\begin{enumerate}
\setcounter{enumi}{0}
{% for key in keys %}\item
\begin{pspicture}(1in,1in)
\psbarcode{%raw%}{{%endraw%}{{ key.address }}{%raw%}}{%endraw%}{eclevel=M width=0.8 height=0.8}{qrcode}
\end{pspicture}
{% if not exclude_private_keys %}\hfill
\begin{pspicture}(1in,1in)
\psbarcode{%raw%}{{%endraw%}{{ key.private_key }}{%raw%}}{%endraw%}{eclevel=H width=0.8 height=0.8}{qrcode}
\end{pspicture}
{% endif %}
{{ key.address }}
{% if not exclude_private_keys and not exclude_private_key_text %}
{\footnotesize {{ key.private_key }} }
{% endif %}{% if loop.index % 5 != 0 %}\hrule{% else %}\pagebreak
{% endif %}
{% endfor %}
\end{enumerate}
\end{document}"""
HTML_TEMPLATE = r"""<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "OCR A Extended", "OCR-A", Calibri, "Times New Roman", serif;
font-size:12px;
}
.wallet {
margin: auto; padding: 16px 8px; border-bottom: 1px dotted;
page-break-inside: avoid;
}
.private-key {
float: right;
}
.footer {
margin: auto;
padding-top: 0.5em;
}
</style>
</head>
<body>
{% for key in keys %}
<div class="wallet">
{{ loop.index }}. <img src="data:image/png,{{ key.address_data_url }}" />
{% if not exclude_private_keys %}
<span class="private-key">
<img src="data:image/png,{{ key.private_key_data_url }}" />
</span>
{% endif %}
<br />{{ key.address }}
{% if not exclude_private_keys and not exclude_private_key_text %}
<br />{{ key.private_key }}
{% endif %}
</div>
{% endfor %}
<div class="footer">Generated {{ today }}</div>
</body>
</html>"""
def make_data_url(data, important=False):
import StringIO
import urllib
import qrcode
box_size=3
if important:
error_correction=qrcode.constants.ERROR_CORRECT_H
else:
error_correction=qrcode.constants.ERROR_CORRECT_M
qr = qrcode.QRCode(version=1,
box_size=box_size,
error_correction=error_correction)
qr.add_data(data)
qr.make()
img = qr.make_image()
f = StringIO.StringIO()
img.save(f, "PNG")
return urllib.quote(f.getvalue())
def print_pages(args):
if args.filename:
f = open(args.filename)
lines = f.readlines()
f.close()
else:
lines = sys.stdin.readlines()
if args.html:
template = Template(HTML_TEMPLATE)
else:
template = Template(TEX_TEMPLATE)
keys = []
for line in lines:
key = {}
values = line.split(":")
address, private_key = values[0], values[1]
if args.exclude_addresses:
address = '[address hidden]'
if args.elide_addresses:
address = address[0] + "..." + address[-8:]
(key['address'], key['private_key']) = address.strip(), private_key.strip()
if args.html:
key['address_data_url'] = make_data_url(address, False)
key['private_key_data_url'] = make_data_url(private_key, True)
keys.append(key)
print template.render(keys=keys,
today=datetime.date.today(),
exclude_private_keys=args.exclude_private_keys,
exclude_private_key_text=args.exclude_private_key_text)
parser = argparse.ArgumentParser(description='Generates a TeX-format document ' +
'that nicely formats a Bitcoin paper wallet for offline storage.')
parser.add_argument("filename", nargs='?',
help="file of Electrum-format Bitcoin addresses to read. Otherwise reads from stdin.")
parser.add_argument("-x", "--exclude-private-keys", action="store_true",
help="excludes private keys from document")
parser.add_argument("--exclude-private-key-text", action="store_true",
help="excludes text representations of private keys from document")
parser.add_argument("--html", action="store_true",
help="generate HTML instead of TeX")
parser.add_argument("-a", "--exclude-addresses", action="store_true",
help="excludes addresses from document")
parser.add_argument("-e", "--elide-addresses", action="store_true",
help="includes only partial addresses")
args = parser.parse_args()
print_pages(args)