yhtx 1.1.2 → 1.2.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.
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const crypto = require("crypto");
2
2
  const axios = require("axios");
3
+ const https = require("https"); //4 agent
3
4
 
4
5
  function getSign(queryString, secret, method, path) {
5
6
  let signString = [
@@ -62,45 +63,59 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
62
63
  ...(method == "POST" && { data }), //must be "data" //if GET, don't send
63
64
  };
64
65
 
65
- let r = await axios(config)
66
- .then((r) => {
67
- let dt = r.data;
68
- let rs = r.data.data;
66
+ const agent = new https.Agent({ rejectUnauthorized: false });
67
+ async function queryApi({ useAgent } = {}) {
68
+ if (useAgent) config.httpsAgent = agent;
69
+ let r = await axios(config)
70
+ .then((r) => {
71
+ let dt = r.data;
72
+ let rs = r.data.data;
69
73
 
70
- if (dt?.code == 403 && dt?.msg == "Verification failure [校验失败]") {
71
- console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
72
- } else if (dt?.status == "error") {
73
- if (dt?.["err-code"] == "api-signature-not-valid") {
74
- console.log("api bug2: null"); //{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
75
- } else if (dt?.err_code == 403) {
76
- console.log("api bug3: undefined"); //{status:"error",err_code:403,err_msg:"Verification failure [校验失败]",ts:1758854139970};
77
- } else if (dt?.["err-msg"] == "Format Error: account-id.") {
78
- console.log("api bug(accId): null"); //{status: 'error','err-code': 'validation-format-error','err-msg': 'Format Error: account-id.',data:null}
79
- } else {
80
- console.log(dt);
81
- rs = dt; //rs={status:'error',}
82
- }
83
- } else if (dt?.message == "Verification failure [校验失败]") {
84
- if (dt?.code == 1003) {
85
- console.log("api bug4: undefined"); //{ message: 'Verification failure [校验失败]', code: 1003, success: false }
86
- } else if (dt?.code == 513) {
87
- console.log("api bug4.1: undefined"); //{code: 513,data: null,message:"Verification failure [校验失败]",success:false,"print-log":true}
88
- }
89
- } else if (dt?.message == "Verification failure") {
90
- if (dt?.code == 12008) {
91
- console.log("api bug5: null"); //{ code: 12008, message: "Verification failure", data: null };
92
- } else console.log(`api bug5.1: uncaught`);
93
- } else if (!rs /*null*/) {
94
- console.log(dt); // console.log(`rs:${rs}`, dt);
95
- rs = dt;
96
- } //else rs=DATA
74
+ if (dt?.code == 403 && dt?.msg == "Verification failure [校验失败]") {
75
+ console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
76
+ } else if (dt?.status == "error") {
77
+ if (dt?.["err-code"] == "api-signature-not-valid") {
78
+ console.log("api bug2: null"); //{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
79
+ } else if (dt?.err_code == 403) {
80
+ console.log("api bug3: undefined"); //{status:"error",err_code:403,err_msg:"Verification failure [校验失败]",ts:1758854139970};
81
+ } else if (dt?.["err-msg"] == "Format Error: account-id.") {
82
+ console.log("api bug(accId): null"); //{status: 'error','err-code': 'validation-format-error','err-msg': 'Format Error: account-id.',data:null}
83
+ } else {
84
+ console.log(dt);
85
+ rs = dt; //rs={status:'error',}
86
+ }
87
+ } else if (dt?.message == "Verification failure [校验失败]") {
88
+ if (dt?.code == 1003) {
89
+ console.log("api bug4: undefined"); //{ message: 'Verification failure [校验失败]', code: 1003, success: false }
90
+ } else if (dt?.code == 513) {
91
+ console.log("api bug4.1: undefined"); //{code: 513,data: null,message:"Verification failure [校验失败]",success:false,"print-log":true}
92
+ }
93
+ } else if (dt?.message == "Verification failure") {
94
+ if (dt?.code == 12008) {
95
+ console.log("api bug5: null"); //{ code: 12008, message: "Verification failure", data: null };
96
+ } else console.log(`api bug5.1: uncaught`);
97
+ } else if (!rs /*null*/) {
98
+ console.log(dt); // console.log(`rs:${rs}`, dt);
99
+ rs = dt;
100
+ } //else rs=DATA
97
101
 
98
- return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
99
- })
100
- .catch(function (e) {
101
- console.log("catch", e.response?.data);
102
- });
102
+ return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
103
+ })
104
+ .catch(async (e) => {
105
+ if (
106
+ !useAgent &&
107
+ e?.code == "UNABLE_TO_GET_ISSUER_CERT_LOCALLY" &&
108
+ path.includes("linear-swap-api")
109
+ ) {
110
+ console.log("catch", e.code);
111
+ return await queryApi({ useAgent: true });
112
+ } else console.log("catch", e.response?.data);
113
+ });
103
114
 
115
+ return r;
116
+ }
117
+
118
+ let r = await queryApi();
104
119
  return r;
105
120
  }
