-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nip 98 support for nostr.build #1471
Add nip 98 support for nostr.build #1471
Conversation
f79e14f
to
0782cfb
Compare
I have no idea why the test failed, given that I am using the identical way of creating event as any other function, e.g., "let ev = NostrEvent(content: content, pubkey: pubkey, kind: 7, tags: tags)" |
Ok, submitted a potential fix for the test, but I am not sure if that will help. |
It seems that test expects me to use NostrEventOld which is not even available in master. Please advise. |
20c2e06
to
0782cfb
Compare
On Sat, Aug 12, 2023 at 09:13:38PM +0900, Fishcake wrote:
---
damus/Nostr/Nip98HTTPAuth.swift | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100644 damus/Nostr/Nip98HTTPAuth.swift
diff --git a/damus/Nostr/Nip98HTTPAuth.swift b/damus/Nostr/Nip98HTTPAuth.swift
new file mode 100644
index 000000000..e79e592f0
--- /dev/null
+++ b/damus/Nostr/Nip98HTTPAuth.swift
@@ -0,0 +1,25 @@
+//
+// Nip98HTTPAuth.swift
+// damus
+//
+// Created by Fishcake on 2023/08/12.
+//
+
+import Foundation
+
+func create_nip98_signature (pubkey: String, privkey: String, method: String, url: URL) -> String {
+ let tags = [
+ ["u", url.standardized.absoluteString], // Ensure that we standardise the URL before extracting string value.
+ ["method", method]
+ ]
+ let ev = NostrEvent(content: "",
+ pubkey: pubkey,
+ kind: NostrKind.http_auth.rawValue,
+ tags: tags)
+ ev.calculate_id()
+ ev.sign(privkey: privkey)
This doesn't work anymore, events are now immutable and you need to
calculate ids and sign in the constructor by passing a privkey.
|
0782cfb
to
505d4f0
Compare
@jb55 I fixed the issue, and it fixed the tests too. It seems that my local clone was broken and that's why the original patch was not aligned with the latest changes. Please let me know if I missed anything else. Thanks. |
On Sat, Aug 19, 2023 at 07:54:34AM +0900, Fishcake wrote:
diff --git a/damus/Nostr/Nip98HTTPAuth.swift b/damus/Nostr/Nip98HTTPAuth.swift
new file mode 100644
index 000000000..f8f1163cf
--- /dev/null
+++ b/damus/Nostr/Nip98HTTPAuth.swift
@@ -0,0 +1,27 @@
+//
+// Nip98HTTPAuth.swift
+// damus
+//
+// Created by Fishcake on 2023/08/12.
+//
+
+import Foundation
+
+func create_nip98_signature (keypair: Keypair, method: String, url: URL) -> String {
This should return String? on failure, not empty strings
+ let tags = [
+ ["u", url.standardized.absoluteString], // Ensure that we standardise the URL before extracting string value.
+ ["method", method]
+ ]
+ let ev = NostrEvent(content: "",
+ keypair: keypair,
+ kind: NostrKind.http_auth.rawValue,
+ tags: tags)
+
+ guard let safeEv = ev else {
+ return ""
+ }
nit: you can just do
guard let ev else {
return nil
}
or
guard let ev = NostrEvent(content: "", keypair: keypair, kind: NostrKind.http_auth.rawValue, tags: tags) else {
return nil
}
|
On Sat, Aug 19, 2023 at 07:56:11AM +0900, Fishcake wrote:
---
damus/Views/PostView.swift | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift
index 8c7897087..c5645e9eb 100644
--- a/damus/Views/PostView.swift
+++ b/damus/Views/PostView.swift
@@ -255,7 +255,7 @@ struct PostView: View {
let img = getImage(media: media)
print("img size w:\(img.size.width) h:\(img.size.height)")
async let blurhash = calculate_blurhash(img: img)
- let res = await image_upload.start(media: media, uploader: uploader)
+ let res = await image_upload.start(media: media, uploader: uploader, damus_state: damus_state)
Should be
let res = await image_upload.start(media: media, uploader: uploader, keypair: damus_state?.keypair)
|
On Sat, Aug 19, 2023 at 07:55:19AM +0900, Fishcake wrote:
---
damus/Views/AttachMediaUtility.swift | 32 +++++++++++++++++++++++++---
1 file changed, 29 insertions(+), 3 deletions(-)
diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift
index 5702f05c2..fdf5f2dc7 100644
--- a/damus/Views/AttachMediaUtility.swift
+++ b/damus/Views/AttachMediaUtility.swift
@@ -28,7 +28,7 @@ fileprivate func create_upload_body(mediaData: Data, boundary: String, mediaUplo
return body as Data
}
-func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate) async -> ImageUploadResult {
+func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate, damus_state: DamusState?) async -> ImageUploadResult {
Just pass in keypair instead of DamusState since it looks like we only use that.
var mediaData: Data?
guard let url = URL(string: mediaUploader.postAPI) else {
return .failed(nil)
@@ -39,6 +39,13 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa
let boundary = "Boundary-\(UUID().description)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
+ // If uploading to a media host that support NIP-98 authorization, add the header
+ if mediaUploader == .nostrBuild,
+ let keypair = damus_state?.keypair {
+ let signature =
+ request.setValue(signature, forHTTPHeaderField: "Authorization")
+ }
This should be:
if mediaUploader == .nostrBuild,
let keypair,
let method = request.httpMethod,
let signature = create_nip98_signature(keypair: keypair, method: method, url: url) {
request.setValue(signature, forHTTPHeaderField: "Authorization")
}
|
On Sat, Aug 19, 2023 at 07:58:46AM +0900, Fishcake wrote:
---
damus/Views/Profile/EditPictureControl.swift | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/damus/Views/Profile/EditPictureControl.swift b/damus/Views/Profile/EditPictureControl.swift
index 7abdc5fa8..858385356 100644
--- a/damus/Views/Profile/EditPictureControl.swift
+++ b/damus/Views/Profile/EditPictureControl.swift
@@ -98,7 +98,7 @@ struct EditPictureControl: View {
private func handle_upload(media: MediaUpload) {
uploadObserver.isLoading = true
Task {
- let res = await image_upload.start(media: media, uploader: uploader)
+ let res = await image_upload.start(media: media, uploader: uploader, damus_state: nil)
Patches 3,4,5,6 can all be squashed together. Since patch 3 updates the
function, you would need to update all the callsites in the same patch.
Looking good, getting there.
Thanks!
Will
|
Since keypair is not optional in damus_state, I cannot use 'damus_state?.keypair', otherwise I'll get: Cannot use optional chaining on non-optional value of type 'DamusState' |
505d4f0
to
9641330
Compare
All comments are addressed, and I managed to reduce change by one less file with optional parameter. Please take a look and let me know if anything else is missing. I've tested it, too, so it works on my iPhone. Thank you! |
On Sun, Aug 20, 2023 at 10:09:30AM +0900, Fishcake wrote:
[PATCH 2/3] add function to create nip98 http authorization header
Reviewed-by: William Casarin ***@***.***>
|
On Sun, Aug 20, 2023 at 10:10:24AM +0900, Fishcake wrote:
[PATCH 3/3] use nostr.build api v2 with optional nip98 support
---
damus/Models/ImageUploadModel.swift | 4 ++--
damus/Views/AttachMediaUtility.swift | 34 +++++++++++++++++++++++++---
damus/Views/PostView.swift | 2 +-
3 files changed, 34 insertions(+), 6 deletions(-)
Reviewed-by: William Casarin ***@***.***>
|
Closes: #1471 Signed-off-by: William Casarin <[email protected]>
Closes: #1471 Signed-off-by: William Casarin <[email protected]>
Closes: damus-io#1471 Signed-off-by: William Casarin <[email protected]>
Closes: damus-io#1471 Signed-off-by: William Casarin <[email protected]>
Closes: damus-io#1471 Signed-off-by: William Casarin <[email protected]>
This change implements support for NIP-98 and uses it for uploads to nostr.build. It is also changing the nostr.build API end-point to V2. Implements #823