support for sending/receiving multiple events from external signer

This commit is contained in:
greenart7c3
2023-11-24 12:33:10 -03:00
parent ad52855f5a
commit 8a18781048
@@ -5,10 +5,13 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import android.util.LruCache import android.util.LruCache
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.events.EventInterface import com.vitorpamplona.quartz.events.EventInterface
import com.vitorpamplona.quartz.events.LnZapRequestEvent import com.vitorpamplona.quartz.events.LnZapRequestEvent
enum class SignerType { enum class SignerType {
SIGN_EVENT, SIGN_EVENT,
NIP04_ENCRYPT, NIP04_ENCRYPT,
@@ -28,6 +31,15 @@ class Permission(
} }
} }
class Result(
@JsonProperty("package")
val `package`: String?,
@JsonProperty("signature")
val signature: String?,
@JsonProperty("id")
val id: String?
)
class ExternalSignerLauncher( class ExternalSignerLauncher(
private val npub: String, private val npub: String,
val signerPackageName: String = "com.greenart7c3.nostrsigner" val signerPackageName: String = "com.greenart7c3.nostrsigner"
@@ -57,16 +69,36 @@ class ExternalSignerLauncher(
} }
fun newResult(data: Intent) { fun newResult(data: Intent) {
val signature = data.getStringExtra("signature") ?: "" val results = data.getStringExtra("results")
val packageName = data.getStringExtra("package") ?: "" if (results != null) {
val id = data.getStringExtra("id") ?: "" val objectMapper = ObjectMapper()
if (id.isNotBlank()) { val localResults: Array<Result> = objectMapper.readValue(
val result = if (packageName.isNotBlank()) "$signature-$packageName" else signature results,
contentCache.get(id)?.invoke(result) Array<Result>::class.java
)
localResults.forEach {
val signature = it.signature ?: ""
val packageName = it.`package` ?: ""
val id = it.id ?: ""
if (id.isNotBlank()) {
val result = if (packageName.isNotBlank()) "$signature-$packageName" else signature
val contentCache = contentCache.get(id)
contentCache?.invoke(result)
}
}
} else {
val signature = data.getStringExtra("signature") ?: ""
val packageName = data.getStringExtra("package") ?: ""
val id = data.getStringExtra("id") ?: ""
if (id.isNotBlank()) {
val result = if (packageName.isNotBlank()) "$signature-$packageName" else signature
val contentCache = contentCache.get(id)
contentCache?.invoke(result)
}
} }
} }
fun openSignerApp( fun openSignerApp(
data: String, data: String,
type: SignerType, type: SignerType,
@@ -145,6 +177,8 @@ class ExternalSignerLauncher(
intent.`package` = signerPackageName intent.`package` = signerPackageName
} }
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
contentCache.put(id, onReady) contentCache.put(id, onReady)
intentLauncher(intent) intentLauncher(intent)