yhtx 1.1.1 → 1.1.2
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 copy 2.js +140 -0
- package/index.js +8 -13
- package/package.json +1 -1
package/index copy 2.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
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 (!(r.data?.code == 200 /*success*/ || r.data?.status == "ok")) {
|
|
94
|
+
// console.log(r.data); //if not code:200 or status:ok, then log //
|
|
95
|
+
// } //eg{message:'The account API interface queried is empty',code:40237,success:false}
|
|
96
|
+
// else if (rs == undefined) {
|
|
97
|
+
// console.log({ rs }, { dt });
|
|
98
|
+
// console.log("rs:undefined", dt);
|
|
99
|
+
// rs = dt; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
|
|
100
|
+
// } // rs=DATA(if ok)|{status:'error',}|null(api fail)|undefined(api fail)
|
|
101
|
+
else if (!rs /*null*/) {
|
|
102
|
+
console.log(dt); // console.log(`rs:${rs}`, dt);
|
|
103
|
+
rs = dt;
|
|
104
|
+
} //else rs=DATA
|
|
105
|
+
|
|
106
|
+
return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
|
|
107
|
+
})
|
|
108
|
+
.catch(function (e) {
|
|
109
|
+
console.log("catch", e.response?.data);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return r;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class api {
|
|
116
|
+
constructor(apiKey = "", secret = "") {
|
|
117
|
+
this.apiKey = apiKey;
|
|
118
|
+
this.secret = secret;
|
|
119
|
+
}
|
|
120
|
+
async get(path, pr = {}) {
|
|
121
|
+
let data = {
|
|
122
|
+
apiKey: this.apiKey,
|
|
123
|
+
secret: this.secret,
|
|
124
|
+
data: pr,
|
|
125
|
+
};
|
|
126
|
+
return call(path, data);
|
|
127
|
+
}
|
|
128
|
+
async post(path, pr = {}) {
|
|
129
|
+
let data = {
|
|
130
|
+
apiKey: this.apiKey,
|
|
131
|
+
secret: this.secret,
|
|
132
|
+
method: "POST",
|
|
133
|
+
data: pr,
|
|
134
|
+
};
|
|
135
|
+
return call(path, data);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = api;
|
|
140
|
+
module.exports.call = call;
|
package/index.js
CHANGED
|
@@ -67,7 +67,7 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
|
|
|
67
67
|
let dt = r.data;
|
|
68
68
|
let rs = r.data.data;
|
|
69
69
|
|
|
70
|
-
if (dt?.code == 403 && msg == "Verification failure [校验失败]") {
|
|
70
|
+
if (dt?.code == 403 && dt?.msg == "Verification failure [校验失败]") {
|
|
71
71
|
console.log("api bug: null"); //{code:403,msg:'Verification failure [校验失败]',data:null,ts:1758849372250}
|
|
72
72
|
} else if (dt?.status == "error") {
|
|
73
73
|
if (dt?.["err-code"] == "api-signature-not-valid") {
|
|
@@ -86,24 +86,19 @@ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
|
|
|
86
86
|
} else if (dt?.code == 513) {
|
|
87
87
|
console.log("api bug4.1: undefined"); //{code: 513,data: null,message:"Verification failure [校验失败]",success:false,"print-log":true}
|
|
88
88
|
}
|
|
89
|
-
} else if (dt?.message == "Verification failure"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
console.log(
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
console.log(dt);
|
|
96
|
-
rs = dt; //eg rs={message:'The account API interface queried is empty',code:40237,success:false}
|
|
97
|
-
} // rs=DATA(if ok)|{status:'error',}|null(api fail)|undefined(api fail)
|
|
98
|
-
else if (!rs /*null*/) {
|
|
99
|
-
console.log(dt);
|
|
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);
|
|
100
95
|
rs = dt;
|
|
101
96
|
} //else rs=DATA
|
|
102
97
|
|
|
103
98
|
return rs; //rs=DATA | {status:'error','err=code','err-msg',data:null} | null(if api fail) | undefined(if apiFail)
|
|
104
99
|
})
|
|
105
100
|
.catch(function (e) {
|
|
106
|
-
console.log(e.response?.data);
|
|
101
|
+
console.log("catch", e.response?.data);
|
|
107
102
|
});
|
|
108
103
|
|
|
109
104
|
return r;
|