Open ghost opened 1 year ago
I have an app where I generate my own links, so I dont need validation, and uses this regex pattern = re.compile(r"^monero:(?P<address>[A-Za-z0-9]+)\?tx_amount=(?P<amount>[0-9]+\.[0-9]+)$")
you get the address with address = match.group("address")
.
I think you could "validate" it if it matches https://github.com/monero-ecosystem/monero-python/blob/3f3a4cdc2a92a088172266d1916fbf2f59c23f2d/monero/address.py#L11-L13
of just adapt the 1st regex using the second one inside.
Thank you @euri10, your reply has been very helpful and I feel my question is answered.
I wonder if a function to check whether a payment link is valid and/or to extract the different values like the address, description and amount have a place in monero-python? We could add this to the code?
Using this wiki page from the monero project I wrote the function below. It returns None if the uri is invalid and returns a dictionary with all the parameters if the uri is valid.
import re
from urllib.parse import *
def get_uri_parameters(uri):
''' Takes uri as a String.
Returns a dictionary with the parameters and their values if uri is a valid
TX uri Scheme, if not, then returns None.'''
parameters = ["tx_amount", "tx_description", "recipient_name", "tx_payment_id"]
parsed_uri = urlparse(uri)
result = {}
queries = re.split("=|&", parsed_uri.query)
# first we check if this is a valid TX Scheme uri
if not (re.search(r"^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}$",
parsed_uri.path)):
return None
if len(queries) % 2 == 1 and parsed_uri.scheme == "monero":
# if the length of queries is uneven, then something odd is going on!
return None
# make sure that no parameters other than those allowed are present
for i in range(0, len(queries), 2):
if queries[i] not in parameters:
return None
# now we put the data in `result`
result["address"] = parsed_uri.path
for parameter in parameters:
if parameter in queries:
result[parameter] = queries[queries.index(parameter)+1]
return result
edit: Parsing a payment uri is currently not available in monero-python. See both @euri10 's and my comments below for useful examples if you want to write your own function. I don't feel comfortable enough (yet) with monero-python to create a pull request that fits with the rest of the code base. It would be great to be able to get monero-python type objects straight out of the uri in the future (if that fits what monero-python is built for).
Hi all,
First of all. Thank you for the great project. The documentation is very good.
I have a question:
I currently scan a QR code and am looking to make a payment to the address encoded in the QR. I want to check whether the QR is valid and if so, get the payment address out of the encoded link (plus possibly the amount, name and description).
My question: do I need to write my own regex to parse the link or is there a function for this?
Example we might have a link of the following format:
monero:<address>?tx_amount=<tx_amount>&recipient_name=<recipient_name>&tx_description=<tx_description>