forked from petermrg/node-cryptsy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptsy.js
More file actions
144 lines (133 loc) · 4.19 KB
/
cryptsy.js
File metadata and controls
144 lines (133 loc) · 4.19 KB
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
var crypto = require('crypto');
var querystring = require('querystring');
var request = require('request');
/**
* Cryptsy
*
* @param {string} key API Key
* @param {string} secret API Secret
*/
var Cryptsy = function(key, secret) {
this.key = key;
this.secret = secret;
this.nonce = Math.floor((new Date()).getTime() / 1000);
this.userAgent = 'Mozilla/4.0 (compatible; Cryptsy API Node.js client; NodeJS/' + process.version + ')';
this.privateApiUrl = 'https://www.cryptsy.com/api';
this.publicApiUrl = 'http://pubapi.cryptsy.com/api.php?';
this.publicMethods = [
'marketdata', 'marketdatav2', 'singlemarketdata', 'orderdata', 'singleorderdata'
];
this.privateMethods = [
'getinfo', 'getmarkets', 'mytransactions', 'markettrades', 'marketorders', 'mytrades',
'allmytrades', 'myorders', 'depth', 'allmyorders', 'createorder', 'cancelorder',
'cancelmarketorders', 'cancelallorders', 'calculatefees', 'generatenewaddress'
];
}
/**
* API call
*
* @param {string} method API Method
* @param {object} params Method parameters
* @param {Function} callback (err, data)
*/
Cryptsy.prototype.api = function(method, params, callback) {
params = params || {};
params.method = method;
params.nonce = ++this.nonce;
if (this.publicMethods.indexOf(method) >= 0) {
this.getRequest(params, callback);
} else if (this.privateMethods.indexOf(method) >= 0) {
this.postRequest(params, callback);
} else {
callback(new Error('Unknown method: ' + method), null);
}
}
/**
* Creates a Base64 signature of the request
*
* @param {string} str Text string
* @return {string} Base64 encoded signature
*/
Cryptsy.prototype.getSignatureFromString = function(str) {
var hmac = new crypto.createHmac('sha512', this.secret);
hmac.update(str);
return hmac.digest('hex');
}
/**
* HTTP GET request
*
* @param {Object} params GET parameters
* @param {Function} callback (err, data)
*/
Cryptsy.prototype.getRequest = function(params, callback) {
var options = {
url: this.publicApiUrl + querystring.stringify(params),
method: 'GET',
headers: {
'User-Agent': this.userAgent,
}
}
request.get(options, function (err, response, body) {
this.parseResponse(err, response, body, callback);
}.bind(this));
}
/**
* HTTP POST request
*
* @param {Object} params POST parameters
* @param {Function} callback (err, data)
*/
Cryptsy.prototype.postRequest = function(params, callback) {
var options = {
url: this.privateApiUrl,
method: 'POST',
form: params,
headers: {
'Sign': this.getSignatureFromString(querystring.stringify(params)),
'Key': this.key,
'User-Agent': this.userAgent,
}
};
request.post(options, function (err, response, body) {
this.parseResponse(err, response, body, callback);
}.bind(this));
}
/**
* Parse server response
*
* @param {[type]} err [description]
* @param {[type]} response [description]
* @param {[type]} body [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Cryptsy.prototype.parseResponse = function(err, response, body, callback) {
if (err) {
callback(new Error('Error in server response: ' + JSON.stringify(err)), null);
} else {
if (typeof callback === 'function') {
var data = null;
var err = null;
try {
data = JSON.parse(body);
} catch(e) {
err = new Error('Error parsing JSON: ' + body);
}
if (err) {
callback(err, data);
} else {
if (data && !!(data.success|0) && data.return) {
callback(err, data.return);
} else {
if (data && data.error) {
err = new Error(data.error);
} else {
err = new Error('Unknown error');
}
callback(err, null);
}
}
}
}
}
module.exports = Cryptsy;