nip42: add initial relay auth support

Lightning-Invoice: lnbc1pjcpaakpp5gjs4f626hf8w6slx84xz3wwhlf309z503rjutckdxv6wwg5ldavsdqqcqzpgxqrrs0fppqjaxxw43p7em4g59vedv7pzl76kt0qyjfsp5qcp9de7a7t8h6zs5mcssfaqp4exrnkehqtg2hf0ary3z5cjnasvs9qyyssq55523e4h3cazhkv7f8jqf5qp0n8spykls49crsu5t3m636u3yj4qdqjkdl2nxf6jet5t2r2pfrxmm8rjpqjd3ylrzqq89m4gqt5l6ycqf92c7h
Closes: https://github.com/damus-io/damus/issues/940
Signed-off-by: Charlie Fish <contact@charlie.fish>
Signed-off-by: William Casarin <jb55@jb55.com>
Changelog-Added: Add NIP-42 relay auth support
This commit is contained in:
Charlie Fish
2023-12-24 14:22:25 -07:00
committed by William Casarin
parent 4c37bfc128
commit 84cfeb1604
29 changed files with 484 additions and 25 deletions

View File

@@ -16,7 +16,34 @@ final class RequestTests: XCTestCase {
let expectedResult = "[\"CLOSE\",\"64FD064D-EB9E-4771-8255-8D16981B920B\"]"
XCTAssertEqual(result, expectedResult)
}
func testMakeAuthRequest() {
let challenge_string = "8bc847dd-f2f6-4b3a-9c8a-71776ad9b071"
let url = RelayURL("wss://example.com")!
let relayInfo = RelayInfo(read: true, write: true)
let relayDescriptor = RelayDescriptor(url: url, info: relayInfo)
let relayConnection = RelayConnection(url: url) { _ in
} processEvent: { _ in
}
let relay = Relay(descriptor: relayDescriptor, connection: relayConnection)
let event = make_auth_request(keypair: FullKeypair.init(pubkey: Pubkey.empty, privkey: Privkey.empty), challenge_string: challenge_string, relay: relay)!
let result = make_nostr_auth_event(ev: event)
let json = try! JSONSerialization.jsonObject(with: result!.data(using: .utf8)!, options: []) as! [Any]
XCTAssertEqual(json[0] as! String, "AUTH")
let dictionary = json[1] as! [String: Any]
XCTAssertEqual(dictionary["content"] as! String, "")
XCTAssertEqual(dictionary["kind"] as! Int, 22242)
XCTAssertEqual(dictionary["sig"] as! String, String(repeating: "0", count: 128))
XCTAssertEqual(dictionary["pubkey"] as! String, String(repeating: "0", count: 64))
let tags = dictionary["tags"] as! [[String]]
XCTAssertEqual(tags.first { $0[0] == "relay" }![1], "wss://example.com")
XCTAssertEqual(tags.first { $0[0] == "challenge" }![1], challenge_string)
XCTAssertEqual(dictionary["id"] as! String, String(repeating: "0", count: 64))
}
/* FIXME: these tests depend on order of json fields which is undefined
func testMakePushEvent() {
let now = Int64(Date().timeIntervalSince1970)