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

View File

@ -318,11 +318,11 @@ object LocalPreferences {
}?.ifEmpty { listOf("+") } ?: listOf("+")
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
val defaultFileServer = getString(PrefKeys.DEFAULT_FILE_SERVER, "")?.let { serverName ->
ServersAvailable.values().first { it.name == serverName }
ServersAvailable.values().firstOrNull { it.name == serverName }
} ?: ServersAvailable.NOSTR_BUILD
val zapPaymentRequestServer = try {

View File

@ -169,13 +169,14 @@ fun NewPostView(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it, relayList = relayList)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent, relayList = relayList)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
onClose()
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -100,14 +100,15 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
postViewModel.clear()
onClose()
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -79,14 +79,15 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
postViewModel.clear()
onClose()
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -41,22 +41,36 @@ import androidx.compose.ui.window.DialogProperties
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.theme.ButtonBorder
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
import com.vitorpamplona.quartz.encoders.HexKey
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.utils.TimeUtils
import kotlinx.coroutines.launch
enum class SignerType {
SIGN_EVENT,
NIP04_ENCRYPT,
NIP04_DECRYPT,
NIP44_ENCRYPT,
NIP44_DECRYPT
}
fun openAmber(
event: EventInterface,
intentResult: ManagedActivityResultLauncher<Intent, ActivityResult>
data: String,
type: SignerType,
intentResult: ManagedActivityResultLauncher<Intent, ActivityResult>,
pubKey: HexKey
) {
val json = event.toJson()
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$json"))
if (event is PrivateDmEvent) {
intent.putExtra("type", "nip04_decrypt")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$data"))
val signerType = when (type) {
SignerType.SIGN_EVENT -> "sign_event"
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"
intentResult.launch(intent)
}
@ -64,8 +78,10 @@ fun openAmber(
@Composable
fun SignerDialog(
onClose: () -> Unit,
onPost: (signedEvent: Event) -> Unit,
event: EventInterface
onPost: (content: String) -> Unit,
data: String,
type: SignerType = SignerType.SIGN_EVENT,
pubKey: HexKey = ""
) {
var signature by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
@ -85,24 +101,16 @@ fun SignerDialog(
}
signature = it.data?.getStringExtra("signature") ?: ""
if (event is PrivateDmEvent) {
if (type == SignerType.NIP04_DECRYPT) {
onPost(
Event(
event.id(),
event.pubKey(),
event.createdAt(),
event.kind(),
event.tags(),
signature,
event.sig()
)
signature
)
}
}
)
LaunchedEffect(Unit) {
openAmber(event, intentResult)
openAmber(data, type, intentResult, pubKey)
}
Dialog(
@ -138,18 +146,9 @@ fun SignerDialog(
PostButton(
onPost = {
val signedEvent = if (event is PrivateDmEvent) {
Event(
event.id(),
event.pubKey(),
event.createdAt(),
event.kind(),
event.tags(),
signature,
event.sig()
)
} else {
Event(
if (type == SignerType.SIGN_EVENT) {
val event = Event.fromJson(data)
val signedEvent = Event(
event.id(),
event.pubKey(),
event.createdAt(),
@ -158,18 +157,21 @@ fun SignerDialog(
event.content(),
signature
)
}
if (!signedEvent.hasValidSignature() && event !is PrivateDmEvent) {
scope.launch {
Toast.makeText(
context,
"Invalid signature",
Toast.LENGTH_SHORT
).show()
if (!signedEvent.hasValidSignature()) {
scope.launch {
Toast.makeText(
context,
"Invalid signature",
Toast.LENGTH_SHORT
).show()
}
return@PostButton
}
return@PostButton
onPost(signedEvent.toJson())
} else {
onPost(signature)
}
onPost(signedEvent)
},
isActive = true
)
@ -199,7 +201,7 @@ fun SignerDialog(
)
Button(
shape = ButtonBorder,
onClick = { openAmber(event, intentResult) }
onClick = { openAmber(data, type, intentResult, pubKey) }
) {
Text("Open Amber")
}
@ -214,6 +216,8 @@ fun Test() {
SignerDialog(
onClose = { },
onPost = { },
event = TextNoteEvent("", "", TimeUtils.now(), emptyList(), "test", "")
data = TextNoteEvent("", "", TimeUtils.now(), emptyList(), "test", "").toJson(),
type = SignerType.SIGN_EVENT,
pubKey = ""
)
}

View File

@ -47,6 +47,7 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.PackageUtils
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.CreateTextWithEmoji
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.placeholderText
import com.vitorpamplona.amethyst.ui.theme.subtleBorder
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.ChannelCreateEvent
import com.vitorpamplona.quartz.events.ChannelMetadataEvent
import com.vitorpamplona.quartz.events.ChatMessageEvent
@ -649,10 +651,12 @@ private fun RenderRegularTextNote(
eventContent = accountViewModel.decrypt(note)
},
onPost = {
eventContent = it.content
eventContent = it
triedToDecrypt = true
},
event = note.event!!
data = eventContent ?: "",
type = SignerType.NIP04_DECRYPT,
pubKey = (note.event as PrivateDmEvent).talkingWith(accountViewModel.account.keyPair.pubKey.toHexKey()) ?: ""
)
}

View File

@ -693,12 +693,13 @@ fun BoostReaction(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}
@ -810,12 +811,13 @@ fun LikeReaction(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}
@ -1405,13 +1407,14 @@ private fun ActionableReactionButton(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
onDismiss()
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -43,6 +43,7 @@ import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
import com.vitorpamplona.amethyst.ui.theme.Size55dp
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapEvent
import com.vitorpamplona.quartz.events.LnZapRequestEvent
import kotlinx.coroutines.Dispatchers
@ -219,12 +220,13 @@ fun ShowFollowingOrUnfollowingButton(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -1092,12 +1092,13 @@ fun JoinChatButton(accountViewModel: AccountViewModel, channel: Channel, nav: (S
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}
@ -1131,12 +1132,13 @@ fun LeaveChatButton(accountViewModel: AccountViewModel, channel: Channel, nav: (
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}
@ -1170,12 +1172,13 @@ fun JoinCommunityButton(accountViewModel: AccountViewModel, note: AddressableNot
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}
@ -1209,12 +1212,13 @@ fun LeaveCommunityButton(accountViewModel: AccountViewModel, note: AddressableNo
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -159,12 +159,13 @@ fun HashtagActionOptions(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}

View File

@ -101,6 +101,7 @@ import com.vitorpamplona.quartz.events.BadgeDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeProfilesEvent
import com.vitorpamplona.quartz.events.ChatroomKey
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.GitHubIdentity
import com.vitorpamplona.quartz.events.IdentityClaim
import com.vitorpamplona.quartz.events.ImmutableListOfLists
@ -759,12 +760,13 @@ private fun DisplayFollowUnfollowButton(
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
event = event!!
data = event!!.toJson()
)
}