|
| 1 | +package dev.nse |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.security.keystore.KeyGenParameterSpec |
| 5 | +import android.security.keystore.KeyProperties |
| 6 | +import androidx.biometric.BiometricPrompt |
| 7 | +import java.security.KeyPairGenerator |
| 8 | +import java.security.KeyStore |
| 9 | + |
| 10 | +/** |
| 11 | + * Nostr Secure Enclave — Android implementation |
| 12 | + * Hardware-backed key management using StrongBox/TEE P-256 to protect secp256k1 keys |
| 13 | + */ |
| 14 | +object NSE { |
| 15 | + |
| 16 | + private const val KEYSTORE_PROVIDER = "AndroidKeyStore" |
| 17 | + private const val ENCLAVE_KEY_ALIAS = "dev.nse.enclave.p256" |
| 18 | + private const val BLOB_PREF_KEY = "dev.nse.encrypted.secp256k1" |
| 19 | + |
| 20 | + /** |
| 21 | + * Generate a new secp256k1 keypair, protected by a StrongBox/TEE P-256 key |
| 22 | + */ |
| 23 | + suspend fun generate(context: Context): KeyInfo { |
| 24 | + // TODO: Phase 2 implementation |
| 25 | + // 1. Generate P-256 key in StrongBox/TEE (setIsStrongBoxBacked, biometric-gated) |
| 26 | + // 2. Generate secp256k1 keypair |
| 27 | + // 3. Derive AES-256-GCM key from P-256 via ECDH + HKDF |
| 28 | + // 4. Encrypt secp256k1 private key with AES key |
| 29 | + // 5. Store encrypted blob in EncryptedSharedPreferences |
| 30 | + // 6. Zero the plaintext: privateKeyBytes.fill(0) |
| 31 | + // 7. Return pubkey + npub |
| 32 | + throw NotImplementedError("Not yet implemented") |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Sign a Nostr event |
| 37 | + * Biometric unlock → decrypt secp256k1 key → Schnorr sign → zero memory |
| 38 | + */ |
| 39 | + suspend fun sign(event: NostrEvent, promptInfo: BiometricPrompt.PromptInfo): SignedEvent { |
| 40 | + // TODO: Phase 2 implementation |
| 41 | + // 1. Biometric unlock via BiometricPrompt |
| 42 | + // 2. Access KeyStore P-256 key |
| 43 | + // 3. Derive AES key |
| 44 | + // 4. Decrypt secp256k1 private key into memory |
| 45 | + // 5. Schnorr sign the event (BIP-340) |
| 46 | + // 6. Zero the plaintext: privateKeyBytes.fill(0) |
| 47 | + // 7. Return signed event |
| 48 | + throw NotImplementedError("Not yet implemented") |
| 49 | + } |
| 50 | + |
| 51 | + /** Get the hex pubkey (does not require biometric unlock) */ |
| 52 | + suspend fun getPublicKey(context: Context): String { |
| 53 | + throw NotImplementedError("Not yet implemented") |
| 54 | + } |
| 55 | + |
| 56 | + /** Get the bech32 npub */ |
| 57 | + suspend fun getNpub(context: Context): String { |
| 58 | + throw NotImplementedError("Not yet implemented") |
| 59 | + } |
| 60 | + |
| 61 | + /** Check if a key exists */ |
| 62 | + fun exists(): Boolean { |
| 63 | + val keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER) |
| 64 | + keyStore.load(null) |
| 65 | + return keyStore.containsAlias(ENCLAVE_KEY_ALIAS) |
| 66 | + } |
| 67 | + |
| 68 | + /** Wipe all key material */ |
| 69 | + suspend fun destroy(context: Context) { |
| 70 | + // TODO: Delete KeyStore entry + EncryptedSharedPreferences blob |
| 71 | + val keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER) |
| 72 | + keyStore.load(null) |
| 73 | + keyStore.deleteEntry(ENCLAVE_KEY_ALIAS) |
| 74 | + } |
| 75 | + |
| 76 | + // Types |
| 77 | + |
| 78 | + data class KeyInfo( |
| 79 | + val pubkey: String, |
| 80 | + val npub: String, |
| 81 | + val createdAt: Long, |
| 82 | + val hardwareBacked: Boolean |
| 83 | + ) |
| 84 | + |
| 85 | + data class NostrEvent( |
| 86 | + val kind: Int, |
| 87 | + val content: String, |
| 88 | + val tags: List<List<String>>, |
| 89 | + val createdAt: Long |
| 90 | + ) |
| 91 | + |
| 92 | + data class SignedEvent( |
| 93 | + val id: String, |
| 94 | + val pubkey: String, |
| 95 | + val sig: String, |
| 96 | + val kind: Int, |
| 97 | + val content: String, |
| 98 | + val tags: List<List<String>>, |
| 99 | + val createdAt: Long |
| 100 | + ) |
| 101 | +} |
0 commit comments