106
121
 
package/indexOri.js ADDED
@@ -0,0 +1,132 @@
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
+ let dt = r.data;
68
+ let rs = r.data.data;
69
+
70
+ if (dt?.code == 403 && dt?.msg == "Verification failure [校验失败]") {
71
+ console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
72
+ } else if (dt?.status == "error") {
73
+ if (dt?.["err-code"] == "api-signature-not-valid") {
74
+ console.log("api bug2: null"); //{status: 'error','err-code': 'api-signature-not-valid','err-msg': 'Signature not valid: Verification failure [校验失败]',data:null}
75
+ } else if (dt?.err_code == 403) {
76
+ console.log("api bug3: undefined"); //{status:"error",err_code:403,err_msg:"Verification failure [校验失败]",ts:1758854139970};
77
+ } else if (dt?.["err-msg"] == "Format Error: account-id.") {
78
+ console.log("api bug(accId): null"); //{status: 'error','err-code': 'validation-format-error','err-msg': 'Format Error: account-id.',data:null}
79
+ } else {
80
+ console.log(dt);
81
+ rs = dt; //rs={status:'error',}
82
+ }
83
+ } else if (dt?.message == "Verification failure [校验失败]") {
84
+ if (dt?.code == 1003) {
85
+ console.log("api bug4: undefined"); //{ message: 'Verification failure [校验失败]', code: 1003, success: false }
86
+ } else if (dt?.code == 513) {
87
+ console.log("api bug4.1: undefined"); //{code: 513,data: null,message:"Verification failure [校验失败]",success:false,"print-log":true}
88
+ }
89
+ } else if (dt?.message == "Verification failure") {
90
+ if (dt?.code == 12008) {
91
+ console.log("api bug5: null"); //{ code: 12008, message: "Verification failure", data: null };
92
+ } else console.log(`api bug5.1: uncaught`);
93
+ } else if (!rs /*null*/) {
94
+ console.log(dt); // console.log(`rs:${rs}`, dt);
95
+ rs = dt;
96
+ } //else rs=DATA
97
+
98
+ return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
99
+ })
100
+ .catch(function (e) {
101
+ console.log("catch", e.response?.data);
102
+ });
103
+
104
+ return r;
105
+ }
106
+
107
+ class api {
108
+ constructor(apiKey = "", secret = "") {
109
+ this.apiKey = apiKey;
110
+ this.secret = secret;
111
+ }
112
+ async get(path, pr = {}) {
113
+ let data = {
114
+ apiKey: this.apiKey,
115
+ secret: this.secret,
116
+ data: pr,
117
+ };
118
+ return call(path, data);
119
+ }
120
+ async post(path, pr = {}) {
121
+ let data = {
122
+ apiKey: this.apiKey,
123
+ secret: this.secret,
124
+ method: "POST",
125
+ data: pr,
126
+ };
127
+ return call(path, data);
128
+ }
129
+ }
130
+
131
+ module.exports = api;
132
+ module.exports.call = call;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yhtx",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "HTX API",
5
5
  "main": "index.js",
6
6
  "scripts": {
File without changes
File without changes
File without changes