#!/usr/bin/env python3 """Create a persistent local signing identity for the independent Android app.""" import os from pathlib import Path import secrets import shutil import subprocess root = Path(__file__).resolve().parents[1] directory = root / "work/android-signing" directory.mkdir(parents=True, exist_ok=True, mode=0o700) directory.chmod(0o700) properties = directory / "standalone.properties" keystore = directory / "standalone.jks" if properties.exists() and keystore.exists(): print("Reusing the independent app signing identity") elif properties.exists() or keystore.exists(): raise SystemExit("Incomplete signing identity; restore its backup instead of generating a new key") else: java_home = os.environ.get("JAVA_HOME", "/Applications/Android Studio.app/Contents/jbr/Contents/Home") keytool = Path(java_home) / "bin/keytool" if not keytool.is_file(): keytool = shutil.which("keytool") if not keytool: raise SystemExit("Set JAVA_HOME to a JDK containing keytool") password = secrets.token_hex(32) environment = dict(os.environ, MESHPORT_SIGNING_PASSWORD=password) subprocess.run([ str(keytool), "-genkeypair", "-keystore", str(keystore), "-storetype", "JKS", "-storepass:env", "MESHPORT_SIGNING_PASSWORD", "-keypass:env", "MESHPORT_SIGNING_PASSWORD", "-alias", "meshportstandalone", "-keyalg", "RSA", "-keysize", "3072", "-validity", "10000", "-dname", "CN=MeshPort Standalone, O=MeshPort", "-noprompt", ], env=environment, check=True) keystore.chmod(0o600) with properties.open("x") as output: properties.chmod(0o600) output.write(f"storeFile={keystore}\nstorePassword={password}\n" f"keyAlias=meshportstandalone\nkeyPassword={password}\n") print("Created the independent app signing identity; keep work/android-signing for future upgrades")