Moving the NIP98 event to it's own class.
This commit is contained in:
@@ -24,6 +24,7 @@ import kotlinx.coroutines.GlobalScope
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import nostr.postr.Persona
|
import nostr.postr.Persona
|
||||||
import nostr.postr.Utils
|
import nostr.postr.Utils
|
||||||
|
import retrofit2.http.HTTP
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
@@ -322,6 +323,12 @@ class Account(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createHTTPAuthorization(url: String, method: String, body: String? = null): HTTPAuthorizationEvent? {
|
||||||
|
if (!isWriteable()) return null
|
||||||
|
|
||||||
|
return HTTPAuthorizationEvent.create(url, method, body, loggedIn.privKey!!)
|
||||||
|
}
|
||||||
|
|
||||||
fun boost(note: Note) {
|
fun boost(note: Note) {
|
||||||
if (!isWriteable()) return
|
if (!isWriteable()) return
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.vitorpamplona.amethyst.service.model
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Immutable
|
||||||
|
import com.vitorpamplona.amethyst.model.HexKey
|
||||||
|
import com.vitorpamplona.amethyst.model.toHexKey
|
||||||
|
import nostr.postr.Utils
|
||||||
|
import java.security.MessageDigest
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
class HTTPAuthorizationEvent(
|
||||||
|
id: HexKey,
|
||||||
|
pubKey: HexKey,
|
||||||
|
createdAt: Long,
|
||||||
|
tags: List<List<String>>,
|
||||||
|
content: String,
|
||||||
|
sig: HexKey
|
||||||
|
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val kind = 27235
|
||||||
|
|
||||||
|
fun create(
|
||||||
|
url: String,
|
||||||
|
method: String,
|
||||||
|
body: String? = null,
|
||||||
|
privateKey: ByteArray,
|
||||||
|
createdAt: Long = Date().time / 1000
|
||||||
|
): HTTPAuthorizationEvent {
|
||||||
|
val sha256 = MessageDigest.getInstance("SHA-256")
|
||||||
|
|
||||||
|
var hash = ""
|
||||||
|
body?.let {
|
||||||
|
hash = sha256.digest(it.toByteArray()).toHexKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
val tags = listOfNotNull(
|
||||||
|
listOf("u", url),
|
||||||
|
listOf("method", method),
|
||||||
|
listOf("payload", hash)
|
||||||
|
)
|
||||||
|
|
||||||
|
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
|
||||||
|
val id = generateId(pubKey, createdAt, kind, tags, "")
|
||||||
|
val sig = Utils.sign(id, privateKey)
|
||||||
|
return HTTPAuthorizationEvent(id.toHexKey(), pubKey, createdAt, tags, "", sig.toHexKey())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import com.vitorpamplona.amethyst.model.Account
|
|||||||
import com.vitorpamplona.amethyst.model.toHexKey
|
import com.vitorpamplona.amethyst.model.toHexKey
|
||||||
import com.vitorpamplona.amethyst.service.HttpClient
|
import com.vitorpamplona.amethyst.service.HttpClient
|
||||||
import com.vitorpamplona.amethyst.service.model.Event
|
import com.vitorpamplona.amethyst.service.model.Event
|
||||||
|
import com.vitorpamplona.amethyst.service.model.HTTPAuthorizationEvent
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okio.BufferedSink
|
import okio.BufferedSink
|
||||||
@@ -142,13 +143,7 @@ object ImageUploader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun NIP98Header(url: String, method: String, body: String): String {
|
fun NIP98Header(url: String, method: String, body: String): String {
|
||||||
var hash = ""
|
val noteJson = account.createHTTPAuthorization(url, method, body)?.toJson() ?: ""
|
||||||
if (body != "") {
|
|
||||||
val sha256 = MessageDigest.getInstance("SHA-256")
|
|
||||||
hash = sha256.digest(body.toByteArray()).toHexKey()
|
|
||||||
}
|
|
||||||
var tags = listOf(listOf("u", url), listOf("method", method), listOf("payload", hash))
|
|
||||||
var noteJson = (Event.create(account.loggedIn.privKey!!, 27235, tags, "")).toJson()
|
|
||||||
val encodedNIP98Event: String = Base64.getEncoder().encodeToString(noteJson.toByteArray())
|
val encodedNIP98Event: String = Base64.getEncoder().encodeToString(noteJson.toByteArray())
|
||||||
return "Nostr " + encodedNIP98Event
|
return "Nostr " + encodedNIP98Event
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user