More forgiving Wallet Connect interface (nsec or hexes are accepted, relay url is trimmed and cleaned up, if relay doesn't have a wss://, adds one)

This commit is contained in:
Vitor Pamplona 2023-03-29 16:21:11 -04:00
parent 5038588d70
commit 0abac47ff2
2 changed files with 27 additions and 8 deletions

View File

@ -1,10 +1,11 @@
package com.vitorpamplona.amethyst.ui.note package com.vitorpamplona.amethyst.ui.note
import android.net.Uri import android.net.Uri
import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.decodePublicKey import com.vitorpamplona.amethyst.model.decodePublicKey
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
data class Nip47URI(val pubKeyHex: String, val relayUri: String?, val secret: String?) data class Nip47URI(val pubKeyHex: HexKey, val relayUri: String?, val secret: HexKey?)
// Rename to the corect nip number when ready. // Rename to the corect nip number when ready.
object Nip47 { object Nip47 {

View File

@ -114,23 +114,41 @@ class UpdateZapAmountViewModel : ViewModel() {
account?.changeZapAmounts(amountSet) account?.changeZapAmounts(amountSet)
if (walletConnectRelay.text.isNotBlank() && walletConnectPubkey.text.isNotBlank()) { if (walletConnectRelay.text.isNotBlank() && walletConnectPubkey.text.isNotBlank()) {
val pubkeyHex = try {
decodePublicKey(walletConnectPubkey.text.trim()).toHexKey()
} catch (e: Exception) {
null
}
val relayUrl = walletConnectRelay.text.ifBlank { null }?.let {
var addedWSS =
if (!it.startsWith("wss://") && !it.startsWith("ws://")) "wss://$it" else it
if (addedWSS.endsWith("/")) addedWSS = addedWSS.dropLast(1)
addedWSS
}
val unverifiedPrivKey = walletConnectSecret.text.ifBlank { null } val unverifiedPrivKey = walletConnectSecret.text.ifBlank { null }
val privKey = try { val privKeyHex = try {
unverifiedPrivKey?.let { decodePublicKey(it).toHexKey() } unverifiedPrivKey?.let { decodePublicKey(it).toHexKey() }
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
if (pubkeyHex != null) {
account?.changeZapPaymentRequest( account?.changeZapPaymentRequest(
Nip47URI( Nip47URI(
walletConnectPubkey.text, pubkeyHex,
walletConnectRelay.text.ifBlank { null }, relayUrl,
privKey privKeyHex
) )
) )
} else { } else {
account?.changeZapPaymentRequest(null) account?.changeZapPaymentRequest(null)
} }
} else {
account?.changeZapPaymentRequest(null)
}
nextAmount = TextFieldValue("") nextAmount = TextFieldValue("")
} }