Renames Hex.toByteArray() to avoid confusing with String.toByteArray()

This commit is contained in:
Vitor Pamplona
2023-05-06 09:24:39 -04:00
parent e3ccf71a84
commit f3316f53ea
10 changed files with 29 additions and 29 deletions
@@ -9,7 +9,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.GLOBAL_FOLLOWS import com.vitorpamplona.amethyst.model.GLOBAL_FOLLOWS
import com.vitorpamplona.amethyst.model.KIND3_FOLLOWS import com.vitorpamplona.amethyst.model.KIND3_FOLLOWS
import com.vitorpamplona.amethyst.model.RelaySetupInfo import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.service.model.ContactListEvent import com.vitorpamplona.amethyst.service.model.ContactListEvent
import com.vitorpamplona.amethyst.service.model.Event import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.model.Event.Companion.getRefinedEvent import com.vitorpamplona.amethyst.service.model.Event.Companion.getRefinedEvent
@@ -276,7 +276,7 @@ object LocalPreferences {
val hideBlockAlertDialog = getBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, false) val hideBlockAlertDialog = getBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, false)
val a = Account( val a = Account(
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()), Persona(privKey = privKey?.hexToByteArray(), pubKey = pubKey.hexToByteArray()),
followingChannels, followingChannels,
hiddenUsers, hiddenUsers,
localRelays, localRelays,
@@ -178,7 +178,7 @@ class Account(
} }
fun isNIP47Author(pubkeyHex: String?): Boolean { fun isNIP47Author(pubkeyHex: String?): Boolean {
val privKey = zapPaymentRequest?.secret?.toByteArray() ?: loggedIn.privKey val privKey = zapPaymentRequest?.secret?.hexToByteArray() ?: loggedIn.privKey
if (privKey == null) return false if (privKey == null) return false
@@ -189,8 +189,8 @@ class Account(
fun decryptZapPaymentResponseEvent(zapResponseEvent: LnZapPaymentResponseEvent): Response? { fun decryptZapPaymentResponseEvent(zapResponseEvent: LnZapPaymentResponseEvent): Response? {
val myNip47 = zapPaymentRequest ?: return null val myNip47 = zapPaymentRequest ?: return null
val privKey = myNip47.secret?.toByteArray() ?: loggedIn.privKey val privKey = myNip47.secret?.hexToByteArray() ?: loggedIn.privKey
val pubKey = myNip47.pubKeyHex.toByteArray() val pubKey = myNip47.pubKeyHex.hexToByteArray()
if (privKey == null) return null if (privKey == null) return null
@@ -202,8 +202,8 @@ class Account(
} }
fun calculateZappedAmount(zappedNote: Note?): BigDecimal { fun calculateZappedAmount(zappedNote: Note?): BigDecimal {
val privKey = zapPaymentRequest?.secret?.toByteArray() ?: loggedIn.privKey val privKey = zapPaymentRequest?.secret?.hexToByteArray() ?: loggedIn.privKey
val pubKey = zapPaymentRequest?.pubKeyHex?.toByteArray() val pubKey = zapPaymentRequest?.pubKeyHex?.hexToByteArray()
return zappedNote?.zappedAmount(privKey, pubKey) ?: BigDecimal.ZERO return zappedNote?.zappedAmount(privKey, pubKey) ?: BigDecimal.ZERO
} }
@@ -211,16 +211,16 @@ class Account(
if (!isWriteable()) return if (!isWriteable()) return
zapPaymentRequest?.let { nip47 -> zapPaymentRequest?.let { nip47 ->
val event = LnZapPaymentRequestEvent.create(bolt11, nip47.pubKeyHex, nip47.secret?.toByteArray() ?: loggedIn.privKey!!) val event = LnZapPaymentRequestEvent.create(bolt11, nip47.pubKeyHex, nip47.secret?.hexToByteArray() ?: loggedIn.privKey!!)
val wcListener = NostrLnZapPaymentResponseDataSource(nip47.pubKeyHex, event.pubKey, event.id) val wcListener = NostrLnZapPaymentResponseDataSource(nip47.pubKeyHex, event.pubKey, event.id)
wcListener.start() wcListener.start()
LocalCache.consume(event, zappedNote) { LocalCache.consume(event, zappedNote) {
// After the response is received. // After the response is received.
val privKey = nip47.secret?.toByteArray() val privKey = nip47.secret?.hexToByteArray()
if (privKey != null) { if (privKey != null) {
onResponse(it.response(privKey, nip47.pubKeyHex.toByteArray())) onResponse(it.response(privKey, nip47.pubKeyHex.hexToByteArray()))
} }
} }
@@ -826,7 +826,7 @@ class Account(
pubkeyToUse = recepientPK pubkeyToUse = recepientPK
} }
event.plainContent(loggedIn.privKey!!, pubkeyToUse.toByteArray()) event.plainContent(loggedIn.privKey!!, pubkeyToUse.hexToByteArray())
} else if (event is LnZapRequestEvent && loggedIn.privKey != null) { } else if (event is LnZapRequestEvent && loggedIn.privKey != null) {
decryptZapContentAuthor(note)?.content() decryptZapContentAuthor(note)?.content()
} else { } else {
@@ -29,7 +29,7 @@ fun ByteArray.toHexKey(): HexKey {
return toHex() return toHex()
} }
fun HexKey.toByteArray(): ByteArray { fun HexKey.hexToByteArray(): ByteArray {
return Hex.decode(this) return Hex.decode(this)
} }
@@ -39,7 +39,7 @@ fun HexKey.toDisplayHexKey(): String {
fun decodePublicKey(key: String): ByteArray { fun decodePublicKey(key: String): ByteArray {
val parsed = Nip19.uriToRoute(key) val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray() val pubKeyParsed = parsed?.hex?.hexToByteArray()
return if (key.startsWith("nsec")) { return if (key.startsWith("nsec")) {
Persona(privKey = key.bechToBytes()).pubKey Persona(privKey = key.bechToBytes()).pubKey
@@ -1,7 +1,7 @@
package com.vitorpamplona.amethyst.service.model package com.vitorpamplona.amethyst.service.model
import android.util.Log import android.util.Log
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.service.nip19.Tlv import com.vitorpamplona.amethyst.service.nip19.Tlv
import fr.acinq.secp256k1.Hex import fr.acinq.secp256k1.Hex
@@ -14,7 +14,7 @@ data class ATag(val kind: Int, val pubKeyHex: String, val dTag: String, val rela
fun toNAddr(): String { fun toNAddr(): String {
val kind = kind.toByteArray() val kind = kind.toByteArray()
val author = pubKeyHex.toByteArray() val author = pubKeyHex.hexToByteArray()
val dTag = dTag.toByteArray(Charsets.UTF_8) val dTag = dTag.toByteArray(Charsets.UTF_8)
val relay = relay?.toByteArray(Charsets.UTF_8) val relay = relay?.toByteArray(Charsets.UTF_8)
@@ -3,7 +3,7 @@ package com.vitorpamplona.amethyst.service.model
import android.util.Log import android.util.Log
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import nostr.postr.Utils import nostr.postr.Utils
abstract class GeneralListEvent( abstract class GeneralListEvent(
@@ -24,7 +24,7 @@ abstract class GeneralListEvent(
fun plainContent(privKey: ByteArray): String? { fun plainContent(privKey: ByteArray): String? {
return try { return try {
val sharedSecret = Utils.getSharedSecret(privKey, pubKey.toByteArray()) val sharedSecret = Utils.getSharedSecret(privKey, pubKey.hexToByteArray())
return Utils.decrypt(content, sharedSecret) return Utils.decrypt(content, sharedSecret)
} catch (e: Exception) { } catch (e: Exception) {
@@ -6,7 +6,7 @@ import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement import com.google.gson.JsonElement
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Utils import nostr.postr.Utils
import java.lang.reflect.Type import java.lang.reflect.Type
@@ -53,7 +53,7 @@ class LnZapPaymentRequestEvent(
val content = Utils.encrypt( val content = Utils.encrypt(
serializedRequest, serializedRequest,
privateKey, privateKey,
walletServicePubkey.toByteArray() walletServicePubkey.hexToByteArray()
) )
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
@@ -59,7 +59,7 @@ class LnZapRequestEvent(
} else if (zapType == LnZapEvent.ZapType.PRIVATE) { } else if (zapType == LnZapEvent.ZapType.PRIVATE) {
var encryptionPrivateKey = createEncryptionPrivateKey(privateKey.toHexKey(), originalNote.id(), createdAt) var encryptionPrivateKey = createEncryptionPrivateKey(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 encryptedContent = encryptPrivateZapMessage(noteJson, encryptionPrivateKey, originalNote.pubKey().toByteArray()) var encryptedContent = encryptPrivateZapMessage(noteJson, encryptionPrivateKey, originalNote.pubKey().hexToByteArray())
tags = tags + listOf(listOf("anon", encryptedContent)) tags = tags + listOf(listOf("anon", encryptedContent))
content = "" // make sure public content is empty, as the content is encrypted content = "" // make sure public content is empty, as the content is encrypted
privkey = encryptionPrivateKey // sign event with generated privkey privkey = encryptionPrivateKey // sign event with generated privkey
@@ -92,7 +92,7 @@ class LnZapRequestEvent(
} else if (zapType == LnZapEvent.ZapType.PRIVATE) { } else if (zapType == LnZapEvent.ZapType.PRIVATE) {
var encryptionPrivateKey = createEncryptionPrivateKey(privateKey.toHexKey(), userHex, createdAt) var encryptionPrivateKey = createEncryptionPrivateKey(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 encryptedContent = encryptPrivateZapMessage(noteJson, encryptionPrivateKey, userHex.toByteArray()) var encryptedContent = encryptPrivateZapMessage(noteJson, encryptionPrivateKey, userHex.hexToByteArray())
tags = tags + listOf(listOf("anon", encryptedContent)) tags = tags + listOf(listOf("anon", encryptedContent))
content = "" content = ""
privkey = encryptionPrivateKey privkey = encryptionPrivateKey
@@ -157,7 +157,7 @@ class LnZapRequestEvent(
val encnote = anonTag[1] val encnote = anonTag[1]
if (encnote.isNotBlank()) { if (encnote.isNotBlank()) {
try { try {
val note = decryptPrivateZapMessage(encnote, loggedInUserPrivKey, pubKey.toByteArray()) val note = decryptPrivateZapMessage(encnote, loggedInUserPrivKey, pubKey.hexToByteArray())
val decryptedEvent = fromJson(note) val decryptedEvent = fromJson(note)
if (decryptedEvent.kind == 9733) { if (decryptedEvent.kind == 9733) {
return decryptedEvent return decryptedEvent
@@ -3,7 +3,7 @@ package com.vitorpamplona.amethyst.service.model
import android.util.Log import android.util.Log
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Utils import nostr.postr.Utils
import java.util.Date import java.util.Date
@@ -21,7 +21,7 @@ class MuteListEvent(
fun plainContent(privKey: ByteArray): String? { fun plainContent(privKey: ByteArray): String? {
return try { return try {
val sharedSecret = Utils.getSharedSecret(privKey, pubKey.toByteArray()) val sharedSecret = Utils.getSharedSecret(privKey, pubKey.hexToByteArray())
return Utils.decrypt(content, sharedSecret) return Utils.decrypt(content, sharedSecret)
} catch (e: Exception) { } catch (e: Exception) {
@@ -1,7 +1,7 @@
package com.vitorpamplona.amethyst.service.nip19 package com.vitorpamplona.amethyst.service.nip19
import android.util.Log import android.util.Log
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Bech32 import nostr.postr.Bech32
import nostr.postr.bechToBytes import nostr.postr.bechToBytes
@@ -147,8 +147,8 @@ object Nip19 {
public fun createNEvent(idHex: String, author: String?, kind: Int?, relay: String?): String { public fun createNEvent(idHex: String, author: String?, kind: Int?, relay: String?): String {
val kind = kind?.toByteArray() val kind = kind?.toByteArray()
val author = author?.toByteArray() val author = author?.hexToByteArray()
val idHex = idHex.toByteArray() val idHex = idHex.hexToByteArray()
val relay = relay?.toByteArray(Charsets.UTF_8) val relay = relay?.toByteArray(Charsets.UTF_8)
var fullArray = byteArrayOf(Tlv.Type.SPECIAL.id, idHex.size.toByte()) + idHex var fullArray = byteArrayOf(Tlv.Type.SPECIAL.id, idHex.size.toByte()) + idHex
@@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.ServiceManager import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.hexToByteArray
import com.vitorpamplona.amethyst.service.nip19.Nip19 import com.vitorpamplona.amethyst.service.nip19.Nip19
import fr.acinq.secp256k1.Hex import fr.acinq.secp256k1.Hex
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -42,7 +42,7 @@ class AccountStateViewModel() : ViewModel() {
fun startUI(key: String) { fun startUI(key: String) {
val pattern = Pattern.compile(".+@.+\\.[a-z]+") val pattern = Pattern.compile(".+@.+\\.[a-z]+")
val parsed = Nip19.uriToRoute(key) val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray() val pubKeyParsed = parsed?.hex?.hexToByteArray()
val account = val account =
if (key.startsWith("nsec")) { if (key.startsWith("nsec")) {