Debian Codegraph @

tests/test_lnurl.py

raw symbols SCIP
1 from unittest import TestCase
2
3 from electrum import lnurl
4
5
6 class TestLnurl(TestCase):
7 def test_decode(self):
8 LNURL = (
9 "LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9"
10 )
11 url = lnurl.decode_lnurl(LNURL)
12 self.assertEqual("https://service.io/?q=3fc3645b439ce8e7", url)
13
14 def test_encode(self):
15 lnurl_ = lnurl.encode_lnurl("https://jhoenicke.de/.well-known/lnurlp/mempool")
16 self.assertEqual(
17 "LNURL1DP68GURN8GHJ76NGDAJKU6TRDDJJUER99UH8WETVDSKKKMN0WAHZ7MRWW4EXCUP0D4JK6UR0DAKQHMHNX2",
18 lnurl_)
19
20 def test_lightning_address_to_url(self):
21 for address, expected in (
22 ("[email protected]",
23 "https://jhoenicke.de/.well-known/lnurlp/mempool"),
24 ("[email protected]",
25 "https://sub.domain.example.co.uk/.well-known/lnurlp/first.last"),
26 ("[email protected]",
27 "https://Example.COM/.well-known/lnurlp/Some_User-1"),
28 ("[email protected]",
29 "https://domain.com/.well-known/lnurlp/user-with+tag")
30 ):
31 self.assertEqual(expected, lnurl.lightning_address_to_url(address))
32
33 # only one tag
34 for address in (
35 "[email protected]",
36 "[email protected]",
37 "[email protected]",
38 "[email protected]",
39 "[email protected]",
40 ):
41 self.assertIsNone(lnurl.lightning_address_to_url(address), f"should be rejected: {address!r}")
42
43 def test_lightning_address_to_url_rejects_url_metacharacters(self):
44 """username and domain are interpolated into the url, so they must not be able to
45 steer the request elsewhere."""
46 for address in (
47 # path traversal / path append
48 "../../admin/[email protected]",
49 "a/[email protected]",
50 "[email protected]",
51 "[email protected]",
52 # percent-encoding, which the server may well decode back into a '/'
53 "a%2e%2e%[email protected]",
54 # query and fragment
55 "[email protected]",
56 "user#@bank.com",
57 # host trickery
58 "[email protected]/x.y",
59 "[email protected]:8443",
60 "[email protected]@evil.com",
61 "[email protected]",
62 # trailing newline
63 "[email protected]\n",
64 # not an address at all
65 "user@localhost",
66 "userbank.com",
67 "",
68 ):
69 self.assertIsNone(lnurl.lightning_address_to_url(address), f"should be rejected: {address!r}")
70
71 def test_parse_lnurl3_response(self):
72 # Test successful parsing with all fields
73 sample_response = {
74 'callback': 'https://service.io/withdraw?sessionid=123',
75 'k1': 'abcdef1234567890',
76 'defaultDescription': 'Withdraw from service',
77 'minWithdrawable': 10_000_000,
78 'maxWithdrawable': 100_000_000,
79 }
80
81 result = lnurl._parse_lnurl3_response(sample_response)
82
83 self.assertEqual('https://service.io/withdraw?sessionid=123', result.callback_url)
84 self.assertEqual('abcdef1234567890', result.k1)
85 self.assertEqual('Withdraw from service', result.default_description)
86 self.assertEqual(10_000, result.min_withdrawable_sat)
87 self.assertEqual(100_000, result.max_withdrawable_sat)
88
89 # Test with .onion URL
90 onion_response = {
91 'callback': 'http://robosatsy56bwqn56qyadmcxkx767hnabg4mihxlmgyt6if5gnuxvzad.onion/withdraw?sessionid=123',
92 'k1': 'abcdef1234567890',
93 'minWithdrawable': 10_000_000,
94 'maxWithdrawable': 100_000_000
95 }
96
97 result = lnurl._parse_lnurl3_response(onion_response)
98 self.assertEqual('http://robosatsy56bwqn56qyadmcxkx767hnabg4mihxlmgyt6if5gnuxvzad.onion/withdraw?sessionid=123',
99 result.callback_url)
100 self.assertEqual('', result.default_description) # Missing defaultDescription uses empty string
101
102 # Test missing callback (should raise error)
103 no_callback_response = {
104 'k1': 'abcdef1234567890',
105 'minWithdrawable': 10_000_000,
106 'maxWithdrawable': 100_000_000
107 }
108
109 with self.assertRaises(lnurl.LNURLError):
110 lnurl._parse_lnurl3_response(no_callback_response)
111
112 # Test unsafe callback URL
113 unsafe_response = {
114 'callback': 'http://service.io/withdraw?sessionid=123', # HTTP URL
115 'k1': 'abcdef1234567890',
116 'minWithdrawable': 10_000_000,
117 'maxWithdrawable': 100_000_000
118 }
119
120 with self.assertRaises(lnurl.LNURLError):
121 lnurl._parse_lnurl3_response(unsafe_response)
122
123 # Test missing k1 (should raise error)
124 no_k1_response = {
125 'callback': 'https://service.io/withdraw?sessionid=123',
126 'minWithdrawable': 10_000_000,
127 'maxWithdrawable': 100_000_000
128 }
129
130 with self.assertRaises(lnurl.LNURLError):
131 lnurl._parse_lnurl3_response(no_k1_response)
132
133 # Test missing withdrawable amounts (should raise error)
134 no_amounts_response = {
135 'callback': 'https://service.io/withdraw?sessionid=123',
136 'k1': 'abcdef1234567890',
137 }
138
139 with self.assertRaises(lnurl.LNURLError):
140 lnurl._parse_lnurl3_response(no_amounts_response)
141
142 # Test malformed withdrawable amounts (should raise error)
143 bad_amounts_response = {
144 'callback': 'https://service.io/withdraw?sessionid=123',
145 'k1': 'abcdef1234567890',
146 'minWithdrawable': 'this is not a number',
147 'maxWithdrawable': 100_000_000
148 }
149
150 with self.assertRaises(lnurl.LNURLError):
151 lnurl._parse_lnurl3_response(bad_amounts_response)