-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92e0601
commit 842b064
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// A utility class to handle Nostr URIs according to NIP-21 specification. | ||
/// Provides encode, decode functionalities for Nostr URIs. | ||
class NIP21 { | ||
static const String _prefix = 'nostr:'; | ||
|
||
/// Parses a `nostr:` URI and extracts the identifier. | ||
/// | ||
/// Throws an [Exception] if the prefix `nostr:` is missing | ||
static String decode(String uri) { | ||
if (!uri.startsWith(_prefix)) { | ||
throw Exception('Invalid Nostr URI: must start with "nostr:"'); | ||
} | ||
|
||
return uri.substring(_prefix.length); | ||
} | ||
|
||
/// Generates a `nostr:` URI from a given content | ||
static String encode(String content) => _prefix + content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'package:nostr/nostr.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('NIP21 URI Tests', () { | ||
test('Parse valid npub URI', () { | ||
expect( | ||
NIP21.decode( | ||
'nostr:npub1sn0wdenkukak0d9dfczzeacvhkrgz92ak56egt7vdgzn8pv2wfqqhrjdv9'), | ||
equals( | ||
'npub1sn0wdenkukak0d9dfczzeacvhkrgz92ak56egt7vdgzn8pv2wfqqhrjdv9')); | ||
}); | ||
|
||
test('Parse valid nprofile URI', () { | ||
expect( | ||
NIP21.decode( | ||
'nostr:nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p'), | ||
equals( | ||
'nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p')); | ||
}); | ||
|
||
test('Generate npub URI', () { | ||
expect( | ||
NIP21.encode( | ||
'npub1sn0wdenkukak0d9dfczzeacvhkrgz92ak56egt7vdgzn8pv2wfqqhrjdv9'), | ||
equals( | ||
'nostr:npub1sn0wdenkukak0d9dfczzeacvhkrgz92ak56egt7vdgzn8pv2wfqqhrjdv9')); | ||
}); | ||
|
||
test('Invalid Nostr URI parsing', () { | ||
expect(() => NIP21.decode('noprefix'), throwsA(isA<Exception>())); | ||
}); | ||
}); | ||
} |