diff --git a/damus/Models/Purple/DamusPurple.swift b/damus/Models/Purple/DamusPurple.swift index bfe76aee4..ce84996c0 100644 --- a/damus/Models/Purple/DamusPurple.swift +++ b/damus/Models/Purple/DamusPurple.swift @@ -62,17 +62,14 @@ class DamusPurple: StoreObserverDelegate { func create_account(pubkey: Pubkey) async throws { let url = environment.get_base_url().appendingPathComponent("accounts") - let payload: [String: String] = [ - "pubkey": pubkey.hex() - ] - let encoded_payload = try JSONEncoder().encode(payload) Log.info("Creating account with Damus Purple server", for: .damus_purple) let (data, response) = try await make_nip98_authenticated_request( method: .post, url: url, - payload: encoded_payload, + payload: nil, + payload_type: nil, auth_keypair: self.keypair ) @@ -81,7 +78,7 @@ class DamusPurple: StoreObserverDelegate { case 200: Log.info("Created an account with Damus Purple server", for: .damus_purple) default: - Log.error("Error in creating account with Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode) + Log.error("Error in creating account with Damus Purple. HTTP status code: %d; Response: %s", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown") } } @@ -110,6 +107,7 @@ class DamusPurple: StoreObserverDelegate { method: .post, url: url, payload: receiptData, + payload_type: .binary, auth_keypair: self.keypair ) @@ -118,7 +116,7 @@ class DamusPurple: StoreObserverDelegate { case 200: Log.info("Sent in-app purchase receipt to Damus Purple server successfully", for: .damus_purple) default: - Log.error("Error in sending in-app purchase receipt to Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode) + Log.error("Error in sending in-app purchase receipt to Damus Purple. HTTP status code: %d; Response: %s", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown") } } diff --git a/damus/Nostr/NIP98AuthenticatedRequest.swift b/damus/Nostr/NIP98AuthenticatedRequest.swift index 2d613813c..84d2dd680 100644 --- a/damus/Nostr/NIP98AuthenticatedRequest.swift +++ b/damus/Nostr/NIP98AuthenticatedRequest.swift @@ -14,28 +14,41 @@ enum HTTPMethod: String { case delete = "DELETE" } -func make_nip98_authenticated_request(method: HTTPMethod, url: URL, payload: Data, auth_keypair: Keypair) async throws -> (data: Data, response: URLResponse) { +enum HTTPPayloadType: String { + case json = "application/json" + case binary = "application/octet-stream" +} + +func make_nip98_authenticated_request(method: HTTPMethod, url: URL, payload: Data?, payload_type: HTTPPayloadType?, auth_keypair: Keypair) async throws -> (data: Data, response: URLResponse) { var request = URLRequest(url: url) request.httpMethod = method.rawValue request.httpBody = payload - let payload_hash = sha256(payload) - let payload_hash_hex = payload_hash.map({ String(format: "%02hhx", $0) }).joined() + var tag_pairs = [ + ["u", url.absoluteString], + ["method", method.rawValue], + ] + if let payload { + let payload_hash = sha256(payload) + let payload_hash_hex = hex_encode(payload_hash) + tag_pairs.append(["payload", payload_hash_hex]) + } + let auth_note = NdbNote( content: "", keypair: auth_keypair, kind: 27235, - tags: [ - ["u", url.absoluteString], - ["method", method.rawValue], - ["payload", payload_hash_hex] - ], + tags: tag_pairs, createdAt: UInt32(Date().timeIntervalSince1970) ) + let auth_note_json_data: Data = try JSONEncoder().encode(auth_note) let auth_note_base64: String = base64_encode(auth_note_json_data.bytes) request.setValue("Nostr " + auth_note_base64, forHTTPHeaderField: "Authorization") + if let payload_type { + request.setValue(payload_type.rawValue, forHTTPHeaderField: "Content-Type") + } return try await URLSession.shared.data(for: request) } diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift index 1eb5f376e..1fe7d8b39 100644 --- a/damus/Util/Constants.swift +++ b/damus/Util/Constants.swift @@ -9,7 +9,7 @@ import Foundation class Constants { static let PURPLE_API_PRODUCTION_BASE_URL: URL = URL(string: "https://purple.damus.io")! - static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "http://localhost:8989")! + static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "http://127.0.0.1:8989")! static let DEVICE_TOKEN_RECEIVER_PRODUCTION_URL: URL = URL(string: "https://notify.damus.io:8000/user-info")! static let DEVICE_TOKEN_RECEIVER_TEST_URL: URL = URL(string: "http://localhost:8000/user-info")! static let EXAMPLE_DEMOS: DamusState = .empty