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 {
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray()
return if (key.startsWith("nsec")) {
Persona(privKey = key.bechToBytes()).pubKey
} else if (key.startsWith("npub")) {
key.bechToBytes()
} else if (key.startsWith("note")) {
key.bechToBytes()
} else { // if (pattern.matcher(key).matches()) {
// } else {
} else if (pubKeyParsed != null) {
pubKeyParsed
} else {
Hex.decode(key)
}
}

View File

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