kesa 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kesa/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ """Kesa — batch Lightning payouts from your own wallet.
2
+
3
+ A CSV of recipients and amounts in; each row paid individually over Lightning
4
+ from the user's OWN wallet, to any address (custodial or self-custodial); an
5
+ annotated results CSV out. Kesa never receives funds: every payment goes straight
6
+ from the user's wallet. It does hold the wallet's credential (an API key or an NWC
7
+ connection string), which can spend.
8
+ """
9
+
10
+ # The one place the version is set (pyproject.toml reads it). Bump it for a
11
+ # release and tag that commit vX.Y.Z. The app shows it beside the stage.
12
+ __version__ = "0.1.0"
13
+
14
+ # The release stage the app shows top right ("Alpha · v0.1.0"). Alpha while it
15
+ # is a work in progress, public or not; change it here only.
16
+ __stage__ = "alpha"
@@ -0,0 +1,115 @@
1
+ """Payment backends.
2
+
3
+ The core talks to exactly one interface — PaymentBackend — so new rails
4
+ (phoenixd, LND/CLN) slot in without touching the planner or runner. Blink and
5
+ NWC (Nostr Wallet Connect) are built.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import urllib.parse
11
+ from typing import TYPE_CHECKING
12
+
13
+ from .base import PaymentBackend
14
+
15
+ if TYPE_CHECKING:
16
+ from ..config import Config
17
+
18
+ # Registry of available backends by name. Add new rails here.
19
+ _BACKENDS = {
20
+ "blink": "kesa.backends.blink:BlinkBackend",
21
+ "nwc": "kesa.backends.nwc:NwcBackend",
22
+ }
23
+
24
+ # How each wallet is named in the app's words.
25
+ BACKEND_TITLES = {"blink": "Blink", "nwc": "NWC"}
26
+
27
+
28
+ # The only hosts the Blink backend may talk to, so a changed ``api_url`` (in
29
+ # kesa.toml, the environment or a form) can never send the API key, or any
30
+ # request, to another server. Extend this deliberately when a backend is added.
31
+ #
32
+ # The NWC backend has no such list: it connects ONLY to the relay named in the
33
+ # connection string, which is read from the encrypted key file and nowhere else
34
+ # (never kesa.toml, a form field or the environment), over wss:// with the
35
+ # same TLS rules (nostr.relay_url_ok, relay.RelayConnection). A private or LAN
36
+ # relay is allowed when the user's own connection string names one: it is the
37
+ # wallet's choice, and everything sent over it is signed and end-to-end
38
+ # encrypted for the wallet.
39
+ ALLOWED_API_HOSTS = {"api.blink.sv"}
40
+
41
+
42
+ def api_url_ok(url: str) -> bool:
43
+ """True if ``url`` is https and points to an allowed backend host.
44
+
45
+ Whitespace or control characters anywhere fail: the URL parser drops some
46
+ of them, so the checked URL could differ from the one actually used."""
47
+ url = url or ""
48
+ if any(c.isspace() or ord(c) < 32 or ord(c) == 127 for c in url):
49
+ return False
50
+ try:
51
+ parsed = urllib.parse.urlparse(url)
52
+ except ValueError:
53
+ return False
54
+ return parsed.scheme == "https" and parsed.hostname in ALLOWED_API_HOSTS
55
+
56
+
57
+ def available_backends():
58
+ return sorted(_BACKENDS)
59
+
60
+
61
+ def wallet_label(backend) -> str:
62
+ """How messages name a backend's wallet ("Blink", "your wallet"); else its
63
+ name, capitalised."""
64
+ return (getattr(backend, "label", "")
65
+ or str(getattr(backend, "name", "") or "wallet").capitalize())
66
+
67
+
68
+ def wallet_ref(backend) -> str:
69
+ """Which wallet ``backend`` sends from, as recorded in the results file's
70
+ ``wallet`` column: the backend name and a short hash of the wallet's
71
+ public identity (Blink: the wallet id; NWC: the wallet service's key). Never
72
+ the id or key itself. "" when it can't be told."""
73
+ import hashlib
74
+ name = str(getattr(backend, "name", "") or "")
75
+ ident = str(getattr(backend, "wallet_identity", "") or "")
76
+ if not name or not ident:
77
+ return ""
78
+ return f"{name}:{hashlib.sha256(ident.encode('utf-8')).hexdigest()[:12]}"
79
+
80
+
81
+ def capitalised(text: str) -> str:
82
+ return text[:1].upper() + text[1:]
83
+
84
+
85
+ def build_backend(config: "Config"):
86
+ """Instantiate the configured backend. Imported lazily so a missing optional
87
+ dependency for one rail never blocks the others."""
88
+ name = (config.backend or "blink").lower()
89
+ if name not in _BACKENDS:
90
+ raise ValueError(
91
+ f"Unknown backend {name!r}. Available: {', '.join(available_backends())}"
92
+ )
93
+ if name == "blink" and not api_url_ok(config.api_url):
94
+ from ..config import ConfigError
95
+ raise ConfigError(
96
+ f"The API URL must be https and point to {', '.join(sorted(ALLOWED_API_HOSTS))}. "
97
+ "Check api_url in kesa.toml (or BLINK_API_URL).")
98
+ module_path, _, cls_name = _BACKENDS[name].partition(":")
99
+ import importlib
100
+
101
+ module = importlib.import_module(module_path)
102
+ cls = getattr(module, cls_name)
103
+ if name == "nwc":
104
+ from .nostr import NostrError
105
+ try:
106
+ return cls(config)
107
+ except NostrError as exc: # a plain, secret-free reason
108
+ from ..config import ConfigError
109
+ raise ConfigError(str(exc)) from None
110
+ return cls(config)
111
+
112
+
113
+ __all__ = ["PaymentBackend", "build_backend", "available_backends", "api_url_ok",
114
+ "ALLOWED_API_HOSTS", "BACKEND_TITLES", "wallet_label", "wallet_ref",
115
+ "capitalised"]