-
Notifications
You must be signed in to change notification settings - Fork 2
/
coinex.js
164 lines (125 loc) · 3.83 KB
/
coinex.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// From https://github.com/coinexcom/coinex_exchange_api/wiki/012security_authorization
const assert = require('assert');
const request = require('superagent');
const crypto = require('crypto');
const querystring = require('querystring');
const { get } = require('lodash');
require('superagent-proxy')(request);
const baseUrl = 'https://api.coinex.com/v1';
const USER_AGENT =
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36';
const maybeAddProxy = _ => (process.env.HTTP_PROXY ? _.proxy(process.env.HTTP_PROXY) : _);
const ok = _ => {
if (!_.ok) {
return false;
}
let body;
try {
body = JSON.parse(_.text);
} catch (error) {
return false;
}
return body && body.code === 0;
};
const wrapResponseError = error => {
if (!error.response) {
throw error;
}
const status = error.status || '<none>';
let body;
try {
body = JSON.parse(error.response.text);
} catch (error) {}
if (!body) {
const text = error.response.text || '<none>';
const wrapped = Object.assign(new Error(`Coinex error. Status=${status}; Text=${text}`), {
inner: error,
response: error.response,
status,
});
throw wrapped;
}
const { code = '<none>', message = '<none>' } = body;
const wrapped = Object.assign(new Error(`Coinex error. Code=${code}; Status=${status} Message=${message}`), {
response: error.response,
status,
coinex: body,
});
throw wrapped;
};
const createCoinexClient = (apiKey, apiSecret) => {
const coinexGet = async (path, fields = {}, method = 'GET') => {
assert(path, 'path is required');
const tonce = Date.now().toString();
const completeUrl = baseUrl + path;
const body = {
access_id: apiKey,
tonce,
...fields,
};
const bodySorted = Object.keys(body)
.slice()
.sort()
.reduce(
(prev, key) => ({
...prev,
[key]: body[key],
}),
{}
);
const bodyAsQueryString = querystring.stringify(bodySorted);
const bodyAsQueryStringWithSecret = bodyAsQueryString + '&secret_key=' + apiSecret;
const signature = crypto
.createHash('md5')
.update(bodyAsQueryStringWithSecret)
.digest('hex')
.toUpperCase();
const separator = completeUrl.includes('?') ? '&' : '?';
const methodLowerCase = method.toLowerCase();
return maybeAddProxy(
request[methodLowerCase](completeUrl + separator + bodyAsQueryString)
.set('authorization', signature)
.set('User-Agent', USER_AGENT)
.accept('application/json')
.ok(ok)
).then(_ => JSON.parse(_.text).data, wrapResponseError);
};
const coinexPost = async (path, fields = {}, method = 'POST') => {
assert(path, 'path is required');
const tonce = Date.now().toString();
const completeUrl = baseUrl + path;
const body = {
access_id: apiKey,
tonce,
...fields,
};
const bodySorted = Object.keys(body)
.slice()
.sort()
.reduce(
(prev, key) => ({
...prev,
[key]: body[key],
}),
{}
);
const bodyAsQueryString = querystring.stringify(bodySorted);
const bodyAsQueryStringWithSecret = bodyAsQueryString + '&secret_key=' + apiSecret;
const signature = crypto
.createHash('md5')
.update(bodyAsQueryStringWithSecret)
.digest('hex')
.toUpperCase();
const methodLowerCase = method.toLowerCase();
return maybeAddProxy(
request[methodLowerCase](completeUrl)
.send(bodySorted)
.set('authorization', signature)
.set('User-Agent', USER_AGENT)
.accept('application/json')
.ok(ok)
).then(_ => JSON.parse(_.text).data, wrapResponseError);
};
return { get: coinexGet, post: coinexPost };
};
module.exports = createCoinexClient;