Skip to content

Commit

Permalink
Add AuthenticationEvent.Builder to deprecate equivalent function in E…
Browse files Browse the repository at this point in the history
…ventCreating (#183)
  • Loading branch information
tyiu authored Oct 5, 2024
1 parent 77e59b4 commit 0c89ed9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Sources/NostrSDK/Events/AuthenticationEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthenticationEvent> {
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))
}
}
}
14 changes: 14 additions & 0 deletions Tests/NostrSDKTests/Events/AuthenticationEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0c89ed9

Please sign in to comment.