Skip to content

Commit

Permalink
change database operations to be suspending, fix multiple bunker requ…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
greenart7c3 committed Dec 30, 2024
1 parent 09139c5 commit 8a30860
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 342 deletions.
188 changes: 106 additions & 82 deletions app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapRequestEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class SignerProvider : ContentProvider() {
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

override fun delete(
uri: Uri,
selection: String?,
Expand Down Expand Up @@ -68,16 +74,18 @@ class SignerProvider : ContentProvider() {
)
val isRemembered = if (signPolicy == 2) true else permission?.acceptable ?: return null
if (!isRemembered) {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
}
val cursor =
MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
Expand All @@ -87,16 +95,18 @@ class SignerProvider : ContentProvider() {
}

val result = CryptoUtils.signString(message, account.signer.keyPair.privKey!!).toHexKey()
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN_MESSAGE",
null,
TimeUtils.now(),
true,
),
)
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN_MESSAGE",
null,
TimeUtils.now(),
true,
),
)
}

val localCursor = MatrixCursor(arrayOf("signature", "event", "result")).also {
it.addRow(arrayOf(result, result, result))
Expand Down Expand Up @@ -145,16 +155,18 @@ class SignerProvider : ContentProvider() {
val signPolicy = database.applicationDao().getSignPolicy(sortOrder ?: packageName)
val isRemembered = if (signPolicy == 2) true else permission?.acceptable ?: return null
if (!isRemembered) {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
event.kind,
TimeUtils.now(),
false,
),
)
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
event.kind,
TimeUtils.now(),
false,
),
)
}

val cursor =
MatrixCursor(arrayOf("rejected")).also {
Expand All @@ -168,30 +180,34 @@ class SignerProvider : ContentProvider() {

if (signedEvent == null) {
Log.d("SignerProvider", "Failed to sign event from $packageName")
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN_EVENT",
event.kind,
TimeUtils.now(),
false,
),
)
}
return null
}

scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN_EVENT",
event.kind,
TimeUtils.now(),
false,
true,
),
)
return null
}

database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
"SIGN_EVENT",
event.kind,
TimeUtils.now(),
true,
),
)

val cursor =
MatrixCursor(arrayOf("signature", "event", "result")).also {
val signature =
Expand Down Expand Up @@ -254,16 +270,18 @@ class SignerProvider : ContentProvider() {
val signPolicy = database.applicationDao().getSignPolicy(sortOrder ?: packageName)
val isRemembered = if (signPolicy == 2) true else permission?.acceptable ?: return null
if (!isRemembered) {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
}

val cursor =
MatrixCursor(arrayOf("rejected")).also {
Expand Down Expand Up @@ -295,16 +313,18 @@ class SignerProvider : ContentProvider() {
"Could not decrypt the message"
}

database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
true,
),
)
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
true,
),
)
}

val cursor = MatrixCursor(arrayOf("signature", "event", "result"))
cursor.addRow(arrayOf<Any>(result, result, result))
Expand All @@ -326,36 +346,40 @@ class SignerProvider : ContentProvider() {
val signPolicy = database.applicationDao().getSignPolicy(sortOrder ?: packageName)
val isRemembered = if (signPolicy == 2) true else permission?.acceptable ?: return null
if (!isRemembered) {
scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
),
)
}

val cursor =
MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}

return cursor
}

scope.launch {
database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
false,
true,
),
)

val cursor =
MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}

return cursor
}

database.applicationDao().addHistory(
HistoryEntity(
0,
sortOrder ?: packageName,
uri.toString().replace("content://$appId.", ""),
null,
TimeUtils.now(),
true,
),
)

val cursor = MatrixCursor(arrayOf("signature", "result"))
val result = if (sortOrder == null) account.signer.keyPair.pubKey.toNpub() else account.signer.keyPair.pubKey.toHexKey()
cursor.addRow(arrayOf<Any>(result, result))
Expand Down
Loading

0 comments on commit 8a30860

Please sign in to comment.