feat: Implement secure database operations and caching for encrypted accounts
This commit is contained in:
@@ -29,6 +29,10 @@ object CryptoManager {
|
||||
private const val KEY_SIZE = 256
|
||||
private const val SALT = "rosetta"
|
||||
|
||||
// 🚀 ОПТИМИЗАЦИЯ: Кэш для генерации ключей (seedPhrase -> KeyPair)
|
||||
private val keyPairCache = mutableMapOf<String, KeyPairData>()
|
||||
private val privateKeyHashCache = mutableMapOf<String, String>()
|
||||
|
||||
init {
|
||||
// Add BouncyCastle provider for secp256k1 support
|
||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
||||
@@ -74,8 +78,14 @@ object CryptoManager {
|
||||
|
||||
/**
|
||||
* Generate key pair from seed phrase using secp256k1 curve
|
||||
* 🚀 ОПТИМИЗАЦИЯ: Кэшируем результаты для избежания повторных вычислений
|
||||
*/
|
||||
fun generateKeyPairFromSeed(seedPhrase: List<String>): KeyPairData {
|
||||
val cacheKey = seedPhrase.joinToString(" ")
|
||||
|
||||
// Проверяем кэш
|
||||
keyPairCache[cacheKey]?.let { return it }
|
||||
|
||||
val privateKeyHex = seedPhraseToPrivateKey(seedPhrase)
|
||||
val ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1")
|
||||
|
||||
@@ -91,20 +101,40 @@ object CryptoManager {
|
||||
val publicKeyHex = publicKeyPoint.getEncoded(false)
|
||||
.joinToString("") { "%02x".format(it) }
|
||||
|
||||
return KeyPairData(
|
||||
val keyPair = KeyPairData(
|
||||
privateKey = privateKeyHex.take(64),
|
||||
publicKey = publicKeyHex
|
||||
)
|
||||
|
||||
// Сохраняем в кэш (ограничиваем размер до 5 записей)
|
||||
keyPairCache[cacheKey] = keyPair
|
||||
if (keyPairCache.size > 5) {
|
||||
keyPairCache.remove(keyPairCache.keys.first())
|
||||
}
|
||||
|
||||
return keyPair
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate private key hash for protocol (SHA256(privateKey + "rosetta"))
|
||||
* 🚀 ОПТИМИЗАЦИЯ: Кэшируем хэши для избежания повторных вычислений
|
||||
*/
|
||||
fun generatePrivateKeyHash(privateKey: String): String {
|
||||
// Проверяем кэш
|
||||
privateKeyHashCache[privateKey]?.let { return it }
|
||||
|
||||
val data = (privateKey + SALT).toByteArray()
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
val hash = digest.digest(data)
|
||||
return hash.joinToString("") { "%02x".format(it) }
|
||||
val hashHex = hash.joinToString("") { "%02x".format(it) }
|
||||
|
||||
// Сохраняем в кэш
|
||||
privateKeyHashCache[privateKey] = hashHex
|
||||
if (privateKeyHashCache.size > 10) {
|
||||
privateKeyHashCache.remove(privateKeyHashCache.keys.first())
|
||||
}
|
||||
|
||||
return hashHex
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user