made some fields optional on JsonDeserializer, created button to copy the raw json

This commit is contained in:
greenart7c3
2023-08-07 18:51:42 -03:00
parent e08d8edf19
commit 25d84c65a2
4 changed files with 46 additions and 10 deletions
@@ -17,6 +17,7 @@ import com.greenart7c3.nostrsigner.models.HexKey
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.models.toHexKey
import com.greenart7c3.nostrsigner.service.CryptoUtils
import com.greenart7c3.nostrsigner.service.KeyPair
import fr.acinq.secp256k1.Hex
import java.lang.reflect.Type
import java.math.BigDecimal
@@ -198,15 +199,15 @@ open class Event(
): Event {
val jsonObject = json.asJsonObject
return Event(
id = jsonObject.get("id").asString,
pubKey = jsonObject.get("pubkey").asString,
createdAt = jsonObject.get("created_at").asLong,
id = jsonObject.get("id")?.asString ?: "",
pubKey = jsonObject.get("pubkey")?.asString ?: "",
createdAt = jsonObject.get("created_at")?.asLong ?: TimeUtils.now(),
kind = jsonObject.get("kind").asInt,
tags = jsonObject.get("tags").asJsonArray.map {
tags = jsonObject.get("tags")?.asJsonArray?.map {
it.asJsonArray.mapNotNull { s -> if (s.isJsonNull) null else s.asString }
},
} ?: emptyList(),
content = jsonObject.get("content").asString,
sig = jsonObject.get("sig").asString
sig = jsonObject.get("sig")?.asString ?: ""
)
}
}
@@ -296,10 +297,12 @@ open class Event(
return CryptoUtils.sha256(rawEventJson.toByteArray())
}
fun setPubKeyIfEmpty(event: Event, pubKey: HexKey): Event {
fun setPubKeyIfEmpty(event: Event, keyPair: KeyPair): Event {
if (event.pubKey.isEmpty()) {
val pubKey = keyPair.pubKey.toHexKey()
val id = generateId(pubKey, event.createdAt, event.kind, event.tags, event.content)
return Event(id.toHexKey(), pubKey, event.createdAt, event.kind, event.tags, event.content, "")
val signature = CryptoUtils.sign(id, keyPair.privKey!!).toHexKey()
return Event(id.toHexKey(), pubKey, event.createdAt, event.kind, event.tags, event.content, signature)
}
return event
@@ -8,7 +8,6 @@ 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.models.toHexKey
import com.greenart7c3.nostrsigner.service.model.Event
import java.net.URLDecoder
@@ -43,7 +42,7 @@ fun AccountScreen(
}
var event = Event.fromJson(data)
if (event.pubKey.isEmpty()) {
event = Event.setPubKeyIfEmpty(event, state.account.keyPair.pubKey.toHexKey())
event = Event.setPubKeyIfEmpty(event, state.account.keyPair)
}
intentData = IntentData(event, name)
}
@@ -1,6 +1,7 @@
package com.greenart7c3.nostrsigner.ui
import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@@ -39,6 +40,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.AnnotatedString
@@ -61,6 +63,7 @@ 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
@@ -106,6 +109,9 @@ fun MainScreen(account: Account, accountStateViewModel: AccountStateViewModel, j
it.name,
event,
event.toJson(),
coroutineScope,
clipboardManager,
context,
{
if (event.pubKey != account.keyPair.pubKey.toHexKey()) {
coroutineScope.launch {
@@ -146,6 +152,9 @@ fun EventData(
appName: String,
event: Event,
rawJson: String,
coroutineScope: CoroutineScope,
clipboardManager: ClipboardManager,
context: Context,
onAccept: () -> Unit,
onReject: () -> Unit
) {
@@ -231,6 +240,27 @@ fun EventData(
modifier = Modifier.padding(5.dp),
text = JSONObject(rawJson).toString(2)
)
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
Button(
shape = ButtonBorder,
onClick = {
clipboardManager.setText(AnnotatedString(rawJson))
coroutineScope.launch {
Toast.makeText(
context,
context.getString(R.string.raw_json_copied_to_the_clipboard),
Toast.LENGTH_SHORT
).show()
}
}
) {
Text("Copy raw json")
}
}
}
Spacer(modifier = Modifier.weight(1f))
// Row(
@@ -324,6 +354,9 @@ fun Preview() {
"App",
event,
data,
rememberCoroutineScope(),
LocalClipboardManager.current,
LocalContext.current,
{ },
{ }
)
+1
View File
@@ -20,4 +20,5 @@
<string name="scan_qr">Scan QR</string>
<string name="signature_copied_to_the_clipboard">Signature copied to the clipboard</string>
<string name="sign">Sign</string>
<string name="raw_json_copied_to_the_clipboard">Raw json copied to the clipboard</string>
</resources>