-
Notifications
You must be signed in to change notification settings - Fork 8
/
cardprint.py
201 lines (158 loc) · 5.86 KB
/
cardprint.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
import argparse
from bitmerchant.wallet import Wallet
import cStringIO
import cups
import ethereum.keys
from mnemonic import Mnemonic
import os
from PIL import Image
import pystache
from reportlab.pdfgen import canvas
from reportlab.lib import units
from reportlab.lib.utils import ImageReader
from web3.main import to_checksum_address
import qrencode
import yaml
CARD_WIDTH = 54 * units.mm
CARD_HEIGHT = 85 * units.mm
def _addressFromWallet(wallet):
return to_checksum_address(ethereum.keys.sha3(wallet.public_key.get_key().decode('hex')[1:])[12:].encode('hex'))
class BIPWallet(object):
@classmethod
def from_seed(cls, seed):
return cls(Wallet.from_master_secret(seed))
def __init__(self, wallet, derived=False):
self.wallet = wallet
self.derived = derived
@property
def derive(self):
return BIPWallet(self.wallet, True)
def address(self):
if self.derived:
def deriver(path):
return _addressFromWallet(self.wallet.get_child_for_path(path))
return deriver
else:
return _addressFromWallet(self.wallet)
def pubkey(self):
if self.derived:
def deriver(path):
return self.wallet.get_child_for_path(path).public_key.get_key()
return deriver
else:
return self.wallet.public_key.get_key()
def privkey(self):
if self.derived:
def deriver(path):
return self.wallet.get_child_for_path(path).private_key.get_key()
return deriver
else:
return self.wallet.private_key.get_key()
mnemonic = Mnemonic('english')
parser = argparse.ArgumentParser(description='Generate and print ECDSA keypairs.')
parser.add_argument('--printer', type=str, help='Printer name', default='EVOLIS_Primacy')
parser.add_argument('--test', type=str, help='Run in test mode, outputting to the named file')
parser.add_argument('--batchsize', type=int, help='Batch size for print jobs', default=None)
parser.add_argument('template', metavar='FILENAME', type=str, help='Template file to use')
parser.add_argument('count', metavar='COUNT', type=int, help='Number of cards to print')
class CardPrinter(object):
FORMATTERS = {}
@classmethod
def formatter(cls, fun):
cls.FORMATTERS[fun.__name__] = fun
return fun
def __init__(self, format):
self.format = format
def _makeKeypair(self):
pdata = os.urandom(16)
pdata = chr(ord(pdata[0]) & 0x7F) + pdata[1:]
words = mnemonic.to_mnemonic(pdata)
seed = mnemonic.to_seed(words)
privkey = seed[:32]
address = ethereum.keys.privtoaddr(privkey).encode('hex')
return {
'keyphrase': words,
'wallet': BIPWallet.from_seed(seed),
}
def generate(self, count):
data = cStringIO.StringIO()
c = canvas.Canvas(data, pagesize=(CARD_WIDTH, CARD_HEIGHT))
addresses = []
for i in range(count):
c.rotate(90)
context = self._makeKeypair()
context['address'] = pystache.render(self.format['address'], context)
addresses.append(context['address'])
for element in self.format['card']:
self.FORMATTERS[element['type']](c, element, context)
c.showPage()
c.save()
return addresses, data.getvalue()
@CardPrinter.formatter
def QR(c, element, context):
eclevel = {
'L': qrencode.QR_ECLEVEL_L,
'M': qrencode.QR_ECLEVEL_M,
'Q': qrencode.QR_ECLEVEL_Q,
'H': qrencode.QR_ECLEVEL_H
}[element.get('eclevel', 'L')]
version, pixels, qr = qrencode.encode(
pystache.render(element['text'], context),
0,
eclevel,
qrencode.QR_MODE_8,
element.get('caseSensitive', True))
qr = qr.resize((pixels * 64, pixels * 64), Image.NEAREST)
size = element['size'] * units.mm
c.drawImage(
ImageReader(qr),
float(element['x']) * units.mm,
-float(element['y']) * units.mm - size,
size,
size)
def drawText(c, element, x, y, data):
text = c.beginText()
text.setTextOrigin(x, y)
text.setFont(element.get('font', 'Courier'), int(element.get('size', 12)))
text.setLeading(float(element.get('leading', 12)))
text.textLine(data)
c.drawText(text)
@CardPrinter.formatter
def text(c, element, context):
drawText(
c,
element,
float(element['x']) * units.mm,
-float(element['y']) * units.mm,
pystache.render(element['text'], context))
@CardPrinter.formatter
def textArray(c, element, context):
data = pystache.render(element['text'], context).split(element['split'])
cols = int(element['columns'])
startX = float(element['x']) * units.mm
startY = -float(element['y']) * units.mm
colspacing = float(element['columnSpacing']) * units.mm
rowspacing = -float(element['rowSpacing']) * units.mm
for i in range(len(data)):
drawText(c, element, startX + colspacing * (i % cols), startY + rowspacing * (i / cols), data[i])
def printCards(printerName, cards):
conn = cups.Connection()
job = conn.createJob(printerName, 'ether.cards', {'GRibbonType': 'RM_KBLACK'})
conn.startDocument(printerName, job, 'card.pdf', 'application/pdf', 1)
conn.writeRequestData(cards, len(cards))
conn.finishDocument(printerName)
def main(args):
formatter = yaml.load(open(args.template))
generator = CardPrinter(formatter)
batchsize = args.batchsize or args.count
for i in range(0, args.count, batchsize):
count = min(batchsize, args.count - i)
addresses, cards = generator.generate(count)
print '\n'.join(addresses)
if args.test:
open(args.test, 'w').write(cards)
else:
printCards(args.printer, cards)
if __name__ == '__main__':
args = parser.parse_args()
main(args)