Skip to content

Commit

Permalink
feat: NIP21
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Jan 20, 2025
1 parent 92e0601 commit 842b064
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ flutter pub add nostr
- [x] [NIP 15 End of Stored Events Notice](https://github.com/nostr-protocol/nips/blob/master/15.md)
- [x] [NIP 19 bech32-encoded entities](https://github.com/nostr-protocol/nips/blob/master/19.md)
- [x] [NIP 20 Command Results](https://github.com/nostr-protocol/nips/blob/master/20.md)
- [x] [NIP 21 nostr: URI scheme](https://github.com/nostr-protocol/nips/blob/master/21.md)
- [x] [NIP 28 Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md)
- [x] [NIP 50 Search Capability](https://github.com/nostr-protocol/nips/blob/master/50.md)
- [x] [NIP 51 Lists](https://github.com/nostr-protocol/nips/blob/master/51.md)
Expand Down
1 change: 1 addition & 0 deletions lib/nostr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export 'src/nips/nip_010.dart';
export 'src/nips/nip_013.dart';
export 'src/nips/nip_019.dart';
export 'src/nips/nip_020.dart';
export 'src/nips/nip_021.dart';
export 'src/nips/nip_028.dart';
export 'src/nips/nip_051.dart';
19 changes: 19 additions & 0 deletions lib/src/nips/nip_021.dart
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;
}
34 changes: 34 additions & 0 deletions test/nips/nip_021_test.dart
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>()));
});
});
}

0 comments on commit 842b064

Please sign in to comment.