Add NIP-19 (npub/nsec), NIP-05 verification, and arbitrary-pubkey profile fetch
- nip19.dart: bech32 encodeNpub/encodeNsec for raw keys (no TLV entities), verified against the canonical NIP-19 test vectors. - nip05.dart: verifyNip05 with an injectable http.Client plus a pure nip05MatchesPubkey helper for the .well-known/nostr.json check. - fetchProfileFor(fetcher, pubkey): fetches and parses any user's kind-0 into a typed NostrProfile (newest-wins), generalizing the own-key FetchProfile. - Export all three from swarm.dart; unit tests for each (bech32 vectors, MockClient HTTP cases, fake-relay profile parsing). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
719aaca022
commit
582b0505cb
7 changed files with 376 additions and 0 deletions
89
test/nip05_test.dart
Normal file
89
test/nip05_test.dart
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:swarm/swarm.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
const _pubkey =
|
||||
'7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e';
|
||||
|
||||
void main() {
|
||||
group('nip05MatchesPubkey (pure)', () {
|
||||
test('true when names maps the name to the pubkey', () {
|
||||
final body = jsonEncode({
|
||||
'names': {'alice': _pubkey},
|
||||
});
|
||||
expect(nip05MatchesPubkey(body, 'alice', _pubkey), isTrue);
|
||||
});
|
||||
|
||||
test('case-insensitive on the pubkey', () {
|
||||
final body = jsonEncode({
|
||||
'names': {'alice': _pubkey.toUpperCase()},
|
||||
});
|
||||
expect(nip05MatchesPubkey(body, 'alice', _pubkey), isTrue);
|
||||
});
|
||||
|
||||
test('false on name mismatch, missing name, or junk', () {
|
||||
final body = jsonEncode({
|
||||
'names': {'alice': _pubkey},
|
||||
});
|
||||
expect(nip05MatchesPubkey(body, 'bob', _pubkey), isFalse);
|
||||
expect(
|
||||
nip05MatchesPubkey(jsonEncode({'names': {}}), 'alice', _pubkey),
|
||||
isFalse,
|
||||
);
|
||||
expect(nip05MatchesPubkey('not json', 'alice', _pubkey), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('verifyNip05 (mocked HTTP)', () {
|
||||
test('verifies a well-formed match and queries the right URL', () async {
|
||||
late Uri requested;
|
||||
final client = MockClient((req) async {
|
||||
requested = req.url;
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'names': {'alice': _pubkey},
|
||||
}),
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
final ok = await verifyNip05('alice@example.com', _pubkey,
|
||||
client: client);
|
||||
expect(ok, isTrue);
|
||||
expect(requested.host, equals('example.com'));
|
||||
expect(requested.path, equals('/.well-known/nostr.json'));
|
||||
expect(requested.queryParameters['name'], equals('alice'));
|
||||
});
|
||||
|
||||
test('false on name mismatch', () async {
|
||||
final client = MockClient((req) async => http.Response(
|
||||
jsonEncode({
|
||||
'names': {'alice': 'deadbeef'},
|
||||
}),
|
||||
200,
|
||||
));
|
||||
expect(
|
||||
await verifyNip05('alice@example.com', _pubkey, client: client),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('false on non-200', () async {
|
||||
final client = MockClient((req) async => http.Response('', 404));
|
||||
expect(
|
||||
await verifyNip05('alice@example.com', _pubkey, client: client),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('false on malformed identifier', () async {
|
||||
final client = MockClient((req) async => http.Response('', 200));
|
||||
expect(await verifyNip05('no-at-sign', _pubkey, client: client), isFalse);
|
||||
expect(await verifyNip05('@example.com', _pubkey, client: client),
|
||||
isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue