branta 3.2.4__tar.gz → 3.2.5__tar.gz

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.
Files changed (32) hide show
  1. branta-3.2.5/.github/workflows/integration.yml +27 -0
  2. {branta-3.2.4 → branta-3.2.5}/.github/workflows/tests.yml +0 -2
  3. {branta-3.2.4 → branta-3.2.5}/CHANGELOG.md +5 -0
  4. {branta-3.2.4 → branta-3.2.5}/CLAUDE.md +4 -4
  5. {branta-3.2.4 → branta-3.2.5}/PKG-INFO +4 -4
  6. {branta-3.2.4 → branta-3.2.5}/README.md +3 -3
  7. {branta-3.2.4 → branta-3.2.5}/branta/v2/parser.py +30 -17
  8. {branta-3.2.4 → branta-3.2.5}/branta/v2/service.py +49 -17
  9. {branta-3.2.4 → branta-3.2.5}/pyproject.toml +1 -1
  10. {branta-3.2.4 → branta-3.2.5}/tests/test_integration.py +10 -6
  11. {branta-3.2.4 → branta-3.2.5}/tests/test_qr_parser.py +28 -0
  12. {branta-3.2.4 → branta-3.2.5}/tests/test_service.py +117 -1
  13. {branta-3.2.4 → branta-3.2.5}/.gitignore +0 -0
  14. {branta-3.2.4 → branta-3.2.5}/LICENSE +0 -0
  15. {branta-3.2.4 → branta-3.2.5}/SECURITY.md +0 -0
  16. {branta-3.2.4 → branta-3.2.5}/branta/__init__.py +0 -0
  17. {branta-3.2.4 → branta-3.2.5}/branta/enums.py +0 -0
  18. {branta-3.2.4 → branta-3.2.5}/branta/exceptions.py +0 -0
  19. {branta-3.2.4 → branta-3.2.5}/branta/extensions.py +0 -0
  20. {branta-3.2.4 → branta-3.2.5}/branta/models.py +0 -0
  21. {branta-3.2.4 → branta-3.2.5}/branta/options.py +0 -0
  22. {branta-3.2.4 → branta-3.2.5}/branta/v2/__init__.py +0 -0
  23. {branta-3.2.4 → branta-3.2.5}/branta/v2/builder.py +0 -0
  24. {branta-3.2.4 → branta-3.2.5}/branta/v2/client.py +0 -0
  25. {branta-3.2.4 → branta-3.2.5}/branta/v2/encryption.py +0 -0
  26. {branta-3.2.4 → branta-3.2.5}/branta/v2/secret_generator.py +0 -0
  27. {branta-3.2.4 → branta-3.2.5}/branta/v2/serialization.py +0 -0
  28. {branta-3.2.4 → branta-3.2.5}/tests/__init__.py +0 -0
  29. {branta-3.2.4 → branta-3.2.5}/tests/test_builder.py +0 -0
  30. {branta-3.2.4 → branta-3.2.5}/tests/test_client.py +0 -0
  31. {branta-3.2.4 → branta-3.2.5}/tests/test_encryption.py +0 -0
  32. {branta-3.2.4 → branta-3.2.5}/tests/test_extensions.py +0 -0
@@ -0,0 +1,27 @@
1
+ name: Live integration
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ schedule:
6
+ - cron: "0 12 * * *"
7
+
8
+ jobs:
9
+ guardrail:
10
+ runs-on: ubuntu-latest
11
+ env:
12
+ BRANTA_RUN_INTEGRATION: "1"
13
+ BRANTA_INTEGRATION_METRICS_TOKEN: ${{ secrets.BRANTA_INTEGRATION_METRICS_TOKEN }}
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-python@v5
17
+ with:
18
+ python-version: "3.12"
19
+ - run: pip install -e ".[dev]"
20
+ - name: Require integration metrics token
21
+ run: |
22
+ if [ -z "$BRANTA_INTEGRATION_METRICS_TOKEN" ]; then
23
+ echo "Set the BRANTA_INTEGRATION_METRICS_TOKEN Actions secret to the same value as Guardrail."
24
+ exit 1
25
+ fi
26
+ - name: Example QR codes against staging and production
27
+ run: pytest tests/test_integration.py -v
@@ -29,8 +29,6 @@ jobs:
29
29
  coverage run -m pytest tests/ --ignore=tests/test_integration.py -v
