-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_account_pdf.py
247 lines (227 loc) · 8.23 KB
/
create_account_pdf.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
#!/bin/usr/python3
"""Create Tezos implicit account data.
After creating the Tezos implicit account data, the created data is written
to a PDF file. This is similar to creating a simple Tezos paper wallet.
The script shows how PDF files can be created. Besides text, graphics are
included. Parts of the text is colored.
Prerequisite is the installation of FPDF2. There are significant differences
between FPDF and FPDF2.
"""
# pylint: disable=useless-return
# pylint: disable=invalid-name
# pylint: disable=global-statement
# pylint: disable=unused-argument
# pylint: disable=deprecated-method
# Import the standard Python modules.
import os
import sys
import base64
import subprocess
from datetime import datetime
# Import the third party Python modules.
import qrcode
from fpdf import FPDF
from pytezos.crypto.key import Key
from mnemonic import Mnemonic
# Set image data.
img_logo = """
iVBORw0KGgoAAAANSUhEUgAAAC8AAABACAMAAAC9W94RAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6
JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAB71BMVEUAAAApevUsffgsffctffckgP8v
e/YsffcwgPcrffcrffosfPYtffcsfvYtfvYsffcsffYtffctffgrfvcsffcsffcsfvcsffcAgP8tfvcs
ffcrfvcsffcsffcndv8sffcsfvcsfvcsfPcsevQugPMsfPcrgPktfvgsffYtffYzgP8rfPcsffcsffcr
ffcsfvZAgP8rgPQsffctffcAAP8rffcsffctffcsfPgqffUtgPgre/YtfPgtffctgPkrgPQsfvctfPgq
ffkqffYsfPgrffcufvdVVf8sffctffYrfPYrgP8tePAsffcuffcsffYsffYsfPcsffcrffcsffcrffcr
ffYsffYsfvcsffcsffgrfvctffgsffgsfvcufPgtffctffgsfvYoefItffUsfPYsffcsffcsffcrffcu
gPYsfvcsffcrffgtfPYzZv8se/UsfvgtfPcrfvYsffgsffcsfPcue/YsgPkngPUrgPcugPkrffcrffcs
ffcrffYsffcsffctfPgsffcsfPckbf8sffcsffcsffgrffYsffcsfvgsffcufPcsfPgsffcrfvgxefMt
fPcsfPctffcsfvctfvcsffgsffcwgO8tffcsfPcpfPgre/csffcsffcrffcrfPgtfvksfff///9fPLdc
AAAAo3RSTlMAGe7mfg4b7CCHL9HIUVXadGBmQfL8XcACYd6C/sQNusWAQBcWfypJVjkKQt36tlcEGOda
Aby+1G0xIjZn5Sgww0orN0zCQwOXsVQGEZs9spV5uN/z2VjQmOqLX2ytlie3iZATM3Xt+P2ZHMf5ancF
NEugWaq5qDguGh4sXp/rdp2nRL/TB8bXrLCjRfshb6GIFaaieGNbbukQg6QlPOH3ZGtPgV0MCgAAAAFi
S0dEpFm+erkAAAAHdElNRQfkAhcOLjI/QbrUAAACg0lEQVRIx+2W6VcTMRTFBwu4FJBWKyNqsQouKAW0
WhALxSIqAm4gIiqibIoKLrgjyqKI+wauuOQftcnLlEnzMqTn+NH3oWd67y8zd5I3kzEMqLRFrFzpGYZW
ZRJei5foDVhqDVhmpDZAlzfcKfJZ//l/wGfnWLUc+NwctDxe7wrKryTa5VuVGs8GpMKTPNNY7bUqH7Q1
XqnS14K1zq81P2YBOOv9evMZQHElv8GH4ip+owvHFTwPL+MKPqDCcR7CYzjKF7qUOMZDeBzH+IADjvAs
vAqXeRZeiUs8C6/GJb6IduSmzVu2Fmvx22j47VQpCZaWLciX77A/KPk7Q868uSvp0dpd7sjT8OGKyj1V
e4PQziRS7cDX+Ei0lkfYl8cHZIp8FOQYPa7bXz9vHDjII5kCfwjUBnp8mP40NjXzlTgCVqnAHwXxWOK0
x8kJa/dsYVarMK0ngW/jsc1ThLRb3mnwOux8I5yEnGH//Gfjh+csrxOs80KgLv7Wu1B5sbunPX7Um5bw
wszqE3bz4jZxifqz571LINUJF7g8YMeveGzWVdCuiUswWBFO4ANDduc6iDekLqq+2dV0a/h2UZUo3wE+
qLtZ3eUX1eVj/I2Omve67ydLtdgCQD2gs+SOieJD4EeQL6dHzGkR230U+B6Z56tMHgvqExDHZH6c8xN2
MRSBi07K/FPOP0Omfwq53SnOP7eL06C9QPhcIketecmkcAjhX/Wzzn1tkzLcyruN1xtqvbUJ76LKyWcL
0EvN91Z7DnoifP+dVPTKGKRt/fDx08zsyGd+PwVflM01UyJ9afR9LXPoxqGsJPrb9wX698f0nAX/HJ4t
1Ppkr2/49bvjT6cQ5C867T6qHbazEwAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMC0wMi0yM1QxNDo0Njo1
MCswMDowMC1NwncAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjAtMDItMjNUMTQ6NDY6NTArMDA6MDBcEHrL
AAAAAElFTkSuQmCC"""
# Create png image.
fn_logo = "tezos_logo.png"
img_bytestr = img_logo.encode('utf-8')
png_recovered = base64.decodestring(img_bytestr)
with open(fn_logo, 'wb') as fo:
fo.write(png_recovered)
# Set the mnemonic variables.
STRENGTH = 128
LANGUAGE = 'english'
# Other constants.
EMAIL = ''
PASSPHRASE = ''
# ================
# Function reset()
# ================
def reset():
"""Reset terminal window."""
# Write ANSI code to terminal.
sys.stdout.write("\033c")
# Write buffer to screen immediately.
sys.stdout.flush()
# =======================
# Function account_data()
# =======================
def account_data():
"""Function account_data()."""
# Get the 12 word mnemonic.
mnemonic = Mnemonic(LANGUAGE).generate(STRENGTH)
# Create a secret key object.
sk = Key.from_mnemonic(mnemonic=mnemonic, email=EMAIL, passphrase=PASSPHRASE)
# Determine the secret key.
edsk = sk.secret_key()
# Determine the full secret key.
edsk_full = sk.secret_key(ed25519_seed=False)
# Get the public key from the secret key object.
pk = sk.public_key()
# Get the public key hash from the secret key object.
pkh = sk.public_key_hash()
# Return account data.
return mnemonic, edsk, edsk_full, pk, pkh
# =============================
# Function print_account_data()
# =============================
def print_account_data(mnemonic, edsk, edsk_full, pk, pkh):
"""Function print_account_data()."""
# Print the 12 word mnemonic to screen.
print("{1}{0}{2}{0}".format("\n", "12 word mnemonic:", mnemonic))
# Print edsk to screen.
print("{1}{0}{2}{0}".format("\n", "Secret key (private key):", edsk))
# Print full edsk to screen.
print("{1}{0}{2}{0}".format("\n", "Full secret key (full private key):", edsk_full))
# Print pk to screen.
print("{1}{0}{2}{0}".format("\n", "Public key (is not a public key):", pk))
# Print pkh to screen.
print("{1}{0}{2}".format("\n", "Public key hash (tezos address is public key):", pkh))
return None
# ====================
# Function create_qr()
# ====================
def print_qr(data, fn):
"""Function create_qr()."""
img = qrcode.make(data)
img.save(fn)
PDF.image(fn, x=None, y=None, w=38, h=38, type='', link='')
os.remove(fn)
return None
# ======================
# Function print_sline()
# ======================
def print_sline(w, h, text, size):
"""Print single line."""
PDF.set_font("Helvetica", size=size)
PDF.cell(w, h, txt=text, align='L')
PDF.ln(10)
return None
# ======================
# Function print_mline()
# ======================
def print_mline(w, h, text, size):
"""Print multiple line."""
PDF.set_font("Helvetica", size=size)
PDF.multi_cell(w, h, txt=text, align='L')
PDF.ln(0)
return None
# ======
# Header
# ======
def header():
"""Header."""
PDF.set_font(family="Helvetica", style="B", size=16)
PDF.set_text_color(r=44, g=125, b=247)
PDF.image("tezos_logo.png", x=10, y=12, w=5)
PDF.cell(6)
PDF.set_font(family="Helvetica", style="", size=16)
PDF.cell(20, 10, txt="Tezos", align="L")
PDF.cell(120)
PDF.set_font(family="Helvetica", style="B", size=16)
PDF.set_text_color(r=0, g=0, b=0)
PDF.cell(30, 10, txt="Paper Wallet", align="R")
PDF.ln(10)
return None
# ======
# Footer
# ======
def footer():
"""Footer."""
PDF.set_font("Helvetica", size=10)
presentime = datetime.now()
dt = presentime.strftime('%d.%m.%Y %H:%M')
PDF.cell(80)
PDF.cell(200, 10, txt=dt, align="L")
return None
# ====================
# Function write_pdf()
# ====================
def write_pdf(mnemonic, edsk, edsk_full, pk, pkh):
"""Function write_pdf()."""
global EMAIL, PASSPHRASE
# Create a new PDF page.
PDF.add_page()
# Print header.
header()
# Write to PDF.
print_mline(200, 10, "Mnemonic:\n" + mnemonic, 12)
if EMAIL == '':
EMAIL = "None"
if PASSPHRASE == '':
PASSPHRASE = "None"
print_sline(200, 10, "Email: " + EMAIL + " " + "Passphrase: " + PASSPHRASE, 12)
# Create QR code.
fn = "mnemonic.png"
print_qr(mnemonic, fn)
# Write to PDF.
print_mline(200, 10, "Public key:\n" + pkh, 12)
# Create QR code.
fn = "pkh.png"
print_qr(pkh, fn)
# Write to PDF.
print_mline(200, 10, "Private Key (short):\n" + edsk, 12)
# Create QR code.
fn = "edsk.png"
print_qr(edsk, fn)
# Write to PDF.
print_mline(200, 10, "Private key (long):\n" + edsk_full, 8.5)
# Create QR code.
fn = "edsk_full.png"
print_qr(edsk_full, fn)
# Print footer.
footer()
# Write everything to PDF.
PDF.output(pkh + ".pdf")
# Run evince to show PDF.
subprocess.call(["evince", pkh + ".pdf", "-s"])
#subprocess.call(["evince", pkh + ".pdf" ])
# #############
# Main function
# #############
def main():
"""Main function."""
# Get account data.
mnemonic, edsk, edsk_full, pk, pkh = account_data()
# Print account data.
print_account_data(mnemonic, edsk, edsk_full, pk, pkh)
# Write PDF.
write_pdf(mnemonic, edsk, edsk_full, pk, pkh)
# Execute script as program or as module.
if __name__ == "__main__":
# Reset screen.
reset()
# Create an instance of FPDF.
PDF = FPDF(orientation='P', unit='mm', format='A4')
# Call main function.
main()