forked from changelly/api-changelly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
117 lines (102 loc) · 2.92 KB
/
lib.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
'use strict';
module.exports = (function() {
var URL = 'https://api.changelly.com';
var io = require('socket.io-client');
var jayson = require('jayson');
var crypto = require('crypto');
var client = jayson.client.https(URL);
function Changelly(apiKey, apiSecret) {
this._id = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
this._sign = function(message) {
return crypto
.createHmac('sha512', apiSecret)
.update(JSON.stringify(message))
.digest('hex');
};
this._request = function(method, options, callback) {
var id = this._id();
var message = jayson.utils.request(method, options, id);
client.options.headers = {
'api-key': apiKey,
'sign': this._sign(message)
};
client.request(method, options, id, function(err, response) {
callback(err, response);
});
};
var self = this;
this._socket = io.connect(URL, {
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionDelayMax': 5000,
'reconnectionAttempts': 'Infinity'
});
this._socket.on('connect', function() {
var message = {
"Login": {}
};
self._socket.emit('subscribe',
{
apiKey: apiKey,
sign: self._sign(message),
message: message
}
);
});
}
Changelly.prototype = {
getCurrencies: function(callback) {
return this._request('getCurrencies', {}, callback);
},
createTransaction: function(from, to, address, amount, extraId, callback) {
var params = {
from: from,
to: to,
address: address,
amount: amount,
extraId: extraId
};
return this._request('createTransaction', params, callback);
},
getMinAmount: function(from, to, callback) {
var params = {
from: from,
to: to
};
return this._request('getMinAmount', params, callback);
},
getExchangeAmount: function(from, to, amount, callback) {
var params = {
from: from,
to: to,
amount: amount
};
return this._request('getExchangeAmount', params, callback);
},
getTransactions: function(limit, offset, currency, address, extraId, callback) {
var params = {
limit: limit,
offset: offset,
currency: currency,
address: address,
extraId: extraId
};
return this._request('getTransactions', params, callback);
},
getStatus: function(id, callback) {
var params = {
id: id
};
return this._request('getStatus', params, callback);
},
on: function(channel, callback) {
this._socket.on(channel, callback);
}
};
return Changelly;
})();