Skip to content

Instantly share code, notes, and snippets.

@rawinng
Created December 16, 2021 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rawinng/868fa65d0c151f0ab4e1db350703e9f3 to your computer and use it in GitHub Desktop.
Save rawinng/868fa65d0c151f0ab4e1db350703e9f3 to your computer and use it in GitHub Desktop.
Connect to Bitkub API
var hmacSHA256 = require("crypto-js/hmac-sha256");
var hex = require("crypto-js/enc-hex");
var axios = require("axios");
var bitkubConf = require("../conf/bitkub");
const requestBitkub = async (method, url, body = null) => {
const headers = {
Accept: "application/json",
"Content-Type": "application/json",
"X-BTK-APIKEY": bitkubConf.apiKey,
};
console.log(JSON.stringify(headers));
var req = {
baseURL: bitkubConf.url,
method: method,
url: url,
headers: headers,
};
if (body !== null) {
const res = await axios.get("/api/servertime", {
baseURL: bitkubConf.url,
});
const tsApplyBody = { ...body, ts: parseInt(res.data) };
console.log(JSON.stringify(tsApplyBody));
const sig = hex.stringify(
hmacSHA256(JSON.stringify(tsApplyBody), bitkubConf.apiSecret)
);
console.log(JSON.stringify({ sig: sig, ...tsApplyBody }));
req = { ...req, data: { sig: sig, ...tsApplyBody } };
}
return await axios.request(req);
};
requestBitkub("POST", `/api/market/wallet`, {}).then(
(resp) => {
console.log(resp.statusText);
console.log(JSON.stringify(resp.data));
},
(err) => {
console.log(err.stack);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment