verifyNostrEvent helper

This commit is contained in:
randogoth 2026-07-05 16:49:10 +03:00
parent f69fcb661f
commit 0e70aba755
4 changed files with 80 additions and 17 deletions

View file

@ -0,0 +1,53 @@
import 'dart:convert';
import 'package:swarm/swarm.dart';
import 'package:test/test.dart';
void main() {
// Produces a genuinely-signed event so verifyNostrEvent has a real signature
// to check (signed with the throwaway key below).
const privKey =
'0000000000000000000000000000000000000000000000000000000000000001';
Future<Map<String, dynamic>> signedEvent() async {
final signer = NostrSignerActor(privKey);
final result =
await signer.handle(
const SignEvent(kind: 1985, content: '', tags: [
['L', 'app.xfay.moderation'],
['l', 'hide', 'app.xfay.moderation'],
['e', 'imageeventid'],
]),
)
as EventSigned;
return jsonDecode(result.event) as Map<String, dynamic>;
}
test('accepts a genuinely signed event', () async {
expect(verifyNostrEvent(await signedEvent()), isTrue);
});
test('rejects a tampered content', () async {
final event = await signedEvent();
event['content'] = 'tampered';
expect(verifyNostrEvent(event), isFalse);
});
test('rejects a tampered id', () async {
final event = await signedEvent();
event['id'] = '${event['id']}'.replaceRange(0, 1, '0');
expect(verifyNostrEvent(event), isFalse);
});
test('rejects a forged author (pubkey swapped)', () async {
final event = await signedEvent();
event['pubkey'] =
'0000000000000000000000000000000000000000000000000000000000000002';
expect(verifyNostrEvent(event), isFalse);
});
test('returns false for a malformed map instead of throwing', () {
expect(verifyNostrEvent(const {}), isFalse);
expect(verifyNostrEvent(const {'kind': 1}), isFalse);
});
}