Files
buzz/mobile/lib/shared/relay/relay_session_auth.dart
2026-10-20 10:15:58 -07:00

38 lines
1.1 KiB
Dart

part of 'relay_session.dart';
/// Pass the signer's hex [pubkey] when it is already known: deriving it from
/// the key costs about a third of a signature, on the UI isolate.
String buildNip98AuthHeader({
required String method,
required String url,
required List<int> bodyBytes,
required String? nsec,
String? pubkey,
}) {
if (nsec == null || nsec.isEmpty) {
throw Exception('Cannot query relay: no signing key available');
}
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
if (privkeyHex.isEmpty) {
throw Exception('Invalid nsec');
}
final payloadHash = SHA256Digest()
.process(Uint8List.fromList(bodyBytes))
.map((byte) => byte.toRadixString(16).padLeft(2, '0'))
.join();
final event = signEvent(
kind: 27235,
content: '',
tags: [
['u', url],
['method', method.toUpperCase()],
['payload', payloadHash],
['nonce', const Uuid().v4()],
],
secretKey: privkeyHex,
pubkey: pubkey,
verify: false,
);
return 'Nostr ${base64.encode(utf8.encode(event.toJson()))}';
}