forked from mayeaux/localbitcoins-node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
211 lines (177 loc) · 5.77 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var request = require('request')
var crypto = require('crypto');
var querystring = require('querystring');
var nonce = (new Date).getTime();
function LBCClient(key, secret, otp) {
var self = this;
var config = {
url: 'https://localbitcoins.com/api',
key: key,
secret: secret,
otp: otp,
timeoutMS: 5000
};
/**
* This method makes a public or private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function api(method, ad_id, params, callback) {
var methods = {
onlineAds: ['buy-bitcoins-online'],
public: ['countrycodes'],
private: ['ad-get', 'ad-get/ad_id', 'myself', 'ads',
'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed',
'dashboard/released/buyer', 'dashboard/canceled/buyer', 'dashboard/closed/buyer',
'dashboard/released/seller', 'dashboard/canceled/seller', 'dashboard/closed/seller',
'wallet-send', 'wallet'
]
};
if(methods.public.indexOf(method) !== -1) {
return publicMethod(method, params, ad_id, callback);
}
else if(methods.private.indexOf(method) !== -1) {
return privateMethod(method, params, ad_id, callback);
}
else {
throw new Error(method + ' is not a valid API method.');
}
}
/**
* This method makes a public API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function publicMethod(method, params, ad_id, callback) {
params = params || {};
var path;
if (ad_id) {
path = '/' + method + '/' + ad_id;
} else {
path = '/' + method;
}
var url = config.url + path;
return rawRequest(url, {}, params, callback);
}
/**
* This method makes a private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function privateMethod(method, params, ad_id, callback) {
params = params || {};
var path;
if (ad_id) {
path = '/' + method + '/' + ad_id;
} else {
path = '/' + method;
}
var url = config.url + path;
var signature = getMessageSignature(path, params, nonce);
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Apiauth-Key': config.key,
'Apiauth-Nonce': nonce,
'Apiauth-Signature': signature
};
return rawRequest(url, headers, params, method, callback);
}
/**
* This method returns a signature for a request as a Base64-encoded string
* @param {String} path The relative URL path for the request
* @param {Object} request The POST body
* @param {Integer} nonce A unique, incrementing integer
* @return {String} The request signature
*/
function getMessageSignature(path, params, nonce) {
var postParameters = querystring.stringify(params);
var path = '/api' + path + '/';
var message = nonce + config.key + path + postParameters;
var auth_hash = crypto.createHmac("sha256", config.secret).update(message).digest('hex').toUpperCase();
return auth_hash;
}
/**
* This method sends the actual HTTP request
* @param {String} url The URL to make the request
* @param {Object} headers Request headers
* @param {Object} params POST body
* @param {Function} callback A callback function to call when the request is complete
* @return {Object} The request object
*/
function rawRequest(url, headers, params, method, callback) {
var gets = ['ad-get', 'dashboard', 'dashboard/released', 'dashboard/canceled',
'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer',
'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller',
'dashboard/closed/seller', 'wallet'];
var posts = [ 'ad-get/ad_id', 'myself', 'ads',
'wallet-send', 'wallet-balance', 'wallet-addr'];
if (posts.indexOf(method) !== -1) {
var options = {
url: url + '/',
headers: headers,
form: params,
};
var req = request.post(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;
if(error) {
callback.call(self, new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}
try {
data = JSON.parse(body);
}
catch(e) {
callback.call(self, new Error('Could not understand response from server: ' + body), null);
return;
}
if(data.error && data.error.length) {
callback.call(self, data.error, null);
}
else {
callback.call(self, null, data);
}
}
});
return req;
} else {
var options = {
url: url + '/',
headers: headers,
};
var req = request.get(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;
if(error) {
callback.call(self, new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}
try {
data = JSON.parse(body);
}
catch(e) {
callback.call(self, new Error('Could not understand response from server: ' + body), null);
return;
}
if(data.error && data.error.length) {
callback.call(self, data.error, null);
}
else {
callback.call(self, null, data);
}
}
});
return req;
}
}
self.api = api;
self.publicMethod = publicMethod;
self.privateMethod = privateMethod;
}
module.exports = LBCClient;