-
Notifications
You must be signed in to change notification settings - Fork 4
Add async pubkey cache and key fetch method #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4d4cd45
c83ebbb
0322568
bbe00ba
6741a38
021efdb
9f329e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from uuid import uuid4 | ||
|
|
||
| import requests | ||
| from aiohttp import ClientSession | ||
| from coincurve.ecdsa import cdata_to_der, der_to_cdata, signature_normalize | ||
| from coincurve.keys import PublicKey | ||
| from cryptography.exceptions import InvalidSignature | ||
|
|
@@ -52,7 +53,7 @@ | |
| ) | ||
| from uma.protocol.post_tx_callback import PostTransactionCallback, UtxoWithAmount | ||
| from uma.protocol.pubkey_response import PubkeyResponse | ||
| from uma.public_key_cache import IPublicKeyCache | ||
| from uma.public_key_cache import IPublicKeyCache, IAsyncPublicKeyCache | ||
| from uma.signing_utils import sign_payload | ||
| from uma.type_utils import none_throws | ||
| from uma.uma_invoice_creator import IUmaInvoiceCreator | ||
|
|
@@ -86,6 +87,27 @@ def fetch_public_key_for_vasp( | |
| return public_key | ||
|
|
||
|
|
||
| async def fetch_public_key_for_vasp_async( | ||
| vasp_domain: str, cache: IAsyncPublicKeyCache | ||
| ) -> PubkeyResponse: | ||
| public_key = await cache.fetch_public_key_for_vasp(vasp_domain) | ||
| if public_key: | ||
| return public_key | ||
|
|
||
| scheme = "http://" if is_domain_local(vasp_domain) else "https://" | ||
| url = scheme + vasp_domain + "/.well-known/lnurlpubkey" | ||
| try: | ||
| response_text = await _run_http_get_async(url) | ||
| except Exception as ex: | ||
| raise InvalidRequestException( | ||
| f"Unable to fetch pubkey from {vasp_domain}. Make sure the vasp domain is correct." | ||
| ) from ex | ||
|
|
||
| public_key = PubkeyResponse.from_json(response_text) | ||
| await cache.add_public_key_for_vasp(vasp_domain, public_key) | ||
| return public_key | ||
|
|
||
|
|
||
| def create_pubkey_response( | ||
| signing_cert_chain: str, | ||
| encryption_cert_chain: str, | ||
|
|
@@ -124,6 +146,13 @@ def _run_http_get(url: str) -> str: | |
| return response.text | ||
|
|
||
|
|
||
| async def _run_http_get_async(url: str) -> str: | ||
| async with ClientSession() as session: | ||
| async with session.get(url) as response: # pyre-ignore [16] | ||
|
||
| response.raise_for_status() | ||
| return await response.text() | ||
|
|
||
|
|
||
| def generate_nonce() -> str: | ||
| return str(random.randint(0, 0xFFFFFFFF)) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we care about using the
gen_*convention here like we do internally? I don't have a strong preference, but was curious if anyone else does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yunyuyunyu do you have any thoughts?
from quick googling it doesn't seem like a common convention so I almost prefer not to, but also not a strong preference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. I actually don't see many external libraries using prefix gen for their async functions. Hmm, @vdurmont do you have a preference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly for external stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks all -- going to leave as is then!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I'd lean towards keeping our consistency and use
gen_. I acknowledge it's not widely used in the outside world, but it doesn't hurt at all.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update!