-
Notifications
You must be signed in to change notification settings - Fork 9
/
kucoin.js
63 lines (58 loc) · 1.79 KB
/
kucoin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const qs = require('querystring')
const crypto = require('crypto')
class Kucoin {
constructor() {
}
init(config) {
let url = ''
if (config.environment === 'live') {
url = 'https://api-futures.kucoin.com'
} else {
url = 'https://api-sandbox-futures.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(endpoint, method, params = {}) {
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')
let passphraseResult = crypto.createHmac('sha256', this.secretKey)
.update(this.passphrase)
.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'] = passphraseResult
header.headers['KC-API-KEY-VERSION'] = 2
return header
}
formatQuery(queryObj) {
if (queryObj !== undefined && JSON.stringify(queryObj).length !== 2) {
return '?' + qs.stringify(queryObj)
} else {
return ''
}
}
}
module.exports = Kucoin