Moving the NIP98 event to it's own class.

This commit is contained in:
Vitor Pamplona 2023-06-23 13:40:57 -04:00
parent 976d96ef35
commit 5b2a878f5d
3 changed files with 58 additions and 7 deletions

View File

@ -24,6 +24,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import nostr.postr.Persona
import nostr.postr.Utils
import retrofit2.http.HTTP
import java.math.BigDecimal
import java.net.Proxy
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) {
if (!isWriteable()) return

View File

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

View File

@ -11,6 +11,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.model.HTTPAuthorizationEvent
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okio.BufferedSink
@ -142,13 +143,7 @@ object ImageUploader {
}
fun NIP98Header(url: String, method: String, body: String): String {
var hash = ""
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 noteJson = account.createHTTPAuthorization(url, method, body)?.toJson() ?: ""
val encodedNIP98Event: String = Base64.getEncoder().encodeToString(noteJson.toByteArray())
return "Nostr " + encodedNIP98Event
}