yhtx 1.0.9 → 1.1.0

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 copy.js +157 -0
  2. package/index.js +11 -23
  3. package/package.json +1 -1
@@ -0,0 +1,157 @@
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 || undefined,
10
+ ]
11
+ .filter(Boolean)
12
+ .join("\n");
13
+ return crypto.createHmac("sha256", secret).update(signString).digest("base64");
14
+ }
15
+
16
+ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
17
+ apiKey ||= "";
18
+ secret ||= "";
19
+ method ||= "GET";
20
+ recvWindow ??= 30000;
21
+ const origin = path.includes("linear-swap-api")
22
+ ? "https://api.hbdm.com" //UM //for debig:https:api.btcgateway.pro, aws:https://api.hbdm.vn
23
+ : "https://api.huobi.pro"; //spot //https:api-aws.huobi.pro
24
+ data ||= {};
25
+
26
+ let timestamp = Date.now();
27
+ let url;
28
+ let sign;
29
+
30
+ timestamp = new Date(timestamp).toISOString().slice(0, 19);
31
+ let preBody = {
32
+ AccessKeyId: apiKey,
33
+ SignatureMethod: "HmacSHA256", //Ed25519
34
+ SignatureVersion: 2,
35
+ Timestamp: timestamp,
36
+ };
37
+ body = {
38
+ ...preBody,
39
+ ...data,
40
+ };
41
+
42
+ let pars = [];
43
+ for (let k in method == "GET" ? body : preBody)
44
+ pars.push(k + "=" + encodeURIComponent(body[k]));
45
+ let queryString = pars.sort().join("&");
46
+
47
+ if (method === "POST") {
48
+ url = origin + path + "?" + queryString;
49
+ sign = getSign(queryString, secret, method, path);
50
+ url += `&Signature=${sign}`;
51
+ } /*GET*/ else {
52
+ url = origin + path + "?" + queryString;
53
+ sign = getSign(queryString, secret, method, path);
54
+ url += `&Signature=${sign}`;
55
+ }
56
+
57
+ let headers = { "Content-Type": "application/json" };
58
+ let config = {
59
+ method,
60
+ url,
61
+ headers,
62
+ ...(method == "POST" && { data }), //must be "data" //if GET, don't send
63
+ };
64
+
65
+ // let r = await axios(config)
66
+ // .then((r) => {
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
69
+ // let rs = r.data.data;
70
+ // return rs;
71
+ // })
72
+ // .catch(function (e) {
73
+ // console.log(e.response.data);
74
+ // }); //old
75
+ let r = await axios(config)
76
+ .then((r) => {
77
+ let dt = r.data;
78
+ let rs = r.data.data;
79
+ if (dt?.code == 403 && msg == "Verification failure [校验失败]") {
80
+ console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
81
+ } else if (dt?.status == "error") {
82
+ if (dt?.["err-code"] == "api-signature-not-valid") {
83
+ console.log("api bug2: null"); //{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
84
+ } else if (dt?.err_code == 403) {
85
+ console.log("api bug3: undefined"); //{status:"error",err_code:403,err_msg:"Verification failure [校验失败]",ts:1758854139970};
86
+ } else if (dt?.["err-msg"] == "Format Error: account-id.") {
87
+ console.log("api bug(accId): null"); //{status: 'error','err-code': 'validation-format-error','err-msg': 'Format Error: account-id.',data:null}
88
+ } else {
89
+ console.log(dt);
90
+ rs = dt; //rs={status:'error',}
91
+ }
92
+ } else if (dt?.message == "Verification failure [校验失败]" && dt?.code == 1003) {
93
+ console.log("api bug4: undefined"); //{ message: 'Verification failure [校验失败]', code: 1003, success: false }
94
+ } else if (dt?.message == "Verification failure" && dt?.code == 12008) {
95
+ console.log("api bug5: null"); //{ code: 12008, message: "Verification failure", data: null };
96
+ } else if (!(r.data?.code == 200 /*success*/ || r.data?.status == "ok")) {
97
+ console.log(r.data); //if not code:200 or status:ok, then log //
98
+ } //eg{message:'The account API interface queried is empty',code:40237,success:false}
99
+ else if (rs == undefined) {
100
+ console.log(dt);
101
+ rs = dt; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
102
+ } // rs=DATA(if ok)|{status:'error',}|null(api fail)|undefined(api fail)
103
+ else if (!rs) console.log(dt);
104
+
105
+ // let rs = r.data?.data;
106
+ // if (
107
+ // rs == null &&
108
+ // dt?.status == "error" &&
109
+ // dt?.err_code != 403 &&
110
+ // dt?.["err-code"] != "api-signature-not-valid"
111
+ // ) {
112
+ // if (dt?.["err-msg"] != "Format Error: account-id.") {
113
+ // console.log(dt);
114
+ // rs = r.data; //eg rs={status:"error","err-code":"dw-withdraw-precision-limit","err-msg":"withdraw amount precision cannot be greater than 6",data:null}
115
+ // }
116
+ // } else if (rs == undefined) {
117
+ // if (![dt?.err_msg, dt?.message].includes("Verification failure [校验失败]")) {
118
+ // console.log(dt);
119
+ // rs = r.data; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
120
+ // } // if (dt?.success == false && dt?.code != 1003 && /*bug3*/ dt?.err_code != 403) {
121
+ // }
122
+
123
+ return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
124
+ })
125
+ .catch(function (e) {
126
+ console.log(e.response?.data);
127
+ });
128
+
129
+ return r;
130
+ }
131
+
132
+ class api {
133
+ constructor(apiKey = "", secret = "") {
134
+ this.apiKey = apiKey;
135
+ this.secret = secret;
136
+ }
137
+ async get(path, pr = {}) {
138
+ let data = {
139
+ apiKey: this.apiKey,
140
+ secret: this.secret,
141
+ data: pr,
142
+ };
143
+ return call(path, data);
144
+ }
145
+ async post(path, pr = {}) {
146
+ let data = {
147
+ apiKey: this.apiKey,
148
+ secret: this.secret,
149
+ method: "POST",
150
+ data: pr,
151
+ };
152
+ return call(path, data);
153
+ }
154
+ }
155
+
156
+ module.exports = api;
157
+ module.exports.call = call;
package/index.js CHANGED
@@ -62,29 +62,23 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
62
62
  ...(method == "POST" && { data }), //must be "data" //if GET, don't send
