mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-26 19:46:57 +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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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())
|
||||||
|
Reference in New Issue
Block a user