created SignerType to decrypt/encrypt nip04

This commit is contained in:
greenart7c3
2023-08-16 11:32:22 -03:00
parent 355daef1fc
commit 14fe5a3489
5 changed files with 81 additions and 40 deletions
@@ -1,8 +1,15 @@
package com.greenart7c3.nostrsigner.models
import com.greenart7c3.nostrsigner.service.model.Event
class IntentData(
val data: Event,
val name: String
val data: String,
val name: String,
val type: SignerType
)
enum class SignerType {
SIGN_EVENT,
NIP04_ENCRYPT,
NIP04_DECRYPT,
NIP44_ENCRYPT,
NIP44_DECRYPT
}
@@ -2,28 +2,45 @@ package com.greenart7c3.nostrsigner.service
import android.content.Intent
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.service.model.Event
import java.net.URLDecoder
object IntentUtils {
fun getIntentData(intent: Intent, keyPair: KeyPair): IntentData? {
fun getIntentData(intent: Intent): IntentData? {
if (intent.data != null) {
var data = URLDecoder.decode(intent.data?.toString()?.replace("+", "%2b") ?: "", "utf-8").replace("nostrsigner:", "")
val data = URLDecoder.decode(intent.data?.toString()?.replace("+", "%2b") ?: "", "utf-8").replace("nostrsigner:", "")
val type = when (intent.extras?.getString("type")) {
"sign_event" -> SignerType.SIGN_EVENT
"nip04_encrypt" -> SignerType.NIP04_ENCRYPT
"nip04_decrypt" -> SignerType.NIP04_DECRYPT
else -> SignerType.SIGN_EVENT
}
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 IntentData(data, name, type)
}
return null
}
fun getIntent(data: String, keyPair: KeyPair): Event {
var tempData = data
val split = data.split(";")
if (split.isNotEmpty()) {
if (split.last().lowercase().contains("name=")) {
val newList = split.toList().dropLast(1)
tempData = newList.joinToString("")
}
}
var event = Event.fromJson(tempData)
if (event.pubKey.isEmpty()) {
event = Event.setPubKeyIfEmpty(event, keyPair)
}
return event
}
}
@@ -27,7 +27,7 @@ fun AccountScreen(
LoginPage(accountStateViewModel)
}
is AccountState.LoggedIn -> {
val intentData = IntentUtils.getIntentData(intent, state.account.keyPair)
val intentData = IntentUtils.getIntentData(intent)
MainScreen(state.account, accountStateViewModel, intentData, packageName)
}
@@ -49,22 +49,22 @@ import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.models.toHexKey
import com.greenart7c3.nostrsigner.service.CryptoUtils
import com.greenart7c3.nostrsigner.service.IntentUtils
import com.greenart7c3.nostrsigner.service.getAppCompatActivity
import com.greenart7c3.nostrsigner.service.model.Event
import com.greenart7c3.nostrsigner.service.toShortenHex
import com.greenart7c3.nostrsigner.ui.components.Drawer
import com.greenart7c3.nostrsigner.ui.components.MainAppBar
import com.greenart7c3.nostrsigner.ui.theme.ButtonBorder
import com.greenart7c3.nostrsigner.ui.theme.NostrSignerTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.json.JSONObject
@@ -105,9 +105,10 @@ fun MainScreen(account: Account, accountStateViewModel: AccountStateViewModel, j
}
} else {
json.let {
val event = it.data
val event = IntentUtils.getIntent(it.data, account.keyPair)
EventData(
it.type,
packageName ?: it.name,
event,
event.toJson(),
@@ -158,6 +159,7 @@ fun MainScreen(account: Account, accountStateViewModel: AccountStateViewModel, j
@Composable
fun EventData(
type: SignerType,
appName: String,
event: Event,
rawJson: String,
@@ -333,26 +335,3 @@ fun EventRow(
)
}
}
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Preview(device = "id:Nexus S")
@Composable
fun Preview() {
val event = Event("123", "7579076d9aff0a4cfdefa7e2045f2486c7e5d8bc63bfc6b45397233e1bbfcb19", TimeUtils.now(), 1, listOf(), "This is a test 123 123 123 123 123 123", "")
val data = event.toJson()
NostrSignerTheme(darkTheme = true) {
Scaffold {
EventData(
"App",
event,
data,
rememberCoroutineScope(),
LocalClipboardManager.current,
LocalContext.current,
{ },
{ }
)
}
}
}
@@ -0,0 +1,38 @@
package com.greenart7c3.nostrsigner.ui.previews
import android.annotation.SuppressLint
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.service.model.Event
import com.greenart7c3.nostrsigner.ui.EventData
import com.greenart7c3.nostrsigner.ui.theme.NostrSignerTheme
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Preview(device = "id:Nexus S")
@Composable
fun EventDataPreview() {
val event = Event("123", "7579076d9aff0a4cfdefa7e2045f2486c7e5d8bc63bfc6b45397233e1bbfcb19", TimeUtils.now(), 1, listOf(), "This is a test 123 123 123 123 123 123", "")
val data = event.toJson()
NostrSignerTheme(darkTheme = true) {
Scaffold {
EventData(
SignerType.SIGN_EVENT,
"App",
event,
data,
rememberCoroutineScope(),
LocalClipboardManager.current,
LocalContext.current,
{ },
{ }
)
}
}
}