sendLegacyNip04Message method

Future<void> sendLegacyNip04Message({
  1. required String recipientPubKey,
  2. required String content,
  3. required Iterable<String> rendezvousRelays,
})

Sends a legacy NIP-04 text DM through explicitly supplied rendezvous relays.

This is intentionally separate from sendMessage: callers must make a conscious privacy downgrade and choose relays both parties can use. NIP-04 exposes the sender, recipient, timestamp and kind to those relays, has no sender-history copy, and only carries text. Do not use it for files, credentials, invoices, preimages, or commands that change application state.

Implementation

Future<void> sendLegacyNip04Message({
  required String recipientPubKey,
  required String content,
  required Iterable<String> rendezvousRelays,
}) async {
  final account = _accounts.getLoggedAccount();
  if (account == null || !account.signer.canSign()) {
    throw Exception('NIP-04 requires a logged-in signing account.');
  }
  if (!_hexPubKey.hasMatch(recipientPubKey)) {
    throw ArgumentError.value(recipientPubKey, 'recipientPubKey');
  }
  if (content.trim().isEmpty) {
    throw ArgumentError.value(content, 'content');
  }
  final relays = _cleanExplicitRelays(rendezvousRelays);
  if (relays.isEmpty) {
    throw ArgumentError.value(
      rendezvousRelays,
      'rendezvousRelays',
      'At least one explicit rendezvous relay is required',
    );
  }

  // ignore: deprecated_member_use
  final encrypted = await account.signer.encrypt(content, recipientPubKey);
  if (encrypted == null || encrypted.isEmpty) {
    throw StateError('The signer did not produce a NIP-04 ciphertext.');
  }
  final event = Nip01Event(
    pubKey: account.pubkey,
    kind: kLegacyNip04MessageKind,
    createdAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
    content: encrypted,
    tags: [
      ['p', recipientPubKey],
    ],
  );
  await _broadcast
      .broadcast(nostrEvent: event, specificRelays: relays)
      .broadcastDoneFuture;
}