Completed
Pull Request — master (#99)
by
unknown
01:16
created

describe(ꞌbtccom.convertꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 330

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 330
rs 8.2857
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var assert = require('assert');
2
var BtccomConverter = require("../lib/btccom.convert");
3
var network = {
4
    messagePrefix: '\u0018Bitcoin Signed Message:\n',
5
    bech32: 'bc',
6
    bip32: { public: 76067358, private: 76066276 },
7
    pubKeyHash: 0,
8
    scriptHash: 5,
9
    wif: 128
10
};
11
var useNewCashAddr = true;
12
var converter = new BtccomConverter(network, useNewCashAddr);
13
14
describe("btccom.convert", function() {
15
    describe("convertBlock", function() {
16
        it("works", function(cb) {
17
            var input = {
18
                "data": {
19
                    "height": 200000,
20
                    "version": 2,
21
                    "mrkl_root": "a08f8101f50fd9c9b3e5252aff4c1c1bd668f878fffaf3d0dbddeb029c307e88",
22
                    "timestamp": 1348310759,
23
                    "bits": 436591499,
24
                    "nonce": 4158183488,
25
                    "hash": "000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf",
26
                    "prev_block_hash": "00000000000003a20def7a05a77361b9657ff954b2f2080e135ea6f5970da215",
27
                    "next_block_hash": "00000000000002e3269b8a00caf315115297c626f954770e8398470d7f387e1c",
28
                    "size": 247533,
29
                    "pool_difficulty": 5097877,
30
                    "difficulty": 2864140.5078109736,
31
                    "tx_count": 388,
32
                    "reward_block": 5000000000,
33
                    "reward_fees": 63517500,
34
                    "created_at": 1492101215,
35
                    "confirmations": 310251,
36
                    "is_orphan": false,
37
                    "curr_max_timestamp": 1348310759,
38
                    "is_sw_block": false,
39
                    "stripped_size": 247533,
40
                    "weight": 990132,
41
                    "extras": {
42
                        "pool_name": "unknown",
43
                        "pool_link": ""
44
                    }
45
                },
46
                "err_no": 0,
47
                "err_msg": null
48
            };
49
50
            var output = converter.convertBlock(input);
51
52
            // assert fields match
53
            [
54
                ["height", "height"],
55
                ["is_orphan", "is_orphan"],
56
                ["merkleroot", "mrkl_root"],
57
                ["hash", "hash"],
58
                ["prev_block", "prev_block_hash"],
59
                ["next_block", "next_block_hash"],
60
                ["transactions", "tx_count"],
61
            ].forEach(function(keyAssoc) {
62
                var blocktrailKey = keyAssoc[0];
63
                var btccomKey = keyAssoc[1];
64
                assert.ok(blocktrailKey in output);
65
                assert.ok(input.data[btccomKey] == output[blocktrailKey])
66
            });
67
68
            cb();
69
        });
70
    });
71
72
    describe("convertBlockTxs", function() {
73
        it("works", function(cb) {
74
            var input = {
75
                "data": {
76
                    "total_count": 388,
77
                    "page": 1,
78
                    "pagesize": 50,
79
                    "list": [
80
                        {
81
                            "confirmations": 310251,
82
                            "block_height": 200000,
83
                            "block_time": 1348310759,
84
                            "created_at": 1492101215,
85
                            "fee": 63517500,
86
                            "hash": "dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10",
87
                            "inputs_count": 1,
88
                            "inputs_value": 0,
89
                            "is_coinbase": true,
90
                            "is_double_spend": false,
91
                            "is_sw_tx": false,
92
                            "weight": 768,
93
                            "vsize": 192,
94
                            "witness_hash": "dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10",
95
                            "lock_time": 0,
96
                            "outputs_count": 1,
97
                            "outputs_value": 5063517500,
98
                            "size": 192,
99
                            "sigops": 4,
100
                            "version": 1,
101
                            "inputs": [
102
                                {
103
                                    "prev_addresses": [],
104
                                    "prev_position": -1,
105
                                    "prev_tx_hash": "0000000000000000000000000000000000000000000000000000000000000000",
106
                                    "prev_type": "NONSTANDARD",
107
                                    "prev_value": 0,
108
                                    "sequence": 4294967295,
109
                                    "script_asm": "",
110
                                    "script_hex": "03400d0302ef02062f503253482f522cfabe6d6dd90d39663d10f8fd25ec88338295d4c6ce1c90d4aeb368d8bdbadcc1da3b635801000000000000000474073e03",
111
                                    "witness": []
112
                                }
113
                            ],
114
                            "outputs": [
115
                                {
116
                                    "addresses": [
117
                                        "1MdYC22Gmjp2ejVPCxyYjFyWbQCYTGhGq8"
118
                                    ],
119
                                    "value": 5063517500,
120
                                    "type": "P2PKH_PUBKEY",
121
                                    "script_asm": "04b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b OP_CHECKSIG",
122
                                    "script_hex": "4104b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7bac",
123
                                    "spent_by_tx": "5c76eb4dfb0941856a229833ef05b2f5c669dadc98ed2a34ea11974cacba9dc7",
124
                                    "spent_by_tx_position": 0
125
                                }
126
                            ]
127
                        },
128
                        {
129
                            "confirmations": 310251,
130
                            "block_height": 200000,
131
                            "block_time": 1348310759,
132
                            "created_at": 1492101215,
133
                            "fee": 0,
134
                            "hash": "ee475443f1fbfff84ffba43ba092a70d291df233bd1428f3d09f7bd1a6054a1f",
135
                            "inputs_count": 1,
136
                            "inputs_value": 10212000000,
137
                            "is_coinbase": false,
138
                            "is_double_spend": false,
139
                            "is_sw_tx": false,
140
                            "weight": 904,
141
                            "vsize": 226,
142
                            "witness_hash": "ee475443f1fbfff84ffba43ba092a70d291df233bd1428f3d09f7bd1a6054a1f",
143
                            "lock_time": 0,
144
                            "outputs_count": 2,
145
                            "outputs_value": 10212000000,
146
                            "size": 226,
147
                            "sigops": 8,
148
                            "version": 1,
149
                            "inputs": [
150
                                {
151
                                    "prev_addresses": [
152
                                        "1CfD77hupeUvFwBPxZ2fA8iyWmVwQY22oh"
153
                                    ],
154
                                    "prev_position": 1,
155
                                    "prev_tx_hash": "6e046b9c7683b5887bbde42ec358542ffe9c250306edb2d0cf386394aa96ee10",
156
                                    "prev_type": "P2PKH",
157
                                    "prev_value": 10212000000,
158
                                    "sequence": 4294967295,
159
                                    "script_asm": "3045022100e5e4749d539a163039769f52e1ebc8e6f62e39387d61e1a305bd722116cded6c022014924b745dd02194fe6b5cb8ac88ee8e9a2aede89e680dcea6169ea696e24d52[ALL] 02b4b754609b46b5d09644c2161f1767b72b93847ce8154d795f95d31031a08aa2",
160
                                    "script_hex": "483045022100e5e4749d539a163039769f52e1ebc8e6f62e39387d61e1a305bd722116cded6c022014924b745dd02194fe6b5cb8ac88ee8e9a2aede89e680dcea6169ea696e24d52012102b4b754609b46b5d09644c2161f1767b72b93847ce8154d795f95d31031a08aa2",
161
                                    "witness": []
162
                                }
163
                            ],
164
                            "outputs": [
165
                                {
166
                                    "addresses": [
167
                                        "1FhNPRh1TxVidoKkWFEpdmK5RXw9vG1KUb"
168
                                    ],
169
                                    "value": 5586000000,
170
                                    "type": "P2PKH",
171
                                    "script_asm": "OP_DUP OP_HASH160 a134408afa258a50ed7a1d9817f26b63cc9002cc OP_EQUALVERIFY OP_CHECKSIG",
172
                                    "script_hex": "76a914a134408afa258a50ed7a1d9817f26b63cc9002cc88ac",
173
                                    "spent_by_tx": "12c7db0b0f4baae58ee1be0fd925fbbf7588ea3339f38557978015ba120ca086",
174
                                    "spent_by_tx_position": 0
175
                                },
176
                                {
177
                                    "addresses": [
178
                                        "1QE7Tazy1uHD1utCb1fCW6aLF3RZ1sNwd2"
179
                                    ],
180
                                    "value": 4626000000,
181
                                    "type": "P2PKH",
182
                                    "script_asm": "OP_DUP OP_HASH160 fec5b1145596b35f59f8be1daf169f3759421433 OP_EQUALVERIFY OP_CHECKSIG",
183
                                    "script_hex": "76a914fec5b1145596b35f59f8be1daf169f375942143388ac",
184
                                    "spent_by_tx": "6cf0a98e6290c343c8eed9373ec63329a5147a491974d3a146bdbf466978b306",
185
                                    "spent_by_tx_position": 0
186
                                }
187
                            ]
188
                        },
189
                        {
190
                            "confirmations": 310251,
191
                            "block_height": 200000,
192
                            "block_time": 1348310759,
193
                            "created_at": 1492101215,
194
                            "fee": 0,
195
                            "hash": "e03a9a4b5c557f6ee3400a29ff1475d1df73e9cddb48c2391abdc391d8c1504a",
196
                            "inputs_count": 1,
197
                            "inputs_value": 9500000000,
198
                            "is_coinbase": false,
199
                            "is_double_spend": false,
200
                            "is_sw_tx": false,
201
                            "weight": 900,
202
                            "vsize": 225,
203
                            "witness_hash": "e03a9a4b5c557f6ee3400a29ff1475d1df73e9cddb48c2391abdc391d8c1504a",
204
                            "lock_time": 0,
205
                            "outputs_count": 2,
206
                            "outputs_value": 9500000000,
207
                            "size": 225,
208
                            "sigops": 8,
209
                            "version": 1,
210
                            "inputs": [
211
                                {
212
                                    "prev_addresses": [
213
                                        "1DC8mbgdVFNZn7ie6wUVGegKHsqvKkN7Qn"
214
                                    ],
215
                                    "prev_position": 0,
216
                                    "prev_tx_hash": "0530375a5bf4ea9a82494fcb5ef4a61076c2af807982076fa810851f4bc31c09",
217
                                    "prev_type": "P2PKH",
218
                                    "prev_value": 9500000000,
219
                                    "sequence": 4294967295,
220
                                    "script_asm": "304402202b5869e0b4e711784daecd511f9dadf28e37113ef5054a6898d0367034afb067022014dfd2f58da949550ddc4ebb1135527cdbc48b25c6eb6388cc88231fd7ea04a9[ALL] 02fec863d4d0baf5e01a9bd9c59b790bbf683c5b7b6624d7aa7ab9405c5fdb9786",
221
                                    "script_hex": "47304402202b5869e0b4e711784daecd511f9dadf28e37113ef5054a6898d0367034afb067022014dfd2f58da949550ddc4ebb1135527cdbc48b25c6eb6388cc88231fd7ea04a9012102fec863d4d0baf5e01a9bd9c59b790bbf683c5b7b6624d7aa7ab9405c5fdb9786",
222
                                    "witness": []
223
                                }
224
                            ],
225
                            "outputs": [
226
                                {
227
                                    "addresses": [
228
                                        "1HtF7xbaDwJu1j5sKpetG91xr4AZHYmJTL"
229
                                    ],
230
                                    "value": 9400000000,
231
                                    "type": "P2PKH",
232
                                    "script_asm": "OP_DUP OP_HASH160 b933036b1f143e6ee5f9038c4205f6bbe41e49cc OP_EQUALVERIFY OP_CHECKSIG",
233
                                    "script_hex": "76a914b933036b1f143e6ee5f9038c4205f6bbe41e49cc88ac",
234
                                    "spent_by_tx": "360c0ee799aa6e00032f87d62751d5a20cd9e6c6245f6420971647c45aca59e9",
235
                                    "spent_by_tx_position": 0
236
                                },
237
                                {
238
                                    "addresses": [
239
                                        "1AQHgGfYswNkput7vzGzCUUzH4wQQkWKXr"
240
                                    ],
241
                                    "value": 100000000,
242
                                    "type": "P2PKH",
243
                                    "script_asm": "OP_DUP OP_HASH160 67209ed8971b9bdf4147cd25e5f09c77f43c0dac OP_EQUALVERIFY OP_CHECKSIG",
244
                                    "script_hex": "76a91467209ed8971b9bdf4147cd25e5f09c77f43c0dac88ac",
245
                                    "spent_by_tx": "4f67254491474ae03d8e023fae4bf249e1f81b07efb6ea763a13c9bc7b4210b3",
246
                                    "spent_by_tx_position": 1
247
                                }
248
                            ]
249
                        },
250
                        {
251
                            "confirmations": 310251,
252
                            "block_height": 200000,
253
                            "block_time": 1348310759,
254
                            "created_at": 1492101215,
255
                            "fee": 0,
256
                            "hash": "ffa0267c8f2af736858894d6f3e5081a05e2ec16dc98f78a80f376ce35077491",
257
                            "inputs_count": 1,
258
                            "inputs_value": 37325663465,
259
                            "is_coinbase": false,
260
                            "is_double_spend": false,
261
                            "is_sw_tx": false,
262
                            "weight": 1032,
263
                            "vsize": 258,
264
                            "witness_hash": "ffa0267c8f2af736858894d6f3e5081a05e2ec16dc98f78a80f376ce35077491",
265
                            "lock_time": 0,
266
                            "outputs_count": 2,
267
                            "outputs_value": 37325663465,
268
                            "size": 258,
269
                            "sigops": 8,
270
                            "version": 1,
271
                            "inputs": [
272
                                {
273
                                    "prev_addresses": [
274
                                        "1AGt1P3fhCF9MUue3dheegBZgGVHeLAtLr"
275
                                    ],
276
                                    "prev_position": 1,
277
                                    "prev_tx_hash": "1fa6ecf02e5de2001e4aec6d3e90b5ef6d81aead39e7f04f303e6c8415547bba",
278
                                    "prev_type": "P2PKH",
279
                                    "prev_value": 37325663465,
280
                                    "sequence": 4294967295,
281
                                    "script_asm": "3045022001b572518fe610121b777286b424d89ba73fe503ff440458f01361a29348248b022100fa1afae0ccf3007d208605662685dd57e6940066ae6be81aa8b2af319060fceb[ALL] 04f28840b52c60c0fa041b9f4b2fa3dd00bccd71c0ab03f4db3c4295a23b117535fdd6d34787f95870d51ec245c800d851dd8509de42c47a0dc7a09300053110b8",
282
                                    "script_hex": "483045022001b572518fe610121b777286b424d89ba73fe503ff440458f01361a29348248b022100fa1afae0ccf3007d208605662685dd57e6940066ae6be81aa8b2af319060fceb014104f28840b52c60c0fa041b9f4b2fa3dd00bccd71c0ab03f4db3c4295a23b117535fdd6d34787f95870d51ec245c800d851dd8509de42c47a0dc7a09300053110b8",
283
                                    "witness": []
284
                                }
285
                            ],
286
                            "outputs": [
287
                                {
288
                                    "addresses": [
289
                                        "1D5Em12TqtZtX3LJHYiGjarKCVss4nMmGP"
290
                                    ],
291
                                    "value": 17749527722,
292
                                    "type": "P2PKH",
293
                                    "script_asm": "OP_DUP OP_HASH160 846ea88b63aa1dbf265a33127f518974402870bd OP_EQUALVERIFY OP_CHECKSIG",
294
                                    "script_hex": "76a914846ea88b63aa1dbf265a33127f518974402870bd88ac",
295
                                    "spent_by_tx": "0e6641d59cbcbbb2df61f1f80b044642dd68916096de00f9d9e4f0106e148d2c",
296
                                    "spent_by_tx_position": 1
297
                                },
298
                                {
299
                                    "addresses": [
300
                                        "13b9rHKTNBMdKWmoiKM8o7Gx5zkvJwZG6x"
301
                                    ],
302
                                    "value": 19576135743,
303
                                    "type": "P2PKH",
304
                                    "script_asm": "OP_DUP OP_HASH160 1c65c81774aa85e123b0e5c51bc7cda6805f154e OP_EQUALVERIFY OP_CHECKSIG",
305
                                    "script_hex": "76a9141c65c81774aa85e123b0e5c51bc7cda6805f154e88ac",
306
                                    "spent_by_tx": "93ee77570a26841bc55a76a0739024b6521183a7d2fee3a0320570c41d01b76f",
307
                                    "spent_by_tx_position": 0
308
                                }
309
                            ]
310
                        },
311
                        {
312
                            "confirmations": 310251,
313
                            "block_height": 200000,
314
                            "block_time": 1348310759,
315
                            "created_at": 1492101215,
316
                            "fee": 50000,
317
                            "hash": "2674c8a46b75e5a5e3287a34d7999a1e5e6f052be36f63f3cb483ec148c9b86c",
318
                            "inputs_count": 1,
319
                            "inputs_value": 946622517855,
320
                            "is_coinbase": false,
321
                            "is_double_spend": false,
322
                            "is_sw_tx": false,
323
                            "weight": 908,
324
                            "vsize": 227,
325
                            "witness_hash": "2674c8a46b75e5a5e3287a34d7999a1e5e6f052be36f63f3cb483ec148c9b86c",
326
                            "lock_time": 0,
327
                            "outputs_count": 2,
328
                            "outputs_value": 946622467855,
329
                            "size": 227,
330
                            "sigops": 8,
331
                            "version": 1,
332
                            "inputs": [
333
                                {
334
                                    "prev_addresses": [
335
                                        "14DeXEwWrpo7urTz76LgQpxXtF8SAQMTbe"
336
                                    ],
337
                                    "prev_position": 0,
338
                                    "prev_tx_hash": "a8d4bb8395b6a59ff0355a145b3c0a63d42966af02c373e4c86e4d2779f514e2",
339
                                    "prev_type": "P2PKH",
340
                                    "prev_value": 946622517855,
341
                                    "sequence": 4294967295,
342
                                    "script_asm": "3046022100d9331c464433c16e59947d6d36df23cd6d29160e97e2707c27bd7641d291a3380221008b1927354afb5a1bd0412fdbde7a3141cba991342ac17505bf13452dac4a533a[ALL] 0319a9a55c1e4dfd168adae85af834ec0db91ae1c164c7286316155a9135afcee8",
343
                                    "script_hex": "493046022100d9331c464433c16e59947d6d36df23cd6d29160e97e2707c27bd7641d291a3380221008b1927354afb5a1bd0412fdbde7a3141cba991342ac17505bf13452dac4a533a01210319a9a55c1e4dfd168adae85af834ec0db91ae1c164c7286316155a9135afcee8",
344
                                    "witness": []
345
                                }
346
                            ],
347
                            "outputs": [
348
                                {
349
                                    "addresses": [
350
                                        "1Fg7BsVkTk1gWnWTpgyb26Zo6NGNsx2gcQ"
351
                                    ],
352
                                    "value": 945815591855,
353
                                    "type": "P2PKH",
354
                                    "script_asm": "OP_DUP OP_HASH160 a0f7260313ab5798391cb610bc4fe0a6be883d59 OP_EQUALVERIFY OP_CHECKSIG",
355
                                    "script_hex": "76a914a0f7260313ab5798391cb610bc4fe0a6be883d5988ac",
356
                                    "spent_by_tx": "981db136c4efaa9f91db680bf16aa2e38a0bc13e70f56d5f10b1b86903973f51",
357
                                    "spent_by_tx_position": 0
358
                                },
359
                                {
360
                                    "addresses": [
361
                                        "1Q22kYfL81iaAZZLj4epvUQ3yLv57Mgd56"
362
                                    ],
363
                                    "value": 806876000,
364
                                    "type": "P2PKH",
365
                                    "script_asm": "OP_DUP OP_HASH160 fc7cc53ddff31d4c6b18ea7331b80f2b76d7a45d OP_EQUALVERIFY OP_CHECKSIG",
366
                                    "script_hex": "76a914fc7cc53ddff31d4c6b18ea7331b80f2b76d7a45d88ac",
367
                                    "spent_by_tx": "a68c971e3666c36717fe2ec80e6ebbefd5045601476d5c845b90c26c4fa2a71d",
368
                                    "spent_by_tx_position": 9
369
                                }
370
                            ]
371
                        }
372
                    ]
373
                },
374
                "err_no": 0,
375
                "err_msg": null
376
            };
377
378
            var output = converter.convertBlockTxs(input);
379
            assert.ok("current_page" in output);
380
            assert.ok("per_page" in output);
381
            assert.ok("total" in output);
382
383
            // assert fields match
384
            [
385
                ["size", "size"],
386
                ["hash", "hash"],
387
                ["confirmations", "confirmations"],
388
                ["is_coinbase", "is_coinbase"],
389
                ["total_fee", "fee"],
390
                ["size", "size"],
391
                ["is_double_spend", "is_double_spend"]
392
393
            ].forEach(function(keyAssoc) {
394
                var blocktrailKey = keyAssoc[0];
395
                var btccomKey = keyAssoc[1];
396
                var blockTx = output.data[0];
397
                assert.ok(blocktrailKey in blockTx);
398
                assert.ok(input.data.list[0][btccomKey] == blockTx[blocktrailKey])
399
            });
400
401
            cb();
402
        });
403
    });
404
405
    describe("convertTx", function() {
406
        it("works", function(cb) {
407
            var input = {
408
                "err_no": 0,
409
                "data": {
410
                    "confirmations": 310251,
411
                    "block_height": 200000,
412
                    "block_time": 1348310759,
413
                    "created_at": 1492101215,
414
                    "fee": 63517500,
415
                    "hash": "dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10",
416
                    "inputs_count": 1,
417
                    "inputs_value": 0,
418
                    "is_coinbase": true,
419
                    "is_double_spend": false,
420
                    "is_sw_tx": false,
421
                    "weight": 768,
422
                    "vsize": 192,
423
                    "witness_hash": "dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10",
424
                    "lock_time": 0,
425
                    "outputs_count": 1,
426
                    "outputs_value": 5063517500,
427
                    "size": 192,
428
                    "sigops": 4,
429
                    "version": 1,
430
                    "inputs": [
431
                        {
432
                            "prev_addresses": [],
433
                            "prev_position": -1,
434
                            "prev_tx_hash": "0000000000000000000000000000000000000000000000000000000000000000",
435
                            "prev_type": "NONSTANDARD",
436
                            "prev_value": 0,
437
                            "sequence": 4294967295,
438
                            "script_asm": "",
439
                            "script_hex": "03400d0302ef02062f503253482f522cfabe6d6dd90d39663d10f8fd25ec88338295d4c6ce1c90d4aeb368d8bdbadcc1da3b635801000000000000000474073e03",
440
                            "witness": []
441
                        }
442
                    ],
443
                    "outputs": [
444
                        {
445
                            "addresses": [
446
                                "1MdYC22Gmjp2ejVPCxyYjFyWbQCYTGhGq8"
447
                            ],
448
                            "value": 5063517500,
449
                            "type": "P2PKH_PUBKEY",
450
                            "script_asm": "04b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b OP_CHECKSIG",
451
                            "script_hex": "4104b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7bac",
452
                            "spent_by_tx": "5c76eb4dfb0941856a229833ef05b2f5c669dadc98ed2a34ea11974cacba9dc7",
453
                            "spent_by_tx_position": 0
454
                        }
455
                    ]
456
                }
457
            };
458
459
            var output = converter.convertTx(input);
460
            assert.ok("hash" in output);
461
462
            // assert fields match
463
            [
464
                ["size", "size"],
465
                ["hash", "hash"],
466
                ["confirmations", "confirmations"],
467
                ["is_coinbase", "is_coinbase"],
468
                ["block_height", "block_height"],
469
                ["total_fee", "fee"],
470
                ["size", "size"],
471
                ["is_double_spend", "is_double_spend"]
472
473
            ].forEach(function(keyAssoc) {
474
                var blocktrailKey = keyAssoc[0];
475
                var btccomKey = keyAssoc[1]
476
                var blockTx = output;
477
                assert.ok(blocktrailKey in blockTx);
478
                assert.ok(input.data[btccomKey] == blockTx[blocktrailKey])
479
            });
480
481
            cb();
482
        });
483
484
    });
485
486
    describe("convertAddress", function() {
487
        it("works", function(cb) {
488
            var input = {
489
                "err_no": 0,
490
                "data": {
491
                    "address": "3EU8LRmo5PgcSwnkn6Msbqc8BKNoQ7Xief",
492
                    "received": 190000,
493
                    "sent": 100000,
494
                    "balance": 90000,
495
                    "tx_count": 3,
496
                    "unconfirmed_tx_count": 0,
497
                    "unconfirmed_received": 0,
498
                    "unconfirmed_sent": 0,
499
                    "unspent_tx_count": 1,
500
                    "first_tx": "2613f3f7ccf6ca8beb68f72469905048120d7fc353bb02e81b1b4f570c5ce490",
501
                    "last_tx": "bd430d52f35166a7dd6251c73a48559ad8b5f41b6c5bc4a6c4c1a3e3702f4287"
502
                }
503
            };
504
505
            var output = converter.convertAddress(input);
506
507
            // assert fields match
508
            [
509
                ["address", "address"],
510
                ["balance", "balance"],
511
                ["received", "received"],
512
                ["sent", "sent"],
513
                ["transactions", "tx_count"],
514
                ["unconfirmed_received", "unconfirmed_received"],
515
                ["unconfirmed_sent", "unconfirmed_sent"],
516
                ["unconfirmed_transactions", "unconfirmed_tx_count"],
517
                ["unconfirmed_utxos", "unspent_tx_count"],
518
                ["first_tx", "first_tx"],
519
                ["last_tx", "last_tx"]
520
521
            ].forEach(function(keyAssoc) {
522
                var blocktrailKey = keyAssoc[0];
523
                var btccomKey = keyAssoc[1];
524
525
                var addressData = output.data;
526
                assert.ok(blocktrailKey in addressData);
527
                assert.ok(input.data[btccomKey] == addressData[blocktrailKey])
528
            });
529
530
            cb();
531
        });
532
    });
533
534
    describe("convertAddressTxs", function() {
535
        it("works", function(cb) {
536
            var input = {
537
                "data": {
538
                    "total_count": 3,
539
                    "page": 1,
540
                    "pagesize": 50,
541
                    "list": [
542
                        {
543
                            "confirmations": 28118,
544
                            "block_height": 482133,
545
                            "block_time": 1503801123,
546
                            "created_at": 1503563229,
547
                            "fee": 13409,
548
                            "hash": "bd430d52f35166a7dd6251c73a48559ad8b5f41b6c5bc4a6c4c1a3e3702f4287",
549
                            "inputs_count": 1,
550
                            "inputs_value": 100000,
551
                            "is_coinbase": false,
552
                            "is_double_spend": false,
553
                            "is_sw_tx": true,
554
                            "weight": 578,
555
                            "vsize": 145,
556
                            "witness_hash": "8d558281ffc88ca7c294e15ce0b778b9c85490dd1ff3121305c4f51b86a758f1",
557
                            "lock_time": 0,
558
                            "outputs_count": 1,
559
                            "outputs_value": 86591,
560
                            "size": 227,
561
                            "sigops": 1,
562
                            "version": 1,
563
                            "inputs": [
564
                                {
565
                                    "prev_addresses": [
566
                                        "3EU8LRmo5PgcSwnkn6Msbqc8BKNoQ7Xief"
567
                                    ],
568
                                    "prev_position": 1,
569
                                    "prev_tx_hash": "4de2b03b46c8c1ab5e582ea3ab4f1b055c91798ffd8e9cbe05010c99bc914947",
570
                                    "prev_type": "P2SH",
571
                                    "prev_value": 100000,
572
                                    "sequence": 4294967295,
573
                                    "script_asm": "00145d6f02f47dc6c57093df246e3742cfe1e22ab410",
574
                                    "script_hex": "1600145d6f02f47dc6c57093df246e3742cfe1e22ab410",
575
                                    "witness": [
576
                                        "3045022100a1707fff61f5add9ca4cf7cdb39aa0b3fd9f672274715749c9356d4eda57becd02201aea20e0ba29adc67fba8fdcf727297b841017cb8b0ff58ea5c5160037975f0201",
577
                                        "03534da516a0ab32f30246620fdfbfaf1921228c1e222c6bd2fcddbcfd9024a1b6"
578
                                    ]
579
                                }
580
                            ],
581
                            "outputs": [
582
                                {
583
                                    "addresses": [
584
                                        "916ff972855bf7589caf8c46a31f7f33b07d0100d953fde95a8354ac36e98165"
585
                                    ],
586
                                    "value": 86591,
587
                                    "type": "P2WSH_V0",
588
                                    "script_asm": "0 916ff972855bf7589caf8c46a31f7f33b07d0100d953fde95a8354ac36e98165",
589
                                    "script_hex": "0020916ff972855bf7589caf8c46a31f7f33b07d0100d953fde95a8354ac36e98165",
590
                                    "spent_by_tx": "cab75da6d7fe1531c881d4efdb4826410a2604aa9e6442ab12a08363f34fb408",
591
                                    "spent_by_tx_position": 0
592
                                }
593
                            ],
594
                            "balance_diff": -100000
595
                        },
596
                        {
597
                            "confirmations": 28162,
598
                            "block_height": 482089,
599
                            "block_time": 1503765455,
600
                            "created_at": 1503560516,
601
                            "fee": 83409,
602
                            "hash": "4de2b03b46c8c1ab5e582ea3ab4f1b055c91798ffd8e9cbe05010c99bc914947",
603
                            "inputs_count": 1,
604
                            "inputs_value": 316400,
605
                            "is_coinbase": false,
606
                            "is_double_spend": false,
607
                            "is_sw_tx": false,
608
                            "weight": 1484,
609
                            "vsize": 371,
610
                            "witness_hash": "4de2b03b46c8c1ab5e582ea3ab4f1b055c91798ffd8e9cbe05010c99bc914947",
611
                            "lock_time": 0,
612
                            "outputs_count": 2,
613
                            "outputs_value": 232991,
614
                            "size": 371,
615
                            "sigops": 12,
616
                            "version": 1,
617
                            "inputs": [
618
                                {
619
                                    "prev_addresses": [
620
                                        "3Fzr53M54N8nWwTPt7btU4pcXQ3kNUUtrx"
621
                                    ],
622
                                    "prev_position": 0,
623
                                    "prev_tx_hash": "d37f2a922c3e03d5c8e3bbb5e8f704d4eb221e4c9e1e3165201e58ddea54b440",
624
                                    "prev_type": "P2SH",
625
                                    "prev_value": 316400,
626
                                    "sequence": 4294967295,
627
                                    "script_asm": "0 3045022100dc0eb9ebb44e21a2334b1a9936ff2d3e6f85b6a8b0bae2462640ad574d5653570220591365f8795c68b788d3949e9d6a984dae2f79d49bf3b0b184edf9f676732524[ALL] 3045022100aac67f935a52fd961b404dd4d273600924f74498062e237fec3ef50416440e3d02204fcf8deaee86c5a95c8c8c235bb252a5d4d304b9bd3e01d9c7098fae3c9172cf[ALL] 522102ef6587d4850890bd42e7779365ed2baeec17824b84a285894ec090edf0b604e4210322f35f79c76036a68746d8e3804b9097a6575ae6d7fc9dfb99c06280b656dfc12103f73d0f5d4f8919a74d4b0cc72fc0ef5ee9eaf759d3731319294491883d97881b53ae",
628
                                    "script_hex": "00483045022100dc0eb9ebb44e21a2334b1a9936ff2d3e6f85b6a8b0bae2462640ad574d5653570220591365f8795c68b788d3949e9d6a984dae2f79d49bf3b0b184edf9f67673252401483045022100aac67f935a52fd961b404dd4d273600924f74498062e237fec3ef50416440e3d02204fcf8deaee86c5a95c8c8c235bb252a5d4d304b9bd3e01d9c7098fae3c9172cf014c69522102ef6587d4850890bd42e7779365ed2baeec17824b84a285894ec090edf0b604e4210322f35f79c76036a68746d8e3804b9097a6575ae6d7fc9dfb99c06280b656dfc12103f73d0f5d4f8919a74d4b0cc72fc0ef5ee9eaf759d3731319294491883d97881b53ae",
629
                                    "witness": []
630
                                }
631
                            ],
632
                            "outputs": [
633
                                {
634
                                    "addresses": [
635
                                        "32bH64DNFHV3y74Qyx3FSAKykUauUB94as"
636
                                    ],
637
                                    "value": 132991,
638
                                    "type": "P2SH",
639
                                    "script_asm": "OP_HASH160 09e249cd56381853743834cf7c367093f4f84970 OP_EQUAL",
640
                                    "script_hex": "a91409e249cd56381853743834cf7c367093f4f8497087",
641
                                    "spent_by_tx": "2562317b87aeddd42fff89ecd0802f4c7cfb0cc404c37534b33a10fa4391878e",
642
                                    "spent_by_tx_position": 2
643
                                },
644
                                {
645
                                    "addresses": [
646
                                        "3EU8LRmo5PgcSwnkn6Msbqc8BKNoQ7Xief"
647
                                    ],
648
                                    "value": 100000,
649
                                    "type": "P2SH",
650
                                    "script_asm": "OP_HASH160 8c299479cf35311569d8c1450a85de22d206ab49 OP_EQUAL",
651
                                    "script_hex": "a9148c299479cf35311569d8c1450a85de22d206ab4987",
652
                                    "spent_by_tx": "bd430d52f35166a7dd6251c73a48559ad8b5f41b6c5bc4a6c4c1a3e3702f4287",
653
                                    "spent_by_tx_position": 0
654
                                }
655
                            ],
656
                            "balance_diff": 100000
657
                        },
658
                        {
659
                            "confirmations": 28319,
660
                            "block_height": 481932,
661
                            "block_time": 1503632682,
662
                            "created_at": 1503562862,
663
                            "fee": 159820,
664
                            "hash": "2613f3f7ccf6ca8beb68f72469905048120d7fc353bb02e81b1b4f570c5ce490",
665
                            "inputs_count": 1,
666
                            "inputs_value": 9223832,
667
                            "is_coinbase": false,
668
                            "is_double_spend": false,
669
                            "is_sw_tx": false,
670
                            "weight": 1468,
671
                            "vsize": 367,
672
                            "witness_hash": "2613f3f7ccf6ca8beb68f72469905048120d7fc353bb02e81b1b4f570c5ce490",
673
                            "lock_time": 0,
674
                            "outputs_count": 2,
675
                            "outputs_value": 9064012,
676
                            "size": 367,
677
                            "sigops": 12,
678
                            "version": 1,
679
                            "inputs": [
680
                                {
681
                                    "prev_addresses": [
682
                                        "36398e4nx2uXBw83wFusq2u6CwrAYMRUd6"
683
                                    ],
684
                                    "prev_position": 0,
685
                                    "prev_tx_hash": "317f62e894f112bd7042e3352f4e5fc546dfa1a55e7d86da13de3feb800f1991",
686
                                    "prev_type": "P2SH",
687
                                    "prev_value": 9223832,
688
                                    "sequence": 4294967295,
689
                                    "script_asm": "0 304402206578aea3eecb83bb9727257e3b1a798d2cc6bf177e2f0e55b81dccb99c52d58602201af4ba5938dc290a1d3b380d120b468258a01abb035b3f8d451564927d668fae[ALL] 304402204910eec198eb9476c7e1d352b3fe3e4a3307422a442f7972d6a0eb838346bf4102205f18db326d81ca8dc4cadbc817551bab3fad369b245e8d7e30d8aa00a642ea82[ALL] 522102a75a0a36bbd4bc4ef4b015d1ecf477558fd13024e5de28eb4ce1db8bd9b1c92c2102d34d25fffe77d7142e2b2b4937bf970a3fc2c01e8200bd05172c6ce7b85ed1282103b04a7f2da2f3b16fcf9b7472a053cf89251b64636f6b2c55bf9e34026a4c756553ae",
690
                                    "script_hex": "0047304402206578aea3eecb83bb9727257e3b1a798d2cc6bf177e2f0e55b81dccb99c52d58602201af4ba5938dc290a1d3b380d120b468258a01abb035b3f8d451564927d668fae0147304402204910eec198eb9476c7e1d352b3fe3e4a3307422a442f7972d6a0eb838346bf4102205f18db326d81ca8dc4cadbc817551bab3fad369b245e8d7e30d8aa00a642ea82014c69522102a75a0a36bbd4bc4ef4b015d1ecf477558fd13024e5de28eb4ce1db8bd9b1c92c2102d34d25fffe77d7142e2b2b4937bf970a3fc2c01e8200bd05172c6ce7b85ed1282103b04a7f2da2f3b16fcf9b7472a053cf89251b64636f6b2c55bf9e34026a4c756553ae",
691
                                    "witness": []
692
                                }
693
                            ],
694
                            "outputs": [
695
                                {
696
                                    "addresses": [
697
                                        "3EU8LRmo5PgcSwnkn6Msbqc8BKNoQ7Xief"
698
                                    ],
699
                                    "value": 90000,
700
                                    "type": "P2SH",
701
                                    "script_asm": "OP_HASH160 8c299479cf35311569d8c1450a85de22d206ab49 OP_EQUAL",
702
                                    "script_hex": "a9148c299479cf35311569d8c1450a85de22d206ab4987",
703
                                    "spent_by_tx": null,
704
                                    "spent_by_tx_position": -1
705
                                },
706
                                {
707
                                    "addresses": [
708
                                        "3Qfkqzpd6EjGSzDXQXNcYCJE78BH7poKoD"
709
                                    ],
710
                                    "value": 8974012,
711
                                    "type": "P2SH",
712
                                    "script_asm": "OP_HASH160 fc0de3e893c40ae834ee970d5178eba47c37b642 OP_EQUAL",
713
                                    "script_hex": "a914fc0de3e893c40ae834ee970d5178eba47c37b64287",
714
                                    "spent_by_tx": "7b7462ade8518268c2cf89d2b5750a8058f18e40d224ee55ab38ebeb1d696e26",
715
                                    "spent_by_tx_position": 0
716
                                }
717
                            ],
718
                            "balance_diff": 90000
719
                        }
720
                    ]
721
                },
722
                "err_no": 0,
723
                "err_msg": null
724
            };
725
726
            var output = converter.convertAddressTxs(input);
727
            assert.ok("total" in output);
728
            cb();
729
        });
730
    });
731
732
    describe ("convertAddressUnspentOutputs", function() {
733
        it("works", function(cb) {
734
            var input = {
735
                "data": {
736
                    "total_count": 1200,
737
                    "page": 1,
738
                    "pagesize": 50,
739
                    "list": [
740
                        {
741
                            "tx_hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
742
                            "tx_output_n": 0,
743
                            "tx_output_n2": 0,
744
                            "value": 5000000000,
745
                            "confirmations": 510251
746
                        },
747
                        {
748
                            "tx_hash": "3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e688",
749
                            "tx_output_n": 1,
750
                            "tx_output_n2": 0,
751
                            "value": 1000000,
752
                            "confirmations": 386528
753
                        },
754
                        {
755
                            "tx_hash": "8f6b63012753005236b1b76e4884e4dee7415e05ab96604d353001662cde6b53",
756
                            "tx_output_n": 1,
757
                            "tx_output_n2": 0,
758
                            "value": 222222,
759
                            "confirmations": 382308
760
                        },
761
                        {
762
                            "tx_hash": "60ff2dfdf67917040139903a0141f7525a7d152365b371b35fd1cf83f1d7f704",
763
                            "tx_output_n": 1,
764
                            "tx_output_n2": 0,
765
                            "value": 333333,
766
                            "confirmations": 382308
767
                        },
768
                        {
769
                            "tx_hash": "1b703ca32b2da08cf896081a96c48f6433641e2c7d805fd170a31b0cbfb900df",
770
                            "tx_output_n": 1,
771
                            "tx_output_n2": 0,
772
                            "value": 500000,
773
                            "confirmations": 381697
774
                        },
775
                        {
776
                            "tx_hash": "495167f5b9d55519717cb171208a98ffd347410169f258b598b1b65447d8e078",
777
                            "tx_output_n": 1,
778
                            "tx_output_n2": 0,
779
                            "value": 500000,
780
                            "confirmations": 381697
781
                        },
782
                        {
783
                            "tx_hash": "2c484eaaf9bdaa42caee501523c631b1ceaef89838ea93bd5d199ac275469d22",
784
                            "tx_output_n": 1,
785
                            "tx_output_n2": 0,
786
                            "value": 1000000,
787
                            "confirmations": 378843
788
                        },
789
                        {
790
                            "tx_hash": "02a206264aecaf9f45f725533496b1b80bdd5a239194d8dd8100eed5415b6a14",
791
                            "tx_output_n": 1,
792
                            "tx_output_n2": 0,
793
                            "value": 6660000,
794
                            "confirmations": 375256
795
                        },
796
                        {
797
                            "tx_hash": "fd3c66b9c981a3ccc40ae0f631f45286e7b31cf6d9afa1acaf8be1261f133690",
798
                            "tx_output_n": 0,
799
                            "tx_output_n2": 0,
800
                            "value": 100000,
801
                            "confirmations": 375016
802
                        }
803
                    ]
804
                },
805
                "err_no": 0,
806
                "err_msg": null
807
            };
808
809
            var output = converter.convertAddressUnspentOutputs(input, "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa");
810
811
            // assert fields match
812
            [
813
                ["hash", "tx_hash"],
814
                ["confirmations", "confirmations"],
815
                ["value", "value"],
816
                ["index", "tx_output_n"]
817
818
            ].forEach(function(keyAssoc) {
819
                var blocktrailKey = keyAssoc[0];
820
                var btccomKey = keyAssoc[1];
821
822
                var blockTx = output.data[0];
823
                assert.ok(blocktrailKey in blockTx);
824
                assert.ok(input.data.list[0][btccomKey] == blockTx[blocktrailKey]);
825
            });
826
827
            assert.ok("total" in output);
828
            cb();
829
        });
830
    });
831
});
832
833
834
835
836