From 750910325610a6cba38ef3043fcc042f054fedf4 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Sat, 3 Jan 2026 16:51:18 -0500 Subject: [PATCH] Add Android Keystore protection for Nostr private keys in APK builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add @capacitor-community/secure-storage plugin to Capacitor dependencies - Inject localStorage shim that intercepts 'nostr:*' keys - Redirect sensitive keys to Android Keystore via SecureStorage plugin - Users' nsec keys are now encrypted at rest using hardware-backed security 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- scripts/setup-apk-build-service.sh | 131 ++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/scripts/setup-apk-build-service.sh b/scripts/setup-apk-build-service.sh index bff6485c..8dde6824 100755 --- a/scripts/setup-apk-build-service.sh +++ b/scripts/setup-apk-build-service.sh @@ -666,7 +666,8 @@ export async function buildAPK(buildId, zipBuffer, config, builds, timeout) { build.progress = 20; log('Installing Capacitor dependencies (this may take a minute)...'); // Use Capacitor 5.x for Java 17 compatibility (Capacitor 6+ requires Java 21) - await execAsync('npm install @capacitor/cli@5 @capacitor/core@5 @capacitor/android@5 --loglevel=error', { + // Include secure-storage plugin for Android Keystore protection of sensitive data (nsec) + await execAsync('npm install @capacitor/cli@5 @capacitor/core@5 @capacitor/android@5 capacitor-secure-storage-plugin@0.9.0 --loglevel=error', { cwd: buildDir, timeout: 180000 }); @@ -709,6 +710,10 @@ export async function buildAPK(buildId, zipBuffer, config, builds, timeout) { timeout: 120000 }); + // Inject secure storage shim to protect Nostr keys (nsec) using Android Keystore + build.progress = 52; + await injectSecureStorageShim(buildDir, log); + // Update app icon - use provided icon or auto-detect from project build.progress = 55; let iconSource = null; @@ -903,6 +908,130 @@ async function updateAppIconFromFile(buildDir, iconFilePath) { } } +/** + * Inject secure storage shim into index.html to protect sensitive Nostr data (nsec). + * This intercepts localStorage operations for 'nostr:*' keys and redirects them + * to Capacitor's SecureStorage plugin, which uses Android Keystore for encryption. + */ +async function injectSecureStorageShim(buildDir, log) { + const indexPath = path.join(buildDir, 'android/app/src/main/assets/public/index.html'); + + try { + let html = await fs.readFile(indexPath, 'utf8'); + + // The shim script that intercepts localStorage for nostr:* keys + const shimScript = ` +`; + + // Inject the shim script right after tag so it runs before any app code + if (html.includes('')) { + html = html.replace('', '' + shimScript); + await fs.writeFile(indexPath, html, 'utf8'); + log('Secure storage shim injected for Nostr key protection'); + return true; + } else { + log('Warning: Could not find tag to inject secure storage shim'); + return false; + } + } catch (error) { + log(`Warning: Failed to inject secure storage shim: ${error.message}`); + return false; + } +} + async function autoDetectIcon(distPath, log) { // Common icon file patterns to search for (in priority order) const iconPatterns = [