@psf/bch-js 4.22.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,10 +22,10 @@ describe('#TransactionLib', () => {
22
22
  })
23
23
  afterEach(() => sandbox.restore())
24
24
 
25
- describe('#get', () => {
25
+ describe('#getOld', () => {
26
26
  it('should throw an error if txid is not specified', async () => {
27
27
  try {
28
- await bchjs.Transaction.get()
28
+ await bchjs.Transaction.getOld()
29
29
 
30
30
  assert.fail('Unexpected code path!')
31
31
  } catch (err) {
@@ -45,7 +45,7 @@ describe('#TransactionLib', () => {
45
45
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
46
46
  .resolves(mockData.nonSlpTxDetails)
47
47
 
48
- const result = await bchjs.Transaction.get(txid)
48
+ const result = await bchjs.Transaction.getOld(txid)
49
49
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
50
50
 
51
51
  // Assert that there are stanardized properties.
@@ -84,7 +84,7 @@ describe('#TransactionLib', () => {
84
84
  const txid =
85
85
  '266844d53e46bbd7dd37134688dffea6e54d944edff27a0add63dd0908839bc1'
86
86
 
87
- const result = await bchjs.Transaction.get(txid)
87
+ const result = await bchjs.Transaction.getOld(txid)
88
88
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
89
89
 
90
90
  // Assert that there are stanardized properties.
@@ -114,7 +114,7 @@ describe('#TransactionLib', () => {
114
114
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
115
115
  .rejects(new Error('test error'))
116
116
 
117
- await bchjs.Transaction.get(txid)
117
+ await bchjs.Transaction.getOld(txid)
118
118
 
119
119
  assert.fail('Unexpected code path')
120
120
  } catch (err) {
@@ -147,7 +147,7 @@ describe('#TransactionLib', () => {
147
147
  const txid =
148
148
  '874306bda204d3a5dd15e03ea5732cccdca4c33a52df35162cdd64e30ea7f04e'
149
149
 
150
- const result = await bchjs.Transaction.get(txid)
150
+ const result = await bchjs.Transaction.getOld(txid)
151
151
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
152
152
 
153
153
  // Assert that there are stanardized properties.
@@ -195,7 +195,7 @@ describe('#TransactionLib', () => {
195
195
  const txid =
196
196
  '4640a734063ea79fa587a3cac38a70a2f6f3db0011e23514024185982110d0fa'
197
197
 
198
- const result = await bchjs.Transaction.get(txid)
198
+ const result = await bchjs.Transaction.getOld(txid)
199
199
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
200
200
 
201
201
  // Assert that there are stanardized properties.
@@ -246,7 +246,7 @@ describe('#TransactionLib', () => {
246
246
  const txid =
247
247
  '6bc111fbf5b118021d68355ca19a0e77fa358dd931f284b2550f79a51ab4792a'
248
248
 
249
- const result = await bchjs.Transaction.get(txid)
249
+ const result = await bchjs.Transaction.getOld(txid)
250
250
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
251
251
 
252
252
  // Assert that there are stanardized properties.
@@ -705,4 +705,15 @@ describe('#TransactionLib', () => {
705
705
  assert.equal(result.isSlpTx, true)
706
706
  })
707
707
  })
708
+
709
+ describe('#get', () => {
710
+ it('should proxy psf-slp-indexer', async () => {
711
+ // console.log('bchjs.Transaction: ', bchjs.Transaction)
712
+ sandbox.stub(bchjs.Transaction.psfSlpIndexer, 'tx').resolves('test data')
713
+
714
+ const result = await bchjs.Transaction.get()
715
+
716
+ assert.equal(result, 'test data')
717
+ })
718
+ })
708
719
  })
@@ -15,7 +15,7 @@ describe('#utxo', () => {
15
15
  beforeEach(() => (sandbox = sinon.createSandbox()))
16
16
  afterEach(() => sandbox.restore())
17
17
 
18
- describe('#get', () => {
18
+ describe('#getOld', () => {
19
19
  it('should get hydrated and filtered UTXOs for an address', async () => {
20
20
  // Mock dependencies.
21
21
  sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves(mockData.mockUtxoData)
@@ -25,7 +25,7 @@ describe('#utxo', () => {
25
25
 
26
26
  const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
27
27
 
28
- const result = await bchjs.Utxo.get(addr)
28
+ const result = await bchjs.Utxo.getOld(addr)
29
29
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
30
30
 
31
31
  assert.isArray(result)
@@ -46,7 +46,7 @@ describe('#utxo', () => {
46
46
 
47
47
  const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
48
48
 
49
- await bchjs.Utxo.get(addr)
49
+ await bchjs.Utxo.getOld(addr)
50
50
 
51
51
  assert.fail('Unexpected code path')
52
52
  } catch (err) {
@@ -66,7 +66,7 @@ describe('#utxo', () => {
66
66
  'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9'
67
67
  ]
68
68
 
69
- const result = await bchjs.Utxo.get(addr)
69
+ const result = await bchjs.Utxo.getOld(addr)
70
70
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
71
71
 
72
72
  assert.isArray(result)
@@ -88,7 +88,7 @@ describe('#utxo', () => {
88
88
  addrs.push(addr)
89
89
  }
90
90
 
91
- await bchjs.Utxo.get(addrs)
91
+ await bchjs.Utxo.getOld(addrs)
92
92
 
93
93
  assert.fail('Unexpected code path')
94
94
  } catch (err) {
@@ -105,7 +105,7 @@ describe('#utxo', () => {
105
105
 
106
106
  const addr = 'simpleledger:qrm0c67wwqh0w7wjxua2gdt2xggnm90xwsr5k22euj'
107
107
 
108
- const result = await bchjs.Utxo.get(addr)
108
+ const result = await bchjs.Utxo.getOld(addr)
109
109
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
110
110
 
111
111
  assert.isArray(result)
@@ -133,7 +133,7 @@ describe('#utxo', () => {
133
133
  const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
134
134
  const useWhitelist = true
135
135
 
136
- const result = await bchjs.Utxo.get(addr, useWhitelist)
136
+ const result = await bchjs.Utxo.getOld(addr, useWhitelist)
137
137
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
138
138
 
139
139
  assert.isArray(result)
@@ -188,4 +188,89 @@ describe('#utxo', () => {
188
188
  assert.equal(result.satoshis, 800)
189
189
  })
190
190
  })
191
+
192
+ describe('#hydrateTokenData', () => {
193
+ it('should hydrate token UTXOs', async () => {
194
+ // Mock dependencies
195
+ sandbox
196
+ .stub(bchjs.Utxo.psfSlpIndexer, 'tokenStats')
197
+ .onCall(0)
198
+ .resolves(mockData.genesisData01)
199
+ .onCall(1)
200
+ .resolves(mockData.genesisData02)
201
+ .onCall(2)
202
+ .resolves(mockData.genesisData03)
203
+
204
+ const result = await bchjs.Utxo.hydrateTokenData(mockData.tokenUtxos01)
205
+ // console.log('result: ', result)
206
+
207
+ assert.equal(result.length, 4)
208
+ assert.property(result[0], 'qtyStr')
209
+ assert.property(result[0], 'ticker')
210
+ assert.property(result[0], 'name')
211
+ assert.property(result[0], 'documentUri')
212
+ assert.property(result[0], 'documentHash')
213
+ })
214
+
215
+ it('should should catch and throw errors', async () => {
216
+ try {
217
+ // Force error
218
+ sandbox
219
+ .stub(bchjs.Utxo.psfSlpIndexer, 'tokenStats')
220
+ .rejects(new Error('test error'))
221
+
222
+ await bchjs.Utxo.hydrateTokenData(mockData.tokenUtxos01)
223
+
224
+ assert.fail('Unexpected code path')
225
+ } catch (err) {
226
+ assert.equal(err.message, 'test error')
227
+ }
228
+ })
229
+ })
230
+
231
+ describe('#get', () => {
232
+ it('should throw an error if input is not a string', async () => {
233
+ try {
234
+ const addr = 123
235
+
236
+ await bchjs.Utxo.get(addr)
237
+
238
+ assert.fail('Unexpected code path')
239
+ } catch (err) {
240
+ assert.include(err.message, 'address input must be a string')
241
+ }
242
+ })
243
+
244
+ it('should return UTXO information', async () => {
245
+ // mock dependencies
246
+ sandbox
247
+ .stub(bchjs.Utxo.electrumx, 'utxo')
248
+ .resolves(mockData.fulcrumUtxos01)
249
+ sandbox
250
+ .stub(bchjs.Utxo.psfSlpIndexer, 'balance')
251
+ .resolves(mockData.psfSlpIndexerUtxos01)
252
+ // Mock function to return the same input. Good enough for this test.
253
+ sandbox.stub(bchjs.Utxo, 'hydrateTokenData').resolves(x => x)
254
+
255
+ const addr = 'simpleledger:qrm0c67wwqh0w7wjxua2gdt2xggnm90xwsr5k22euj'
256
+
257
+ const result = await bchjs.Utxo.get(addr)
258
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
259
+
260
+ // Assert expected properties exist
261
+ assert.property(result, 'address')
262
+ assert.property(result, 'bchUtxos')
263
+ assert.property(result, 'slpUtxos')
264
+ assert.property(result.slpUtxos, 'type1')
265
+ assert.property(result.slpUtxos.type1, 'tokens')
266
+ assert.property(result.slpUtxos.type1, 'mintBatons')
267
+ assert.property(result.slpUtxos, 'nft')
268
+ assert.property(result, 'nullUtxos')
269
+
270
+ assert.equal(result.bchUtxos.length, 4)
271
+ assert.equal(result.slpUtxos.type1.tokens.length, 1)
272
+ assert.equal(result.slpUtxos.type1.mintBatons.length, 1)
273
+ assert.equal(result.nullUtxos.length, 0)
274
+ })
275
+ })
191
276
  })
package/src/ninsight.js DELETED
@@ -1,319 +0,0 @@
1
- /*
2
- This library interacts with the ninsight REST API endpoints operated by
3
- Bitcoin.com
4
- */
5
-
6
- const axios = require('axios')
7
-
8
- // let _this
9
-
10
- class Ninsight {
11
- constructor (config) {
12
- // this.restURL = config.restURL
13
- // this.apiToken = config.apiToken
14
-
15
- // this.ninsightURL = `https://bch-explorer.api.bitcoin.com/v1/`
16
- if (config) {
17
- this.ninsightURL = config.ninsightURL
18
- ? config.ninsightURL
19
- : 'https://rest.bitcoin.com/v2'
20
- }
21
-
22
- // Add JWT token to the authorization header.
23
- this.axiosOptions = {
24
- headers: {
25
- authorization: `Token ${this.apiToken}`,
26
- timeout: 15000
27
- }
28
- }
29
-
30
- // _this = this
31
- }
32
-
33
- /**
34
- * @api Ninsight.utxo() utxo()
35
- * @apiName Ninsight Utxo
36
- * @apiGroup Ninsight
37
- * @apiDescription Return list of uxto for address.
38
- *
39
- * @apiExample Example usage:
40
- * (async () => {
41
- * try {
42
- * let utxo = await bchjs.Ninsight.utxo('bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c');
43
- * console.log(utxo);
44
- * } catch(error) {
45
- * console.error(error)
46
- * }
47
- * })()
48
- *
49
- * // [
50
- * // {
51
- * // "txid": "d31dc2cf66fe4d3d3ae18e1065def58a64920746b1702b52f060e5edeea9883b",
52
- * // "vout": 1,
53
- * // "amount": 0.03608203,
54
- * // "satoshis": 3608203,
55
- * // "height": 585570,
56
- * // "confirmations": 10392
57
- * // },
58
- * // {
59
- * // "txid": "41e9a118765ecf7a1ba4487c0863e23dba343cc5880381a72f0365ac2546c5fa",
60
- * // "vout": 0,
61
- * // "amount": 0.03608203,
62
- * // "satoshis": 3608203,
63
- * // "height": 577125,
64
- * // "confirmations": 18837
65
- * // }
66
- * // ]
67
- *
68
- */
69
- async utxo (address) {
70
- try {
71
- // Handle single address.
72
- if (typeof address === 'string') {
73
- const response = await axios.post(
74
- `${this.ninsightURL}/address/utxo`,
75
- {
76
- addresses: [address]
77
- },
78
- this.axiosOptions
79
- )
80
- return response.data
81
-
82
- // Handle array of addresses.
83
- } else if (Array.isArray(address)) {
84
- const response = await axios.post(
85
- `${this.ninsightURL}/address/utxo`,
86
- {
87
- addresses: address
88
- },
89
- this.axiosOptions
90
- )
91
-
92
- return response.data
93
- }
94
-
95
- throw new Error('Input address must be a string or array of strings.')
96
- } catch (error) {
97
- if (error.response && error.response.data) throw error.response.data
98
- else throw error
99
- }
100
- }
101
-
102
- /**
103
- * @api Ninsight.unconfirmed() unconfirmed()
104
- * @apiName Ninsight Unconfirmed Utxo
105
- * @apiGroup Ninsight
106
- * @apiDescription Returns a list of unconfirmed UTXOs for an address.
107
- *
108
- * @apiExample Example usage:
109
- * (async () => {
110
- * try {
111
- * let utxo = await bchjs.Ninsight.unconfirmed('bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c');
112
- * console.log(utxo);
113
- * } catch(error) {
114
- * console.error(error)
115
- * }
116
- * })()
117
- *
118
- * // [
119
- * // {
120
- * // "txid": "d31dc2cf66fe4d3d3ae18e1065def58a64920746b1702b52f060e5edeea9883b",
121
- * // "vout": 1,
122
- * // "amount": 0.03608203,
123
- * // "satoshis": 3608203,
124
- * // "confirmations": 0
125
- * // "ts": 1559670801
126
- * // },
127
- * // {
128
- * // "txid": "41e9a118765ecf7a1ba4487c0863e23dba343cc5880381a72f0365ac2546c5fa",
129
- * // "vout": 0,
130
- * // "amount": 0.03608203,
131
- * // "satoshis": 3608203,
132
- * // "confirmations": 0
133
- * // "ts": 1559670902
134
- * // }
135
- * // ]
136
- *
137
- */
138
- async unconfirmed (address) {
139
- try {
140
- if (typeof address === 'string') {
141
- const response = await axios.post(
142
- `${this.ninsightURL}/address/unconfirmed`,
143
- {
144
- addresses: [address]
145
- },
146
- this.axiosOptions
147
- )
148
-
149
- return response.data
150
- } else if (Array.isArray(address)) {
151
- const response = await axios.post(
152
- `${this.ninsightURL}/address/unconfirmed`,
153
- {
154
- addresses: address
155
- },
156
- this.axiosOptions
157
- )
158
-
159
- return response.data
160
- }
161
-
162
- throw new Error('Input address must be a string or array of strings.')
163
- } catch (error) {
164
- if (error.response && error.response.data) throw error.response.data
165
- else throw error
166
- }
167
- }
168
-
169
- /**
170
- * @api Ninsight.transactions() transactions()
171
- * @apiName Ninsight TX History
172
- * @apiGroup Ninsight
173
- * @apiDescription Return transactions history for address.
174
- *
175
- * @apiExample Example usage:
176
- * (async () => {
177
- * try {
178
- * let txs = await bchjs.Ninsight.transactions('bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c');
179
- * console.log(txs);
180
- * } catch(error) {
181
- * console.error(error)
182
- * }
183
- * })()
184
- *
185
- * // [
186
- * // {
187
- * // "pagesTotal": 1,
188
- * // "txs": [
189
- * // {
190
- * // "txid": "ec7bc8349386e3e1939bbdc4f8092fdbdd6a380734e68486b558cd594c451d5b",
191
- * // "version": 2,
192
- * // "locktime": 0,
193
- * // "vin": [
194
- * // {
195
- * // "txid": "4f1fc57c33659628938db740449bf92fb75799e1d5750a4aeef80eb52d6df1e0",
196
- * // "vout": 0,
197
- * // "sequence": 4294967295,
198
- * // "n": 0,
199
- * // ...
200
- * // }
201
- * // ...
202
- * // ]
203
- * // ...
204
- * // }
205
- * // ],
206
- * // "legacyAddress": "1LpbYkEM5cryfhs58tH8c93p4SGzit7UrP",
207
- * // "cashAddress": "bitcoincash:qrvk436u4r0ew2wj0rd9pnxhx4w90p2yfc29ta0d2n",
208
- * // "currentPage": 0
209
- * // }
210
- * // ]
211
- * //
212
- *
213
- */
214
- async transactions (address) {
215
- try {
216
- if (typeof address === 'string') {
217
- const response = await axios.post(
218
- `${this.ninsightURL}/address/transactions`,
219
- {
220
- addresses: [address]
221
- },
222
- this.axiosOptions
223
- )
224
-
225
- return response.data
226
- } else if (Array.isArray(address)) {
227
- const response = await axios.post(
228
- `${this.ninsightURL}/address/transactions`,
229
- {
230
- addresses: address
231
- },
232
- this.axiosOptions
233
- )
234
-
235
- return response.data
236
- }
237
-
238
- throw new Error('Input address must be a string or array of strings.')
239
- } catch (error) {
240
- if (error.response && error.response.data) throw error.response.data
241
- else throw error
242
- }
243
- }
244
-
245
- /**
246
- * @api Ninsight.txDetails() txDetails()
247
- * @apiName Ninsight TX Details
248
- * @apiGroup Ninsight
249
- * @apiDescription Return transactions details for address(es).
250
- *
251
- * @apiExample Example usage:
252
- * (async () => {
253
- * try {
254
- * let result = await bchjs.Ninsight.txDetails('fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33');
255
- * console.log(result);
256
- * } catch(error) {
257
- * console.error(error)
258
- * }
259
- * })()
260
- *
261
- * // [
262
- * // {
263
- * // "txid": "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33",
264
- * // "version": 1,
265
- * // "locktime": 0,
266
- * // "vin": [
267
- * // {
268
- * // "sequence": 4294967295,
269
- * // "n": 0
270
- * // }
271
- * // ],
272
- * // "vout": [
273
- * // ...
274
- * // ],
275
- * // "blockhash": "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09",
276
- * // "blockheight": 1000,
277
- * // "confirmations": 659909,
278
- * // "time": 1232346882,
279
- * // "blocktime": 1232346882,
280
- * // "firstSeenTime": null,
281
- * // "valueOut": 50,
282
- * // "size": 135
283
- * // }
284
- * // ]
285
- *
286
- */
287
- async txDetails (txid) {
288
- try {
289
- if (typeof txid === 'string') {
290
- const response = await axios.post(
291
- `${this.ninsightURL}/transaction/details`,
292
- {
293
- txids: [txid]
294
- },
295
- this.axiosOptions
296
- )
297
-
298
- return response.data
299
- } else if (Array.isArray(txid)) {
300
- const response = await axios.post(
301
- `${this.ninsightURL}/transaction/details`,
302
- {
303
- txids: txid
304
- },
305
- this.axiosOptions
306
- )
307
-
308
- return response.data
309
- }
310
-
311
- throw new Error('Transaction ID must be a string or array of strings.')
312
- } catch (error) {
313
- if (error.response && error.response.data) throw error.response.data
314
- else throw error
315
- }
316
- }
317
- }
318
-
319
- module.exports = Ninsight