-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
184 lines (159 loc) · 6.78 KB
/
common.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
const axios = require('axios');
const crypto = require('crypto');
const Message = require('bitcoinjs-message');
const btc = require('bitcoinjs-lib');
const DEFAULT_EXPIRATION_SEC = 60 * 60 * 3; // 3h
const JSON_ERROR = { status : { success : false }};
module.exports = {
DEPOSIT_HARD_LIMIT : 500010,
DEPOSIT_HARD_MINIMUM : 20,
NETWORK_XBT_FEE_HARD_LIMIT_MAX : 0.001,
NETWORK_XBT_FEE_HARD_LIMIT_MIN : 0.0001,
nonce : function() {
return crypto.randomBytes(32).toString('hex');
},
apiInfo : apiInfo,
followTheRabbit : followTheRabbit,
callCommand : function (uuid, address, decryptedKey, nonce,
command, version, params, verbose, json, testnet) {
return new Promise(function (resolve, reject) {
try {
process.on('SIGINT', function () {
console.log('');
console.log('**********');
console.log('*** INTERRUPTED! You can try to get the server reply anytime:');
console.log('**********');
console.log('============================');
console.log('walltime follow -' + (testnet ? 't' : '') + 'v ' + nonce);
console.log('============================');
console.log('FAQ: Why this command is taking too long?');
console.log('-----------------------------------------');
console.log('Some errors, specially related with repeated nonce, wrong credentials etc. Walltime server will simply ignore without return an error to the final user. It is possible that the command was lost, or it is in the queue to be processed (in case of high load on server, or if the server is temporarily down). Each request has a timeout that can be defined by the user. In this version, the timeout is set hardcoded to 3h. You can use the command "follow" to try to get the asynchronous reply from server now or later.');
process.exit();
});
var globalResult;
if (verbose) {
console.log('Retrieving endpoint information...');
}
apiInfo(testnet).then(result => {
globalResult = result;
if (verbose) {
console.log('Sending message to queue...');
}
var keyPair = btc.ECPair.fromWIF(decryptedKey);
var expiration = new Date();
expiration.setSeconds(expiration.getSeconds() + DEFAULT_EXPIRATION_SEC);
var data = JSON.stringify({
'expiration': expiration.toISOString(),
'nonce': nonce,
'version': 'v1',
'command': command,
'user': uuid,
'body': JSON.stringify(params)
});
var rawSignature = Message.sign(data, keyPair.privateKey, true);
var signature = rawSignature.toString('base64');
var body = JSON.stringify({
'bitcoin-address': address,
'bitcoin-signature': signature,
'data': data
});
if (verbose) {
console.log('Message to send:', body);
}
return insertOnQueue(globalResult.queueUrl, body);
}).then(a => {
if (verbose) {
console.log('Sent!');
console.log(a);
process.stdout.write('Following the rabbit');
}
return followTheRabbit(globalResult.responsePrefix + nonce, verbose);
}).then(a => {
if (verbose) {
console.log();
}
if (json) {
console.log(JSON.stringify(a, null, 2));
resolve();
} else {
if (!a.status || !a.status.success) {
console.error(a.status && a.status.description);
console.error('CODE:', a.status && a.status.code);
reject(a);
} else {
resolve(a);
}
}
}).catch(error => {
if (verbose) {
console.error('ERROR:', error);
}
if (json) {
console.log(JSON.stringify(JSON_ERROR));
}
reject(error);
});
} catch (err) {
reject(err);
}
});
},
metaInfo : metaInfo
};
function metaInfo(env) {
var tokenToAvoidCache = crypto.randomBytes(32).toString('hex');
return axios.get('https://s3.amazonaws.com/data-' + env
+ '-walltime-info/' + env + '/dynamic/meta.json?now=' + tokenToAvoidCache);
}
function apiInfo(testnet) {
var url;
if (testnet) {
url = 'https://walltime.info/testnet/data/dynamic/api.json';
} else {
url = 'https://walltime.info/data/dynamic/api.json';
}
return new Promise(function (resolve, reject) {
axios.get(url)
.then(response => {
var queueUrl = response.data['api-queue-url'];
var responsePrefix = response.data['api-response-url-prefix'];
if (testnet) {
queueUrl = queueUrl.replace(/production/g, 'testnet');
responsePrefix = responsePrefix.replace(/production/g, 'testnet');
}
resolve({
queueUrl: queueUrl,
responsePrefix: responsePrefix
});
})
.catch(error => {
reject(error);
});
});
}
function insertOnQueue(queueUrl, message) {
return axios({
method: 'post',
url: queueUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'MessageBody=' + encodeURIComponent(message)
});
}
function followTheRabbit(url, verbose, resolveParam) {
return new Promise(function (resolve, reject) {
axios.get(url)
.then(response => {
resolveParam(response.data);
}).catch(error => {
if (verbose) {
process.stdout.write('.');
}
setTimeout(function () {
followTheRabbit(url, verbose, resolveParam || resolve);
}, 1000);
});
});
}