Skip to content
TradingSignalBotPublic

About

TradingSignalBot API clients for JavaScript and Python. For now: verify webhook signatures.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

TradingSignalBot SDK

Clients for the TradingSignalBot API, for JavaScript and Python.

The API is a preview. It is not live yet, so this repository holds only the part you need first: verifying the signature of a webhook delivery, so you know an event came from TradingSignalBot and was not changed or replayed. The full client (render a chart from your own levels, register webhooks, read signals) comes here when the sandbox opens. Get early access to get sandbox keys first.

Folder Package Install
js/ npm tradingsignalbot npm install tradingsignalbot
python/ PyPI tradingsignalbot pip install tradingsignalbot

Neither has any dependency: the JavaScript one uses Web Crypto (Node 18+, Deno, Bun, Cloudflare Workers), the Python one the standard library's hmac.

Verify a delivery

JavaScript (Express):

import express from 'express';
import { verifyWebhook } from 'tradingsignalbot';

const app = express();
app.post('/hooks/signals', express.raw({ type: 'application/json' }), async (req, res) => {
  try {
    const event = await verifyWebhook(req.body, req.get('TSB-Signature'), process.env.TSB_WEBHOOK_SECRET);
    // event.type: 'signal.created' | 'signal.target_hit' | 'signal.stopped' | 'signal.closed'
    res.sendStatus(204);
  } catch (e) {
    res.status(400).send(e.code ?? 'bad request');
  }
});

Python (Flask):

from flask import Flask, request, abort
from tradingsignalbot import verify_webhook, WebhookVerificationError

app = Flask(__name__)

@app.post("/hooks/signals")
def signals():
    try:
        event = verify_webhook(request.get_data(), request.headers.get("TSB-Signature"), SECRET)
    except WebhookVerificationError as e:
        abort(400, e.code)
    return "", 204

Pass the raw body, exactly as it arrived. Parsed and re-serialised JSON will not match.

The scheme

The scheme, as the API page states it:

Header TSB-Signature: t=<timestamp>,v1=<signature> (shown on the preview page)
Timestamp Unix seconds (the preview's example uses the event's time in seconds)
Signed "<timestamp>.<raw body>", HMAC-SHA256 with your webhook secret
Signature lowercase hex, 64 characters
Window the timestamp within 300 seconds of your clock, against replays (tolerance changes it, 0 turns it off)
Several v1= any one matching is enough, for secret rotation

test-vectors.json holds the cases both packages are tested against.

Both packages compare in constant time and check every signature and every secret, so a mismatch takes as long as a match.

API

JavaScript Python Does
verifyWebhook(body, header, secret, { tolerance, now }) verify_webhook(body, header, secret, tolerance=300, now=None) Verifies, returns the parsed event
verifySignature(...) verify_signature(...) Verifies, returns the timestamp; throws/raises WebhookVerificationError
isValidSignature(...) is_valid_signature(...) true / false
signPayload(body, secret, { timestamp }) sign_payload(body, secret, timestamp=None) The header TradingSignalBot would send: for your tests
computeSignature(body, secret, t) compute_signature(body, secret, t) The hex HMAC
parseSignatureHeader(header) parse_signature_header(header) { timestamp, signatures } / (timestamp, signatures)

secret can be a list while you rotate it. WebhookVerificationError.code is one of missing_header, bad_header, no_signature, timestamp, mismatch, bad_json.

Tests

cd js && npm test
cd python && python -m unittest discover -s tests

Licence

MIT.


Part of the Forexia network: InterestRateDecision.com · Forexia Academy · TradingSignalBot · PropSim · OpenCharts

About

TradingSignalBot API clients for JavaScript and Python. For now: verify webhook signatures.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages