Skip to content

Commit 8ede0c5

Browse files
committed
Cleanup for address_utils file
1 parent b5ab399 commit 8ede0c5

1 file changed

Lines changed: 83 additions & 96 deletions

File tree

lib/utilities/address_utils.dart

Lines changed: 83 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -41,59 +41,59 @@ class AddressUtils {
4141
}
4242

4343
/// Parses a URI string and returns a map with parsed components.
44-
static Map<String, String> _parseUri(String uri) {
45-
final Map<String, String> result = {};
46-
47-
try {
48-
final Uri parsedUri = Uri.parse(uri);
49-
50-
if (parsedUri.hasScheme) {
51-
final String scheme = parsedUri.scheme.toLowerCase();
52-
result["scheme"] = scheme;
53-
54-
if (scheme == "bitcoin" || scheme == "bitcoincash") {
55-
result["address"] = parsedUri.path;
56-
} else if (scheme == "monero") {
57-
// Monero addresses can contain '?' which Uri.parse interprets
58-
// as the start of the query.
59-
final int addressEnd = uri.indexOf(
60-
"?",
61-
7, // Length of "monero:".
62-
);
63-
64-
result["address"] = addressEnd != -1
65-
? uri.substring(7, addressEnd)
66-
: uri.substring(7);
44+
static Map<String, String> _parseUri(String uri) {
45+
final Map<String, String> result = {};
46+
try {
47+
final Uri parsedUri = Uri.parse(uri);
48+
49+
if (parsedUri.hasScheme) {
50+
final String scheme = parsedUri.scheme.toLowerCase();
51+
result["scheme"] = scheme;
52+
53+
// Handle different URI formats.
54+
if (scheme == "bitcoin" || scheme == "bitcoincash") {
55+
result["address"] = parsedUri.path;
56+
} else if (scheme == "monero") {
57+
// Monero addresses can contain '?' which Uri.parse interprets as query start.
58+
final int addressEnd = uri.indexOf(
59+
"?",
60+
7, // 7 is the length of "monero:".
61+
);
62+
63+
result["address"] = addressEnd != -1
64+
? uri.substring(7, addressEnd)
65+
: uri.substring(7);
66+
} else {
67+
// Default case, treat path as address.
68+
result["address"] = parsedUri.path;
69+
}
6770
} else {
71+
// Plain address, including an Epicbox address containing '@'.
6872
result["address"] = parsedUri.path;
6973
}
70-
} else {
71-
// Plain address, including an Epicbox address containing '@'.
72-
result["address"] = parsedUri.path;
73-
}
7474

75-
result.addAll(
76-
_parseQueryParameters(parsedUri.queryParameters),
77-
);
75+
// Parse query parameters.
76+
result.addAll(_parseQueryParameters(parsedUri.queryParameters));
7877

79-
if (
80-
parsedUri.fragment.isNotEmpty &&
81-
result["scheme"] == "monero"
82-
) {
83-
result["tx_description"] = Uri.decodeComponent(
84-
parsedUri.fragment,
78+
// Handle Monero-specific fragment (tx_description).
79+
if (
80+
parsedUri.fragment.isNotEmpty &&
81+
result["scheme"] == "monero"
82+
) {
83+
result["tx_description"] = Uri.decodeComponent(
84+
parsedUri.fragment,
85+
);
86+
}
87+
} catch (e, s) {
88+
Logging.instance.d(
89+
"Exception caught in _parseUri($uri): $e",
90+
error: e,
91+
stackTrace: s,
8592
);
8693
}
87-
} catch (e, s) {
88-
Logging.instance.d(
89-
"Exception caught in _parseUri($uri): $e",
90-
error: e,
91-
stackTrace: s,
92-
);
93-
}
9494

95-
return result;
96-
}
95+
return result;
96+
}
9797

9898
/// Helper method to parse and normalize query parameters.
9999
static Map<String, String> _parseQueryParameters(Map<String, String> params) {
@@ -143,65 +143,52 @@ static Map<String, String> _parseUri(String uri) {
143143
/// Centralized method to handle various cryptocurrency URIs and return a common object.
144144
///
145145
/// Returns null on failure to parse
146-
static PaymentUriData? parsePaymentUri(
147-
String uri, {
148-
Logging? logging,
149-
}) {
150-
// Hacky check that it is not just a bcash, ecash, or xel address.
151-
final parts = uri.split(":");
152-
153-
if (parts.length == 2) {
154-
if ([
155-
"xel",
156-
"bitcoincash",
157-
"bchtest",
158-
"ecash",
159-
"ectest",
160-
].contains(parts.first.toLowerCase())) {
161-
return null;
146+
static PaymentUriData? parsePaymentUri(String uri, {Logging? logging}) {
147+
// Hacky check that it is not just a bcash, ecash, or xel address.
148+
final parts = uri.split(":");
149+
150+
if (parts.length == 2) {
151+
if ([
152+
"xel",
153+
"bitcoincash",
154+
"bchtest",
155+
"ecash",
156+
"ectest",
157+
].contains(parts.first.toLowerCase())) {
158+
return null;
159+
}
162160
}
163-
}
164161

165-
try {
166-
final Map<String, String> parsedData = _parseUri(uri);
162+
try {
163+
final Map<String, String> parsedData = _parseUri(uri);
167164

168-
final String scheme = parsedData["scheme"] ?? "";
169-
parsedData.remove("scheme");
165+
// Normalize the URI scheme.
166+
final String scheme = parsedData["scheme"] ?? "";
167+
parsedData.remove("scheme");
170168

171-
final String? address = parsedData["address"];
169+
// Filter out unrecognized parameters.
170+
final String? address = parsedData["address"];
172171

173-
if (address == null || address.trim().isEmpty) {
174-
return null;
175-
}
172+
if (address == null || address.trim().isEmpty) {
173+
return null;
174+
}
176175

177-
final Map<String, String> filteredParams =
178-
_filterParams(parsedData);
179-
180-
return PaymentUriData(
181-
scheme: scheme,
182-
address: address.trim(),
183-
amount:
184-
filteredParams["amount"] ??
185-
filteredParams["tx_amount"],
186-
label:
187-
filteredParams["label"] ??
188-
filteredParams["recipient_name"],
189-
message:
190-
filteredParams["message"] ??
191-
filteredParams["tx_description"],
192-
paymentId: filteredParams["tx_payment_id"],
193-
additionalParams: filteredParams,
194-
);
195-
} catch (e, s) {
196-
logging?.i(
197-
"Invalid payment URI: $uri",
198-
error: e,
199-
stackTrace: s,
200-
);
176+
final Map<String, String> filteredParams = _filterParams(parsedData);
201177

202-
return null;
178+
return PaymentUriData(
179+
scheme: scheme,
180+
address: address.trim(),
181+
amount: filteredParams["amount"] ?? filteredParams["tx_amount"],
182+
label: filteredParams["label"] ?? filteredParams["recipient_name"],
183+
message: filteredParams["message"] ?? filteredParams["tx_description"],
184+
paymentId: filteredParams["tx_payment_id"],
185+
additionalParams: filteredParams,
186+
);
187+
} catch (e, s) {
188+
logging?.i("Invalid payment URI: $uri", error: e, stackTrace: s);
189+
return null;
190+
}
203191
}
204-
}
205192

206193
/// Builds a uri string with the given address and query parameters (if any)
207194
static String buildUriString(

0 commit comments

Comments
 (0)