swarm/test/nip19_test.dart
randogoth 582b0505cb 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>
2026-06-24 11:34:35 +03:00

41 lines
1.2 KiB
Dart

import 'package:swarm/swarm.dart';
import 'package:test/test.dart';
void main() {
group('NIP-19 encoding', () {
// Canonical test vectors from the NIP-19 specification.
test('encodeNpub matches the spec vector', () {
expect(
encodeNpub(
'7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e',
),
equals(
'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg',
),
);
});
test('encodeNsec matches the spec vector', () {
expect(
encodeNsec(
'67dea2ed018072d675f5415ecfaed7d2597555e202d85b3d65ea4e58d2d92ffa',
),
equals(
'nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5',
),
);
});
test('npub/nsec carry the right human-readable prefix', () {
const hex =
'0000000000000000000000000000000000000000000000000000000000000001';
expect(encodeNpub(hex), startsWith('npub1'));
expect(encodeNsec(hex), startsWith('nsec1'));
});
test('rejects wrong-length hex', () {
expect(() => encodeNpub('abcd'), throwsArgumentError);
expect(() => encodeNsec(''), throwsArgumentError);
});
});
}