yhtx 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +12 -21
  2. package/index2.js +113 -0
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -2,13 +2,14 @@ const crypto = require("crypto");
2
2
  const axios = require("axios");
3
3
 
4
4
  function getSign(queryString, secret, method, path) {
5
- // let signString = [method, "api.huobi.pro", path, queryString].join("\n");
6
5
  let signString = [
7
6
  method,
8
7
  path.includes("linear-swap-api") ? "api.hbdm.com" : "api.huobi.pro",
9
8
  path,
10
- queryString,
11
- ].join("\n");
9
+ queryString || undefined,
10
+ ]
11
+ .filter(Boolean)
12
+ .join("\n");
12
13
  return crypto.createHmac("sha256", secret).update(signString).digest("base64");
13
14
  }
14
15
 
@@ -39,42 +40,32 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
39
40
  };
40
41
 
41
42
  let pars = [];
42
- for (let k in body) pars.push(k + "=" + encodeURIComponent(body[k]));
43
+ for (let k in method == "GET" ? body : preBody)
44
+ pars.push(k + "=" + encodeURIComponent(body[k]));
43
45
  let queryString = pars.sort().join("&");
44
46
 
45
- if (/*payload not yet supported*/ method === "POST") {
47
+ if (method === "POST") {
46
48
  url = origin + path + "?" + queryString;
47
49
  sign = getSign(queryString, secret, method, path);
48
50
  url += `&Signature=${sign}`;
49
- body.Signature = sign;
50
51
  } /*GET*/ else {
51
52
  url = origin + path + "?" + queryString;
52
53
  sign = getSign(queryString, secret, method, path);
53
54
  url += `&Signature=${sign}`;
54
- body.Signature = sign;
55
- }
56
-
57
- var headers = {
58
- // "Content-Type": "application/json",
59
- // "User-Agent":
60
- // "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
61
- };
62
-
63
- if (method === "POST") {
64
- headers["Content-Type"] = "application/json; charset=utf-8";
65
55
  }
66
56
 
67
- var config = {
57
+ let headers = { "Content-Type": "application/json" };
58
+ let config = {
68
59
  method,
69
60
  url,
70
61
  headers,
71
- ...body,
62
+ ...(method == "POST" && { data }), //must be "data" //if GET, don't send
72
63
  };
73
64
 
74
65
  let r = await axios(config)
75
66
  .then((r) => {
76
- // log(r);
77
- if (r.data?.code != 200 /*success*/) console.log(r.data);
67
+ if (!(r.data?.code == 200 /*success*/ || r.data?.status == "ok"))
68
+ console.log(r.data); //if not code:200 or status:ok, then log
78
69
  let rs = r.data.data;
79
70
  return rs;
80
71
  })
package/index2.js ADDED
@@ -0,0 +1,113 @@
1
+ const crypto = require("crypto");
2
+ const axios = require("axios");
3
+
4
+ function getSign(queryString, secret, method, path) {
5
+ let signString = [
6
+ method,
7
+ path.includes("linear-swap-api") ? "api.hbdm.com" : "api.huobi.pro",
8
+ path,
9
+ queryString,
10
+ ].join("\n");
11
+ return crypto.createHmac("sha256", secret).update(signString).digest("base64");
12
+ }
13
+
14
+ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
15
+ apiKey ||= "";
16
+ secret ||= "";
17
+ method ||= "GET";
18
+ recvWindow ??= 30000;
19
+ const origin = path.includes("linear-swap-api")
20
+ ? "https://api.hbdm.com" //UM //for debig:https:api.btcgateway.pro, aws:https://api.hbdm.vn
21
+ : "https://api.huobi.pro"; //spot //https:api-aws.huobi.pro
22
+ data ||= {};
23
+
24
+ let timestamp = Date.now();
25
+ let url;
26
+ let sign;
27
+
28
+ timestamp = new Date(timestamp).toISOString().slice(0, 19);
29
+ let preBody = {
30
+ AccessKeyId: apiKey,
31
+ SignatureMethod: "HmacSHA256", //Ed25519
32
+ SignatureVersion: 2,
33
+ Timestamp: timestamp,
34
+ };
35
+ body = {
36
+ ...preBody,
37
+ ...data,
38
+ };
39
+
40
+ let pars = [];
41
+ for (let k in body) pars.push(k + "=" + encodeURIComponent(body[k]));
42
+ let queryString = pars.sort().join("&");
43
+
44
+ if (/*payload not yet supported*/ method === "POST") {
45
+ url = origin + path + "?" + queryString;
46
+ sign = getSign(queryString, secret, method, path);
47
+ url += `&Signature=${sign}`;
48
+ body.Signature = sign;
49
+ } /*GET*/ else {
50
+ url = origin + path + "?" + queryString;
51
+ sign = getSign(queryString, secret, method, path);
52
+ url += `&Signature=${sign}`;
53
+ body.Signature = sign;
54
+ }
55
+
56
+ var headers = {
57
+ // "Content-Type": "application/json",
58
+ // "User-Agent":
59
+ // "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
60
+ };
61
+
62
+ if (method === "POST") {
63
+ headers["Content-Type"] = "application/json; charset=utf-8";
64
+ }
65
+
66
+ var config = {
67
+ method,
68
+ url,
69
+ headers,
70
+ ...body,
71
+ data,
72
+ };
73
+
74
+ let r = await axios(config)
75
+ .then((r) => {
76
+ // log(r);
77
+ if (r.data?.code != 200 /*success*/) console.log(r.data);
78
+ let rs = r.data.data;
79
+ return rs;
80
+ })
81
+ .catch(function (e) {
82
+ console.log(e.response.data);
83
+ });
84
+
85
+ return r;
86
+ }
87
+
88
+ class api {
89
+ constructor(apiKey = "", secret = "") {
90
+ this.apiKey = apiKey;
91
+ this.secret = secret;
92
+ }
93
+ async get(path, pr = {}) {
94
+ let data = {
95
+ apiKey: this.apiKey,
96
+ secret: this.secret,
97
+ data: pr,
98
+ };
99
+ return call(path, data);
100
+ }
101
+ async post(path, pr = {}) {
102
+ let data = {
103
+ apiKey: this.apiKey,
104
+ secret: this.secret,
105
+ method: "POST",
106
+ data: pr,
107
+ };
108
+ return call(path, data);
109
+ }
110
+ }
111
+
112
+ module.exports = api;
113
+ module.exports.call = call;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yhtx",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "HTX API",
5
5
  "main": "index.js",
6
6
  "scripts": {