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
import android.net.Uri
import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.decodePublicKey
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.
object Nip47 {

View File

@ -114,20 +114,38 @@ class UpdateZapAmountViewModel : ViewModel() {
account?.changeZapAmounts(amountSet)
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 privKey = try {
val privKeyHex = try {
unverifiedPrivKey?.let { decodePublicKey(it).toHexKey() }
} catch (e: Exception) {
null
}
account?.changeZapPaymentRequest(
Nip47URI(
walletConnectPubkey.text,
walletConnectRelay.text.ifBlank { null },
privKey
if (pubkeyHex != null) {
account?.changeZapPaymentRequest(
Nip47URI(
pubkeyHex,
relayUrl,
privKeyHex
)
)
)
} else {
account?.changeZapPaymentRequest(null)
}
} else {
account?.changeZapPaymentRequest(null)
}