commit 39b6dfb47e0aacdb0b2dfa77d203c5a07eea337c
parent 5ca5420ce264e1f224d0ed51e6d1bd4d6e283db6
Author: Daniel D’Aquino <daniel@daquino.me>
Date: Thu, 28 Dec 2023 00:08:07 +0000
purple: update client to work with damus-api post express refactor
these changes were made to adapt the client-side to the new improvements
done to the Purple API server (See
https://github.com/damus-io/api/issues/1)
Testing
-------
PASS
Device: iPhone 15 Pro simulator
iOS: 17.2
Damus: This commit
damus-api: bda56590be7eb47e21dfd61ab94b17f6a8595d0c
Server: Ubuntu 22.04 (VM)
Setup:
1. On the server, delete the `mdb` database files to start from scratch
2. Enable subscriptions support via developer settings with localhost
test mode and restart app
3. Get Damus Purple (Purchase it via Xcode test environment)
4. Start server with mock parameters (Run `npm run dev`)
5. Restart app
Steps
-----
1. Post something
2. Gold star should appear beside your name
3. Look at the server logs. There should be some requests to create the
account (POST), to send the receipt (POST), and to get account
status. Those three requests should have returned HTTP status 200.
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 27 insertions(+), 16 deletions(-)
diff --git 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
@@ -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
@@ -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