From 0c89ed98e4bfc53e28d1752e63a33b670dfb0ce3 Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:19:41 -0400 Subject: [PATCH] Add AuthenticationEvent.Builder to deprecate equivalent function in EventCreating (#183) --- .../NostrSDK/Events/AuthenticationEvent.swift | 31 +++++++++++++++---- .../Events/AuthenticationEventTests.swift | 14 +++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Sources/NostrSDK/Events/AuthenticationEvent.swift b/Sources/NostrSDK/Events/AuthenticationEvent.swift index 964899c..3306ec2 100644 --- a/Sources/NostrSDK/Events/AuthenticationEvent.swift +++ b/Sources/NostrSDK/Events/AuthenticationEvent.swift @@ -52,14 +52,33 @@ public final class AuthenticationEvent: NostrEvent, RelayProviding { public extension EventCreating { + @available(*, deprecated, message: "Deprecated in favor of AuthenticationEvent.Builder.") func authenticate(relayURL: URL, challenge: String, signedBy keypair: Keypair) throws -> AuthenticationEvent { - let validatedRelayURL = try RelayURLValidator.shared.validateRelayURL(relayURL) + try AuthenticationEvent.Builder() + .relayURL(relayURL) + .challenge(challenge) + .build(signedBy: keypair) + } +} + +public extension AuthenticationEvent { + /// Builder of a ``AuthenticationEvent``. + final class Builder: NostrEvent.Builder { + public init() { + super.init(kind: .authentication) + } - let tags: [Tag] = [ - Tag(name: "relay", value: validatedRelayURL.absoluteString), - Tag(name: "challenge", value: challenge) - ] + /// The relay URL this event authenticates to. + @discardableResult + public final func relayURL(_ relayURL: URL) throws -> Self { + let validatedRelayURL = try RelayURLValidator.shared.validateRelayURL(relayURL) + return appendTags(Tag(name: "relay", value: validatedRelayURL.absoluteString)) + } - return try AuthenticationEvent(content: "", tags: tags, signedBy: keypair) + /// The challenge string as received from the relay. + @discardableResult + public final func challenge(_ challenge: String) throws -> Self { + appendTags(Tag(name: "challenge", value: challenge)) + } } } diff --git a/Tests/NostrSDKTests/Events/AuthenticationEventTests.swift b/Tests/NostrSDKTests/Events/AuthenticationEventTests.swift index fc7116d..9195c35 100644 --- a/Tests/NostrSDKTests/Events/AuthenticationEventTests.swift +++ b/Tests/NostrSDKTests/Events/AuthenticationEventTests.swift @@ -11,6 +11,20 @@ import XCTest final class AuthenticationEventTests: XCTestCase, EventCreating, EventVerifying, FixtureLoading { func testCreateAuthenticationEvent() throws { + let relayURL = try XCTUnwrap(URL(string: "wss://relay.example.com/")) + let event = try AuthenticationEvent.Builder() + .relayURL(relayURL) + .challenge("some-challenge-string") + .build(signedBy: Keypair.test) + + XCTAssertEqual(event.kind, .authentication) + XCTAssertEqual(event.relayURL, relayURL) + XCTAssertEqual(event.challenge, "some-challenge-string") + + try verifyEvent(event) + } + + func testCreateAuthenticationEventDeprecated() throws { let relayURL = try XCTUnwrap(URL(string: "wss://relay.example.com/")) let event = try authenticate(relayURL: relayURL, challenge: "some-challenge-string", signedBy: Keypair.test)