X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=plugins%2Fqrscanner.py;h=c42347c27cb5dfa3142c1cfd614f4eda57b11e2d;hb=b614a673eb1e05d95e626a658651151b6cf8cc7e;hp=a9f42dcd3a449e1d7da1f227b580e34e9e2c97b9;hpb=ca4e9261e0a5d05c0a9d10d6221e4ec840291a52;p=electrum-nvc.git diff --git a/plugins/qrscanner.py b/plugins/qrscanner.py index a9f42dc..c42347c 100644 --- a/plugins/qrscanner.py +++ b/plugins/qrscanner.py @@ -35,6 +35,8 @@ class Plugin(BasePlugin): try: proc = zbar.Processor() proc.init(video_device=self.video_device()) + except zbar.UnsupportedError: + return False except zbar.SystemError: # Cannot open video device pass @@ -45,20 +47,16 @@ class Plugin(BasePlugin): def init(self): self.win = self.gui.main_window self.win.raw_transaction_menu.addAction(_("&From QR code"), self.read_raw_qr) - b = QPushButton(_("Scan QR code")) - b.clicked.connect(self.fill_from_qr) - self.win.send_grid.addWidget(b, 1, 5) - self.win.send_grid.setColumnStretch(5, 0) - self.win.send_grid.setColumnStretch(6, 1) - - def init_transaction_dialog(self, dialog, buttons): - b = QPushButton(_("Show QR code")) - b.clicked.connect(lambda: self.show_raw_qr(dialog.tx)) - buttons.insertWidget(1,b) def is_available(self): return self._is_available + def scan_qr_hook(self, func): + data = self.scan_qr() + if type(data) != str: + return + func(data) + def scan_qr(self): proc = zbar.Processor() try: @@ -81,38 +79,24 @@ class Plugin(BasePlugin): continue return r.data - def show_raw_qr(self, tx): - try: - json_text = json.dumps(tx.as_dict()).replace(' ', '') - self.win.show_qrcode(json_text, 'Unsigned Transaction') - except Exception as e: - self.win.show_message(str(e)) - def read_raw_qr(self): qrcode = self.scan_qr() if not qrcode: return - tx = self.win.tx_from_text(qrcode) + data = qrcode + + # transactions are binary, but qrcode seems to return utf8... + z = data.decode('utf8') + s = '' + for b in z: + s += chr(ord(b)) + data = s.encode('hex') + tx = self.win.tx_from_text(data) if not tx: return self.win.show_transaction(tx) - - def fill_from_qr(self): - qrcode = parse_uri(self.scan_qr()) - if not qrcode: - return - - if 'address' in qrcode: - self.win.payto_e.setText(qrcode['address']) - if 'amount' in qrcode: - self.win.amount_e.setText(str(qrcode['amount'])) - if 'label' in qrcode: - self.win.message_e.setText(qrcode['label']) - if 'message' in qrcode: - self.win.message_e.setText("%s (%s)" % (self.win.message_e.text(), qrcode['message'])) - def video_device(self): device = self.config.get("video_device", "default") if device == 'default': @@ -155,7 +139,7 @@ class Plugin(BasePlugin): if self.config.get("video_device") == "default": self.video_device_edit.setText("") else: - self.video_device_edit.setText(self.config.get("video_device")) + self.video_device_edit.setText(self.config.get("video_device",'')) else: custom_device_label.setVisible(False) self.video_device_edit.setVisible(False) @@ -195,55 +179,3 @@ class Plugin(BasePlugin): return True else: return False - - - -def parse_uri(uri): - if not uri: - return {} - - if ':' not in uri: - # It's just an address (not BIP21) - return {'address': uri} - - if '//' not in uri: - # Workaround for urlparse, it don't handle bitcoin: URI properly - uri = uri.replace(':', '://') - - uri = urlparse(uri) - result = {'address': uri.netloc} - - if uri.query.startswith('?'): - params = parse_qs(uri.query[1:]) - else: - params = parse_qs(uri.query) - - for k,v in params.items(): - if k in ('amount', 'label', 'message'): - result[k] = v[0] - - return result - - - - - -if __name__ == '__main__': - # Run some tests - - assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') == - {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') == - {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - -