kucoin-universal-sdk
Version:
Official KuCoin Universal SDK.
112 lines • 4.63 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.KcSigner = void 0;
const crypto = __importStar(require("crypto"));
const common_1 = require("../../common");
/**
* KcSigner contains information about `apiKey`, `apiSecret`, `apiPassPhrase`, and `apiKeyVersion`
* and provides methods to sign and generate headers for API requests.
*/
class KcSigner {
constructor(apiKey = '', apiSecret = '', apiPassphrase = '', brokerName = '', brokerPartner = '', brokerKey = '') {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.apiPassphrase =
apiPassphrase && apiSecret
? this.sign(Buffer.from(apiPassphrase), Buffer.from(apiSecret))
: apiPassphrase;
this.brokerName = brokerName;
this.brokerPartner = brokerPartner;
this.brokerKey = brokerKey;
if (!apiKey || !apiSecret || !apiPassphrase) {
common_1.logger.warn('[AUTH WARNING] API credentials incomplete. Access is restricted to public interfaces only.');
}
}
/**
* Sign the input data with the given key using HMAC-SHA256
*/
sign(plain, key) {
const hmac = crypto.createHmac('sha256', key);
hmac.update(plain);
const digest = hmac.digest();
return digest.toString('base64');
}
/**
* Headers method generates and returns a map of signature headers needed for API authorization
* It takes a plain string as an argument to help form the signature
*/
headers(plain) {
const timestamp = Date.now().toString();
const signatureInput = timestamp + plain;
const signature = this.sign(Buffer.from(signatureInput), Buffer.from(this.apiSecret));
const headers = {
'KC-API-KEY': this.apiKey,
'KC-API-PASSPHRASE': this.apiPassphrase,
'KC-API-TIMESTAMP': timestamp,
'KC-API-SIGN': signature,
'KC-API-KEY-VERSION': '3',
};
return headers;
}
/**
* Generate broker-specific headers including partner verification
*/
brokerHeaders(plain) {
if (!this.brokerPartner || !this.brokerName) {
common_1.logger.error('[BROKER ERROR] Missing broker information');
throw new Error('Broker information cannot be empty');
}
const timestamp = Date.now().toString();
const signatureInput = timestamp + plain;
const signature = this.sign(Buffer.from(signatureInput), Buffer.from(this.apiSecret));
const partnerInput = timestamp + this.brokerPartner + this.apiKey;
const partnerSignature = this.sign(Buffer.from(partnerInput), Buffer.from(this.brokerKey));
const headers = {
'KC-API-KEY': this.apiKey,
'KC-API-PASSPHRASE': this.apiPassphrase,
'KC-API-TIMESTAMP': timestamp,
'KC-API-SIGN': signature,
'KC-API-KEY-VERSION': '3',
'KC-API-PARTNER': this.brokerPartner,
'KC-BROKER-NAME': this.brokerName,
'KC-API-PARTNER-VERIFY': 'true',
'KC-API-PARTNER-SIGN': partnerSignature,
};
return headers;
}
}
exports.KcSigner = KcSigner;
//# sourceMappingURL=default_signer.js.map