on master help sign in
diagonal/scripts/gen-key.sh
33 lines 1.4 kB history raw
1#!/usr/bin/env bash
2# Pins the extension ID (section 14). Creates key.pem once (keep it out of the repo), writes the
3# public key into manifest.json, the ID into ./extension-id and into host/diagonal-host.py's
4# ALLOWED_ORIGIN, and prints the ID. Re-running with an existing key.pem changes nothing.
5set -euo pipefail
6cd "$(dirname "$0")/.."
8if [[ ! -f key.pem ]]; then
9 openssl genrsa 2048 2>/dev/null | openssl pkcs8 -topk8 -nocrypt -out key.pem
10 chmod 600 key.pem
11 echo "created key.pem (private; not needed unless you pack a .crx)" >&2
12fi
14KEY=$(openssl rsa -in key.pem -pubout -outform DER 2>/dev/null | openssl base64 -A)
15ID=$(openssl rsa -in key.pem -pubout -outform DER 2>/dev/null | openssl dgst -sha256 -binary | od -An -tx1 | tr -d " \n" | head -c 32 | tr 0-9a-f a-p)
17python3 - "$KEY" "$ID" <<'PY'
18import json, re, sys
19key, ext_id = sys.argv[1], sys.argv[2]
20with open("manifest.json", encoding="utf-8") as f:
21 m = json.load(f)
22m["key"] = key
23with open("manifest.json", "w", encoding="utf-8") as f:
24 json.dump(m, f, indent=2, ensure_ascii=False)
25 f.write("\n")
26p = "host/diagonal-host.py"
27s = open(p, encoding="utf-8").read()
28s = re.sub(r'^ALLOWED_ORIGIN = ".*"$', f'ALLOWED_ORIGIN = "chrome-extension://{ext_id}/"', s, count=1, flags=re.M)
29open(p, "w", encoding="utf-8").write(s)
30open("extension-id", "w").write(ext_id + "\n")
31PY
33echo "$ID"