-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
142 lines (117 loc) · 3.98 KB
/
client.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
var https = require("https"),
crypto = require("crypto");
function CryptopiaClient(keys){
var self = this;
self.keys = keys;
function fetch_data(api_method, callback, is_private, params) {
//If it's a public method, make public request
if(!is_private){
return public_request(callback, api_method, params);
} else {
//If it's a private method, make a private request
//If any keys are missing, throw an error...
if(!self.keys.api_key || !self.keys.secret_key){
throw new Error("I couldn't find one of the keys.");
} else {
return private_request(callback, api_method, params);
}
}
}
function public_request(callback, api_method, params) {
var params = params || {}
var options = {
host: 'www.cryptopia.co.nz',
path: '/Api/' + api_method,
method: 'GET'
}
sendRequestCallback(callback, options, '');
}
function private_request(callback, api_method, params) {
var params = params || {}
var amx_value = generate_amx(params, api_method);
var body = JSON.stringify(params);
var headers = {
'Authorization': amx_value,
'Content-Type':'application/json; charset=utf-8',
'Content-Length' : Buffer.byteLength(body),
};
var options = {
host: 'www.cryptopia.co.nz',
path: '/Api/' + api_method,
method: 'POST',
headers: headers
}
//console.log(options)
sendRequestCallback(callback, options, body);
}
var sendRequestCallback = function(callback, options, body) {
var req = https.request(options, (res) => {
var json = "";
res.setEncoding('utf8');
res.on('data', (chunk) => {
json += chunk;
});
res.on('end', () => {
console.log(json);
result = JSON.parse(json);
//if (result.Success) {
callback.call(this, null, result.Data);
//} else {
// it is actually an error
//callback.call(this, e, null);
//}
})
});
req.on('error', (e) => {
callback.call(this, e, null);
});
req.write(body);
req.end();
};
function generate_amx(params, api_method) {
//I made this function out of a block of code that can be found here https://www.cryptopia.co.nz/Forum/Thread/262
var nonce = Math.floor(new Date().getTime() /10);
var md5 = crypto.createHash('md5').update( JSON.stringify( params ) ).digest();
var requestContentBase64String = md5.toString('base64');
var signature = self.keys.api_key + "POST" + encodeURIComponent( 'https://www.cryptopia.co.nz/Api/' + api_method ).toLowerCase() + nonce + requestContentBase64String;
var hmacsignature = crypto.createHmac('sha256', new Buffer( self.keys.secret_key, "base64" ) ).update( signature ).digest().toString('base64');
var amx = "amx " + self.keys.api_key + ":" + hmacsignature + ":" + nonce;
return amx;
}
// public methods
self.GetCurrencies = (callback, params) => {
fetch_data('GetCurrencies', callback, false);
};
self.GetMarketOrders = (callback, market, count) => {
if (market) {
market = market.replace('/', '_');
}
fetch_data( 'GetMarketOrders/' + market + '/' + count, callback, false);
};
// private methods
self.GetBalance = (callback, currency) => {
var params = {Currency : currency};
fetch_data('GetBalance', callback, true, params);
};
self.GetOpenOrders = (callback, market) => {
var params = {Market : market};
fetch_data('GetOpenOrders', callback, true, params);
};
self.GetTradeHistory = (callback, market, count) => {
var params = {Market : market, Count: count};
fetch_data('GetTradeHistory', callback, true, params);
};
self.SubmitTrade = (callback, market, tradeType, rate, amount) => {
var params = {Market : market, Type : tradeType, Rate : rate, Amount : amount};
fetch_data('SubmitTrade', callback, true, params);
};
self.CancelTrade = (callback, orderId) => {
var params = {Type : 'Trade', OrderId : orderId};
fetch_data('CancelTrade', callback, true, params);
};
self.CancelAllTrades = (callback) => {
var params = {Type : 'All'};
fetch_data('CancelTrade', callback, true, params);
};
}
module.exports = CryptopiaClient;