my-kucoin-node-api
Version:
Node.js KuCoin Cryptocurrency Exchange v2 API Wrapper/SDK
55 lines (52 loc) • 1.64 kB
JavaScript
const qs = require('querystring')
const crypto = require('crypto-js')
const Kucoin = {
init: function(config) {
let url = ''
if (config.environment === 'live') {
url = 'https://openapi-v2.kucoin.com'
} else {
url = 'https://openapi-sandbox.kucoin.com'
}
this.environment = config.environment
this.baseURL = url
this.secretKey = config.secretKey
this.apiKey = config.apiKey
this.passphrase = config.passphrase
const User = require('./lib/user')
const Market = require('./lib/market')
const Trade = require('./lib/trade')
const Sockets = require('./lib/websockets')
Object.assign(this, User, Market, Trade, Sockets)
},
sign: function(endpoint, params, method) {
let header = {
headers: {
'Content-Type': 'application/json'
}
}
let nonce = Date.now() + ''
let strForSign = ''
if (method === 'GET' || method === 'DELETE') {
strForSign = nonce + method + endpoint + this.formatQuery(params)
} else {
strForSign = nonce + method + endpoint + JSON.stringify(params)
}
let signatureResult = crypto.createHmac('sha256', this.secretKey)
.update(strForSign)
.digest('base64')
header.headers['KC-API-SIGN'] = signatureResult
header.headers['KC-API-TIMESTAMP'] = nonce
header.headers['KC-API-KEY'] = this.apiKey
header.headers['KC-API-PASSPHRASE'] = this.passphrase
return header
},
formatQuery: function(queryObj) {
if (JSON.stringify(queryObj).length !== 2) {
return '?' + qs.stringify(queryObj)
} else {
return ''
}
}
}
module.exports = Kucoin