#!/bin/sh
set -e
apk add --no-cache openssl wget >/dev/null 2>&1 || true
SECRET=$(tr -d '\n' </jwtsecret | sed 's/^0x//')
H=$(printf '{"alg":"HS256","typ":"JWT"}' | openssl base64 -e -A | tr '+/' '-_' | tr -d '=')
P=$(printf '{"iat":%s}' "$(date +%s)" | openssl base64 -e -A | tr '+/' '-_' | tr -d '=')
SIG=$(printf '%s' "$H.$P" | openssl dgst -sha256 -mac HMAC -macopt hexkey:"$SECRET" -binary | openssl base64 -e -A | tr '+/' '-_' | tr -d '=')
wget -q -O /dev/null \
  --header="Content-Type: application/json" \
  --header="Authorization: Bearer $H.$P.$SIG" \
  --post-data='{"jsonrpc":"2.0","method":"engine_exchangeCapabilities","params":[],"id":1}' \
  http://127.0.0.1:8551
