@psf/bch-js 6.8.3 → 7.0.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.
Files changed (111) hide show
  1. package/README.md +10 -28
  2. package/fix-docs-contrast.sh +38 -0
  3. package/package.json +37 -46
  4. package/src/address.js +12 -23
  5. package/src/bch-js.js +81 -48
  6. package/src/bitcoincash.js +10 -11
  7. package/src/blockchain.js +57 -68
  8. package/src/control.js +11 -22
  9. package/src/crypto.js +3 -3
  10. package/src/dsproof.js +6 -17
  11. package/src/ecash.js +1 -1
  12. package/src/ecpair.js +3 -3
  13. package/src/electrumx.js +32 -44
  14. package/src/encryption.js +8 -17
  15. package/src/generating.js +8 -19
  16. package/src/hdnode.js +5 -5
  17. package/src/mining.js +11 -22
  18. package/src/mnemonic.js +8 -11
  19. package/src/price.js +7 -108
  20. package/src/psf-slp-indexer.js +21 -32
  21. package/src/raw-transactions.js +22 -35
  22. package/src/schnorr.js +5 -16
  23. package/src/script.js +4 -2
  24. package/src/slp/address.js +4 -4
  25. package/src/slp/ecpair.js +4 -4
  26. package/src/slp/nft1.js +6 -6
  27. package/src/slp/slp.js +11 -22
  28. package/src/slp/tokentype1.js +11 -22
  29. package/src/slp/utils.js +9 -19
  30. package/src/transaction-builder.js +5 -5
  31. package/src/transaction.js +5 -5
  32. package/src/util.js +5 -98
  33. package/src/utxo.js +6 -6
  34. package/test/e2e/bch-js-e2e-tests.js +4 -3
  35. package/test/e2e/ipfs/ipfs-e2e.js +8 -2
  36. package/test/e2e/rate-limits/anonymous-rate-limits.js +5 -4
  37. package/test/e2e/rate-limits/basic-auth-rate-limits.js +4 -3
  38. package/test/e2e/rate-limits/free-rate-limits.js +4 -3
  39. package/test/e2e/rate-limits/full-node-rate-limits.js +4 -3
  40. package/test/e2e/rate-limits/indexer-rate-limits.js +4 -3
  41. package/test/e2e/send-raw-transaction-bulk/sendrawtransaction.js +28 -18
  42. package/test/e2e/send-raw-transaction-single/sendrawtransaction.js +28 -18
  43. package/test/e2e/send-token/send-token.js +4 -4
  44. package/test/e2e/util/e2e-util.js +6 -3
  45. package/test/e2e/utxo/unsynced-indexer.js +1 -1
  46. package/test/integration/blockchain.js +5 -5
  47. package/test/integration/control.js +2 -2
  48. package/test/integration/{chains/bchn/dsproof.js → dsproof.js} +6 -2
  49. package/test/integration/electrumx.js +5 -12
  50. package/test/integration/price.js +3 -39
  51. package/test/integration/{chains/bchn/psf-slp-indexer.integration.js → psf-slp-indexer.integration.js} +5 -6
  52. package/test/integration/rawtransaction.js +39 -5
  53. package/test/integration/slp.js +147 -5
  54. package/test/integration/transaction-integration.js +27 -2
  55. package/test/integration/{chains/bchn/utxo-integration.js → utxo-integration.js} +3 -2
  56. package/test/unit/address.js +7 -4
  57. package/test/unit/bitcoin-cash.js +5 -3
  58. package/test/unit/blockchain.js +23 -22
  59. package/test/unit/control.js +6 -6
  60. package/test/unit/crypto.js +5 -3
  61. package/test/unit/dsproof.js +5 -4
  62. package/test/unit/ecash.js +3 -2
  63. package/test/unit/ecpairs.js +6 -4
  64. package/test/unit/electrumx.js +7 -7
  65. package/test/unit/encryption.js +6 -5
  66. package/test/unit/fixtures/bitcore-mock.js +1 -1
  67. package/test/unit/fixtures/block-mock.js +1 -1
  68. package/test/unit/fixtures/blockchain-mock.js +1 -1
  69. package/test/unit/fixtures/dsproof-mock.js +1 -1
  70. package/test/unit/fixtures/electrumx-mock.js +1 -1
  71. package/test/unit/fixtures/encryption-mock.js +1 -1
  72. package/test/unit/fixtures/ipfs-mock.js +1 -1
  73. package/test/unit/fixtures/openbazaar-mock.js +1 -1
  74. package/test/unit/fixtures/price-mocks.js +1 -1
  75. package/test/unit/fixtures/psf-slp-indexer-mock.js +1 -1
  76. package/test/unit/fixtures/rawtransaction-mock.js +1 -1
  77. package/test/unit/fixtures/slp/mock-utils.js +1 -1
  78. package/test/unit/fixtures/transaction-mock.js +1 -1
  79. package/test/unit/fixtures/utxo-mocks.js +1 -1
  80. package/test/unit/generating.js +5 -5
  81. package/test/unit/hdnode.js +6 -4
  82. package/test/unit/mining.js +8 -8
  83. package/test/unit/mnemonic.js +4 -2
  84. package/test/unit/price.js +5 -14
  85. package/test/unit/psf-slp-indexer.js +7 -7
  86. package/test/unit/raw-tranactions.js +14 -13
  87. package/test/unit/scripts.js +8 -6
  88. package/test/unit/slp-address.js +6 -4
  89. package/test/unit/slp-ecpair.js +5 -3
  90. package/test/unit/slp-nft1.js +7 -6
  91. package/test/unit/slp-tokentype1.js +7 -6
  92. package/test/unit/slp-utils.js +8 -7
  93. package/test/unit/transaction-builder.js +7 -4
  94. package/test/unit/transaction-unit.js +5 -4
  95. package/test/unit/util.js +11 -32
  96. package/test/unit/utxo-unit.js +6 -5
  97. package/test/unit/x402.js +193 -0
  98. package/test/integration/chains/abc/psf-slp-indexer-integration.js +0 -38
  99. package/test/integration/chains/abc/rawtransaction.js +0 -73
  100. package/test/integration/chains/abc/utxo-integration.js +0 -38
  101. package/test/integration/chains/bchn/rawtransaction.js +0 -71
  102. package/test/integration/chains/bchn/slp.js +0 -301
  103. package/test/integration/chains/bchn/transaction-integration.js +0 -37
  104. package/test/integration/chains/bchn/util.js +0 -103
  105. package/test/integration/chains/testnet/blockchain.js +0 -260
  106. package/test/integration/chains/testnet/control.js +0 -32
  107. package/test/integration/chains/testnet/electrumx.js +0 -376
  108. package/test/integration/chains/testnet/rawtransaction.js +0 -296
  109. package/test/integration/chains/testnet/slp.js +0 -172
  110. package/test/integration/chains/testnet/test-free-tier.sh +0 -9
  111. package/test/integration/chains/testnet/util.js +0 -109