30
30
  coverage report
31
31
  coverage xml
32
- env:
33
- BRANTA_SKIP_INTEGRATION: "1"
34
32
 
35
33
  - name: Upload coverage
36
34
  uses: actions/upload-artifact@v4
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [3.2.5] - 2026-10-29
8
+
9
+ ### Fixed
10
+ - `get_payments_by_qr_code` requires every payable instruction in a QR code to match a decrypted destination before it returns a payment. A swapped address with a rewritten `branta_secret`, an added `lightning`/`ark`/`sp` leg, or a conflicting `bc=`/`tb=` no longer comes back as verified. `bolt12`, `lno=`, `pay=`, and LNURL instructions return no payments (ported from `branta-js` PR #78)
11
+
7
12
  ## [3.2.4] - 2026-10-28
8
13
 
9
14
  ### Fixed
@@ -17,7 +17,7 @@ Feature-parity Python port of the Branta SDK. Mirrors `branta-js` and `branta-do
17
17
  - `branta/v2/encryption.py` — `AesEncryption` (static), `AesEncryptionService` (instance wrapper)
18
18
  - `branta/v2/secret_generator.py` — `GuidSecretGenerator`
19
19
  - `branta/v2/serialization.py` — snake_case JSON serialization helpers
20
- - `tests/` — mirrors `branta/`; unit tests use mocked client + AES; integration tests hit live endpoints
20
+ - `tests/` — mirrors `branta/`; unit tests use mocked client + AES; integration tests hit live endpoints only when `BRANTA_RUN_INTEGRATION=1`
21
21
 
22
22
  ## Setup
23
23
 
@@ -27,9 +27,9 @@ pip install -e ".[dev]"
27
27
 
28
28
  ## Commands
29
29
 
30
- - `pytest tests/ --ignore=tests/test_integration.py` — run unit tests (no network)
31
- - `pytest tests/test_integration.py` — run integration tests (requires network)
32
- - `coverage run -m pytest tests/ --ignore=tests/test_integration.py && coverage report` — with coverage
30
+ - `pytest tests/` — unit tests; live lookups skipped unless `BRANTA_RUN_INTEGRATION=1`
31
+ - `BRANTA_RUN_INTEGRATION=1 pytest tests/test_integration.py` — live Guardrail lookups
32
+ - `coverage run -m pytest tests/ && coverage report` — with coverage
33
33
  - `pip install -e .` / `pip publish` — publish to PyPI (requires `twine`)
34
34
 
35
35
  ## Key behaviors to preserve
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: branta
3
- Version: 3.2.4
3
+ Version: 3.2.5
4
4
  Summary: Python SDK for the Branta V2 API — payment destination lookup and registration with zero-knowledge encryption support
5
5
  Project-URL: Homepage, https://branta.pro
6
6
  Project-URL: Repository, https://github.com/BrantaOps/branta-python
@@ -383,9 +383,9 @@ async def is_api_key_valid(options: Optional[BrantaClientOptions] = None) -> boo
383
383
 
384
384
  ```bash
385
385
  pip install -e ".[dev]"
386
- pytest tests/ --ignore=tests/test_integration.py # unit tests
387
- pytest tests/test_integration.py # integration (requires network)
388
- coverage run -m pytest tests/ --ignore=tests/test_integration.py && coverage report
386
+ pytest tests/ # unit tests; live lookups skipped
387
+ BRANTA_RUN_INTEGRATION=1 pytest tests/test_integration.py # live Guardrail lookups (opt-in)
388
+ coverage run -m pytest tests/ && coverage report
389
389
  ```
390
390
 
391
391
  # Responsible Disclosure
@@ -355,9 +355,9 @@ async def is_api_key_valid(options: Optional[BrantaClientOptions] = None) -> boo
355
355
 
356
356
  ```bash
357
357
  pip install -e ".[dev]"
358
- pytest tests/ --ignore=tests/test_integration.py # unit tests
359
- pytest tests/test_integration.py # integration (requires network)
360
- coverage run -m pytest tests/ --ignore=tests/test_integration.py && coverage report
358
+ pytest tests/ # unit tests; live lookups skipped
359
+ BRANTA_RUN_INTEGRATION=1 pytest tests/test_integration.py # live Guardrail lookups (opt-in)
360
+ coverage run -m pytest tests/ && coverage report
361
361
  ```
362
362
 
363
363
  # Responsible Disclosure
@@ -5,7 +5,7 @@ from typing import Dict, List, Optional
5
5
  from urllib.parse import parse_qs, unquote, urlparse
6
6
 
7
7
  from branta.enums import DestinationType
8
- from branta.extensions import is_ark, is_bolt11, is_silent_payment
8
+ from branta.extensions import get_hash_zk_type, is_ark, is_bolt11, is_silent_payment
9
9
 
10
10
  _LN_ADDRESS_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
11
11
  _ETH_HEX_RE = re.compile(r"^[0-9a-fA-F]{40}$")
@@ -119,6 +119,8 @@ class QRParser:
119
119
  self.destinations: List[QrDestination] = []
120
120
  self.on_chain_encryption_text: Optional[str] = None
121
121
  self.on_chain_encryption_secret: Optional[str] = None
122
+ # A payable instruction this SDK cannot bind to a decrypted destination.
123
+ self.has_unverifiable_instruction = False
122
124
 
123
125
  text = _normalize_qr_text(qr_text)
124
126
 
@@ -136,7 +138,14 @@ class QRParser:
136
138
  if scheme in ("bitcoin", "lightning"):
137
139
  dest = _extract_path(text)
138
140
  if dest is not None:
139
- self.destinations.append(QrDestination(value=dest, type=_get_destination_type(text)))
141
+ dest_type = _get_destination_type(text)
142
+ self.destinations.append(QrDestination(value=dest, type=dest_type))
143
+ if (
144
+ dest != ""
145
+ and dest_type != DestinationType.BitcoinAddress
146
+ and get_hash_zk_type(dest) is None
147
+ ):
148
+ self.has_unverifiable_instruction = True
140
149
 
141
150
  query = parsed.query
142
151
  params = _parse_query_string(query)
@@ -144,21 +153,25 @@ class QRParser:
144
153
  self.on_chain_encryption_text = params.get("branta_id")
145
154
  self.on_chain_encryption_secret = params.get("branta_secret")
146
155
 
147
- lightning_value = params.get("lightning")
148
- if lightning_value is not None:
149
- self.destinations.append(QrDestination(value=lightning_value, type=_detect_plain_text_type(lightning_value)))
150
-
151
- bolt12_value = params.get("bolt12")
152
- if bolt12_value is not None:
153
- self.destinations.append(QrDestination(value=bolt12_value, type=_detect_plain_text_type(bolt12_value)))
154
-
155
- ark_value = params.get("ark")
156
- if ark_value is not None:
157
- self.destinations.append(QrDestination(value=ark_value, type=_detect_plain_text_type(ark_value)))
158
-
159
- silent_payment_value = params.get("silent_payment")
160
- if silent_payment_value is not None:
161
- self.destinations.append(QrDestination(value=silent_payment_value, type=_detect_plain_text_type(silent_payment_value)))
156
+ def add_bindable(value: Optional[str], type: Optional[DestinationType], on_chain: bool) -> None:
157
+ if value is None:
158
+ return
159
+ self.destinations.append(QrDestination(value=value, type=type))
160
+ if value != "" and not on_chain and get_hash_zk_type(value) is None:
161
+ self.has_unverifiable_instruction = True
162
+
163
+ for key in ("lightning", "bolt12", "ark", "silent_payment"):
164
+ value = params.get(key)
165
+ add_bindable(value, _detect_plain_text_type(value or ""), False)
166
+ for key in ("bc", "tb"):
167
+ add_bindable(params.get(key), DestinationType.BitcoinAddress, True)
168
+ sp_value = params.get("sp")
169
+ add_bindable(sp_value, _detect_plain_text_type(sp_value or ""), False)
170
+ lno_value = params.get("lno")
171
+ lno_type = (_detect_plain_text_type(lno_value) or DestinationType.Bolt12) if lno_value else None
172
+ add_bindable(lno_value, lno_type, False)
173
+ if params.get("pay"):
174
+ self.has_unverifiable_instruction = True
162
175
 
163
176
  return
164
177
 
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import re
3
4
  from typing import Dict, List, Optional
4
5
  from urllib.parse import quote
5
6
 
@@ -21,15 +22,37 @@ from branta.v2.parser import QRParser
21
22
  from branta.v2.secret_generator import GuidSecretGenerator
22
23
 
23
24
 
25
+ _TAMPERED_MESSAGE = (
26
+ "The Bitcoin address in the QR code does not match the address verified by Branta. "
27
+ "The QR code may have been tampered with."
28
+ )
29
+ _BECH32_RE = re.compile(r"^(bc|tb|bcrt)1", re.IGNORECASE)
30
+
31
+
24
32
  def _addresses_match(a: str, b: str) -> bool:
25
33
  def is_bech32(v: str) -> bool:
26
- return v.lower().startswith("bc1")
34
+ return _BECH32_RE.match(v) is not None
27
35
 
28
36
  if is_bech32(a) and is_bech32(b):
29
37
  return a.lower() == b.lower()
30
38
  return a == b
31
39
 
32
40
 
41
+ def _uniform_on_chain_address(parser: QRParser) -> Optional[str]:
42
+ addresses = [
43
+ d.value
44
+ for d in parser.destinations
45
+ if d.type == DestinationType.BitcoinAddress and d.value.strip() != ""
46
+ ]
47
+ if not addresses:
48
+ return None
49
+ first = addresses[0]
50
+ for address in addresses[1:]:
51
+ if not _addresses_match(first, address):
52
+ raise BrantaPaymentException(_TAMPERED_MESSAGE, BrantaPaymentExceptionReason.Tampered)
53
+ return first
54
+
55
+
33
56
  class BrantaService:
34
57
  def __init__(
35
58
  self,
@@ -53,6 +76,11 @@ class BrantaService:
53
76
  options: Optional[BrantaClientOptions] = None,
54
77
  ) -> PaymentsResult:
55
78
  parser = QRParser(qr_text)
79
+ expected_on_chain_address = _uniform_on_chain_address(parser)
80
+
81
+ if parser.has_unverifiable_instruction:
82
+ lookup = parser.on_chain_encryption_text or parser.destination or ""
83
+ return PaymentsResult(payments=[], verify_url=self._build_verify_url(options, lookup))
56
84
 
57
85
  if parser.is_on_chain_zk():
58
86
  additional_values = [
@@ -60,15 +88,11 @@ class BrantaService:
60
88
  for d in parser.destinations
61
89
  if get_hash_zk_type(d.value) is not None
62
90
  ]
63
- on_chain_address = next(
64
- (d.value for d in parser.destinations if d.type == DestinationType.BitcoinAddress),
65
- None,
66
- )
67
91
  return await self._get_payments_for_zk(
68
92
  parser.on_chain_encryption_text,
69
93
  parser.on_chain_encryption_secret,
70
94
  additional_values,
71
- on_chain_address,
95
+ expected_on_chain_address,
72
96
  options,
73
97
  )
74
98
 
@@ -212,25 +236,31 @@ class BrantaService:
212
236
  payments = await self._client.get_payments(lookup_value, options)
213
237
 
214
238
  keys: Dict[str, str] = {}
239
+ bound: List[Payment] = []
215
240
  for payment in payments:
216
- await self._decrypt_destinations(
241
+ on_chain_bound = await self._decrypt_destinations(
217
242
  payment, lookup_value, encryption_key, None, keys, expected_on_chain_address
218
243
  )
244
+ hashes_bound = True
219
245
  for value in additional_hash_values:
220
- await self._decrypt_hash_zk_destinations(payment, value, keys)
246
+ if not await self._decrypt_hash_zk_destinations(payment, value, keys):
247
+ hashes_bound = False
248
+ if on_chain_bound and hashes_bound:
249
+ bound.append(payment)
221
250
 
222
- return PaymentsResult(payments=payments, verify_url=self._build_verify_url(options, lookup_value, keys))
251
+ return PaymentsResult(payments=bound, verify_url=self._build_verify_url(options, lookup_value, keys))
223
252
 
224
253
  async def _decrypt_hash_zk_destinations(
225
254
  self,
226
255
  payment: Payment,
227
256
  plain_value: str,
228
257
  keys: Dict[str, str],
229
- ) -> None:
258
+ ) -> bool:
230
259
  hash_zk_type = get_hash_zk_type(plain_value)
231
260
  if hash_zk_type is None:
232
- return
261
+ return False
233
262
  key = to_normalized_hash(plain_value)
263
+ bound = False
234
264
  for destination in payment.destinations:
235
265
  if not destination.is_zk or destination.type != hash_zk_type:
236
266
  continue
@@ -240,8 +270,10 @@ class BrantaService:
240
270
  if destination.zk_id is not None and destination.zk_id not in keys:
241
271
  keys[destination.zk_id] = key
242
272
  await self._try_decrypt_metadata(payment, destination, key)
273
+ bound = True
243
274
  except Exception:
244
275
  pass
276
+ return bound
245
277
 
246
278
  async def _decrypt_destinations(
247
279
  self,
@@ -251,7 +283,8 @@ class BrantaService:
251
283
  hash_zk_type: Optional[DestinationType],
252
284
  keys: Dict[str, str],
253
285
  expected_on_chain_address: Optional[str] = None,
254
- ) -> None:
286
+ ) -> bool:
287
+ on_chain_bound = expected_on_chain_address is None
255
288
  for destination in payment.destinations:
256
289
  destination.is_encrypted = bool(destination.is_zk)
257
290
  if not destination.is_zk:
@@ -266,12 +299,10 @@ class BrantaService:
266
299
  continue
267
300
 
268
301
  if expected_on_chain_address is not None and not _addresses_match(decrypted, expected_on_chain_address):
269
- raise BrantaPaymentException(
270
- "The Bitcoin address in the QR code does not match the address verified by Branta. "
271
- "The QR code may have been tampered with.",
272
- BrantaPaymentExceptionReason.Tampered,
273
- )
302
+ raise BrantaPaymentException(_TAMPERED_MESSAGE, BrantaPaymentExceptionReason.Tampered)
274
303
 
304
+ if expected_on_chain_address is not None:
305
+ on_chain_bound = True
275
306
  destination.value = decrypted
276
307
  destination.is_encrypted = False
277
308
  if destination.zk_id is not None and destination.zk_id not in keys:
@@ -287,6 +318,7 @@ class BrantaService:
287
318
  await self._try_decrypt_metadata(payment, destination, key)
288
319
  except Exception:
289
320
  pass
321
+ return on_chain_bound
290
322
 
291
323
  async def _try_decrypt_metadata(
292
324
  self,
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "branta"
7
- version = "3.2.4"
7
+ version = "3.2.5"
8
8
  description = "Python SDK for the Branta V2 API — payment destination lookup and registration with zero-knowledge encryption support"
9
9
  readme = "README.md"
10
10
  license = { text = "MIT" }
@@ -1,30 +1,34 @@
1
1
  """
2
2
  Integration tests against live Branta endpoints.
3
3
 
4
- Run with: pytest tests/test_integration.py -v
5
- These tests require network access. They are skipped when BRANTA_SKIP_INTEGRATION
6
- is set to any value, and will be automatically skipped if aiohttp is not installed.
4
+ Skipped unless BRANTA_RUN_INTEGRATION=1 — local `pytest` and PR CI must not hit Guardrail.
5
+ Run with: BRANTA_RUN_INTEGRATION=1 pytest tests/test_integration.py -v
7
6
  """
8
7
  from __future__ import annotations
9
8
 
10
9
  import os
11
10
 
11
+ import aiohttp
12
12
  import pytest
13
13
 
14
14
  from branta.enums import BrantaServerBaseUrl, PrivacyMode
15
15
  from branta.options import BrantaClientOptions
16
+ from branta.v2.client import BrantaClient
16
17
  from branta.v2.service import BrantaService
17
18
 
18
19
  pytestmark = pytest.mark.skipif(
19
- os.environ.get("BRANTA_SKIP_INTEGRATION") is not None,
20
- reason="Integration tests skipped (BRANTA_SKIP_INTEGRATION set)",
20
+ os.environ.get("BRANTA_RUN_INTEGRATION") != "1",
21
+ reason="Live lookups skipped unless BRANTA_RUN_INTEGRATION=1",
21
22
  )
22
23
 
23
24
  TIMEOUT = 15
24
25
 
25
26
 
26
27
  def make_service(base_url: BrantaServerBaseUrl, privacy: PrivacyMode) -> BrantaService:
27
- return BrantaService(BrantaClientOptions(base_url=base_url, privacy=privacy))
28
+ options = BrantaClientOptions(base_url=base_url, privacy=privacy)
29
+ token = os.environ.get("BRANTA_INTEGRATION_METRICS_TOKEN")
30
+ session = aiohttp.ClientSession(headers={"X-Branta-Integration-Token": token}) if token else None
31
+ return BrantaService(options, client=BrantaClient(options, session=session))
28
32
 
29
33
 
30
34
  # ===== Production — Loose =====
@@ -195,6 +195,34 @@ class TestQRParserEdgeCases:
195
195
  assert parser.destination == "lnbc100n1ptest"
196
196
  assert parser.destination_type == DestinationType.Bolt11
197
197
 
198
+ def test_bip321_bc_and_tb_are_on_chain_addresses(self):
199
+ parser = QRParser("bitcoin:?bc=bc1qmerchant&tb=tb1qtest")
200
+ assert [d.value for d in parser.destinations] == ["bc1qmerchant", "tb1qtest"]
201
+ assert parser.destinations[0].type == DestinationType.BitcoinAddress
202
+ assert parser.destinations[1].type == DestinationType.BitcoinAddress
203
+ assert parser.has_unverifiable_instruction is False
204
+
205
+ def test_sp_param_adds_silent_payment(self):
206
+ parser = QRParser("bitcoin:bc1qmerchant?sp=sp1qqexample")
207
+ assert parser.destinations[1].value == "sp1qqexample"
208
+ assert parser.destinations[1].type == DestinationType.SilentPayment
209
+ assert parser.has_unverifiable_instruction is False
210
+
211
+ def test_lno_and_pay_are_unverifiable(self):
212
+ lno = QRParser("bitcoin:bc1qmerchant?lno=lno1qqexample")
213
+ assert lno.destinations[1].value == "lno1qqexample"
214
+ assert lno.destinations[1].type == DestinationType.Bolt12
215
+ assert lno.has_unverifiable_instruction is True
216
+
217
+ pay = QRParser("bitcoin:bc1qmerchant?pay=bitcoin:bc1qother")
218
+ assert pay.has_unverifiable_instruction is True
219
+ assert [d.value for d in pay.destinations] == ["bc1qmerchant"]
220
+
221
+ def test_non_bitcoin_non_hash_zk_path_is_unverifiable(self):
222
+ assert QRParser("lightning:lnotest").has_unverifiable_instruction is True
223
+ assert QRParser("lightning:lnbc100n1ptest").has_unverifiable_instruction is False
224
+ assert QRParser("bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa").has_unverifiable_instruction is False
225
+
198
226
 
199
227
  class TestQRParserCombined:
200
228
  def test_combined_zk_qr(self):
@@ -322,15 +322,23 @@ class TestGetPaymentsByQrCodeAddressBinding:
322
322
  async def test_lightning_qr_with_zk_params_no_plain_address_decrypts_without_comparison(
323
323
  self, service, client_mock, aes_mock
324
324
  ):
325
+ payment = (
326
+ PaymentBuilder()
327
+ .add_destination(ENCRYPTED_BITCOIN_ADDRESS, DestinationType.BitcoinAddress).set_zk()
328
+ .add_destination(ENCRYPTED_BOLT11, DestinationType.Bolt11).set_zk()
329
+ .build()
330
+ )
331
+
325
332
  async def get_side_effect(lookup, opts=None, signal=None):
326
333
  if lookup == ENCRYPTED_BITCOIN_ADDRESS:
327
- return [zk_bitcoin_payment()]
334
+ return [payment]
328
335
  return []
329
336
  client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
330
337
 
331
338
  qr = f"lightning:{BOLT11_INVOICE}?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
332
339
  result = await service.get_payments_by_qr_code(qr)
333
340
  assert result.payments[0].destinations[0].value == BITCOIN_ADDRESS
341
+ assert result.payments[0].destinations[1].value == DECRYPTED_BOLT11
334
342
 
335
343
  async def test_combined_zk_qr_swapped_address_rejects(self, service, client_mock, aes_mock):
336
344
  payment = (
@@ -355,6 +363,114 @@ class TestGetPaymentsByQrCodeAddressBinding:
355
363
  await service.get_payments_by_qr_code(qr)
356
364
  assert exc_info.value.reason == BrantaPaymentExceptionReason.Tampered
357
365
 
366
+ async def test_swapped_address_and_wrong_secret_returns_no_payments(self, service, client_mock, aes_mock):
367
+ aes_mock.decrypt = AsyncMock(side_effect=Exception("auth tag mismatch"))
368
+
369
+ async def get_side_effect(lookup, opts=None, signal=None):
370
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
371
+ return [zk_bitcoin_payment()]
372
+ return []
373
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
374
+
375
+ qr = f"bitcoin:{SWAPPED_ADDRESS}?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret=00000000-0000-4000-8000-000000000000"
376
+ result = await service.get_payments_by_qr_code(qr)
377
+ assert len(result.payments) == 0
378
+
379
+ async def test_attacker_lightning_leg_returns_no_payments(self, service, client_mock, aes_mock):
380
+ async def get_side_effect(lookup, opts=None, signal=None):
381
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
382
+ return [zk_bitcoin_payment()]
383
+ return []
384
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
385
+
386
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}&lightning=lnbc1attacker"
387
+ result = await service.get_payments_by_qr_code(qr)
388
+ assert len(result.payments) == 0
389
+
390
+ async def test_attacker_ark_leg_returns_no_payments(self, service, client_mock, aes_mock):
391
+ async def get_side_effect(lookup, opts=None, signal=None):
392
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
393
+ return [zk_bitcoin_payment()]
394
+ return []
395
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
396
+
397
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}&ark=ark1attacker"
398
+ result = await service.get_payments_by_qr_code(qr)
399
+ assert len(result.payments) == 0
400
+
401
+ async def test_bip321_bc_swap_rejects(self, service, client_mock, aes_mock):
402
+ async def get_side_effect(lookup, opts=None, signal=None):
403
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
404
+ return [zk_bitcoin_payment()]
405
+ return []
406
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
407
+
408
+ qr = f"bitcoin:?bc={SWAPPED_ADDRESS}&branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
409
+ with pytest.raises(BrantaPaymentException) as exc_info:
410
+ await service.get_payments_by_qr_code(qr)
411
+ assert exc_info.value.reason == BrantaPaymentExceptionReason.Tampered
412
+
413
+ async def test_bip321_bc_match_resolves(self, service, client_mock, aes_mock):
414
+ async def get_side_effect(lookup, opts=None, signal=None):
415
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
416
+ return [zk_bitcoin_payment()]
417
+ return []
418
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
419
+
420
+ qr = f"bitcoin:?bc={BITCOIN_ADDRESS}&branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
421
+ result = await service.get_payments_by_qr_code(qr)
422
+ assert result.payments[0].destinations[0].value == BITCOIN_ADDRESS
423
+ assert result.payments[0].destinations[0].is_encrypted is False
424
+
425
+ async def test_path_and_bc_disagree_rejects_before_lookup(self, service, client_mock, aes_mock):
426
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?bc={SWAPPED_ADDRESS}&branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
427
+ with pytest.raises(BrantaPaymentException) as exc_info:
428
+ await service.get_payments_by_qr_code(qr)
429
+ assert exc_info.value.reason == BrantaPaymentExceptionReason.Tampered
430
+ client_mock.get_payments.assert_not_awaited()
431
+
432
+ async def test_pay_instruction_returns_no_payments(self, service, client_mock, aes_mock):
433
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?pay=bitcoin:{SWAPPED_ADDRESS}&branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
434
+ result = await service.get_payments_by_qr_code(qr)
435
+ assert len(result.payments) == 0
436
+ client_mock.get_payments.assert_not_awaited()
437
+
438
+ async def test_lno_instruction_returns_no_payments(self, service, client_mock, aes_mock):
439
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?lno=lno1qqattacker&branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
440
+ result = await service.get_payments_by_qr_code(qr)
441
+ assert len(result.payments) == 0
442
+ client_mock.get_payments.assert_not_awaited()
443
+
444
+ async def test_no_payable_instruction_wrong_secret_returns_encrypted_record(
445
+ self, service, client_mock, aes_mock
446
+ ):
447
+ aes_mock.decrypt = AsyncMock(side_effect=Exception("auth tag mismatch"))
448
+
449
+ async def get_side_effect(lookup, opts=None, signal=None):
450
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
451
+ return [zk_bitcoin_payment()]
452
+ return []
453
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
454
+
455
+ qr = f"bitcoin:?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret=00000000-0000-4000-8000-000000000000"
456
+ result = await service.get_payments_by_qr_code(qr)
457
+ assert len(result.payments) == 1
458
+ assert result.payments[0].destinations[0].value == ENCRYPTED_BITCOIN_ADDRESS
459
+ assert result.payments[0].destinations[0].is_encrypted is True
460
+
461
+ async def test_zk_bitcoin_uri_wrong_key_returns_no_payments(self, service, client_mock, aes_mock):
462
+ aes_mock.decrypt = AsyncMock(side_effect=Exception("OperationError: operation failed"))
463
+
464
+ async def get_side_effect(lookup, opts=None, signal=None):
465
+ if lookup == ENCRYPTED_BITCOIN_ADDRESS:
466
+ return [zk_bitcoin_payment()]
467
+ return []
468
+ client_mock.get_payments = AsyncMock(side_effect=get_side_effect)
469
+
470
+ qr = f"bitcoin:{BITCOIN_ADDRESS}?branta_id={ENCRYPTED_BITCOIN_ADDRESS}&branta_secret={SECRET}"
471
+ result = await service.get_payments_by_qr_code(qr)
472
+ assert len(result.payments) == 0
473
+
358
474
 
359
475
  # ===== get_payments =====
360
476
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes