package com.turbonativegoproxy import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.Promise import com.facebook.react.module.annotations.ReactModule import android.security.keystore.KeyGenParameterSpec import android.security.keystore.KeyProperties import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import javax.crypto.KeyGenerator import javax.crypto.SecretKey @ReactModule(name = TurboNativeGoproxyModule.NAME) class TurboNativeGoproxyModule(reactContext: ReactApplicationContext) : NativeTurboNativeGoproxySpec(reactContext) { private val sharedPreferences by lazy { val masterKey = MasterKey.Builder(reactApplicationContext) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build() EncryptedSharedPreferences.create( reactApplicationContext, "turbo_native_goproxy_secure_prefs", masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) } override fun getName(): String { return NAME } // Example method // See https://reactnative.dev/docs/native-modules-android override fun multiply(a: Double, b: Double): Double { return a * b } override fun setSecureItem(key: String, value: String, promise: Promise) { try { sharedPreferences.edit().putString(key, value).apply() promise.resolve(null) } catch (e: Exception) { promise.reject("SECURE_STORAGE_ERROR", "Failed to set secure item: ${e.message}", e) } } override fun getSecureItem(key: String, promise: Promise) { try { val value = sharedPreferences.getString(key, null) promise.resolve(value) } catch (e: Exception) { promise.reject("SECURE_STORAGE_ERROR", "Failed to get secure item: ${e.message}", e) } } override fun removeSecureItem(key: String, promise: Promise) { try { sharedPreferences.edit().remove(key).apply() promise.resolve(null) } catch (e: Exception) { promise.reject("SECURE_STORAGE_ERROR", "Failed to remove secure item: ${e.message}", e) } } override fun hasSecureItem(key: String, promise: Promise) { try { val exists = sharedPreferences.contains(key) promise.resolve(exists) } catch (e: Exception) { promise.reject("SECURE_STORAGE_ERROR", "Failed to check secure item: ${e.message}", e) } } companion object { const val NAME = "TurboNativeGoproxy" } }