Refactor SignerDialog to use the rawJson and type

This commit is contained in:
greenart7c3
2023-08-18 07:55:14 -03:00
parent f06e79cfd3
commit 3b07b5d8dd
11 changed files with 111 additions and 88 deletions
@@ -318,11 +318,11 @@ object LocalPreferences {
}?.ifEmpty { listOf("+") } ?: listOf("+") }?.ifEmpty { listOf("+") } ?: listOf("+")
val defaultZapType = getString(PrefKeys.DEFAULT_ZAPTYPE, "")?.let { serverName -> val defaultZapType = getString(PrefKeys.DEFAULT_ZAPTYPE, "")?.let { serverName ->
LnZapEvent.ZapType.values().first { it.name == serverName } LnZapEvent.ZapType.values().firstOrNull { it.name == serverName }
} ?: LnZapEvent.ZapType.PUBLIC } ?: LnZapEvent.ZapType.PUBLIC
val defaultFileServer = getString(PrefKeys.DEFAULT_FILE_SERVER, "")?.let { serverName -> val defaultFileServer = getString(PrefKeys.DEFAULT_FILE_SERVER, "")?.let { serverName ->
ServersAvailable.values().first { it.name == serverName } ServersAvailable.values().firstOrNull { it.name == serverName }
} ?: ServersAvailable.NOSTR_BUILD } ?: ServersAvailable.NOSTR_BUILD
val zapPaymentRequestServer = try { val zapPaymentRequestServer = try {
@@ -169,13 +169,14 @@ fun NewPostView(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it, relayList = relayList) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent, relayList = relayList)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
onClose() onClose()
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -100,14 +100,15 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
postViewModel.clear() postViewModel.clear()
onClose() onClose()
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -79,14 +79,15 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
postViewModel.clear() postViewModel.clear()
onClose() onClose()
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -41,22 +41,36 @@ import androidx.compose.ui.window.DialogProperties
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.theme.ButtonBorder import com.vitorpamplona.amethyst.ui.theme.ButtonBorder
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventInterface
import com.vitorpamplona.quartz.events.PrivateDmEvent
import com.vitorpamplona.quartz.events.TextNoteEvent import com.vitorpamplona.quartz.events.TextNoteEvent
import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
enum class SignerType {
SIGN_EVENT,
NIP04_ENCRYPT,
NIP04_DECRYPT,
NIP44_ENCRYPT,
NIP44_DECRYPT
}
fun openAmber( fun openAmber(
event: EventInterface, data: String,
intentResult: ManagedActivityResultLauncher<Intent, ActivityResult> type: SignerType,
intentResult: ManagedActivityResultLauncher<Intent, ActivityResult>,
pubKey: HexKey
) { ) {
val json = event.toJson() val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$data"))
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$json")) val signerType = when (type) {
if (event is PrivateDmEvent) { SignerType.SIGN_EVENT -> "sign_event"
intent.putExtra("type", "nip04_decrypt") SignerType.NIP04_ENCRYPT -> "nip04_encrypt"
SignerType.NIP04_DECRYPT -> "nip04_decrypt"
SignerType.NIP44_ENCRYPT -> "nip44_encrypt"
SignerType.NIP44_DECRYPT -> "nip44_decrypt"
} }
intent.putExtra("type", signerType)
intent.putExtra("pubKey", pubKey)
intent.`package` = "com.greenart7c3.nostrsigner.debug" intent.`package` = "com.greenart7c3.nostrsigner.debug"
intentResult.launch(intent) intentResult.launch(intent)
} }
@@ -64,8 +78,10 @@ fun openAmber(
@Composable @Composable
fun SignerDialog( fun SignerDialog(
onClose: () -> Unit, onClose: () -> Unit,
onPost: (signedEvent: Event) -> Unit, onPost: (content: String) -> Unit,
event: EventInterface data: String,
type: SignerType = SignerType.SIGN_EVENT,
pubKey: HexKey = ""
) { ) {
var signature by remember { mutableStateOf("") } var signature by remember { mutableStateOf("") }
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@@ -85,24 +101,16 @@ fun SignerDialog(
} }
signature = it.data?.getStringExtra("signature") ?: "" signature = it.data?.getStringExtra("signature") ?: ""
if (event is PrivateDmEvent) { if (type == SignerType.NIP04_DECRYPT) {
onPost( onPost(
Event( signature
event.id(),
event.pubKey(),
event.createdAt(),
event.kind(),
event.tags(),
signature,
event.sig()
)
) )
} }
} }
) )
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
openAmber(event, intentResult) openAmber(data, type, intentResult, pubKey)
} }
Dialog( Dialog(
@@ -138,18 +146,9 @@ fun SignerDialog(
PostButton( PostButton(
onPost = { onPost = {
val signedEvent = if (event is PrivateDmEvent) { if (type == SignerType.SIGN_EVENT) {
Event( val event = Event.fromJson(data)
event.id(), val signedEvent = Event(
event.pubKey(),
event.createdAt(),
event.kind(),
event.tags(),
signature,
event.sig()
)
} else {
Event(
event.id(), event.id(),
event.pubKey(), event.pubKey(),
event.createdAt(), event.createdAt(),
@@ -158,18 +157,21 @@ fun SignerDialog(
event.content(), event.content(),
signature signature
) )
}
if (!signedEvent.hasValidSignature() && event !is PrivateDmEvent) { if (!signedEvent.hasValidSignature()) {
scope.launch { scope.launch {
Toast.makeText( Toast.makeText(
context, context,
"Invalid signature", "Invalid signature",
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
}
return@PostButton
} }
return@PostButton onPost(signedEvent.toJson())
} else {
onPost(signature)
} }
onPost(signedEvent)
}, },
isActive = true isActive = true
) )
@@ -199,7 +201,7 @@ fun SignerDialog(
) )
Button( Button(
shape = ButtonBorder, shape = ButtonBorder,
onClick = { openAmber(event, intentResult) } onClick = { openAmber(data, type, intentResult, pubKey) }
) { ) {
Text("Open Amber") Text("Open Amber")
} }
@@ -214,6 +216,8 @@ fun Test() {
SignerDialog( SignerDialog(
onClose = { }, onClose = { },
onPost = { }, onPost = { },
event = TextNoteEvent("", "", TimeUtils.now(), emptyList(), "test", "") data = TextNoteEvent("", "", TimeUtils.now(), emptyList(), "test", "").toJson(),
type = SignerType.SIGN_EVENT,
pubKey = ""
) )
} }
@@ -47,6 +47,7 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.PackageUtils import com.vitorpamplona.amethyst.service.PackageUtils
import com.vitorpamplona.amethyst.ui.actions.SignerDialog import com.vitorpamplona.amethyst.ui.actions.SignerDialog
import com.vitorpamplona.amethyst.ui.actions.SignerType
import com.vitorpamplona.amethyst.ui.components.CreateClickableTextWithEmoji import com.vitorpamplona.amethyst.ui.components.CreateClickableTextWithEmoji
import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy
@@ -66,6 +67,7 @@ import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.amethyst.ui.theme.subtleBorder import com.vitorpamplona.amethyst.ui.theme.subtleBorder
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.ChannelCreateEvent import com.vitorpamplona.quartz.events.ChannelCreateEvent
import com.vitorpamplona.quartz.events.ChannelMetadataEvent import com.vitorpamplona.quartz.events.ChannelMetadataEvent
import com.vitorpamplona.quartz.events.ChatMessageEvent import com.vitorpamplona.quartz.events.ChatMessageEvent
@@ -649,10 +651,12 @@ private fun RenderRegularTextNote(
eventContent = accountViewModel.decrypt(note) eventContent = accountViewModel.decrypt(note)
}, },
onPost = { onPost = {
eventContent = it.content eventContent = it
triedToDecrypt = true triedToDecrypt = true
}, },
event = note.event!! data = eventContent ?: "",
type = SignerType.NIP04_DECRYPT,
pubKey = (note.event as PrivateDmEvent).talkingWith(accountViewModel.account.keyPair.pubKey.toHexKey()) ?: ""
) )
} }
@@ -693,12 +693,13 @@ fun BoostReaction(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -810,12 +811,13 @@ fun LikeReaction(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -1405,13 +1407,14 @@ private fun ActionableReactionButton(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
onDismiss() onDismiss()
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -43,6 +43,7 @@ import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
import com.vitorpamplona.amethyst.ui.theme.Size55dp import com.vitorpamplona.amethyst.ui.theme.Size55dp
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.ContactListEvent import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapEvent import com.vitorpamplona.quartz.events.LnZapEvent
import com.vitorpamplona.quartz.events.LnZapRequestEvent import com.vitorpamplona.quartz.events.LnZapRequestEvent
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -219,12 +220,13 @@ fun ShowFollowingOrUnfollowingButton(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -1092,12 +1092,13 @@ fun JoinChatButton(accountViewModel: AccountViewModel, channel: Channel, nav: (S
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -1131,12 +1132,13 @@ fun LeaveChatButton(accountViewModel: AccountViewModel, channel: Channel, nav: (
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -1170,12 +1172,13 @@ fun JoinCommunityButton(accountViewModel: AccountViewModel, note: AddressableNot
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -1209,12 +1212,13 @@ fun LeaveCommunityButton(accountViewModel: AccountViewModel, note: AddressableNo
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -159,12 +159,13 @@ fun HashtagActionOptions(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }
@@ -101,6 +101,7 @@ import com.vitorpamplona.quartz.events.BadgeDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeProfilesEvent import com.vitorpamplona.quartz.events.BadgeProfilesEvent
import com.vitorpamplona.quartz.events.ChatroomKey import com.vitorpamplona.quartz.events.ChatroomKey
import com.vitorpamplona.quartz.events.ContactListEvent import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.GitHubIdentity import com.vitorpamplona.quartz.events.GitHubIdentity
import com.vitorpamplona.quartz.events.IdentityClaim import com.vitorpamplona.quartz.events.IdentityClaim
import com.vitorpamplona.quartz.events.ImmutableListOfLists import com.vitorpamplona.quartz.events.ImmutableListOfLists
@@ -759,12 +760,13 @@ private fun DisplayFollowUnfollowButton(
}, },
onPost = { onPost = {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Client.send(it) val signedEvent = Event.fromJson(it)
LocalCache.verifyAndConsume(it, null) Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null event = null
} }
}, },
event = event!! data = event!!.toJson()
) )
} }