toWire static method

WireRequestParts toWire(
  1. SecureMessage msg, {
  2. Map<String, String>? extraHeaders,
})

Serializes a SecureMessage into wire-ready headers/body.

msg The already-built secure message. extraHeaders Optional application headers to merge (e.g., auth, tracing).

  • Keys are treated as case-sensitive for the caller, but we prevent collisions with the reserved protocol keys ("version","window","nonce","ciphertext") regardless of the caller's case.

RETURNS: WireRequestParts with immutable headers map and body string.

Throws InvalidMessageException if extraHeaders attempts to override reserved protocol headers.

Implementation

static WireRequestParts toWire(
  SecureMessage msg, {
  Map<String, String>? extraHeaders,
}) {
  // Start from the protocol headers.
  final proto = msg.toWireHeaders();

  // Merge application headers while preventing collisions.
  final merged = <String, String>{...proto};
  if (extraHeaders != null && extraHeaders.isNotEmpty) {
    for (final entry in extraHeaders.entries) {
      final key = entry.key;
      // If extra header collides with reserved keys (case-insensitive), reject.
      if (_isReserved(key)) {
        throw InvalidMessageException(
          cause: ArgumentError('extraHeaders cannot override reserved key: $key'),
        );
      }
      merged[key] = entry.value;
    }
  }

  return WireRequestParts(
    headers: Map.unmodifiable(merged),
    body: msg.toWireBody(),
  );
}