63
63
  };
64
64
 
65
- // let r = await axios(config)
66
- // .then((r) => {
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
69
- // let rs = r.data.data;
70
- // return rs;
71
- // })
72
- // .catch(function (e) {
73
- // console.log(e.response.data);
74
- // }); //old
75
65
  let r = await axios(config)
76
66
  .then((r) => {
77
67
  let dt = r.data;
68
+ let rs = r.data.data;
78
69
  if (dt?.code == 403 && msg == "Verification failure [校验失败]") {
79
70
  console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
80
71
  } else if (dt?.status == "error") {
81
72
  if (dt?.["err-code"] == "api-signature-not-valid") {
82
- console.log("api bug2: null"); ////{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
73
+ console.log("api bug2: null"); //{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
83
74
  } else if (dt?.err_code == 403) {
84
75
  console.log("api bug3: undefined"); //{status:"error",err_code:403,err_msg:"Verification failure [校验失败]",ts:1758854139970};
85
76
  } else if (dt?.["err-msg"] == "Format Error: account-id.") {
86
77
  console.log("api bug(accId): null"); //{status: 'error','err-code': 'validation-format-error','err-msg': 'Format Error: account-id.',data:null}
87
- } else console.log(dt);
78
+ } else {
79
+ console.log(dt);
80
+ rs = dt; //rs={status:'error',}
81
+ }
88
82
  } else if (dt?.message == "Verification failure [校验失败]" && dt?.code == 1003) {
89
83
  console.log("api bug4: undefined"); //{ message: 'Verification failure [校验失败]', code: 1003, success: false }
90
84
  } else if (dt?.message == "Verification failure" && dt?.code == 12008) {
@@ -92,17 +86,11 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
92
86
  } else if (!(r.data?.code == 200 /*success*/ || r.data?.status == "ok")) {
93
87
  console.log(r.data); //if not code:200 or status:ok, then log //
94
88
  } //eg{message:'The account API interface queried is empty',code:40237,success:false}
95
-
96
- let rs = r.data.data;
97
- // if (rs == null && dt?.status == "error") {
98
- // if (dt?.["err-msg"] != "Format Error: account-id") {
99
- // rs = r.data; //eg rs={status:"error","err-code":"dw-withdraw-precision-limit","err-msg":"withdraw amount precision cannot be greater than 6",data:null}
100
- // }
101
- // } else if (rs == undefined) {
102
- // if (![dt?.err_msg, dt?.message].includes("Verification failure [校验失败]")) {
103
- // rs = r.data; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
104
- // } // if (dt?.success == false && dt?.code != 1003 && /*bug3*/ dt?.err_code != 403) {
105
- // }
89
+ else if (rs == undefined) {
90
+ console.log(dt);
91
+ rs = dt; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
92
+ } // rs=DATA(if ok)|{status:'error',}|null(api fail)|undefined(api fail)
93
+ else if (!rs) console.log(dt); //else rs=DATA
106
94
 
107
95
  return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
108
96
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yhtx",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "HTX API",
5
5
  "main": "index.js",
6
6
  "scripts": {