fix sending zaps using amber, send some default permissions on login, save the signer package name

This commit is contained in:
greenart7c3 2023-11-20 14:47:46 -03:00 committed by greenart7c3
parent c8365439ee
commit 6db031b208
6 changed files with 102 additions and 47 deletions

View File

@ -26,9 +26,11 @@ import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.events.ContactListEvent
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.LnZapEvent
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.NostrSignerInternal
import kotlinx.coroutines.Dispatchers
@ -89,6 +91,7 @@ private object PrefKeys {
const val AUTOMATICALLY_HIDE_NAV_BARS = "automatically_hide_nav_bars"
const val LOGIN_WITH_EXTERNAL_SIGNER = "login_with_external_signer"
const val AUTOMATICALLY_SHOW_PROFILE_PICTURE = "automatically_show_profile_picture"
const val SIGNER_PACKAGE_NAME = "signer_package_name"
const val ALL_ACCOUNT_INFO = "all_saved_accounts_info"
const val SHARED_SETTINGS = "shared_settings"
@ -249,6 +252,7 @@ object LocalPreferences {
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, account.signer is NostrSignerExternal)
if (account.signer is NostrSignerExternal) {
remove(PrefKeys.NOSTR_PRIVKEY)
putString(PrefKeys.SIGNER_PACKAGE_NAME, account.signer.launcher.signerPackageName)
} else {
account.keyPair.privKey?.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
}
@ -475,7 +479,8 @@ object LocalPreferences {
val keyPair = KeyPair(privKey = privKey?.hexToByteArray(), pubKey = pubKey.hexToByteArray())
val signer = if (loginWithExternalSigner) {
NostrSignerExternal(pubKey)
val packageName = getString(PrefKeys.SIGNER_PACKAGE_NAME, null) ?: "com.greenart7c3.nostrsigner"
NostrSignerExternal(pubKey, ExternalSignerLauncher(pubKey.hexToByteArray().toNpub(), packageName))
} else {
NostrSignerInternal(keyPair)
}

View File

@ -34,6 +34,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.ui.theme.QuoteBorder
import com.vitorpamplona.amethyst.ui.theme.placeholderText
@ -170,6 +171,7 @@ fun InvoiceRequest(
)
} else {
account.createZapRequestFor(toUserPubKeyHex, message, account.defaultZapType) { zapRequest ->
LocalCache.justConsume(zapRequest, null)
LightningAddressResolver().lnAddressInvoice(
lud16,
amount * 1000,

View File

@ -14,6 +14,8 @@ import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.encoders.bechToBytes
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.NostrSignerExternal
import com.vitorpamplona.quartz.signers.NostrSignerInternal
import kotlinx.coroutines.DelicateCoroutinesApi
@ -56,7 +58,8 @@ class AccountStateViewModel() : ViewModel() {
key: String,
useProxy: Boolean,
proxyPort: Int,
loginWithExternalSigner: Boolean = false
loginWithExternalSigner: Boolean = false,
packageName: String = ""
) = withContext(Dispatchers.IO) {
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.hexToByteArray()
@ -69,7 +72,8 @@ class AccountStateViewModel() : ViewModel() {
val account =
if (loginWithExternalSigner) {
val keyPair = KeyPair(pubKey = pubKeyParsed)
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerExternal(keyPair.pubKey.toHexKey()))
val localPackageName = packageName.ifBlank { "com.greenart7c3.nostrsigner" }
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerExternal(keyPair.pubKey.toHexKey(), ExternalSignerLauncher(keyPair.pubKey.toNpub(), localPackageName)))
} else if (key.startsWith("nsec")) {
val keyPair = KeyPair(privKey = key.bechToBytes())
Account(keyPair, proxy = proxy, proxyPort = proxyPort, signer = NostrSignerInternal(keyPair))
@ -130,11 +134,12 @@ class AccountStateViewModel() : ViewModel() {
useProxy: Boolean,
proxyPort: Int,
loginWithExternalSigner: Boolean = false,
packageName: String = "",
onError: () -> Unit
) {
viewModelScope.launch(Dispatchers.IO) {
try {
loginAndStartUI(key, useProxy, proxyPort, loginWithExternalSigner)
loginAndStartUI(key, useProxy, proxyPort, loginWithExternalSigner, packageName)
} catch (e: Exception) {
Log.e("Login", "Could not sign in", e)
onError()

View File

@ -108,7 +108,7 @@ fun LoginPage(
var loginWithExternalSigner by remember { mutableStateOf(false) }
if (loginWithExternalSigner) {
val externalSignerLauncher = remember { ExternalSignerLauncher("") }
val externalSignerLauncher = remember { ExternalSignerLauncher("", signerPackageName = "") }
val id = remember { UUID.randomUUID().toString() }
val launcher = rememberLauncherForActivityResult(
@ -162,8 +162,11 @@ fun LoginPage(
SignerType.GET_PUBLIC_KEY,
"",
id
) { pubkey ->
) { result ->
println("AAAA- COME BACK")
val split = result.split("-")
val pubkey = split.first()
val packageName = if (split.size > 1) split[1] else ""
key.value = TextFieldValue(pubkey)
if (!acceptedTerms.value) {
termsAcceptanceIsRequired =
@ -175,7 +178,7 @@ fun LoginPage(
}
if (acceptedTerms.value && key.value.text.isNotBlank()) {
accountViewModel.login(key.value.text, useProxy.value, proxyPort.value.toInt(), true) {
accountViewModel.login(key.value.text, useProxy.value, proxyPort.value.toInt(), true, packageName) {
errorMessage = context.getString(R.string.invalid_key)
}
}
@ -432,33 +435,8 @@ fun LoginPage(
return@Button
}
val result = ExternalSignerLauncher("").getDataFromResolver(
SignerType.GET_PUBLIC_KEY,
arrayOf("login"),
contentResolver = Amethyst.instance.contentResolver
)
if (result == null) {
loginWithExternalSigner = true
return@Button
} else {
key.value = TextFieldValue(result)
if (key.value.text.isBlank()) {
errorMessage = context.getString(R.string.key_is_required)
return@Button
}
if (acceptedTerms.value && key.value.text.isNotBlank()) {
accountViewModel.login(
key.value.text,
useProxy.value,
proxyPort.value.toInt(),
true
) {
errorMessage = context.getString(R.string.invalid_key)
}
}
}
},
shape = RoundedCornerShape(Size35dp),
modifier = Modifier

View File

@ -19,9 +19,18 @@ enum class SignerType {
DECRYPT_ZAP_EVENT
}
class Permission(
val type: String,
val kind: Int? = null
) {
fun toJson(): String {
return "{\"type\":\"${type}\",\"kind\":${kind}}"
}
}
class ExternalSignerLauncher(
private val npub: String,
private val signerPackageName: String = "com.greenart7c3.nostrsigner"
val signerPackageName: String = "com.greenart7c3.nostrsigner"
) {
private val contentCache = LruCache<String, (String) -> Unit>(20)
@ -49,9 +58,11 @@ class ExternalSignerLauncher(
fun newResult(data: Intent) {
val signature = data.getStringExtra("signature") ?: ""
val packageName = data.getStringExtra("package") ?: ""
val id = data.getStringExtra("id") ?: ""
if (id.isNotBlank()) {
contentCache.get(id)?.invoke(signature)
val result = if (packageName.isNotBlank()) "$signature-$packageName" else signature
contentCache.get(id)?.invoke(result)
}
}
@ -70,6 +81,40 @@ class ExternalSignerLauncher(
}
}
private fun defaultPermissions(): String {
val permissions = listOf(
Permission(
"sign_event",
22242
),
Permission(
"nip04_encrypt"
),
Permission(
"nip04_decrypt"
),
Permission(
"nip44_encrypt"
),
Permission(
"nip44_decrypt"
),
Permission(
"decrypt_zap_event"
),
)
val jsonArray = StringBuilder("[")
permissions.forEachIndexed { index, permission ->
jsonArray.append(permission.toJson())
if (index < permissions.size - 1) {
jsonArray.append(",")
}
}
jsonArray.append("]")
return jsonArray.toString()
}
private fun openSignerApp(
data: String,
type: SignerType,
@ -93,8 +138,12 @@ class ExternalSignerLauncher(
intent.putExtra("id", id)
if (type !== SignerType.GET_PUBLIC_KEY) {
intent.putExtra("current_user", npub)
} else {
intent.putExtra("permissions", defaultPermissions())
}
if (signerPackageName.isNotBlank()) {
intent.`package` = signerPackageName
}
contentCache.put(id, onReady)

View File

@ -34,6 +34,20 @@ class NostrSignerExternal(
)
launcher.openSigner(event) { signature ->
if (signature.startsWith("{")) {
val localEvent = Event.fromJson(signature)
(EventFactory.create(
localEvent.id,
localEvent.pubKey,
localEvent.createdAt,
localEvent.kind,
localEvent.tags,
localEvent.content,
localEvent.sig
) as? T?)?.let {
onReady(it)
}
} else {
(EventFactory.create(
event.id,
event.pubKey,
@ -46,6 +60,8 @@ class NostrSignerExternal(
onReady(it)
}
}
}
}
override fun nip04Encrypt(decryptedContent: String, toPublicKey: HexKey, onReady: (String)-> Unit) {