add connect with amber button
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package su.reya.coop
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import rust.nostr.sdk.PublicKey
|
||||
import rust.nostr.sdk.UnsignedEvent
|
||||
|
||||
@@ -9,7 +11,7 @@ import rust.nostr.sdk.UnsignedEvent
|
||||
*/
|
||||
interface ExternalSignerHandler {
|
||||
fun isAvailable(): Boolean
|
||||
|
||||
fun setPackageName(packageName: String)
|
||||
suspend fun getPublicKey(permissions: String? = null): ExternalSignerResult?
|
||||
suspend fun signEvent(event: UnsignedEvent, currentUser: PublicKey): String?
|
||||
suspend fun nip04Encrypt(plaintext: String, pubkey: PublicKey): String?
|
||||
@@ -18,7 +20,25 @@ interface ExternalSignerHandler {
|
||||
suspend fun nip44Decrypt(ciphertext: String, pubkey: PublicKey, currentUser: PublicKey): String?
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class SignerPermission(
|
||||
val type: String,
|
||||
val kind: Int? = null,
|
||||
)
|
||||
|
||||
object SignerPermissions {
|
||||
fun signEvent(kind: Int? = null) = SignerPermission(type = "sign_event", kind = kind)
|
||||
fun nip04Encrypt() = SignerPermission(type = "nip04_encrypt")
|
||||
fun nip04Decrypt() = SignerPermission(type = "nip04_decrypt")
|
||||
fun nip44Encrypt() = SignerPermission(type = "nip44_encrypt")
|
||||
fun nip44Decrypt() = SignerPermission(type = "nip44_decrypt")
|
||||
|
||||
fun toJson(permissions: List<SignerPermission>): String {
|
||||
return Json.encodeToString(permissions)
|
||||
}
|
||||
}
|
||||
|
||||
data class ExternalSignerResult(
|
||||
val pubkey: String,
|
||||
val pubkey: PublicKey,
|
||||
val packageName: String,
|
||||
)
|
||||
@@ -7,7 +7,6 @@ import rust.nostr.sdk.UnsignedEvent
|
||||
|
||||
class ExternalSignerProxy(
|
||||
private val handler: ExternalSignerHandler,
|
||||
private val packageName: String,
|
||||
private val currentUser: PublicKey,
|
||||
) : AsyncNostrSigner {
|
||||
override suspend fun getPublicKeyAsync(): PublicKey {
|
||||
|
||||
@@ -372,33 +372,6 @@ class NostrViewModel(
|
||||
return keys
|
||||
}
|
||||
|
||||
private suspend fun createSigner(secret: String): AsyncNostrSigner {
|
||||
return when {
|
||||
secret.startsWith("nsec1") -> Keys.parse(secret)
|
||||
|
||||
secret.startsWith("bunker://") -> {
|
||||
val appKeys = getOrInitAppKeys()
|
||||
val bunker = NostrConnectUri.parse(secret)
|
||||
val timeout = 50.seconds // or Duration.parse("50s")
|
||||
NostrConnect(uri = bunker, appKeys, timeout, null)
|
||||
}
|
||||
|
||||
secret.startsWith("nip55://") -> {
|
||||
val handler = externalSignerHandler
|
||||
?: throw IllegalStateException("External signer not available on this platform")
|
||||
|
||||
// Format: nip55://packageName/hexPubkey
|
||||
val parts = secret.removePrefix("nip55://").split("/", limit = 2)
|
||||
val packageName = parts[0]
|
||||
val pubkey = PublicKey.parse(parts[1])
|
||||
|
||||
ExternalSignerProxy(handler, packageName, pubkey)
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException("Invalid secret format")
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun blossomUpload(file: ByteArray, contentType: String): String? {
|
||||
try {
|
||||
// Upload picture to Blossom
|
||||
@@ -471,6 +444,34 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun createSigner(secret: String): AsyncNostrSigner {
|
||||
return when {
|
||||
secret.startsWith("nsec1") -> Keys.parse(secret)
|
||||
|
||||
secret.startsWith("bunker://") -> {
|
||||
val appKeys = getOrInitAppKeys()
|
||||
val bunker = NostrConnectUri.parse(secret)
|
||||
val timeout = 50.seconds // or Duration.parse("50s")
|
||||
NostrConnect(uri = bunker, appKeys, timeout, null)
|
||||
}
|
||||
|
||||
secret.startsWith("nip55://") -> {
|
||||
val handler = externalSignerHandler
|
||||
?: throw IllegalStateException("External signer not available on this platform")
|
||||
|
||||
// Format: nip55://packageName/hexPubkey
|
||||
val parts = secret.removePrefix("nip55://").split("/", limit = 2)
|
||||
val packageName = parts[0]
|
||||
val pubkey = PublicKey.parse(parts[1])
|
||||
|
||||
handler.setPackageName(packageName)
|
||||
ExternalSignerProxy(handler, pubkey)
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException("Invalid secret format")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun verifyIdentity(secret: String): PublicKey? {
|
||||
try {
|
||||
val signer = createSigner(secret)
|
||||
@@ -498,6 +499,39 @@ class NostrViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun connectExternalSigner() {
|
||||
val handler = externalSignerHandler ?: throw IllegalStateException("Signer not available")
|
||||
_isLoggedIn.value = true
|
||||
try {
|
||||
val permissions = SignerPermissions.toJson(
|
||||
listOf(
|
||||
SignerPermissions.signEvent(),
|
||||
SignerPermissions.nip04Encrypt(),
|
||||
SignerPermissions.nip04Decrypt(),
|
||||
SignerPermissions.nip44Encrypt(),
|
||||
SignerPermissions.nip44Decrypt(),
|
||||
)
|
||||
)
|
||||
val result = handler.getPublicKey(permissions) ?: throw Exception("Rejected")
|
||||
val signer = ExternalSignerProxy(handler, result.pubkey)
|
||||
|
||||
// Update signer
|
||||
nostr.setSigner(signer)
|
||||
|
||||
// Store the signer in the secret storage
|
||||
secretStore.set("user_signer", "nip55://${result.packageName}/${result.pubkey.toHex()}")
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
} finally {
|
||||
_signerRequired.value = false
|
||||
_isLoggedIn.value = false
|
||||
}
|
||||
}
|
||||
|
||||
fun isExternalSignerAvailable(): Boolean {
|
||||
return externalSignerHandler?.isAvailable() == true
|
||||
}
|
||||
|
||||
suspend fun useDefaultMsgRelayList() {
|
||||
try {
|
||||
val defaultRelays = nostr.getDefaultMsgRelayList()
|
||||
|
||||
Reference in New Issue
Block a user