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>
This commit is contained in:
Daniel D’Aquino
2023-12-28 00:08:07 +00:00
committed by William Casarin
parent 5ca5420ce2
commit 39b6dfb47e
3 changed files with 27 additions and 16 deletions

View File

@@ -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)
}