set-gateway.sh raw
1 #!/bin/sh
2 # Register the gateway public key on swaprelay.smesh.lol.
3 #
4 # set-gateway.sh <gateway-public-key-hex>
5 #
6 # The three gateway-only rules carry an all-zeros placeholder until this is run, so those kinds
7 # are refused rather than trusted. The public key is the one nostrino-bridge prints in its URI:
8 # nostr+chaindata://<PUBKEY>?chains=...
9 set -eu
10
11 key=$(printf '%s' "${1:-}" | tr 'A-Z' 'a-z')
12
13 case "$key" in
14 *[!0-9a-f]*) echo "set-gateway: not hex: $key" >&2; exit 2 ;;
15 esac
16
17 if [ "${#key}" -ne 64 ]; then
18 echo "set-gateway: expected 64 hex characters, got ${#key}" >&2
19 exit 2
20 fi
21
22 policy=/etc/torly/policy/swaprelay.json
23
24 if [ ! -w "$policy" ]; then
25 echo "set-gateway: $policy is not writable (run as root)" >&2
26 exit 2
27 fi
28
29 cp -a "$policy" "$policy.bak"
30
31 old=$(sed -n 's/.*"authors": \["\([0-9a-f]\{64\}\)"\].*/\1/p' "$policy" | head -1)
32
33 sed -i "s/$old/$key/g" "$policy"
34
35 old_short=$(printf %s "$old" | cut -c1-16)
36 echo "replaced the gateway key ${old_short}… with $key"
37 systemctl restart torly-swap
38 sleep 2
39
40 if systemctl is-active --quiet torly-swap; then
41 echo "torly-swap is active"
42 curl -fsS --max-time 5 http://127.0.0.1:3337/health && echo
43 else
44 echo "torly-swap failed to start; restoring $policy.bak" >&2
45 mv "$policy.bak" "$policy"
46 systemctl restart torly-swap
47 exit 1
48 fi
49
50 echo "rollback copy: $policy.bak"
51