Crypto
July 29, 2020, 11:51am
1
Hi,
I wanna fetch data, and also make a trade using React.js
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.
import React, { useState, useEffect } from 'react'
import axios from 'axios'
import CryptoJS from 'crypto-js'
import './App.css';
const burl = 'https://api.binance.com'
const endPoint = '/api/v3/order'
let dataQueryString = 'symbol=BTCUSDT&side=BUY&type=LIMIT&timeInForce=GTC&quantity=0.001&price=5000&recvWindow=60000×tamp=' + Date.now()
// apiKey and secretKey : from Binance API doc
// https://binance-docs.github.io/apidocs/spot/en/#signed-trade-user_data-and-margin-endpoint-security
const keys = {
'akey' : '',
'skey' : ''
}
let signature = CryptoJS.HmacSHA256(dataQueryString, keys['skey']).toString(CryptoJS.enc.Hex)
let url = burl + endPoint + '?' + dataQueryString + '&signature=' + signature
function App() {
const [error, setError] = useState([])
const [res, setRes] = useState([])
const componentDidMount = async () => {
const buyOrder = await axios.post(url, {
headers: {
'X-MBX-APIKEY': keys['akey']
}
})
.then((res) => {
console.log("res: " + JSON.stringify(res))
setRes(JSON.stringify(res))
})
.catch((error) => {
console.error(error)
console.log("error: " + JSON.stringify(error))
setError(JSON.stringify(error))
})
}
useEffect(function() {
componentDidMount()
console.log(url)
}, [])
return (
<div className="App">
<p>{error}</p>
<p>{res}</p>
</div>
);
}
export default App;
Can anyone help me?
dino
July 29, 2020, 11:49pm
2
Please don’t share any secret key here.
Here is a simple about hashing the signature in Nodejs.
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×tamp=1499827319559';
This file has been truncated. show original
Crypto
August 1, 2020, 9:31pm
7
API keys are from Binance documentation, so I thought it’s OK to use them as an example
Await and Then? did not know that they can be combined
dino
July 31, 2020, 7:06am
4
that’s query string, pretty standard.
As I understand “await” appeared to replace “then”, or use “await” or use “then” but not both, example
> await myfunction ()
or
> myfunction (). then ()
So far I have not seen code that combines both
> await myfunction (). then ()
Crypto
August 1, 2020, 9:35pm
8
You could use:
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
But I decided in another way
Crypto
August 1, 2020, 9:27pm
6
Dino, thank you for answering. It seems that my problem is in CORS because I got an error:
Access to XMLHttpRequest at 'https://api.binance.com/api/v3/order?symbol=BTCUSDT&side=BUY&type=LIMIT&timeInForce=GTC&quantity=0.001&price=5000&recvWindow=60000×tamp=1596317131721&signature=43709cc7b0576dff468741b2b7e9069c5fd8b7b4c781b534684a8fe36ba32e13' from origin 'https://kkcrypto.github.io' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
dino
August 2, 2020, 11:26am
11
thanks for your updates, it seems the issue of CORS. Hope you will find a way to send the request in your server side.
@Crypto could you please also add the code of decryption of HMAC or validation of HMAC at API side .
MJW
March 17, 2021, 10:35pm
13
You cannot send authenticated request from frontend. It’d be blocked by CORs policy. Use a backend server to forward the request pls.
“Crypto” NPM package is deprecated in favor of “crypto-js”.
This package is no longer supported and has been deprecated. To avoid malicious use, npm is hanging on to the package name.. Latest version: 1.0.1, last published: 5 years ago. Start using crypto in your project by running `npm i crypto`. There are...