Compare commits
2 Commits
master
...
fix-extern
| Author | SHA1 | Date | |
|---|---|---|---|
| 4449242581 | |||
| 8e7a1e6d7c |
@@ -61,7 +61,7 @@ class AndroidExternalSigner(
|
||||
): String? {
|
||||
// Try Content Resolver first
|
||||
queryContentResolver(type, payload, pubkey, currentUser)?.let {
|
||||
return it.result
|
||||
return if (resultKey == "event") it.event else it.result
|
||||
}
|
||||
|
||||
// Fall back to Intent
|
||||
|
||||
@@ -4,23 +4,30 @@ import android.content.Intent
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ExternalSignerLauncher {
|
||||
private var launcher: ActivityResultLauncher<Intent>? = null
|
||||
private var pendingResult: CompletableDeferred<ActivityResult>? = null
|
||||
private val mutex = Mutex()
|
||||
|
||||
fun register(launcher: ActivityResultLauncher<Intent>) {
|
||||
this.launcher = launcher
|
||||
}
|
||||
|
||||
suspend fun launch(intent: Intent): ActivityResult {
|
||||
val deferred = CompletableDeferred<ActivityResult>()
|
||||
pendingResult = deferred
|
||||
launcher?.launch(intent)
|
||||
?: throw IllegalStateException("ExternalSignerLauncher not registered")
|
||||
return deferred.await()
|
||||
suspend fun launch(intent: Intent): ActivityResult = mutex.withLock {
|
||||
withContext(Dispatchers.Main) {
|
||||
val deferred = CompletableDeferred<ActivityResult>()
|
||||
pendingResult = deferred
|
||||
launcher?.launch(intent) ?: throw IllegalStateException("Signer not registered")
|
||||
deferred.await()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun onResult(result: ActivityResult) {
|
||||
pendingResult?.complete(result)
|
||||
pendingResult = null
|
||||
|
||||
@@ -379,9 +379,6 @@ class Nostr {
|
||||
|
||||
private suspend fun setCachedRumor(giftId: EventId, rumor: UnsignedEvent) {
|
||||
try {
|
||||
val currentUser =
|
||||
signer.currentUser ?: throw IllegalStateException("User not signed in")
|
||||
|
||||
// Construct the room id
|
||||
val roomId = rumor.roomId()
|
||||
|
||||
@@ -396,10 +393,10 @@ class Nostr {
|
||||
// Set event kind
|
||||
val kind = Kind.fromStd(KindStandard.APPLICATION_SPECIFIC_DATA);
|
||||
|
||||
// Construct event
|
||||
val event = EventBuilder(kind, rumor.asJson())
|
||||
.tags(tags)
|
||||
.finalizeUnsigned(currentUser)
|
||||
.signAsync(Keys.generate())
|
||||
.finalizeAsync(signer)
|
||||
|
||||
client?.database()?.saveEvent(event)
|
||||
} catch (e: Exception) {
|
||||
@@ -412,23 +409,16 @@ class Nostr {
|
||||
val cachedRumor = getCachedRumor(event.id())
|
||||
if (cachedRumor != null) return cachedRumor
|
||||
|
||||
// Get all signers
|
||||
val signers = listOfNotNull(signer, deviceSigner)
|
||||
if (signers.isEmpty()) return null
|
||||
|
||||
// Try to unwrap the gift with each signer
|
||||
for (signer in signers) {
|
||||
try {
|
||||
val gift = UnwrappedGift.fromGiftWrapAsync(signer = signer, giftWrap = event)
|
||||
val rumor = gift.rumor()
|
||||
// Save the rumor to the database
|
||||
setCachedRumor(event.id(), rumor)
|
||||
// Return the rumor
|
||||
return rumor
|
||||
} catch (e: Exception) {
|
||||
println("Failed to unwrap gift: ${e.message}")
|
||||
continue
|
||||
}
|
||||
try {
|
||||
val gift = UnwrappedGift.fromGiftWrapAsync(signer = signer, giftWrap = event)
|
||||
val rumor = gift.rumor()
|
||||
// Save the rumor to the database
|
||||
setCachedRumor(event.id(), rumor)
|
||||
// Return the rumor
|
||||
return rumor
|
||||
} catch (e: Exception) {
|
||||
println("Failed to unwrap gift: ${e.message}")
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user