Add support for the Sign message type in the content provider and MultiEventHomeScreen

This commit is contained in:
greenart7c3
2024-05-29 13:07:31 -03:00
parent a36aa4fd27
commit 3f642ab62f
2 changed files with 136 additions and 0 deletions
@@ -9,6 +9,8 @@ import com.greenart7c3.nostrsigner.database.HistoryEntity
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.service.AmberUtils
import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapRequestEvent
@@ -46,6 +48,75 @@ class SignerProvider : ContentProvider() {
): Cursor? {
val appId = BuildConfig.APPLICATION_ID
return when (uri.toString()) {
"content://$appId.SIGN" -> {
val packageName = callingPackage ?: return null
val message = projection?.first() ?: return null
if (!LocalPreferences.containsAccount(projection[2])) return null
val account = LocalPreferences.loadFromEncryptedStorage(projection[2]) ?: return null
val currentSelection = selection ?: "0"
val database = NostrSigner.instance.getDatabase(account.keyPair.pubKey.toNpub())
val permission =
database
.applicationDao()
.getPermission(
sortOrder ?: packageName,
"SIGN",
null,
)
if (currentSelection == "1") {
val isRemembered = permission?.acceptable ?: return null
if (!isRemembered) {
val cursor =
MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
return cursor
}
} else {
val isRemembered = permission?.acceptable ?: false
if (!isRemembered) {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
return null
}
}
val result = CryptoUtils.signString(message, account.keyPair.privKey!!).toHexKey()
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN",
null,
TimeUtils.now(),
true,
),
)
val localCursor = MatrixCursor(arrayOf("signature", "event")).also {
it.addRow(arrayOf(result, result))
}
return localCursor
}
"content://$appId.SIGN_EVENT" -> {
val packageName = callingPackage ?: return null
val json = projection?.first() ?: return null
@@ -70,6 +70,7 @@ import com.greenart7c3.nostrsigner.service.model.AmberEvent
import com.greenart7c3.nostrsigner.service.toShortenHex
import com.greenart7c3.nostrsigner.ui.Result
import com.greenart7c3.nostrsigner.ui.theme.ButtonBorder
import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.LnZapRequestEvent
import kotlinx.coroutines.Dispatchers
@@ -241,6 +242,70 @@ fun MultiEventHomeScreen(
),
)
}
} else if (intentData.type == SignerType.SIGN) {
if (intentData.rememberMyChoice.value) {
AmberUtils.acceptOrRejectPermission(
key,
intentData,
null,
intentData.rememberMyChoice.value,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
localAccount,
database,
)
}
val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
listOf(),
"",
"",
"",
localAccount.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
),
permissions = mutableListOf(),
)
database.applicationDao().insertApplicationWithPermissions(application)
database.applicationDao().addHistory(
HistoryEntity(
0,
key,
intentData.type.toString(),
null,
TimeUtils.now(),
true,
),
)
val signedMessage = CryptoUtils.signString(intentData.data, localAccount.keyPair.privKey!!).toHexKey()
if (intentData.bunkerRequest != null) {
IntentUtils.sendBunkerResponse(
localAccount,
intentData.bunkerRequest.localKey,
BunkerResponse(intentData.bunkerRequest.id, signedMessage, null),
applicationEntity?.application?.relays?.map { url -> Relay(url) } ?: emptyList(),
onLoading = {},
) { }
} else {
results.add(
Result(
null,
signedMessage,
intentData.id,
),
)
}
} else {
if (intentData.rememberMyChoice.value) {
AmberUtils.acceptOrRejectPermission(