Request signing
Every call between your systems and bhub is signed with a shared secret, in both directions. Webhooks are signed with Ed25519, so you verify them with a public key.
The three headers
| Header | Value |
|---|---|
X-Bhub-Key-Id | Which key signed. Two keys may be live during a rotation. |
X-Bhub-Timestamp | Unix seconds. Requests more than 300 seconds away from bhub’s clock are refused, so keep your clock on NTP. |
X-Bhub-Signature | Lowercase hex HMAC-SHA256 of the string to sign, keyed with the secret’s 64 hex characters as bytes. |
The two APIs sign different strings. Keep the keys apart too: the Operator API key and the wallet key are separate secrets.
Operator API: your calls to bhub
The string to sign has four parts joined by a newline:
<timestamp>\n<METHOD>\n<path and query exactly as sent>\n<raw body> For GET /v1/games?limit=2 at 1764763200 the string is 1764763200\nGET\n/v1/games?limit=2\n (the body is empty). /v1/health, /v1/currencies and /v1/webhooks/keys need no signature.
bhub signs every answer to a signed request: X-Bhub-Signature is hex HMAC-SHA256 of <X-Bhub-Timestamp>\n<raw response body> with the same secret. Check it before you trust the answer.
A signed POST is accepted once. The identical signed request again (same timestamp, same body) is answered REPLAYED_REQUEST, so sign each retry afresh.
KEY=opk_0123456789abcdef01234567
SECRET=...64 hex characters...
TS=$(date +%s)
TARGET='/v1/games?limit=2'
SIG=$(printf '%s\n%s\n%s\n%s' "$TS" GET "$TARGET" "" \
| openssl dgst -sha256 -hmac "$SECRET" -hex | sed 's/^.* //')
curl -s "https://sandbox.bhts.dev$TARGET" \
-H "X-Bhub-Key-Id: $KEY" -H "X-Bhub-Timestamp: $TS" -H "X-Bhub-Signature: $SIG" BODY='{"mode":"demo","game_id":"0193a0c4-...","currency":"ZAR","language":"en"}'
TS=$(date +%s)
SIG=$(printf '%s\n%s\n%s\n%s' "$TS" POST /v1/launch "$BODY" \
| openssl dgst -sha256 -hmac "$SECRET" -hex | sed 's/^.* //')
curl -s https://sandbox.bhts.dev/v1/launch -H 'Content-Type: application/json' \
-H "X-Bhub-Key-Id: $KEY" -H "X-Bhub-Timestamp: $TS" -H "X-Bhub-Signature: $SIG" \
--data-binary "$BODY" func sign(req *http.Request, keyID, secret string, body []byte) {
ts := strconv.FormatInt(time.Now().Unix(), 10)
target := req.URL.EscapedPath()
if req.URL.RawQuery != "" {
target += "?" + req.URL.RawQuery
}
mac := hmac.New(sha256.New, []byte(secret)) // the 64 hex characters, as bytes
fmt.Fprintf(mac, "%s\n%s\n%s\n", ts, req.Method, target)
mac.Write(body)
req.Header.Set("X-Bhub-Key-Id", keyID)
req.Header.Set("X-Bhub-Timestamp", ts)
req.Header.Set("X-Bhub-Signature", hex.EncodeToString(mac.Sum(nil)))
}
func checkAnswer(resp *http.Response, body []byte, secret string) bool {
mac := hmac.New(sha256.New, []byte(secret))
fmt.Fprintf(mac, "%s\n", resp.Header.Get("X-Bhub-Timestamp"))
mac.Write(body)
want := hex.EncodeToString(mac.Sum(nil))
return hmac.Equal([]byte(want), []byte(resp.Header.Get("X-Bhub-Signature")))
} Seamless Wallet API: bhub’s calls to your wallet
bhub signs each wallet request over <timestamp>\n<raw body> (no method, no path). Your wallet answers with the same three headers, signed the same way over its answer body, so bhub verifies your answers too.
- Refuse a request whose key id, timestamp or signature does not check out with HTTP 401 and
{"status":"INVALID_SIGNATURE"}. bhub treats that as a definite refusal. - Every business outcome is HTTP 200 with a
status. Any other HTTP answer counts as a timeout: the outcome is unknown. - The optional
GET /v1/transactionsendpoint has no body, so its signed bytes are the request target, for example/bhub/v1/transactions?from=…&limit=5000&to=….
// In your wallet: check bhub's request, then sign your answer the same way.
func verifyBhub(r *http.Request, body []byte, keys map[string][]byte) bool {
key, ok := keys[r.Header.Get("X-Bhub-Key-Id")] // two keys may be live during rotation
if !ok {
return false
}
ts := r.Header.Get("X-Bhub-Timestamp")
sec, err := strconv.ParseInt(ts, 10, 64)
if err != nil || math.Abs(time.Since(time.Unix(sec, 0)).Seconds()) > 300 {
return false
}
mac := hmac.New(sha256.New, key)
fmt.Fprintf(mac, "%s\n", ts)
mac.Write(body)
want := hex.EncodeToString(mac.Sum(nil))
return hmac.Equal([]byte(want), []byte(r.Header.Get("X-Bhub-Signature")))
}
// On failure answer HTTP 401 {"status":"INVALID_SIGNATURE"}. Otherwise answer 200 with your
// X-Bhub-Key-Id, X-Bhub-Timestamp and X-Bhub-Signature = hex HMAC-SHA256(timestamp + "\n" + body). Webhooks: Ed25519
bhub posts webhooks to your message address with X-Bhub-Webhook-Id, X-Bhub-Event, X-Bhub-Key-Id, X-Bhub-Timestamp and X-Bhub-Signature: the base64url Ed25519 signature (no padding) of <timestamp>\n<raw body>.
- Get the public keys from
GET /v1/webhooks/keys. Cache them, and fetch again when a key id is unknown. Two keys are listed during a rotation. - Refuse timestamps more than 5 minutes off. Answer 2xx within 10 seconds. Deduplicate on
X-Bhub-Webhook-Id. - Failed deliveries are retried after 1 minute, 5 minutes, 15 minutes, 1, 2, 4, 8 and 8 hours, then given up (about a day).
// keys: key_id -> ed25519.PublicKey, from GET /v1/webhooks/keys (field "x" is base64url).
func verifyWebhook(r *http.Request, body []byte, keys map[string]ed25519.PublicKey) bool {
pub, ok := keys[r.Header.Get("X-Bhub-Key-Id")]
if !ok {
return false // refetch the keys once, then reject
}
ts := r.Header.Get("X-Bhub-Timestamp")
sec, err := strconv.ParseInt(ts, 10, 64)
if err != nil || math.Abs(time.Since(time.Unix(sec, 0)).Seconds()) > 300 {
return false
}
sig, err := base64.RawURLEncoding.DecodeString(r.Header.Get("X-Bhub-Signature"))
if err != nil {
return false
}
return ed25519.Verify(pub, append([]byte(ts+"\n"), body...), sig)
} Rotating keys
At most two Operator API keys are active. Make the second key in the portal, deploy it, then revoke the first. A revoked key stops working within 30 seconds. A secret is shown once: bhub stores it encrypted and cannot show it again.