Adds NIP49 to login and back up key screens
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
package com.vitorpamplona.quartz.crypto
|
||||
|
||||
import android.util.Log
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import fr.acinq.secp256k1.Secp256k1
|
||||
import java.security.MessageDigest
|
||||
@@ -34,6 +35,7 @@ object CryptoUtils {
|
||||
private val nip04 = Nip04(secp256k1, random)
|
||||
private val nip44v1 = Nip44v1(secp256k1, random)
|
||||
private val nip44v2 = Nip44v2(secp256k1, random)
|
||||
private val nip49 = Nip49(secp256k1, random)
|
||||
|
||||
fun clearCache() {
|
||||
nip04.clearCache()
|
||||
@@ -262,6 +264,21 @@ object CryptoUtils {
|
||||
}
|
||||
}
|
||||
|
||||
fun decryptNIP49(
|
||||
payload: String,
|
||||
password: String,
|
||||
): String? {
|
||||
if (payload.isEmpty() || password.isEmpty()) return null
|
||||
return nip49.decrypt(payload, password)
|
||||
}
|
||||
|
||||
fun encryptNIP49(
|
||||
key: HexKey,
|
||||
password: String,
|
||||
): String? {
|
||||
return nip49.encrypt(key, password, 16, Nip49.EncryptedInfo.CLIENT_DOES_NOT_TRACK)
|
||||
}
|
||||
|
||||
class EncryptedInfoString(
|
||||
val ciphertext: String,
|
||||
val nonce: String,
|
||||
|
||||
@@ -39,14 +39,14 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
||||
fun decrypt(
|
||||
nCryptSec: String,
|
||||
password: String,
|
||||
): HexKey? {
|
||||
): HexKey {
|
||||
return decrypt(EncryptedInfo.decodePayload(nCryptSec), password)
|
||||
}
|
||||
|
||||
fun decrypt(
|
||||
encryptedInfo: EncryptedInfo?,
|
||||
password: String = "",
|
||||
): String? {
|
||||
): String {
|
||||
check(encryptedInfo != null) { "Couldn't decode key" }
|
||||
check(encryptedInfo.version == EncryptedInfo.V) { "invalid version" }
|
||||
|
||||
@@ -66,6 +66,8 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
||||
key,
|
||||
)
|
||||
|
||||
check(m.any { it > 0 }) { "Incorrect password" }
|
||||
|
||||
return m.toHexKey()
|
||||
}
|
||||
|
||||
@@ -129,9 +131,9 @@ class Nip49(val secp256k1: Secp256k1, val random: SecureRandom) {
|
||||
companion object {
|
||||
const val V: Byte = 0x02
|
||||
|
||||
const val UNSAFE_HANDLING = 0x00
|
||||
const val SAFE_HANDLING = 0x01
|
||||
const val CLIENT_DOES_NOT_TRACK = 0x02
|
||||
const val UNSAFE_HANDLING: Byte = 0x00
|
||||
const val SAFE_HANDLING: Byte = 0x01
|
||||
const val CLIENT_DOES_NOT_TRACK: Byte = 0x02
|
||||
|
||||
fun decodePayload(nCryptSec: String): EncryptedInfo? {
|
||||
val byteArray =
|
||||
|
||||
Reference in New Issue
Block a user