1 | /* jshint -W101, -W098 */ |
||
2 | var blocktrail = require('../'); |
||
3 | var assert = require('assert'); |
||
4 | var crypto = require('crypto'); |
||
5 | |||
6 | /** |
||
7 | * @type APIClient |
||
8 | */ |
||
9 | var client = blocktrail.BlocktrailSDK({ |
||
10 | apiKey: process.env.BLOCKTRAIL_SDK_APIKEY || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APIKEY", |
||
11 | apiSecret: process.env.BLOCKTRAIL_SDK_APISECRET || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APISECRET", |
||
12 | btccom: typeof process.env.BLOCKTRAIL_SDK_BTCCOM !== "undefined" ? JSON.parse(process.env.BLOCKTRAIL_SDK_BTCCOM) : true |
||
13 | }); |
||
14 | |||
15 | describe('SDK general', function() { |
||
16 | it('test Coin Value', function(cb) { |
||
17 | assert.equal(blocktrail.toSatoshi(0.00000001), 1); |
||
18 | assert.equal(blocktrail.toBTC(100000000), 1.0); |
||
19 | |||
20 | assert.equal(blocktrail.toSatoshi(1.23456789), 123456789); |
||
21 | assert.equal(blocktrail.toBTC(123456789), 1.23456789); |
||
22 | |||
23 | cb(); |
||
24 | }); |
||
25 | it('test auth failure', function(cb) { |
||
26 | var client = blocktrail.BlocktrailSDK({ |
||
27 | apiKey: "TESTKEY-FAIL", |
||
28 | apiSecret: "TESTSECRET-FAIL", |
||
29 | btccom: false |
||
30 | }); |
||
31 | |||
32 | client.address("1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp", function(err, address) { |
||
0 ignored issues
–
show
|
|||
33 | assert.ok(err); |
||
34 | |||
35 | cb(); |
||
36 | }); |
||
37 | }); |
||
38 | }); |
||
39 | |||
40 | describe('webhooks api', function() { |
||
41 | var createdWebhooks = []; |
||
42 | var cleanup = function(done) { |
||
43 | client.allWebhooks({page:1, limit:500}, function(err, response) { |
||
0 ignored issues
–
show
|
|||
44 | //delete each webhook |
||
45 | //var allWebhooks = response.data; |
||
46 | if (!createdWebhooks.length) { |
||
47 | done(); |
||
48 | } |
||
49 | createdWebhooks.forEach(function(identifier) { |
||
50 | client.deleteWebhook(identifier, function(err, response) { |
||
0 ignored issues
–
show
|
|||
51 | createdWebhooks.splice(identifier, 1); |
||
52 | if (createdWebhooks.length === 0) { |
||
53 | done(); |
||
54 | } |
||
55 | }); |
||
56 | }); |
||
57 | }); |
||
58 | }; |
||
59 | before(function(done) { |
||
60 | // runs before all tests in this block..cleanup any existing data that could conflict with the tests |
||
61 | cleanup(done); |
||
62 | }); |
||
63 | after(function(done) { |
||
64 | //cleanup after all tests |
||
65 | cleanup(done); |
||
66 | }); |
||
67 | |||
68 | //create a custom (yet "random") identifier such to avoid conflicts when running multiple tests simultaneously |
||
69 | var myIdentifier = crypto.randomBytes(24).toString('hex'); |
||
70 | |||
71 | // test cases |
||
72 | it('create new webhook with custom identifier', function(done) { |
||
73 | client.setupWebhook("https://www.blocktrail.com/webhook-test", myIdentifier, function(err, webhook) { |
||
74 | assert.ifError(err); |
||
75 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); |
||
76 | assert.equal(webhook.identifier, myIdentifier); |
||
77 | createdWebhooks.push(webhook.identifier); |
||
78 | done(); |
||
79 | }); |
||
80 | }); |
||
81 | |||
82 | it('create new webhook with random identifier', function(done) { |
||
83 | client.setupWebhook("https://www.blocktrail.com/webhook-test", function(err, webhook) { |
||
84 | assert.ifError(err); |
||
85 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); |
||
86 | assert.ok(webhook.identifier); |
||
87 | createdWebhooks.push(webhook.identifier); |
||
88 | done(); |
||
89 | }); |
||
90 | }); |
||
91 | |||
92 | it('get all user webhooks', function(done) { |
||
93 | client.allWebhooks(null, function(err, response) { |
||
94 | assert.ifError(err); |
||
95 | assert.ok('data' in response, "'data' key not in response"); |
||
96 | assert.ok('total' in response, "'total' key not in response"); |
||
97 | assert.ok(parseInt(response['total']) >= 2, "'total' does not match expected value"); |
||
98 | assert.ok(response['data'].length >= 2, "Count of webhooks returned is not equal to 2"); |
||
99 | |||
100 | assert.ok('url' in response['data'][0], "'url' key not in first webhook of response"); |
||
101 | assert.ok('url' in response['data'][1], "'url' key not in second webhook of response"); |
||
102 | done(); |
||
103 | }); |
||
104 | }); |
||
105 | |||
106 | it('get a single webhook', function(done) { |
||
107 | client.getWebhook(createdWebhooks[0], function(err, response) { |
||
108 | assert.ifError(err); |
||
109 | assert.ok('url' in response, "'url' key not in response"); |
||
110 | assert.ok('identifier' in response, "'identifier' key not in response"); |
||
111 | assert.equal(response['url'], "https://www.blocktrail.com/webhook-test", "'url' does not match expected value"); |
||
112 | assert.equal(response['identifier'], myIdentifier, "'identifier' does not match expected value"); |
||
113 | done(); |
||
114 | }); |
||
115 | }); |
||
116 | |||
117 | it('delete a webhook', function(done) { |
||
118 | client.deleteWebhook(createdWebhooks[0], function(err, response) { |
||
119 | assert.ifError(err); |
||
120 | assert.ok(response); |
||
121 | done(); |
||
122 | }); |
||
123 | }); |
||
124 | |||
125 | it('update a webhook', function(done) { |
||
126 | var newIdentifier = crypto.randomBytes(24).toString('hex'); |
||
127 | var newUrl = "https://www.blocktrail.com/new-webhook-url"; |
||
128 | client.updateWebhook(createdWebhooks[1], {identifier: newIdentifier, url: newUrl}, function(err, response) { |
||
129 | assert.ifError(err); |
||
130 | assert.ok('url' in response, "'url' key not in response"); |
||
131 | assert.ok('identifier' in response, "'identifier' key not in response"); |
||
132 | assert.equal(response['url'], newUrl, "'url' does not match expected value"); |
||
133 | assert.equal(response['identifier'], newIdentifier, "'identifier' does not match expected value"); |
||
134 | |||
135 | createdWebhooks[1] = newIdentifier; |
||
136 | done(); |
||
137 | }); |
||
138 | }); |
||
139 | |||
140 | it('subscribe to address-transaction events', function(done) { |
||
141 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; |
||
142 | client.subscribeAddressTransactions(createdWebhooks[1], address, 2, function(err, response) { |
||
143 | assert.ifError(err); |
||
144 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
145 | assert.ok('address' in response, "'address' key not in response"); |
||
146 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
147 | assert.equal(response['event_type'], "address-transactions", "'event_type' does not match expected value"); |
||
148 | assert.equal(response['address'], address, "'address' does not match expected value"); |
||
149 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); |
||
150 | done(); |
||
151 | }); |
||
152 | }); |
||
153 | |||
154 | it('subscribe to transaction event', function(done) { |
||
155 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; |
||
156 | client.subscribeTransaction(createdWebhooks[1], transaction, 2, function(err, response) { |
||
157 | assert.ifError(err); |
||
158 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
159 | assert.ok('address' in response, "'address' key not in response"); |
||
160 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
161 | assert.equal(response['event_type'], "transaction", "'event_type' does not match expected value"); |
||
162 | assert.equal(response['transaction'], transaction, "'transaction' does not match expected value"); |
||
163 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); |
||
164 | done(); |
||
165 | }); |
||
166 | }); |
||
167 | |||
168 | it('subscribe to new block events', function(done) { |
||
169 | client.subscribeNewBlocks(createdWebhooks[1], function(err, response) { |
||
170 | assert.ifError(err); |
||
171 | assert.ok('event_type' in response, "'event_type' key not in response"); |
||
172 | assert.ok('address' in response, "'address' key not in response"); |
||
173 | assert.ok('confirmations' in response, "'confirmations' key not in response"); |
||
174 | assert.equal(response['event_type'], "block", "'event_type' does not match expected value"); |
||
175 | assert.equal(response['address'], null, "'address' does not match expected value"); |
||
176 | assert.equal(response['confirmations'], null, "'confirmations' does not match expected value"); |
||
177 | done(); |
||
178 | }); |
||
179 | }); |
||
180 | |||
181 | it('batch subscribe to address-transaction events', function(done) { |
||
182 | var batchData = [ |
||
183 | { |
||
184 | 'event_type': 'address-transactions', |
||
185 | 'address': '18FA8Tn54Hu8fjn7kkfAygPoGEJLHMbHzo', |
||
186 | 'confirmations': 1 |
||
187 | }, |
||
188 | { |
||
189 | 'address': '1LUCKYwD6V9JHVXAFEEjyQSD4Dj5GLXmte', |
||
190 | 'confirmations': 1 |
||
191 | }, |
||
192 | { |
||
193 | 'address': '1qMBuZnrmGoAc2MWyTnSgoLuWReDHNYyF' |
||
194 | } |
||
195 | ]; |
||
196 | client.batchSubscribeAddressTransactions(createdWebhooks[1], batchData, function(err, response) { |
||
197 | assert.ifError(err); |
||
198 | assert.ok(response); |
||
199 | done(); |
||
200 | }); |
||
201 | }); |
||
202 | |||
203 | it('get webhook event subscriptions', function(done) { |
||
204 | client.getWebhookEvents(createdWebhooks[1], function(err, response) { |
||
205 | assert.ifError(err); |
||
206 | assert.ok('data' in response, "'data' key not in response"); |
||
207 | assert.ok('total' in response, "'total' key not in response"); |
||
208 | assert.equal(parseInt(response['total']), 6, "'total' does not match expected value"); |
||
209 | assert.equal(response['data'].length, 6, "Count of event subscriptions returned is not equal to 2"); |
||
210 | |||
211 | assert.ok('event_type' in response['data'][0], "'event_type' key not in first event subscription of response"); |
||
212 | |||
213 | done(); |
||
214 | }); |
||
215 | }); |
||
216 | |||
217 | it('unsubscribe from address-transaction events', function(done) { |
||
218 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; |
||
219 | client.unsubscribeAddressTransactions(createdWebhooks[1], address, function(err, response) { |
||
220 | assert.ifError(err); |
||
221 | assert.ok(response); |
||
222 | done(); |
||
223 | }); |
||
224 | }); |
||
225 | |||
226 | it('unsubscribe from new transaction events', function(done) { |
||
227 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; |
||
228 | client.unsubscribeTransaction(createdWebhooks[1], transaction, function(err, response) { |
||
229 | assert.ifError(err); |
||
230 | assert.ok(response); |
||
231 | done(); |
||
232 | }); |
||
233 | }); |
||
234 | |||
235 | it('unsubscribe from new block events', function(done) { |
||
236 | client.unsubscribeNewBlocks(createdWebhooks[1], function(err, response) { |
||
237 | assert.ifError(err); |
||
238 | assert.ok(response); |
||
239 | done(); |
||
240 | }); |
||
241 | }); |
||
242 | }); |
||
243 | |||
244 | describe('market api', function() { |
||
245 | it('should have a price', function(done) { |
||
246 | client.price(function(err, price) { |
||
247 | assert.ifError(err); |
||
248 | assert.ok(price); |
||
249 | assert.ok(price['USD']); |
||
250 | done(); |
||
251 | }); |
||
252 | }); |
||
253 | }); |
||
254 | |||
255 | describe('verify message', function() { |
||
256 | var address = "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G"; |
||
257 | var message = address; |
||
258 | var signature = "H85WKpqtNZDrajOnYDgUY+abh0KCAcOsAIOQwx2PftAbLEPRA7mzXA/CjXRxzz0MC225pR/hx02Vf2Ag2x33kU4="; |
||
259 | |||
260 | it('should verify using bitcoinjs-lib', function(done) { |
||
261 | client.verifyMessage(message, address, signature, function(err, result) { |
||
262 | assert.ifError(err); |
||
263 | assert.ok(result); |
||
264 | done(); |
||
265 | }); |
||
266 | }); |
||
267 | |||
268 | it('should handle errors nicely', function(done) { |
||
269 | signature = "rubensayshi"; |
||
270 | client.verifyMessage(message, address, signature, function(err, result) { |
||
0 ignored issues
–
show
|
|||
271 | assert.ok(err); |
||
272 | done(); |
||
273 | }); |
||
274 | }); |
||
275 | }); |
||
276 | |||
277 | describe('send raw', function() { |
||
278 | /** |
||
279 | * @type APIClient |
||
280 | */ |
||
281 | var client = blocktrail.BlocktrailSDK({ |
||
282 | apiKey: process.env.BLOCKTRAIL_SDK_APIKEY || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APIKEY", |
||
283 | apiSecret: process.env.BLOCKTRAIL_SDK_APISECRET || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APISECRET", |
||
284 | testnet: true, |
||
285 | btccom: false |
||
286 | }); |
||
287 | |||
288 | var tx = "0100000001bee92b36d3092e492e858d1199e46b942b3bddcc4c98071f0d307acced6f7751000000006b48304502210087831790820bf8218dc8df38758660a6f1f54a54d5d45ab0c3384e5ace9253ad0220650bce47447094148d45ec5b9ce4e3008e00723f4de6edd677110b2ebf0ff3da012102d8aa27d34020a6eb06e424787dbbb60f2cf4250a5a1110ab9e15e68fe710abc5ffffffff0131244c00000000001976a914a8d7a8e6724cf3f8ffb92c376ecb0094c18cbaf588ac00000000"; |
||
289 | |||
290 | // note that -27 (already in blockchain) is only when it's unspent |
||
291 | it("should report TX is already in blockchain", function(done) { |
||
292 | client.sendRawTransaction(tx, function(err, result) { |
||
293 | assert.ok(err); |
||
294 | assert.ok(result.code === -27 || result.code === -25); |
||
295 | |||
296 | done(); |
||
297 | }); |
||
298 | }); |
||
299 | |||
300 | it('should error decode failed', function(done) { |
||
301 | client.sendRawTransaction(tx.substr(-2), function(err, result) { |
||
302 | assert.ok(err); |
||
303 | assert.equal(-22, result.code); |
||
304 | |||
305 | done(); |
||
306 | }); |
||
307 | }); |
||
308 | }); |
||
309 | |||
310 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.