1 from electrum.util import print_error
6 print_error("Install zbar package to enable QR scans")
9 from urlparse import urlparse, parse_qs
17 gui.wallet.set_hook('create_send_tab', create_send_tab)
19 gui.wallet.unset_hook('create_send_tab', create_send_tab)
22 return 'QR scans', "QR Scans"
36 proc = zbar.Processor()
38 except zbar.SystemError:
39 # Cannot open video device
45 proc = zbar.Processor()
53 # User closed the preview window
56 for r in proc.results:
57 if str(r.type) != 'QRCODE':
60 return parse_uri(r.data)
64 # It's just an address (not BIP21)
65 return {'address': uri}
68 # Workaround for urlparse, it don't handle bitcoin: URI properly
69 uri = uri.replace(':', '://')
72 result = {'address': uri.netloc}
74 if uri.path.startswith('?'):
75 params = parse_qs(uri.path[1:])
77 params = parse_qs(uri.path)
79 for k,v in params.items():
80 if k in ('amount', 'label', 'message'):
87 def fill_from_qr(self):
88 qrcode = qrscanner.scan_qr()
89 if 'address' in qrcode:
90 self.payto_e.setText(qrcode['address'])
91 if 'amount' in qrcode:
92 self.amount_e.setText(str(qrcode['amount']))
94 self.message_e.setText(qrcode['label'])
95 if 'message' in qrcode:
96 self.message_e.setText("%s (%s)" % (self.message_e.text(), qrcode['message']))
99 def create_send_tab(gui, grid):
100 if qrscanner.is_available():
101 b = QPushButton(_("Scan QR code"))
102 b.clicked.connect(lambda: fill_from_qr(gui))
103 grid.addWidget(b, 1, 5)
107 if __name__ == '__main__':
110 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
111 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
113 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
114 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
116 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
117 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
119 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
120 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
122 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
123 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})