forked from nostr-sdk/nostr-sdk-ios
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relay request and response additions (nostr-sdk#151)
* Reorder relay request and relay response enums to match the listed ordering in the specs for easier maintainability * Fix RelayResponse to not drop human readable messages for OK messages * Add CLOSED relay response * Add AUTH relay request and response * Apply suggestions from code review Co-authored-by: Bryan Montz <[email protected]> * Shorten struct names * Change RelayResponseDecodingTest to use XCTUnwrap instead of if let to make the test code cleaner --------- Co-authored-by: Bryan Montz <[email protected]>
- Loading branch information
1 parent
963a5af
commit 290e886
Showing
13 changed files
with
297 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// AuthenticationEvent.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 5/1/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An event that provides a way for clients to authenticate to relays. | ||
/// This kind is not meant to be published or queried. | ||
/// | ||
/// See [NIP-42](https://github.com/nostr-protocol/nips/blob/master/42.md). | ||
public final class AuthenticationEvent: NostrEvent, RelayProviding, RelayURLValidating { | ||
public required init(from decoder: Decoder) throws { | ||
try super.init(from: decoder) | ||
} | ||
|
||
@available(*, unavailable, message: "This initializer is unavailable for this class.") | ||
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws { | ||
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair) | ||
} | ||
|
||
init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws { | ||
try super.init(kind: .authentication, content: content, tags: tags, createdAt: createdAt, signedBy: keypair) | ||
} | ||
|
||
/// The relay URL this event authenticates to. | ||
public var relayURL: URL? { | ||
guard let relayURLString = firstValueForRawTagName("relay") else { | ||
return nil | ||
} | ||
|
||
return try? validateRelayURLString(relayURLString) | ||
} | ||
|
||
/// The challenge string as received from the relay. | ||
public var challenge: String? { | ||
firstValueForRawTagName("challenge") | ||
} | ||
} | ||
|
||
public extension EventCreating { | ||
|
||
func authenticate(relayURL: URL, challenge: String, signedBy keypair: Keypair) throws -> AuthenticationEvent { | ||
let validatedRelayURL = try RelayURLValidator.shared.validateRelayURL(relayURL) | ||
|
||
let tags: [Tag] = [ | ||
Tag(name: "relay", value: validatedRelayURL.absoluteString), | ||
Tag(name: "challenge", value: challenge) | ||
] | ||
|
||
return try AuthenticationEvent(content: "", tags: tags, signedBy: keypair) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// AuthenticationEventTests.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 5/2/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
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 authenticate(relayURL: relayURL, challenge: "some-challenge-string", signedBy: Keypair.test) | ||
|
||
XCTAssertEqual(event.kind, .authentication) | ||
XCTAssertEqual(event.relayURL, relayURL) | ||
XCTAssertEqual(event.challenge, "some-challenge-string") | ||
|
||
try verifyEvent(event) | ||
} | ||
|
||
func testDecodeAuthenticationEvent() throws { | ||
let event: AuthenticationEvent = try decodeFixture(filename: "authentication_event") | ||
|
||
XCTAssertEqual(event.id, "adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246") | ||
XCTAssertEqual(event.pubkey, "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340") | ||
XCTAssertEqual(event.createdAt, 1714625219) | ||
XCTAssertEqual(event.kind, .authentication) | ||
|
||
let expectedTags: [Tag] = [ | ||
Tag(name: "relay", value: "wss://relay.example.com/"), | ||
Tag(name: "challenge", value: "some-challenge-string") | ||
] | ||
XCTAssertEqual(event.tags, expectedTags) | ||
XCTAssertEqual(event.content, "") | ||
XCTAssertEqual(event.signature, "b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["AUTH",{"id":"adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246","pubkey":"9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340","created_at":1714625219,"kind":22242,"tags":[["relay","wss://relay.example.com/"],["challenge","some-challenge-string"]],"content":"","sig":"b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"id": "adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246", | ||
"pubkey": "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340", | ||
"created_at": 1714625219, | ||
"kind": 22242, | ||
"tags": [ | ||
["relay", "wss://relay.example.com/"], | ||
["challenge", "some-challenge-string"] | ||
], | ||
"content": "", | ||
"sig": "b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["CLOSED", "some-subscription-id", "error: shutting down idle subscription"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
["OK", "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30", true, "pow: difficulty 25>=24"] | ||
["OK", "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30", true, "pow: difficulty: 25>=24"] |
1 change: 1 addition & 0 deletions
1
Tests/NostrSDKTests/Fixtures/ok_success_reason_prefix_no_message.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["OK", "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30", true, "pow:"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["OK", "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30", true, "unknown: reason: unknown"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.