forked from marcbarbosa/mercadobitcoin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mercadobitcoin-v3.js
65 lines (52 loc) · 1.69 KB
/
mercadobitcoin-v3.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
var unirest = require('unirest'),
crypto = require('crypto'),
qs = require('querystring');
var BASE_URL = 'https://www.mercadobitcoin.net',
API_PATH = '/api/v1/',
TAPI_PATH = '/tapi/v3/',
ENDPOINT_API = BASE_URL + API_PATH,
ENDPOINT_TRADE_API = BASE_URL + TAPI_PATH;
var MercadoBitcoin = function () {
this.currency = 'BTC';
}
MercadoBitcoin.prototype = {
get: function (method, currency, res) {
if (currency) {this.currency = currency;}
var isLitecoin = currency === 'LTC';
unirest.get(ENDPOINT_API + method + (isLitecoin ? '_litecoin' : ''))
.headers('Accept', 'application/json')
.end(function (response) {
res(JSON.parse(response.raw_body));
});
}
}
var MercadoBitcoinTrade = function (config) {
this.key = config.key;
this.secret = config.secret;
// this.pin = config.pin;
}
MercadoBitcoinTrade.prototype = {
execute: function (method, parameters, res) {
var now = Math.round(new Date().getTime() / 1000);
var params = new Object;
if (parameters) { params = parameters;}
params.tapi_method = method;
params.tapi_nonce = now;
var url_params = qs.stringify(params);
console.log(url_params);
var signature = crypto.createHmac('sha512', this.secret)
.update(TAPI_PATH + '?' + url_params)
.digest('hex');
unirest.post(ENDPOINT_TRADE_API)
.headers({'TAPI-ID': this.key})
.headers({'TAPI-MAC': signature})
.send(url_params)
.end(function (response) {
res(JSON.parse(response.raw_body));
});
}
}
module.exports = {
MercadoBitcoin: MercadoBitcoin,
MercadoBitcoinTrade: MercadoBitcoinTrade
}