mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-19 15:51:11 +02:00
refactoring
This commit is contained in:
@@ -4,10 +4,10 @@ import android.util.Log
|
|||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import com.vitorpamplona.amethyst.service.NostrAccountDataSource.account
|
||||||
import com.vitorpamplona.amethyst.service.model.*
|
import com.vitorpamplona.amethyst.service.model.*
|
||||||
import com.vitorpamplona.amethyst.service.relays.Relay
|
import com.vitorpamplona.amethyst.service.relays.Relay
|
||||||
import com.vitorpamplona.amethyst.ui.components.BundledInsert
|
import com.vitorpamplona.amethyst.ui.components.BundledInsert
|
||||||
import com.vitorpamplona.amethyst.ui.dal.NotificationFeedFilter
|
|
||||||
import fr.acinq.secp256k1.Hex
|
import fr.acinq.secp256k1.Hex
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import nostr.postr.toNpub
|
import nostr.postr.toNpub
|
||||||
@@ -595,6 +595,16 @@ object LocalCache {
|
|||||||
fun consume(event: LnZapEvent) {
|
fun consume(event: LnZapEvent) {
|
||||||
val note = getOrCreateNote(event.id)
|
val note = getOrCreateNote(event.id)
|
||||||
|
|
||||||
|
var decryptedContent = LnZapRequestEvent.checkForPrivateZap(event.zapRequest!!, account.loggedIn.privKey!!)
|
||||||
|
if (decryptedContent != null) {
|
||||||
|
Log.e(
|
||||||
|
"DC",
|
||||||
|
"Decrypted Event: Sender: {${decryptedContent.pubKey}}, Message: {${decryptedContent.content}} "
|
||||||
|
|
||||||
|
// TODO Update Notification with this Sender and Message
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
@@ -627,27 +637,6 @@ object LocalCache {
|
|||||||
refreshObservers(note)
|
refreshObservers(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkPrivateZap(zaprequest: Event): Event {
|
|
||||||
var anonTag = zaprequest.tags.firstOrNull { t -> t.count() >= 2 && t[0] == "anon" }
|
|
||||||
if (anonTag != null && anonTag.size > 1) {
|
|
||||||
var encnote = anonTag?.elementAt(1)
|
|
||||||
if (encnote != null && encnote != "") {
|
|
||||||
try {
|
|
||||||
val loggedInUserHex = NotificationFeedFilter.account.loggedIn.privKey!! // Replace without Filter
|
|
||||||
var note = LnZapRequestEvent.decrypt_privatezap_message(encnote, loggedInUserHex, zaprequest.pubKey.toByteArray())
|
|
||||||
var decryptedEvent = Event.fromJson(note)
|
|
||||||
if (decryptedEvent.kind == 9733) {
|
|
||||||
zaprequest.pubKey = decryptedEvent.pubKey
|
|
||||||
zaprequest.content = decryptedEvent.content
|
|
||||||
// return decryptedEvent
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return zaprequest
|
|
||||||
}
|
|
||||||
fun consume(event: LnZapRequestEvent) {
|
fun consume(event: LnZapRequestEvent) {
|
||||||
val note = getOrCreateNote(event.id)
|
val note = getOrCreateNote(event.id)
|
||||||
|
|
||||||
|
@@ -15,11 +15,11 @@ import java.util.*
|
|||||||
|
|
||||||
open class Event(
|
open class Event(
|
||||||
val id: HexKey,
|
val id: HexKey,
|
||||||
@SerializedName("pubkey") var pubKey: HexKey,
|
@SerializedName("pubkey") val pubKey: HexKey,
|
||||||
@SerializedName("created_at") val createdAt: Long,
|
@SerializedName("created_at") val createdAt: Long,
|
||||||
val kind: Int,
|
val kind: Int,
|
||||||
val tags: List<List<String>>,
|
val tags: List<List<String>>,
|
||||||
var content: String,
|
val content: String,
|
||||||
val sig: HexKey
|
val sig: HexKey
|
||||||
) : EventInterface {
|
) : EventInterface {
|
||||||
override fun id(): HexKey = id
|
override fun id(): HexKey = id
|
||||||
|
@@ -55,9 +55,9 @@ class LnZapRequestEvent(
|
|||||||
privkey = Utils.privkeyCreate()
|
privkey = Utils.privkeyCreate()
|
||||||
pubKey = Utils.pubkeyCreate(privkey).toHexKey()
|
pubKey = Utils.pubkeyCreate(privkey).toHexKey()
|
||||||
} else if (zapType == LnZapEvent.ZapType.PRIVATE) {
|
} else if (zapType == LnZapEvent.ZapType.PRIVATE) {
|
||||||
var encryptionprkey = create_private_key(privateKey.toHexKey(), originalNote.id(), createdAt)
|
var encryptionprkey = createPrivateKey(privateKey.toHexKey(), originalNote.id(), createdAt)
|
||||||
var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson()
|
var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson()
|
||||||
var privreq = encrypt_privatezap_message(noteJson, encryptionprkey, originalNote.pubKey().toByteArray())
|
var privreq = encryptPrivateZapMessage(noteJson, encryptionprkey, originalNote.pubKey().toByteArray())
|
||||||
tags = tags + listOf(listOf("anon", privreq))
|
tags = tags + listOf(listOf("anon", privreq))
|
||||||
content = ""
|
content = ""
|
||||||
privkey = encryptionprkey
|
privkey = encryptionprkey
|
||||||
@@ -68,13 +68,13 @@ class LnZapRequestEvent(
|
|||||||
return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey())
|
return LnZapRequestEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun create_private_key(privkey: String, id: String, createdAt: Long): ByteArray {
|
fun createPrivateKey(privkey: String, id: String, createdAt: Long): ByteArray {
|
||||||
var str = privkey + id + createdAt.toString()
|
var str = privkey + id + createdAt.toString()
|
||||||
var strbyte = str.toByteArray(Charset.forName("utf-8"))
|
var strbyte = str.toByteArray(Charset.forName("utf-8"))
|
||||||
return sha256.digest(strbyte)
|
return sha256.digest(strbyte)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun encrypt_privatezap_message(msg: String, privkey: ByteArray, pubkey: ByteArray): String {
|
fun encryptPrivateZapMessage(msg: String, privkey: ByteArray, pubkey: ByteArray): String {
|
||||||
var sharedSecret = Utils.getSharedSecret(privkey, pubkey)
|
var sharedSecret = Utils.getSharedSecret(privkey, pubkey)
|
||||||
val iv = ByteArray(16)
|
val iv = ByteArray(16)
|
||||||
SecureRandom().nextBytes(iv)
|
SecureRandom().nextBytes(iv)
|
||||||
@@ -93,7 +93,7 @@ class LnZapRequestEvent(
|
|||||||
return encryptedMsgBech32 + "_" + ivBech32
|
return encryptedMsgBech32 + "_" + ivBech32
|
||||||
}
|
}
|
||||||
|
|
||||||
fun decrypt_privatezap_message(msg: String, privkey: ByteArray, pubkey: ByteArray): String {
|
fun decryptPrivateZapMessage(msg: String, privkey: ByteArray, pubkey: ByteArray): String {
|
||||||
var sharedSecret = Utils.getSharedSecret(privkey, pubkey)
|
var sharedSecret = Utils.getSharedSecret(privkey, pubkey)
|
||||||
if (sharedSecret.size != 16 && sharedSecret.size != 32) {
|
if (sharedSecret.size != 16 && sharedSecret.size != 32) {
|
||||||
throw IllegalArgumentException("Invalid shared secret size")
|
throw IllegalArgumentException("Invalid shared secret size")
|
||||||
@@ -115,6 +115,25 @@ class LnZapRequestEvent(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun checkForPrivateZap(zaprequest: Event, loggedInUserPrivKey: ByteArray): Event? {
|
||||||
|
var anonTag = zaprequest.tags.firstOrNull { t -> t.count() >= 2 && t[0] == "anon" }
|
||||||
|
if (anonTag != null && anonTag.size > 1) {
|
||||||
|
var encnote = anonTag?.elementAt(1)
|
||||||
|
if (encnote != null && encnote != "") {
|
||||||
|
try {
|
||||||
|
var note = decryptPrivateZapMessage(encnote, loggedInUserPrivKey, zaprequest.pubKey.toByteArray())
|
||||||
|
var decryptedEvent = fromJson(note)
|
||||||
|
if (decryptedEvent.kind == 9733) {
|
||||||
|
return decryptedEvent
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
fun create(
|
fun create(
|
||||||
userHex: String,
|
userHex: String,
|
||||||
relays: Set<String>,
|
relays: Set<String>,
|
||||||
@@ -135,9 +154,9 @@ class LnZapRequestEvent(
|
|||||||
pubKey = Utils.pubkeyCreate(privkey).toHexKey()
|
pubKey = Utils.pubkeyCreate(privkey).toHexKey()
|
||||||
tags = tags + listOf(listOf("anon", ""))
|
tags = tags + listOf(listOf("anon", ""))
|
||||||
} else if (zapType == LnZapEvent.ZapType.PRIVATE) {
|
} else if (zapType == LnZapEvent.ZapType.PRIVATE) {
|
||||||
var enc_prkey = create_private_key(privateKey.toHexKey(), userHex, createdAt)
|
var enc_prkey = createPrivateKey(privateKey.toHexKey(), userHex, createdAt)
|
||||||
var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson()
|
var noteJson = (create(privkey, 9733, listOf(tags[0], tags[1]), message)).toJson()
|
||||||
var privreq = encrypt_privatezap_message(noteJson, enc_prkey, userHex.toByteArray())
|
var privreq = encryptPrivateZapMessage(noteJson, enc_prkey, userHex.toByteArray())
|
||||||
tags = tags + listOf(listOf("anon", privreq))
|
tags = tags + listOf(listOf("anon", privreq))
|
||||||
content = ""
|
content = ""
|
||||||
privkey = enc_prkey
|
privkey = enc_prkey
|
||||||
|
Reference in New Issue
Block a user