mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-29 16:31:29 +00:00
util: mv create_URI to create_bip21_uri, and small clean-up
This commit is contained in:
parent
40bf049c82
commit
4ef3eda8da
3 changed files with 22 additions and 12 deletions
|
@ -363,13 +363,13 @@ class ReceiveScreen(CScreen):
|
||||||
Clock.schedule_once(lambda dt: self.update_qr())
|
Clock.schedule_once(lambda dt: self.update_qr())
|
||||||
|
|
||||||
def get_URI(self):
|
def get_URI(self):
|
||||||
from electrum.util import create_URI
|
from electrum.util import create_bip21_uri
|
||||||
amount = self.screen.amount
|
amount = self.screen.amount
|
||||||
if amount:
|
if amount:
|
||||||
a, u = self.screen.amount.split()
|
a, u = self.screen.amount.split()
|
||||||
assert u == self.app.base_unit
|
assert u == self.app.base_unit
|
||||||
amount = Decimal(a) * pow(10, self.app.decimal_point())
|
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
|
@profiler
|
||||||
def update_qr(self):
|
def update_qr(self):
|
||||||
|
|
|
@ -965,16 +965,18 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
||||||
req = self.wallet.receive_requests[addr]
|
req = self.wallet.receive_requests[addr]
|
||||||
message = self.wallet.labels.get(addr, '')
|
message = self.wallet.labels.get(addr, '')
|
||||||
amount = req['amount']
|
amount = req['amount']
|
||||||
URI = util.create_URI(addr, amount, message)
|
extra_query_params = {}
|
||||||
if req.get('time'):
|
if req.get('time'):
|
||||||
URI += "&time=%d"%req.get('time')
|
extra_query_params['time'] = str(int(req.get('time')))
|
||||||
if req.get('exp'):
|
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'):
|
if req.get('name') and req.get('sig'):
|
||||||
sig = bfh(req.get('sig'))
|
sig = bfh(req.get('sig'))
|
||||||
sig = bitcoin.base_encode(sig, base=58)
|
sig = bitcoin.base_encode(sig, base=58)
|
||||||
URI += "&name=" + req['name'] + "&sig="+sig
|
extra_query_params['name'] = req['name']
|
||||||
return str(URI)
|
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):
|
def sign_payment_request(self, addr):
|
||||||
|
@ -1113,7 +1115,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
||||||
amount = self.receive_amount_e.get_amount()
|
amount = self.receive_amount_e.get_amount()
|
||||||
message = self.receive_message_e.text()
|
message = self.receive_message_e.text()
|
||||||
self.save_request_button.setEnabled((amount is not None) or (message != ""))
|
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)
|
self.receive_qr.setData(uri)
|
||||||
if self.qr_window and self.qr_window.isVisible():
|
if self.qr_window and self.qr_window.isVisible():
|
||||||
self.qr_window.qrw.setData(uri)
|
self.qr_window.qrw.setData(uri)
|
||||||
|
|
|
@ -798,17 +798,25 @@ def parse_URI(uri: str, on_pr: Callable=None) -> dict:
|
||||||
return out
|
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
|
from . import bitcoin
|
||||||
if not bitcoin.is_address(addr):
|
if not bitcoin.is_address(addr):
|
||||||
return ""
|
return ""
|
||||||
|
if extra_query_params is None:
|
||||||
|
extra_query_params = {}
|
||||||
query = []
|
query = []
|
||||||
if amount:
|
if amount_sat:
|
||||||
query.append('amount=%s'%format_satoshis_plain(amount))
|
query.append('amount=%s'%format_satoshis_plain(amount_sat))
|
||||||
if message:
|
if message:
|
||||||
query.append('message=%s'%urllib.parse.quote(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='')
|
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
|
# Python bug (http://bugs.python.org/issue1927) causes raw_input
|
||||||
|
|
Loading…
Add table
Reference in a new issue