package/src/blockchain.js CHANGED
@@ -3,33 +3,22 @@
3
3
  - Add blockhash functionality back into getTxOutProof
4
4
  */
5
5
 
6
- const axios = require('axios')
6
+ import axios from 'axios'
7
7
 
8
8
  // let _this
9
9
 
10
10
  class Blockchain {
11
11
  constructor (config) {
12
12
  this.restURL = config.restURL
13
- this.apiToken = config.apiToken
14
13
  this.authToken = config.authToken
14
+ // Use the shared axios instance if provided, otherwise fall back to axios
15
+ this.axios = config.axios || axios
15
16
 
16
- if (this.authToken) {
17
- // Add Basic Authentication token to the authorization header.
18
- this.axiosOptions = {
19
- headers: {
20
- authorization: this.authToken
21
- }
22
- }
23
- } else {
24
- // Add JWT token to the authorization header.
25
- this.axiosOptions = {
26
- headers: {
27
- authorization: `Token ${this.apiToken}`
28
- }
17
+ this.axiosOptions = {
18
+ headers: {
19
+ authorization: this.authToken
29
20
  }
30
21
  }
31
-
32
- // _this = this
33
22
  }
34
23
 
35
24
  /**
@@ -52,8 +41,8 @@ class Blockchain {
52
41
  */
53
42
  async getBestBlockHash () {
54
43
  try {
55
- const response = await axios.get(
56
- `${this.restURL}blockchain/getBestBlockHash`,
44
+ const response = await this.axios.get(
45
+ `${this.restURL}full-node/blockchain/getBestBlockHash`,
57
46
  this.axiosOptions
58
47
  )
59
48
  return response.data
@@ -106,8 +95,8 @@ class Blockchain {
106
95
  if (!blockhash || typeof blockhash !== 'string') {
107
96
  throw new Error('blockhash must be a string')
108
97
  }
109
- const response = await axios.post(
110
- `${this.restURL}blockchain/getblock`,
98
+ const response = await this.axios.post(
99
+ `${this.restURL}full-node/blockchain/getblock`,
111
100
  {
112
101
  blockhash,
113
102
  verbosity
@@ -160,8 +149,8 @@ class Blockchain {
160
149
  */
161
150
  async getBlockchainInfo () {
162
151
  try {
163
- const response = await axios.get(
164
- `${this.restURL}blockchain/getBlockchainInfo`,
152
+ const response = await this.axios.get(
153
+ `${this.restURL}full-node/blockchain/getBlockchainInfo`,
165
154
  this.axiosOptions
166
155
  )
167
156
  return response.data
@@ -191,8 +180,8 @@ class Blockchain {
191
180
  */
192
181
  async getBlockCount () {
193
182
  try {
194
- const response = await axios.get(
195
- `${this.restURL}blockchain/getBlockCount`,
183
+ const response = await this.axios.get(
184
+ `${this.restURL}full-node/blockchain/getBlockCount`,
196
185
  this.axiosOptions
197
186
  )
198
187
  return response.data
@@ -227,8 +216,8 @@ class Blockchain {
227
216
  if (typeof height !== 'string') height = JSON.stringify(height)
228
217
 
229
218
  try {
230
- const response = await axios.get(
231
- `${this.restURL}blockchain/getBlockHash/${height}`,
219
+ const response = await this.axios.get(
220
+ `${this.restURL}full-node/blockchain/getBlockHash/${height}`,
232
221
  this.axiosOptions
233
222
  )
234
223
  return response.data
@@ -274,8 +263,8 @@ class Blockchain {
274
263
  try {
275
264
  // Handle single hash.
276
265
  if (typeof hash === 'string') {
277
- const response = await axios.get(
278
- `${this.restURL}blockchain/getBlockHeader/${hash}?verbose=${verbose}`,
266
+ const response = await this.axios.get(
267
+ `${this.restURL}full-node/blockchain/getBlockHeader/${hash}?verbose=${verbose}`,
279
268
  this.axiosOptions
280
269
  )
281
270
 
@@ -284,11 +273,11 @@ class Blockchain {
284
273
  // Handle array of hashes.
285
274
  } else if (Array.isArray(hash)) {
286
275
  // Dev note: must use axios.post for unit test stubbing.
287
- const response = await axios.post(
288
- `${this.restURL}blockchain/getBlockHeader`,
276
+ const response = await this.axios.post(
277
+ `${this.restURL}full-node/blockchain/getBlockHeader`,
289
278
  {
290
279
  hashes: hash,
291
- verbose: verbose
280
+ verbose
292
281
  },
293
282
  this.axiosOptions
294
283
  )
@@ -335,8 +324,8 @@ class Blockchain {
335
324
  */
336
325
  async getChainTips () {
337
326
  try {
338
- const response = await axios.get(
339
- `${this.restURL}blockchain/getChainTips`,
327
+ const response = await this.axios.get(
328
+ `${this.restURL}full-node/blockchain/getChainTips`,
340
329
  this.axiosOptions
341
330
  )
342
331
  return response.data
@@ -367,8 +356,8 @@ class Blockchain {
367
356
  */
368
357
  async getDifficulty () {
369
358
  try {
370
- const response = await axios.get(
371
- `${this.restURL}blockchain/getDifficulty`,
359
+ const response = await this.axios.get(
360
+ `${this.restURL}full-node/blockchain/getDifficulty`,
372
361
  this.axiosOptions
373
362
  )
374
363
  return response.data
@@ -383,8 +372,8 @@ class Blockchain {
383
372
  if (typeof txid !== 'string') txid = JSON.stringify(txid)
384
373
 
385
374
  try {
386
- const response = await axios.get(
387
- `${this.restURL}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`,
375
+ const response = await this.axios.get(
376
+ `${this.restURL}full-node/blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`,
388
377
  this.axiosOptions
389
378
  )
390
379
  return response.data
@@ -398,8 +387,8 @@ class Blockchain {
398
387
  if (typeof txid !== 'string') txid = JSON.stringify(txid)
399
388
 
400
389
  try {
401
- const response = await axios.get(
402
- `${this.restURL}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`,
390
+ const response = await this.axios.get(
391
+ `${this.restURL}full-node/blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`,
403
392
  this.axiosOptions
404
393
  )
405
394
  return response.data
@@ -495,16 +484,16 @@ class Blockchain {
495
484
 
496
485
  try {
497
486
  if (typeof txid === 'string') {
498
- const response = await axios.get(
499
- `${this.restURL}blockchain/getMempoolEntry/${txid}`,
487
+ const response = await this.axios.get(
488
+ `${this.restURL}full-node/blockchain/getMempoolEntry/${txid}`,
500
489
  this.axiosOptions
501
490
  )
502
491
 
503
492
  return response.data
504
493
  } else if (Array.isArray(txid)) {
505
494
  // Dev note: must use axios.post for unit test stubbing.
506
- const response = await axios.post(
507
- `${this.restURL}blockchain/getMempoolEntry`,
495
+ const response = await this.axios.post(
496
+ `${this.restURL}full-node/blockchain/getMempoolEntry`,
508
497
  {
509
498
  txids: txid
510
499
  },
@@ -546,8 +535,8 @@ class Blockchain {
546
535
  */
547
536
  async getMempoolInfo () {
548
537
  try {
549
- const response = await axios.get(
550
- `${this.restURL}blockchain/getMempoolInfo`,
538
+ const response = await this.axios.get(
539
+ `${this.restURL}full-node/blockchain/getMempoolInfo`,
551
540
  this.axiosOptions
552
541
  )
553
542
  return response.data
@@ -593,8 +582,8 @@ class Blockchain {
593
582
  */
594
583
  async getRawMempool (verbose = false) {
595
584
  try {
596
- const response = await axios.get(
597
- `${this.restURL}blockchain/getRawMempool?vebose=${verbose}`,
585
+ const response = await this.axios.get(
586
+ `${this.restURL}full-node/blockchain/getRawMempool?vebose=${verbose}`,
598
587
  this.axiosOptions
599
588
  )
600
589
  return response.data
@@ -637,14 +626,14 @@ class Blockchain {
637
626
  }
638
627
 
639
628
  // Send the request to the REST API.
640
- // const response = await axios.get(
629
+ // const response = await this.axios.get(
641
630
  // `${this.restURL}blockchain/getTxOut/${txid}/${n}?includeMempool=${includeMempool}`,
642
631
  // this.axiosOptions
643
632
  // )
644
- const response = await axios.post(
645
- `${this.restURL}blockchain/getTxOut`,
633
+ const response = await this.axios.post(
634
+ `${this.restURL}full-node/blockchain/getTxOut`,
646
635
  {
647
- txid: txid,
636
+ txid,
648
637
  vout: n,
649
638
  mempool: includeMempool
650
639
  },
@@ -698,19 +687,19 @@ class Blockchain {
698
687
  try {
699
688
  // Single txid.
700
689
  if (typeof txids === 'string') {
701
- const path = `${this.restURL}blockchain/getTxOutProof/${txids}`
690
+ const path = `${this.restURL}full-node/blockchain/getTxOutProof/${txids}`
702
691
  // if (blockhash) path = `${path}?blockhash=${blockhash}`
703
692
 
704
- const response = await axios.get(path, this.axiosOptions)
693
+ const response = await this.axios.get(path, this.axiosOptions)
705
694
  return response.data
706
695
 
707
696
  // Array of txids.
708
697
  } else if (Array.isArray(txids)) {
709
698
  // Dev note: must use axios.post for unit test stubbing.
710
- const response = await axios.post(
711
- `${this.restURL}blockchain/getTxOutProof`,
699
+ const response = await this.axios.post(
700
+ `${this.restURL}full-node/blockchain/getTxOutProof`,
712
701
  {
713
- txids: txids
702
+ txids
714
703
  },
715
704
  this.axiosOptions
716
705
  )
@@ -727,8 +716,8 @@ class Blockchain {
727
716
 
728
717
  async preciousBlock (blockhash) {
729
718
  try {
730
- const response = await axios.get(
731
- `${this.restURL}blockchain/preciousBlock/${blockhash}`,
719
+ const response = await this.axios.get(
720
+ `${this.restURL}full-node/blockchain/preciousBlock/${blockhash}`,
732
721
  this.axiosOptions
733
722
  )
734
723
  return response.data
@@ -740,8 +729,8 @@ class Blockchain {
740
729
 
741
730
  async pruneBlockchain (height) {
742
731
  try {
743
- const response = await axios.post(
744
- `${this.restURL}blockchain/pruneBlockchain/${height}`,
732
+ const response = await this.axios.post(
733
+ `${this.restURL}full-node/blockchain/pruneBlockchain/${height}`,
745
734
  this.axiosOptions
746
735
  )
747
736
  return response.data
@@ -753,8 +742,8 @@ class Blockchain {
753
742
 
754
743
  async verifyChain (checklevel = 3, nblocks = 6) {
755
744
  try {
756
- const response = await axios.get(
757
- `${this.restURL}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`,
745
+ const response = await this.axios.get(
746
+ `${this.restURL}full-node/blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`,
758
747
  this.axiosOptions
759
748
  )
760
749
  return response.data
@@ -805,8 +794,8 @@ class Blockchain {
805
794
  try {
806
795
  // Single block
807
796
  if (typeof proof === 'string') {
808
- const response = await axios.get(
809
- `${this.restURL}blockchain/verifyTxOutProof/${proof}`,
797
+ const response = await this.axios.get(
798
+ `${this.restURL}full-node/blockchain/verifyTxOutProof/${proof}`,
810
799
  this.axiosOptions
811
800
  )
812
801
  return response.data
@@ -814,8 +803,8 @@ class Blockchain {
814
803
  // Array of hashes.
815
804
  } else if (Array.isArray(proof)) {
816
805
  // Dev note: must use axios.post for unit test stubbing.
817
- const response = await axios.post(
818
- `${this.restURL}blockchain/verifyTxOutProof`,
806
+ const response = await this.axios.post(
807
+ `${this.restURL}full-node/blockchain/verifyTxOutProof`,
819
808
  {
820
809
  proofs: proof
821
810
  },
@@ -833,4 +822,4 @@ class Blockchain {
833
822
  }
834
823
  }
835
824
 
836
- module.exports = Blockchain
825
+ export default Blockchain
package/src/control.js CHANGED
@@ -2,33 +2,22 @@
2
2
  API endpoints for basic control and information of the full node.
3
3
  */
4
4
 
5
- const axios = require('axios')
5
+ import axios from 'axios'
6
6
 
7
7
  // let _this // Global reference to the instance of this class.
8
8
 
9
9
  class Control {
10
10
  constructor (config) {
11
11
  this.restURL = config.restURL
12
- this.apiToken = config.apiToken
13
12
  this.authToken = config.authToken
13
+ // Use the shared axios instance if provided, otherwise fall back to axios
14
+ this.axios = config.axios || axios
14
15
 
15
- if (this.authToken) {
16
- // Add Basic Authentication token to the authorization header.
17
- this.axiosOptions = {
18
- headers: {
19
- authorization: this.authToken
20
- }
21
- }
22
- } else {
23
- // Add JWT token to the authorization header.
24
- this.axiosOptions = {
25
- headers: {
26
- authorization: `Token ${this.apiToken}`
27
- }
16
+ this.axiosOptions = {
17
+ headers: {
18
+ authorization: this.authToken
28
19
  }
29
20
  }
30
-
31
- // _this = this
32
21
  }
33
22
 
34
23
  /**
@@ -79,8 +68,8 @@ class Control {
79
68
  */
80
69
  async getNetworkInfo () {
81
70
  try {
82
- const response = await axios.get(
83
- `${this.restURL}control/getNetworkInfo`,
71
+ const response = await this.axios.get(
72
+ `${this.restURL}full-node/control/getNetworkInfo`,
84
73
  this.axiosOptions
85
74
  )
86
75
  return response.data
@@ -92,8 +81,8 @@ class Control {
92
81
 
93
82
  async getMemoryInfo () {
94
83
  try {
95
- const response = await axios.get(
96
- `${this.restURL}control/getMemoryInfo`,
84
+ const response = await this.axios.get(
85
+ `${this.restURL}full-node/control/getMemoryInfo`,
97
86
  this.axiosOptions
98
87
  )
99
88
  return response.data
@@ -115,4 +104,4 @@ class Control {
115
104
  // }
116
105
  }
117
106
 
118
- module.exports = Control
107
+ export default Control
package/src/crypto.js CHANGED
@@ -1,5 +1,5 @@
1
- const randomBytes = require('randombytes')
2
- const Bitcoin = require('@psf/bitcoincashjs-lib')
1
+ import randomBytes from 'randombytes'
2
+ import Bitcoin from '@psf/bitcoincashjs-lib'
3
3
 
4
4
  class Crypto {
5
5
  /**
@@ -135,4 +135,4 @@ class Crypto {
135
135
  }
136
136
  }
137
137
 
138
- module.exports = Crypto
138
+ export default Crypto
package/src/dsproof.js CHANGED
@@ -1,27 +1,16 @@
1
- const axios = require('axios')
1
+ import axios from 'axios'
2
2
 
3
3
  let _this
4
4
 
5
5
  class DSProof {
6
6
  constructor (config) {
7
7
  this.restURL = config.restURL
8
- this.apiToken = config.apiToken
9
8
  this.authToken = config.authToken
10
9
  this.axios = axios
11
10
 
12
- if (this.authToken) {
13
- // Add Basic Authentication token to the authorization header.
14
- this.axiosOptions = {
15
- headers: {
16
- authorization: this.authToken
17
- }
18
- }
19
- } else {
20
- // Add JWT token to the authorization header.
21
- this.axiosOptions = {
22
- headers: {
23
- authorization: `Token ${this.apiToken}`
24
- }
11
+ this.axiosOptions = {
12
+ headers: {
13
+ authorization: this.authToken
25
14
  }
26
15
  }
27
16
 
@@ -67,7 +56,7 @@ class DSProof {
67
56
  throw new Error(`txid must be of length 64 (not ${txid.length})`)
68
57
  }
69
58
  const response = await _this.axios.get(
70
- `${this.restURL}dsproof/getdsproof/${txid}`,
59
+ `${this.restURL}full-node/dsproof/getdsproof/${txid}`,
71
60
  this.axiosOptions
72
61
  )
73
62
  return response.data
@@ -78,4 +67,4 @@ class DSProof {
78
67
  }
79
68
  }
80
69
 
81
- module.exports = DSProof
70
+ export default DSProof
package/src/ecash.js CHANGED
@@ -44,4 +44,4 @@ class eCash {
44
44
  }
45
45
  }
46
46
 
47
- module.exports = eCash
47
+ export default eCash
package/src/ecpair.js CHANGED
@@ -1,5 +1,5 @@
1
- const Bitcoin = require('@psf/bitcoincashjs-lib')
2
- const coininfo = require('@psf/coininfo')
1
+ import Bitcoin from '@psf/bitcoincashjs-lib'
2
+ import coininfo from '@psf/coininfo'
3
3
 
4
4
  class ECPair {
5
5
  static setAddress (address) {
@@ -166,4 +166,4 @@ class ECPair {
166
166
  }
167
167
  }
168
168
 
169
- module.exports = ECPair
169
+ export default ECPair
package/src/electrumx.js CHANGED
@@ -3,10 +3,10 @@
3
3
  by FullStack.cash
4
4
  */
5
5
  // Public npm libraries
6
- const axios = require('axios')
6
+ import axios from 'axios'
7
7
 
8
8
  // Local libraries.
9
- const Blockchain = require('./blockchain')
9
+ import Blockchain from './blockchain.js'
10
10
  // const Address = require('./address')
11
11
 
12
12
  // let _this
@@ -14,30 +14,18 @@ const Blockchain = require('./blockchain')
14
14
  class ElectrumX {
15
15
  constructor (config) {
16
16
  this.restURL = config.restURL
17
- this.apiToken = config.apiToken
18
17
  this.authToken = config.authToken
18
+ // Use the shared axios instance if provided, otherwise fall back to axios
19
+ this.axios = config.axios || axios
19
20
 
20
- if (this.authToken) {
21
- // Add Basic Authentication token to the authorization header.
22
- this.axiosOptions = {
23
- headers: {
24
- authorization: this.authToken
25
- }
26
- }
27
- } else {
28
- // Add JWT token to the authorization header.
29
- this.axiosOptions = {
30
- headers: {
31
- authorization: `Token ${this.apiToken}`
32
- }
21
+ this.axiosOptions = {
22
+ headers: {
23
+ authorization: this.authToken
33
24
  }
34
25
  }
35
26
 
36
27
  // Encapsulate dependencies
37
28
  this.blockchain = new Blockchain(config)
38
- // this.address = new Address(config)
39
-
40
- // _this = this
41
29
  }
42
30
 
43
31
  /**
@@ -111,16 +99,16 @@ class ElectrumX {
111
99
 
112
100
  // Handle single address.
113
101
  if (typeof address === 'string') {
114
- const response = await axios.get(
115
- `${this.restURL}electrumx/utxos/${address}`,
102
+ const response = await this.axios.get(
103
+ `${this.restURL}fulcrum/utxos/${address}`,
116
104
  this.axiosOptions
117
105
  )
118
106
  return response.data
119
107
 
120
108
  // Handle array of addresses.
121
109
  } else if (Array.isArray(address)) {
122
- const response = await axios.post(
123
- `${this.restURL}electrumx/utxos`,
110
+ const response = await this.axios.post(
111
+ `${this.restURL}fulcrum/utxos`,
124
112
  {
125
113
  addresses: address
126
114
  },
@@ -195,16 +183,16 @@ class ElectrumX {
195
183
  try {
196
184
  // Handle single address.
197
185
  if (typeof address === 'string') {
198
- const response = await axios.get(
199
- `${this.restURL}electrumx/balance/${address}`,
186
+ const response = await this.axios.get(
187
+ `${this.restURL}fulcrum/balance/${address}`,
200
188
  this.axiosOptions
201
189
  )
202
190
  return response.data
203
191
 
204
192
  // Handle array of addresses.
205
193
  } else if (Array.isArray(address)) {
206
- const response = await axios.post(
207
- `${this.restURL}electrumx/balance`,
194
+ const response = await this.axios.post(
195
+ `${this.restURL}fulcrum/balance`,
208
196
  {
209
197
  addresses: address
210
198
  },
@@ -293,16 +281,16 @@ class ElectrumX {
293
281
  try {
294
282
  // Handle single address.
295
283
  if (typeof address === 'string') {
296
- const response = await axios.get(
297
- `${this.restURL}electrumx/transactions/${address}/${allTxs}`,
284
+ const response = await this.axios.get(
285
+ `${this.restURL}fulcrum/transactions/${address}/${allTxs}`,
298
286
  this.axiosOptions
299
287
  )
300
288
  return response.data
301
289
 
302
290
  // Handle array of addresses.
303
291
  } else if (Array.isArray(address)) {
304
- const response = await axios.post(
305
- `${this.restURL}electrumx/transactions`,
292
+ const response = await this.axios.post(
293
+ `${this.restURL}fulcrum/transactions`,
306
294
  {
307
295
  addresses: address,
308
296
  usrObj, // pass user data when making an internal call.
@@ -388,16 +376,16 @@ class ElectrumX {
388
376
  try {
389
377
  // Handle single address.
390
378
  if (typeof address === 'string') {
391
- const response = await axios.get(
392
- `${this.restURL}electrumx/unconfirmed/${address}`,
379
+ const response = await this.axios.get(
380
+ `${this.restURL}fulcrum/unconfirmed/${address}`,
393
381
  this.axiosOptions
394
382
  )
395
383
  return response.data
396
384
 
397
385
  // Handle array of addresses.
398
386
  } else if (Array.isArray(address)) {
399
- const response = await axios.post(
400
- `${this.restURL}electrumx/unconfirmed`,
387
+ const response = await this.axios.post(
388
+ `${this.restURL}fulcrum/unconfirmed`,
401
389
  {
402
390
  addresses: address
403
391
  },
@@ -457,8 +445,8 @@ class ElectrumX {
457
445
  */
458
446
  async blockHeader (height, count = 1) {
459
447
  try {
460
- const response = await axios.get(
461
- `${this.restURL}electrumx/block/headers/${height}?count=${count}`,
448
+ const response = await this.axios.get(
449
+ `${this.restURL}fulcrum/block/headers/${height}?count=${count}`,
462
450
  this.axiosOptions
463
451
  )
464
452
  return response.data
@@ -548,14 +536,14 @@ class ElectrumX {
548
536
  try {
549
537
  // Handle single transaction.
550
538
  if (typeof txid === 'string') {
551
- const response = await axios.get(
552
- `${this.restURL}electrumx/tx/data/${txid}`,
539
+ const response = await this.axios.get(
540
+ `${this.restURL}fulcrum/tx/data/${txid}`,
553
541
  this.axiosOptions
554
542
  )
555
543
  return response.data
556
544
  } else if (Array.isArray(txid)) {
557
- const response = await axios.post(
558
- `${this.restURL}electrumx/tx/data`,
545
+ const response = await this.axios.post(
546
+ `${this.restURL}fulcrum/tx/data`,
559
547
  {
560
548
  txids: txid
561
549
  },
@@ -596,8 +584,8 @@ class ElectrumX {
596
584
  async broadcast (txHex) {
597
585
  try {
598
586
  if (typeof txHex === 'string') {
599
- const response = await axios.post(
600
- `${this.restURL}electrumx/tx/broadcast`,
587
+ const response = await this.axios.post(
588
+ `${this.restURL}fulcrum/tx/broadcast`,
601
589
  { txHex },
602
590
  this.axiosOptions
603
591
  )
@@ -727,4 +715,4 @@ class ElectrumX {
727
715
  }
728
716
  }
729
717
 
730
- module.exports = ElectrumX
718
+ export default ElectrumX
package/src/encryption.js CHANGED
@@ -2,33 +2,24 @@
2
2
  This library contains useful functions that deal with encryption.
3
3
  */
4
4
 
5
- const axios = require('axios')
5
+ import axios from 'axios'
6
6
 
7
7
  let _this
8
8
 
9
9
  class Encryption {
10
10
  constructor (config) {
11
11
  this.restURL = config.restURL
12
- this.apiToken = config.apiToken
13
- this.axios = axios
14
12
  this.authToken = config.authToken
15
13
 
16
- if (this.authToken) {
17
- // Add Basic Authentication token to the authorization header.
18
- this.axiosOptions = {
19
- headers: {
20
- authorization: this.authToken
21
- }
22
- }
23
- } else {
24
- // Add JWT token to the authorization header.
25
- this.axiosOptions = {
26
- headers: {
27
- authorization: `Token ${this.apiToken}`
28
- }
14
+ this.axiosOptions = {
15
+ headers: {
16
+ authorization: this.authToken
29
17
  }
30
18
  }
31
19
 
20
+ // Use the shared axios instance if provided, otherwise fall back to axios
21
+ this.axios = config.axios || axios
22
+
32
23
  _this = this
33
24
  }
34
25
 
@@ -73,4 +64,4 @@ class Encryption {
73
64
  }
74
65
  }
75
66
 
76
- module.exports = Encryption
67
+ export default Encryption