Signed endpoint and HMAC SHA256 in React

Hi,

I wanna fetch data, and also make a trade using signed endpoints, which requires an additional parameter, HMAC SHA256 signature, to be sent in the queryString or requestBody.

Binance API - Endpoint Security Type info
https://binance-docs.github.io/apidocs/spot/en/#endpoint-security-type

API-keys are passed into the Rest API via the X-MBX-APIKEY header.

I’m using “crypto-js” (for HMAC SHA256 signature) and “axios.post()” to send queryString.

I came up with some code for placing an order, but it seems that something is not configured quite right.

I uploaded files to GitHub, so if there’s anyone who could help, can check the PAGE and the CODE.

Just one thing to mention, change API and secret key with your own to test it out.

Thank you!

PAGE:
https://kkcrypto.github.io/signed-endpoint/

CODE:
https://github.com/KKCrypto/signed-endpoint/blob/master/src/App.js

1 Like

The solution/answer to hashing the signature:
https://github.com/binance-exchange/binance-signature-examples/blob/master/nodejs/signature.js

const crypto = require('crypto');

const query_string = 'timestamp=1578963600000';
const apiSecret = 'NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j';

function signature(query_string) {
    return crypto
        .createHmac('sha256', apiSecret)
        .update(query_string)
        .digest('hex');
}

console.log("hashing the string: ");
console.log(query_string);
console.log("and return:");
console.log(signature(query_string));

console.log("\n");

const another_query = 'symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559';
console.log("hashing the string: ");
console.log(another_query);
console.log("and return:");
console.log(signature(another_query));

but there is a new problem arrived

locked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response

…and that’s for a “New Topic”