mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-19 16:51:08 +02:00
Adds full nip19 parsing on key decoding capabilities.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
@@ -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())
|
||||
|
Reference in New Issue
Block a user