add function to create nip98 http authorization header

Closes: https://github.com/damus-io/damus/pull/1471
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Fishcake
2023-08-20 10:09:30 +09:00
committed by William Casarin
parent 1432087edf
commit 9104ddb051
2 changed files with 27 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
//
// Nip98HTTPAuth.swift
// damus
//
// Created by Fishcake on 2023/08/12.
//
import Foundation
func create_nip98_signature (keypair: Keypair, method: String, url: URL) -> String? {
let tags = [
["u", url.standardized.absoluteString], // Ensure that we standardise the URL before extracting string value.
["method", method]
]
guard let ev = NostrEvent(content: "", keypair: keypair, kind: NostrKind.http_auth.rawValue, tags: tags) else {
return nil
}
let json = event_to_json(ev: ev)
let base64Header = base64_encode(Array(json.utf8))
return "Nostr " + base64Header // The returned value should be used in Authorization HTTP header
}