Skip to content

Commit

Permalink
Merge pull request #33 from KoalaSat/revert-32-main
Browse files Browse the repository at this point in the history
Revert "Fix relay auth and content resolver"
  • Loading branch information
KoalaSat authored Nov 5, 2024
2 parents 2903d06 + 21030cd commit 4a586ac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
5 changes: 0 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
<action android:name="android.intent.action.VIEW" />
<data android:scheme="nostr" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="nostrsigner" />
</intent>
</queries>

<application
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/java/com/koalasat/pokey/models/EncryptedStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ object PrefKeys {
const val NOTIFY_MENTIONS = "notify_mentions"
const val NOTIFY_REPOSTS = "notify_reposts"
const val NOTIFY_FOLLOWS = "notify_follows"
const val EXTERNAL_SIGNER = "external_signer"
}
object DefaultKeys {
const val BROADCAST = true
Expand All @@ -30,7 +29,6 @@ object DefaultKeys {
const val NOTIFY_MENTIONS = true
const val NOTIFY_REPOSTS = true
const val NOTIFY_FOLLOWS = true
const val EXTERNAL_SIGNER = "com.greenart7c3.nostrsigner"
}

object EncryptedStorage {
Expand Down Expand Up @@ -59,8 +57,6 @@ object EncryptedStorage {
val notifyResposts: LiveData<Boolean> get() = _notifyResposts
private val _notifyFollows = MutableLiveData<Boolean>().apply { DefaultKeys.NOTIFY_FOLLOWS }
val notifyFollows: LiveData<Boolean> get() = _notifyFollows
private val _externalSigner = MutableLiveData<String>().apply { DefaultKeys.EXTERNAL_SIGNER }
val externalSigner: LiveData<String> get() = _externalSigner

fun init(context: Context) {
val masterKey: MasterKey =
Expand All @@ -86,12 +82,6 @@ object EncryptedStorage {
_notifyMentions.value = sharedPreferences.getBoolean(PrefKeys.NOTIFY_MENTIONS, DefaultKeys.NOTIFY_MENTIONS)
_notifyResposts.value = sharedPreferences.getBoolean(PrefKeys.NOTIFY_REPOSTS, DefaultKeys.NOTIFY_REPOSTS)
_notifyFollows.value = sharedPreferences.getBoolean(PrefKeys.NOTIFY_FOLLOWS, DefaultKeys.NOTIFY_FOLLOWS)
_externalSigner.value = sharedPreferences.getString(PrefKeys.EXTERNAL_SIGNER, DefaultKeys.EXTERNAL_SIGNER) ?: DefaultKeys.EXTERNAL_SIGNER
}

fun updateExternalSigner(newValue: String) {
sharedPreferences.edit().putString(PrefKeys.EXTERNAL_SIGNER, newValue).apply()
_externalSigner.value = newValue
}

fun updatePubKey(newValue: String) {
Expand Down
47 changes: 25 additions & 22 deletions app/src/main/java/com/koalasat/pokey/models/ExternalSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.koalasat.pokey.Pokey
import com.koalasat.pokey.R
import com.vitorpamplona.quartz.events.RelayAuthEvent
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.SignerType
import com.vitorpamplona.quartz.utils.TimeUtils
import java.util.UUID
import kotlin.coroutines.cancellation.CancellationException

object ExternalSigner {
private lateinit var nostrSignerLauncher: ActivityResultLauncher<Intent>
private lateinit var externalSignerLauncher: NostrSignerExternal
private lateinit var externalSignerLauncher: ExternalSignerLauncher

fun init(activity: AppCompatActivity) {
nostrSignerLauncher = activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK) {
Log.e("Pokey", "ExternalSigner result error: ${result.resultCode}")
Toast.makeText(activity, activity.getString(R.string.amber_not_found), Toast.LENGTH_SHORT).show()
} else {
result.data?.let { externalSignerLauncher.launcher.newResult(it) }
result.data?.let { externalSignerLauncher.newResult(it) }
}
}

startLauncher()
}

fun savePubKey() {
externalSignerLauncher.launcher.openSignerApp(
externalSignerLauncher.openSignerApp(
"",
SignerType.GET_PUBLIC_KEY,
"",
Expand All @@ -45,15 +45,13 @@ object ExternalSigner {
val pubkey = split.first()
if (split.first().isNotEmpty()) {
EncryptedStorage.updatePubKey(pubkey)
if (split.size > 1) {
EncryptedStorage.updateExternalSigner(split[1])
}
startLauncher()
}
}
}

fun auth(relayUrl: String, challenge: String, onReady: (RelayAuthEvent) -> Unit) {
fun auth(relayUrl: String, challenge: String, onReady: (String) -> Unit) {
val pubKey = Pokey.getInstance().getHexKey()
val createdAt = TimeUtils.now()
val kind = 22242
val content = ""
Expand All @@ -62,23 +60,28 @@ object ExternalSigner {
arrayOf("relay", relayUrl),
arrayOf("challenge", challenge),
)

externalSignerLauncher.sign(
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
onReady = onReady,
val id = Event.generateId(pubKey, createdAt, kind, tags, content).toHexKey()
val event =
Event(
id = id,
pubKey = pubKey,
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
sig = "",
)
externalSignerLauncher.openSigner(
event,
onReady,
)
}

private fun startLauncher() {
val pubKey = Pokey.getInstance().getHexKey()
var externalSignerPackage = EncryptedStorage.externalSigner.value
if (externalSignerPackage == null) externalSignerPackage = ""
if (pubKey.isEmpty()) externalSignerPackage = ""
externalSignerLauncher = NostrSignerExternal(pubKey, ExternalSignerLauncher(pubKey, signerPackageName = externalSignerPackage))
externalSignerLauncher.launcher.registerLauncher(
var pubKey = EncryptedStorage.pubKey.value
if (pubKey == null) pubKey = ""
externalSignerLauncher = ExternalSignerLauncher(pubKey, signerPackageName = "")
externalSignerLauncher.registerLauncher(
launcher = {
try {
nostrSignerLauncher.launch(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class NotificationsService : Service() {
override fun onAuth(relay: Relay, challenge: String) {
Log.d("Pokey", "Relay on Auth: ${relay.url} : $challenge")
ExternalSigner.auth(relay.url, challenge) { result ->
Client.sendIfExists(result, relay)
Log.d("Pokey", "Relay on Auth response: ${relay.url} : $result")
}
}
Expand Down

0 comments on commit 4a586ac

Please sign in to comment.