DEV Community

Tradepit
Tradepit

Posted on Fully Autonomous

Keeping exchange API keys in the browser: non-extractable WebCrypto keys in a trading terminal

From the Tradepit team. Tradepit is a trading terminal for crypto exchanges, Spot and Futures, that runs in the browser. This post is about one design choice in it: exchange API keys stay only in your browser and never reach our server.

Why keep keys out of the server

To place orders, a terminal needs an API key and its secret. If a terminal's server stores them, that server becomes a place where many traders' keys sit together. We chose the other way: the browser signs every request to the exchange itself, and Tradepit works with the exchange directly, through your API key. An order from Tradepit goes straight to the exchange.

The secret becomes a key that cannot be read back

When you add a key, the secret is imported with the Web Crypto API as an HMAC-SHA-256 key with extractable set to false and the only usage "sign":

crypto.subtle.importKey("raw", secretBytes, { name: "HMAC", hash: "SHA-256" }, false, ["sign"])
Enter fullscreen mode Exit fullscreen mode

From that moment the browser will use the key to sign and will hand it to nothing else: not to our code, not to a devtools console, not to a script. The text of the secret exists only for the length of that one import call and is not in the browser's storage. The CryptoKey object itself is kept in IndexedDB, where a non-extractable key stays non-extractable.

Some exchanges also check a passphrase in every request, so it has to be read back. Tradepit keeps it encrypted with AES-GCM under its own non-extractable key and decrypts it only for the request that needs it. The API key itself is kept as it is: the exchange sees it in every request anyway, and the list of keys shows only its last 4 characters.

Keys are kept per account: another account in the same browser does not see them. When the first key is added, Tradepit asks the browser not to clear the site's storage to free space.

Where a signed request goes

Binance and Bybit accept requests from a web page, so the browser talks to them directly. Bitget, KuCoin and OKX (EEA, US) do not let a website call them, so for them there is the Exchange Bridge extension for Chrome. It runs on your computer, calls only the API addresses of these exchanges and stores nothing. Requests reach it already signed, and the secret never gets to it.

When the key is erased

When you sign out, the keys are erased from this browser. They are also erased when another person signs in on this browser, when the site has not been opened for 7 days, and when you clear the site's data. In a private window the key works until the tab is closed. If you don't visit for a long time, the browser may erase the site's data, keys included; Safari does it after just 7 days without a visit. In every case positions and orders stay on the exchange.

What a non-extractable key does not protect

A non-extractable key cannot be copied, but it can still sign. Code running in an open Tradepit tab could ask it to sign a request. So what a key can do on the exchange is set by its rights. Give a key for Tradepit only the rights to read and to trade, with withdrawals and transfers turned off. If the exchange says the key can withdraw, Tradepit does not keep it. Binance does not tell a browser the rights of a key, so for Binance keys turn withdrawals off on the exchange yourself.

The full description, with how to delete a key in Tradepit and revoke it on each exchange:
https://tradepit.live/guides/api-key-storage?utm_source=devto&utm_medium=channel&utm_campaign=security

Top comments (0)