-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to create events, sign them, and send them to a relay (#46)
* add ability to create events, sign them, and send them to a relay * fixed: relays rejecting events due to incorrect data type for created_at
- Loading branch information
1 parent
609570e
commit bf0a080
Showing
11 changed files
with
221 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// EventCreating.swift | ||
// | ||
// | ||
// Created by Bryan Montz on 6/25/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol EventCreating {} | ||
public extension EventCreating { | ||
|
||
/// Creates a text note event (kind 1) and signs it with the provided ``Keypair`` | ||
/// - Parameters: | ||
/// - content: The content of the text note | ||
/// - keypair: The Keypair to sign with | ||
/// - Returns: The signed text note event | ||
func textNote(withContent content: String, signedBy keypair: Keypair) throws -> NostrEvent { | ||
try NostrEvent(kind: .textNote, content: content, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// EventSerializer.swift | ||
// | ||
// | ||
// Created by Bryan Montz on 6/25/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum EventSerializer { | ||
|
||
/// Serializes properties of an event. | ||
/// | ||
/// The serialization is done over the UTF-8 JSON-serialized string (with no white space or line breaks) of the following structure: | ||
/// | ||
/// ```json | ||
/// [ | ||
/// 0, | ||
/// <pubkey, as a (lowercase) hex string>, | ||
/// <created_at, as a number>, | ||
/// <kind, as a number>, | ||
/// <tags, as an array of arrays of non-null strings>, | ||
/// <content, as a string> | ||
/// ] | ||
/// ``` | ||
/// | ||
/// See [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md#events-and-signatures). | ||
public static func serializedEvent(withPubkey pubkey: String, createdAt: Int64, kind: Int, tags: [Tag], content: String) -> String { | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = .withoutEscapingSlashes | ||
|
||
let tagsString: String | ||
if let tagsData = try? encoder.encode(tags) { | ||
tagsString = String(decoding: tagsData, as: UTF8.self) | ||
} else { | ||
tagsString = "[]" | ||
} | ||
|
||
let contentString: String | ||
if let contentData = try? encoder.encode(content) { | ||
contentString = String(decoding: contentData, as: UTF8.self) | ||
} else { | ||
contentString = "\"\"" | ||
} | ||
return "[0,\"\(pubkey)\",\(createdAt),\(kind),\(tagsString),\(contentString)]" | ||
} | ||
|
||
/// To obtain the event.id, we SHA256 the serialized event. | ||
public static func identifierForEvent(withPubkey pubkey: String, createdAt: Int64, kind: Int, tags: [Tag], content: String) -> String { | ||
serializedEvent(withPubkey: pubkey, | ||
createdAt: createdAt, | ||
kind: kind, | ||
tags: tags, | ||
content: content).data(using: .utf8)!.sha256.hexString | ||
} | ||
} |
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,30 @@ | ||
// | ||
// EventVerifying.swift | ||
// | ||
// | ||
// Created by Bryan Montz on 6/23/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum EventVerifyingError: Error, CustomStringConvertible { | ||
case invalidId | ||
|
||
public var description: String { | ||
switch self { | ||
case .invalidId: return "The id property did not match the calculated id." | ||
} | ||
} | ||
} | ||
|
||
public protocol EventVerifying: SignatureVerifying {} | ||
public extension EventVerifying { | ||
|
||
/// Verifies the identifier and the signature of a ``NostrEvent`` | ||
func verifyEvent(_ event: NostrEvent) throws { | ||
guard event.id == event.calculatedId else { | ||
throw EventVerifyingError.invalidId | ||
} | ||
try verifySignature(event.signature, for: event.id, withPublicKey: event.pubkey) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// EventCreatingTests.swift | ||
// | ||
// | ||
// Created by Bryan Montz on 6/25/23. | ||
// | ||
|
||
import Foundation | ||
import NostrSDK | ||
import XCTest | ||
|
||
final class EventCreatingTests: XCTestCase, EventCreating, EventVerifying { | ||
|
||
func testCreateSignedTextNote() throws { | ||
let note = try textNote(withContent: "Hello world!", | ||
signedBy: Keypair.test) | ||
|
||
XCTAssertEqual(note.kind, .textNote) | ||
XCTAssertEqual(note.content, "Hello world!") | ||
XCTAssertEqual(note.pubkey, Keypair.test.publicKey.hex) | ||
XCTAssertEqual(note.tags, []) | ||
|
||
try verifyEvent(note) | ||
} | ||
} |
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,19 @@ | ||
// | ||
// Keypair+Test.swift | ||
// | ||
// | ||
// Created by Bryan Montz on 6/23/23. | ||
// | ||
|
||
import Foundation | ||
import NostrSDK | ||
|
||
extension Keypair { | ||
|
||
/// A ``Keypair`` for use in unit tests | ||
/// | ||
/// The corresponding npub is npub1n9rljevamqxrdqjq9dsj74z8u2pynxtlkdcf2qxr9fv9avyhwdqqf6w3at. | ||
static var test: Keypair { | ||
Keypair(nsec: "nsec163p74rxf58ndvav7ck8axx39qmt6dvwjgm8z98ckanenzf3mpjyq6875fz")! | ||
} | ||
} |