Skip to content

Commit 36a43c3

Browse files
author
Fmar
committed
fix nwc uri parse with multiple individual relay parameters
1 parent 64e819d commit 36a43c3

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

packages/ndk/lib/domain_layer/usecases/nwc/nostr_wallet_connect_uri.dart

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,35 @@ class NostrWalletConnectUri extends Equatable {
6868
factory NostrWalletConnectUri.parseConnectionUri(String uri) {
6969
Uri parsedUri = Uri.parse(uri);
7070

71-
String pubkey = parsedUri.host;// _extractPubkey(parsedUri.toString());
71+
String pubkey = parsedUri.host; // _extractPubkey(parsedUri.toString());
7272
String? secret = parsedUri.queryParameters['secret'];
7373
String? lud16 = parsedUri.queryParameters['lud16'];
7474

7575
if (pubkey.isEmpty || secret == null || parsedUri.scheme != 'nostr+walletconnect') {
76-
throw Exception(
77-
"Required fields (scheme, pubkey, secret) are missing or incorrect in the connection URI.");
76+
throw Exception("Required fields (scheme, pubkey, secret) are missing or incorrect in the connection URI.");
7877
}
7978

8079
// Parse relays - support both single relay and comma-separated relays
8180
List<String> relaysList = [];
8281

8382
// Check for single relay parameter (backward compatibility)
84-
String? singleRelay = parsedUri.queryParameters['relay'];
85-
if (singleRelay != null) {
86-
List<String> commaSeparatedRelays = singleRelay
87-
.split(',')
88-
.map((r) => r.trim())
89-
.where((r) => r.isNotEmpty)
90-
.toList();
91-
for (String relay in commaSeparatedRelays) {
92-
if (!relaysList.contains(relay)) {
93-
relaysList.add(relay);
83+
List<String>? relayParams = parsedUri.queryParametersAll['relay'];
84+
if (relayParams != null) {
85+
for (var relay in relayParams) {
86+
List<String> commaSeparatedRelays = relay.split(',').map((r) => r.trim()).where((r) => r.isNotEmpty).toList();
87+
for (String relay in commaSeparatedRelays) {
88+
if (!relaysList.contains(relay)) {
89+
relaysList.add(relay);
90+
}
9491
}
9592
}
9693
}
9794

9895
// Check for comma-separated relays in a single parameter
9996
String? relaysParam = parsedUri.queryParameters['relays'];
10097
if (relaysParam != null) {
101-
List<String> commaSeparatedRelays = relaysParam
102-
.split(',')
103-
.map((r) => r.trim())
104-
.where((r) => r.isNotEmpty)
105-
.toList();
98+
List<String> commaSeparatedRelays =
99+
relaysParam.split(',').map((r) => r.trim()).where((r) => r.isNotEmpty).toList();
106100
for (String relay in commaSeparatedRelays) {
107101
if (!relaysList.contains(relay)) {
108102
relaysList.add(relay);

packages/ndk/test/usecases/nwc/nostr_wallet_connect_uri_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ void main() {
3131
]));
3232
expect(nostrUri.secret, equals('secret123'));
3333
});
34+
test('should parse a connection URI with multiple individual relay parameters', () {
35+
final uri =
36+
'nostr+walletconnect://pubkey123?relay=wss://relay1.example.com&relay=wss://relay2.example.com&relay=wss://relay3.example.com&secret=secret123';
37+
final nostrUri = NostrWalletConnectUri.parseConnectionUri(uri);
38+
39+
expect(nostrUri.walletPubkey, equals('pubkey123'));
40+
expect(
41+
nostrUri.relays,
42+
equals([
43+
'wss://relay1.example.com',
44+
'wss://relay2.example.com',
45+
'wss://relay3.example.com'
46+
]));
47+
expect(nostrUri.secret, equals('secret123'));
48+
});
3449

3550
test('should parse a connection URI with mixed relay parameters', () {
3651
final uri =

0 commit comments

Comments
 (0)