@znode/sign
Version:
signature algorithm
44 lines (43 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.signWs = exports.sign = void 0;
/**
* MEXC Exchange:
* https://mxcdevelop.github.io/APIDoc/
*/
const hmac_1 = require("@zodash/hmac");
const qs = require("@zcorky/query-string");
function sign(data, secret) {
let _query = '';
let _body = '';
if (data.query && Object.keys(data.query).length > 0) {
// dict order
const _dict = Object.keys(data.query)
.sort((a, b) => a.localeCompare(b))
.reduce((all, key) => {
all[key] = data.query[key];
return all;
}, {});
_query = qs.stringify(_dict);
}
if (data.body && Object.keys(data.body).length > 0) {
_body = JSON.stringify(data.body);
}
const _text = [data.apiKey, data.timestamp, _query, _body];
const text = _text.join('');
return hmac_1.hmacSHA256(text, secret);
}
exports.sign = sign;
function signWs(timestamp, key, secret) {
// const t = Math.floor(timestamp / 1000);
// const text = `${t}GET/user/verify`;
// return hmacSHA256(text, secret);
return sign({
apiKey: key,
timestamp,
}, secret);
}
exports.signWs = signWs;
const _sign = sign;
_sign.ws = signWs;
exports.default = _sign;