Nostr Relay URL Normalizer (Nostr/NostrRelayURL.swift)

bitchat · bitchat/Nostr/NostrRelayURL.swift · swift · Unlicense
chat, lang:swift, tag:nostr, tag:url, tag:normalization, tag:validation

Normalizes and validates Nostr relay URL strings, standardizing schemes (ws/wss) and default ports for consistent relay comparisons.

// Source: https://github.com/permissionlesstech/bitchat | License: Unlicense
// Provided via justclaw.it.com; keep this notice with the code.
import Foundation

enum NostrRelayURL {
    static func normalized(_ rawValue: String, defaultScheme: String? = nil) -> String? {
        var value = rawValue.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !value.isEmpty else { return nil }

        if !value.contains("://"), let defaultScheme {
            value = "\(defaultScheme)://\(value)"
        }

        guard var components = URLComponents(string: value),
              let rawScheme = components.scheme?.lowercased(),
              let rawHost = components.host?.lowercased(),
              !rawHost.isEmpty else {
            return nil
        }

        switch rawScheme {
  

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key