Adds full nip19 parsing on key decoding capabilities.

This commit is contained in:
Vitor Pamplona
2023-04-03 14:01:43 -04:00
parent 20aca144fa
commit 3e666df896
2 changed files with 12 additions and 8 deletions

View File

@@ -38,14 +38,14 @@ fun HexKey.toDisplayHexKey(): String {
} }
fun decodePublicKey(key: String): ByteArray { fun decodePublicKey(key: String): ByteArray {
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray()
return if (key.startsWith("nsec")) { return if (key.startsWith("nsec")) {
Persona(privKey = key.bechToBytes()).pubKey Persona(privKey = key.bechToBytes()).pubKey
} else if (key.startsWith("npub")) { } else if (pubKeyParsed != null) {
key.bechToBytes() pubKeyParsed
} else if (key.startsWith("note")) { } else {
key.bechToBytes()
} else { // if (pattern.matcher(key).matches()) {
// } else {
Hex.decode(key) Hex.decode(key)
} }
} }

View File

@@ -4,6 +4,8 @@ import androidx.lifecycle.ViewModel
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.ServiceManager import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.toByteArray
import com.vitorpamplona.amethyst.service.nip19.Nip19
import fr.acinq.secp256k1.Hex import fr.acinq.secp256k1.Hex
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
@@ -39,12 +41,14 @@ class AccountStateViewModel() : ViewModel() {
fun login(key: String) { fun login(key: String) {
val pattern = Pattern.compile(".+@.+\\.[a-z]+") val pattern = Pattern.compile(".+@.+\\.[a-z]+")
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray()
val account = val account =
if (key.startsWith("nsec")) { if (key.startsWith("nsec")) {
Account(Persona(privKey = key.bechToBytes())) Account(Persona(privKey = key.bechToBytes()))
} else if (key.startsWith("npub")) { } else if (pubKeyParsed != null) {
Account(Persona(pubKey = key.bechToBytes())) Account(Persona(pubKey = pubKeyParsed))
} else if (pattern.matcher(key).matches()) { } else if (pattern.matcher(key).matches()) {
// Evaluate NIP-5 // Evaluate NIP-5
Account(Persona()) Account(Persona())