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.
Add NIP-19 bech32 identifier generation for note, nevent, and naddr p…
…refixes (nostr-sdk#166) * Add NIP-19 bech32 identifier generation for note, nevent, and naddr prefixes * Apply suggestions from code review Co-authored-by: Bryan Montz <[email protected]> * Fix incomplete sentence in the Bech32IdentifierType documentation * Remove unused bech32NoteIdPrefix constant * Inverse the excludeAuthor and excludeKind parameters in the shareableEventCoordinates function in the ReplaceableEvent protocol for easier readability * Refactor duplicate code for the shareableEventCoordinates function for ReplaceableEvents --------- Co-authored-by: Bryan Montz <[email protected]>
- Loading branch information
1 parent
24a0f9b
commit 74afc55
Showing
14 changed files
with
361 additions
and
25 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,20 @@ | ||
// | ||
// Bech32IdentifierType.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 6/30/24. | ||
// | ||
|
||
/// The type of Bech32-encoded identifier. | ||
/// These identifiers can be used to succinctly encapsulate metadata to aid in the discovery of events and users. | ||
/// See [NIP-19](https://github.com/nostr-protocol/nips/blob/master/19.md) for information about how these | ||
/// identifiers are encoded and used. | ||
public enum Bech32IdentifierType: String { | ||
case publicKey = "npub" | ||
case privateKey = "nsec" | ||
case note = "note" | ||
case profile = "nprofile" | ||
case event = "nevent" | ||
case relay = "nrelay" | ||
case address = "naddr" | ||
} |
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
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
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
77 changes: 77 additions & 0 deletions
77
Tests/NostrSDKTests/Events/NonParameterizedReplaceableEventTests.swift
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,77 @@ | ||
// | ||
// NonParameterizedReplaceableEventTests.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 6/30/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class NonParameterizedReplaceableEventTests: XCTestCase, FixtureLoading, MetadataCoding { | ||
|
||
func testReplaceableEventCoordinates() throws { | ||
let event: MuteListEvent = try decodeFixture(filename: "mute_list") | ||
let publicKey = try XCTUnwrap(PublicKey(hex: "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340")) | ||
let expectedReplaceableEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .muteList, pubkey: publicKey)) | ||
XCTAssertEqual(event.replaceableEventCoordinates(relayURL: nil), expectedReplaceableEventCoordinates) | ||
} | ||
|
||
func testShareableEventCoordinates() throws { | ||
let event: MuteListEvent = try decodeFixture(filename: "mute_list") | ||
let shareableEventCoordinates = try XCTUnwrap(event.shareableEventCoordinates()) | ||
XCTAssertEqual(shareableEventCoordinates, "naddr1qqqqygyeglukt8wcpsmgysptvyh4g3lzsfyejlanwz2spse2tp0tp9mngqpsgqqqyugqp4hw4t") | ||
|
||
let metadata = try XCTUnwrap(decodedMetadata(from: shareableEventCoordinates)) | ||
XCTAssertNil(metadata.eventId) | ||
XCTAssertEqual(metadata.identifier, "") | ||
XCTAssertEqual(metadata.pubkey, event.pubkey) | ||
XCTAssertEqual(metadata.kind, UInt32(event.kind.rawValue)) | ||
XCTAssertEqual(metadata.relays, []) | ||
} | ||
|
||
func testShareableEventCoordinatesWithRelays() throws { | ||
let relay1 = "wss://relay1.com" | ||
let relay2 = "wss://relay2.com" | ||
|
||
let event: MuteListEvent = try decodeFixture(filename: "mute_list") | ||
let shareableEventCoordinates = try XCTUnwrap(event.shareableEventCoordinates(relayURLStrings: [relay1, relay2])) | ||
XCTAssertEqual(shareableEventCoordinates, "naddr1qqqqzyrhwden5te0wfjkcctexyhxxmmdqyg8wumn8ghj7un9d3shjv3wvdhk6q3qn9rljevamqxrdqjq9dsj74z8u2pynxtlkdcf2qxr9fv9avyhwdqqxpqqqqn3qtqh7yd") | ||
|
||
let metadata = try XCTUnwrap(decodedMetadata(from: shareableEventCoordinates)) | ||
XCTAssertNil(metadata.eventId) | ||
XCTAssertEqual(metadata.identifier, "") | ||
XCTAssertEqual(metadata.pubkey, event.pubkey) | ||
XCTAssertEqual(metadata.kind, UInt32(event.kind.rawValue)) | ||
XCTAssertEqual(metadata.relays?.count, 2) | ||
XCTAssertEqual(metadata.relays?[0], relay1) | ||
XCTAssertEqual(metadata.relays?[1], relay2) | ||
} | ||
|
||
func testShareableEventCoordinatesExcludeAuthor() throws { | ||
let event: MuteListEvent = try decodeFixture(filename: "mute_list") | ||
let shareableEventCoordinates = try XCTUnwrap(event.shareableEventCoordinates(includeAuthor: false)) | ||
XCTAssertEqual(shareableEventCoordinates, "naddr1qqqqxpqqqqn3qat5qqg") | ||
|
||
let metadata = try XCTUnwrap(decodedMetadata(from: shareableEventCoordinates)) | ||
XCTAssertNil(metadata.eventId) | ||
XCTAssertEqual(metadata.identifier, "") | ||
XCTAssertNil(metadata.pubkey) | ||
XCTAssertEqual(metadata.kind, UInt32(event.kind.rawValue)) | ||
XCTAssertEqual(metadata.relays, []) | ||
} | ||
|
||
func testShareableEventCoordinatesExcludeKind() throws { | ||
let event: MuteListEvent = try decodeFixture(filename: "mute_list") | ||
let shareableEventCoordinates = try XCTUnwrap(event.shareableEventCoordinates(includeKind: false)) | ||
XCTAssertEqual(shareableEventCoordinates, "naddr1qqqqygyeglukt8wcpsmgysptvyh4g3lzsfyejlanwz2spse2tp0tp9mngq8y2x7g") | ||
|
||
let metadata = try XCTUnwrap(decodedMetadata(from: shareableEventCoordinates)) | ||
XCTAssertNil(metadata.eventId) | ||
XCTAssertEqual(metadata.identifier, "") | ||
XCTAssertEqual(metadata.pubkey, event.pubkey) | ||
XCTAssertNil(metadata.kind) | ||
XCTAssertEqual(metadata.relays, []) | ||
} | ||
|
||
} |
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.