Created
December 16, 2021 09:22
-
-
Save rawinng/868fa65d0c151f0ab4e1db350703e9f3 to your computer and use it in GitHub Desktop.
Connect to Bitkub API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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