electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 4ef3eda8dae966f5b7695cd1a68a3f924a66793c
parent 40bf049c82f9c6850fa0124ed5524b5e42d36406
Author: SomberNight <somber.night@protonmail.com>
Date:   Wed, 20 Feb 2019 21:19:03 +0100

util: mv create_URI to create_bip21_uri, and small clean-up

Diffstat:
Melectrum/gui/kivy/uix/screens.py | 4++--
Melectrum/gui/qt/main_window.py | 14++++++++------
Melectrum/util.py | 16++++++++++++----
3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/electrum/gui/kivy/uix/screens.py b/electrum/gui/kivy/uix/screens.py @@ -363,13 +363,13 @@ class ReceiveScreen(CScreen): Clock.schedule_once(lambda dt: self.update_qr()) def get_URI(self): - from electrum.util import create_URI + from electrum.util import create_bip21_uri amount = self.screen.amount if amount: a, u = self.screen.amount.split() assert u == self.app.base_unit amount = Decimal(a) * pow(10, self.app.decimal_point()) - return create_URI(self.screen.address, amount, self.screen.message) + return create_bip21_uri(self.screen.address, amount, self.screen.message) @profiler def update_qr(self): diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py @@ -965,16 +965,18 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): req = self.wallet.receive_requests[addr] message = self.wallet.labels.get(addr, '') amount = req['amount'] - URI = util.create_URI(addr, amount, message) + extra_query_params = {} if req.get('time'): - URI += "&time=%d"%req.get('time') + extra_query_params['time'] = str(int(req.get('time'))) if req.get('exp'): - URI += "&exp=%d"%req.get('exp') + extra_query_params['exp'] = str(int(req.get('exp'))) if req.get('name') and req.get('sig'): sig = bfh(req.get('sig')) sig = bitcoin.base_encode(sig, base=58) - URI += "&name=" + req['name'] + "&sig="+sig - return str(URI) + extra_query_params['name'] = req['name'] + extra_query_params['sig'] = sig + uri = util.create_bip21_uri(addr, amount, message, extra_query_params=extra_query_params) + return str(uri) def sign_payment_request(self, addr): @@ -1113,7 +1115,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): amount = self.receive_amount_e.get_amount() message = self.receive_message_e.text() self.save_request_button.setEnabled((amount is not None) or (message != "")) - uri = util.create_URI(addr, amount, message) + uri = util.create_bip21_uri(addr, amount, message) self.receive_qr.setData(uri) if self.qr_window and self.qr_window.isVisible(): self.qr_window.qrw.setData(uri) diff --git a/electrum/util.py b/electrum/util.py @@ -798,17 +798,25 @@ def parse_URI(uri: str, on_pr: Callable=None) -> dict: return out -def create_URI(addr, amount, message): +def create_bip21_uri(addr, amount_sat: Optional[int], message: Optional[str], + *, extra_query_params: Optional[dict] = None) -> str: from . import bitcoin if not bitcoin.is_address(addr): return "" + if extra_query_params is None: + extra_query_params = {} query = [] - if amount: - query.append('amount=%s'%format_satoshis_plain(amount)) + if amount_sat: + query.append('amount=%s'%format_satoshis_plain(amount_sat)) if message: query.append('message=%s'%urllib.parse.quote(message)) + for k, v in extra_query_params.items(): + if not isinstance(k, str) or k != urllib.parse.quote(k): + raise Exception(f"illegal key for URI: {repr(k)}") + v = urllib.parse.quote(v) + query.append(f"{k}={v}") p = urllib.parse.ParseResult(scheme='bitcoin', netloc='', path=addr, params='', query='&'.join(query), fragment='') - return urllib.parse.urlunparse(p) + return str(urllib.parse.urlunparse(p)) # Python bug (http://bugs.python.org/issue1927) causes raw_input