created IntentUtils, made privKey non nullable

This commit is contained in:
greenart7c3
2023-08-14 10:17:16 -03:00
parent 75e118cdc7
commit fcf4cd5694
7 changed files with 40 additions and 38 deletions
@@ -149,7 +149,7 @@ object LocalPreferences {
fun saveToEncryptedStorage(account: Account) {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
prefs.edit().apply {
account.keyPair.privKey?.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
account.keyPair.privKey.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
account.keyPair.pubKey.let { putString(PrefKeys.NOSTR_PUBKEY, it.toHexKey()) }
}.apply()
}
@@ -0,0 +1,29 @@
package com.greenart7c3.nostrsigner.service
import android.content.Intent
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.service.model.Event
import java.net.URLDecoder
object IntentUtils {
fun getIntentData(intent: Intent, keyPair: KeyPair): IntentData? {
if (intent.data != null) {
var data = URLDecoder.decode(intent.data?.toString()?.replace("+", "%2b") ?: "", "utf-8").replace("nostrsigner:", "")
val split = data.split(";")
var name = ""
if (split.isNotEmpty()) {
if (split.last().lowercase().contains("name=")) {
name = split.last().replace("name=", "")
val newList = split.toList().dropLast(1)
data = newList.joinToString("")
}
}
var event = Event.fromJson(data)
if (event.pubKey.isEmpty()) {
event = Event.setPubKeyIfEmpty(event, keyPair)
}
return IntentData(event, name)
}
return null
}
}
@@ -6,29 +6,21 @@ class KeyPair(
privKey: ByteArray? = null,
pubKey: ByteArray? = null
) {
val privKey: ByteArray?
val privKey: ByteArray
val pubKey: ByteArray
init {
if (privKey == null) {
if (pubKey == null) {
// create new, random keys
this.privKey = CryptoUtils.privkeyCreate()
this.pubKey = CryptoUtils.pubkeyCreate(this.privKey)
} else {
// this is a read-only account
check(pubKey.size == 32)
this.privKey = null
this.pubKey = pubKey
}
this.privKey = CryptoUtils.privkeyCreate()
this.pubKey = CryptoUtils.pubkeyCreate(this.privKey)
} else {
// as private key is provided, ignore the public key and set keys according to private key
this.privKey = privKey
this.pubKey = CryptoUtils.pubkeyCreate(privKey)
this.pubKey = pubKey ?: CryptoUtils.pubkeyCreate(privKey)
}
}
override fun toString(): String {
return "KeyPair(privateKey=${privKey?.toHexKey()}, publicKey=${pubKey.toHexKey()}"
return "KeyPair(privateKey=${privKey.toHexKey()}, publicKey=${pubKey.toHexKey()}"
}
}
@@ -301,7 +301,7 @@ open class Event(
if (event.pubKey.isEmpty()) {
val pubKey = keyPair.pubKey.toHexKey()
val id = generateId(pubKey, event.createdAt, event.kind, event.tags, event.content)
val signature = CryptoUtils.sign(id, keyPair.privKey!!).toHexKey()
val signature = CryptoUtils.sign(id, keyPair.privKey).toHexKey()
return Event(id.toHexKey(), pubKey, event.createdAt, event.kind, event.tags, event.content, signature)
}
@@ -7,9 +7,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.service.model.Event
import java.net.URLDecoder
import com.greenart7c3.nostrsigner.service.IntentUtils
@Composable
fun AccountScreen(
@@ -30,24 +28,7 @@ fun AccountScreen(
LoginPage(accountStateViewModel)
}
is AccountState.LoggedIn -> {
var intentData: IntentData? = null
if (intent.data != null) {
var data = URLDecoder.decode(intent.data?.toString()?.replace("+", "%2b") ?: "", "utf-8").replace("nostrsigner:", "")
val split = data.split(";")
var name = ""
if (split.isNotEmpty()) {
if (split.last().lowercase().contains("name=")) {
name = split.last().replace("name=", "")
val newList = split.toList().dropLast(1)
data = newList.joinToString("")
}
}
var event = Event.fromJson(data)
if (event.pubKey.isEmpty()) {
event = Event.setPubKeyIfEmpty(event, state.account.keyPair)
}
intentData = IntentData(event, name)
}
val intentData = IntentUtils.getIntentData(intent, state.account.keyPair)
MainScreen(state.account, accountStateViewModel, intentData, packageName)
}
@@ -132,7 +132,7 @@ fun MainScreen(account: Account, accountStateViewModel: AccountStateViewModel, j
}
val id = event.id.hexToByteArray()
val sig = CryptoUtils.sign(id, account.keyPair.privKey!!).toHexKey()
val sig = CryptoUtils.sign(id, account.keyPair.privKey).toHexKey()
clipboardManager.setText(AnnotatedString(sig))
@@ -172,7 +172,7 @@ private fun copyNSec(
account: Account,
clipboardManager: ClipboardManager
) {
account.keyPair.privKey?.let {
account.keyPair.privKey.let {
clipboardManager.setText(AnnotatedString(it.toNsec()))
scope.launch {
Toast.makeText(