@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
@@ -3,22 +3,23 @@
3
3
  */
4
4
 
5
5
  // Public npm libraries
6
- const assert = require('assert')
7
- const assert2 = require('chai').assert
8
- const axios = require('axios')
9
- const sinon = require('sinon')
10
- // const nock = require("nock") // HTTP mocking
6
+ import assert from 'assert'
7
+ import chai from 'chai'
8
+ import axios from 'axios'
9
+ import sinon from 'sinon'
10
+ // import nock from "nock" // HTTP mocking
11
11
 
12
12
  // Unit under test (uut)
13
- const BCHJS = require('../../src/bch-js')
14
- // const bchjs = new BCHJS()
15
- let bchjs
13
+ import BCHJS from '../../src/bch-js.js'
16
14
 
17
15
  // Used for debugging
18
16
  // const util = require("util")
19
17
  // util.inspect.defaultOptions = { depth: 1 }
20
18
 
21
- const mockData = require('./fixtures/rawtransaction-mock')
19
+ import mockData from './fixtures/rawtransaction-mock.js'
20
+ const { assert: assert2 } = chai
21
+ // const bchjs = new BCHJS()
22
+ let bchjs
22
23
 
23
24
  describe('#RawTransactions', () => {
24
25
  let sandbox
@@ -44,7 +45,7 @@ describe('#RawTransactions', () => {
44
45
  vout: []
45
46
  }
46
47
 
47
- const resolved = new Promise(resolve => resolve({ data: data }))
48
+ const resolved = new Promise(resolve => resolve({ data }))
48
49
  sandbox.stub(axios, 'get').returns(resolved)
49
50
 
50
51
  bchjs.RawTransactions.decodeRawTransaction('02000000000000000000')
@@ -67,7 +68,7 @@ describe('#RawTransactions', () => {
67
68
  p2sh: 'bitcoincash:prswx5965nfumux9qng5kj8hw603vcne7q08t8c6jp'
68
69
  }
69
70
 
70
- const resolved = new Promise(resolve => resolve({ data: data }))
71
+ const resolved = new Promise(resolve => resolve({ data }))
71
72
  sandbox.stub(axios, 'get').returns(resolved)
72
73
 
73
74
  const result = await bchjs.RawTransactions.decodeScript(
@@ -88,7 +89,7 @@ describe('#RawTransactions', () => {
88
89
  const data =
89
90
  '020000000160d663961c63c7f0a07f22ec07b8f55b3935bfdbed8b1d8454916e8932fbf109010000006b4830450221008479fab4cfdcb111833d250a43f98ac26d43272b7a29cb1b9a0491eae5c44b3502203448b17253632395c29a7d62058bbfe93efb20fc8636ba6837002d464195aec04121029123258f7cdcd45b864066bcaa9b71f24d5ed1fa1dd36eaf107d8432b5014658ffffffff016d180000000000001976a91479d3297d1823149f4ec61df31d19f2fad5390c0288ac00000000'
90
91
 
91
- const resolved = new Promise(resolve => resolve({ data: data }))
92
+ const resolved = new Promise(resolve => resolve({ data }))
92
93
  sandbox.stub(axios, 'get').returns(resolved)
93
94
 
94
95
  bchjs.RawTransactions.getRawTransaction(
@@ -109,7 +110,7 @@ describe('#RawTransactions', () => {
109
110
  it('should send single raw transaction', async () => {
110
111
  const data = 'Error: transaction already in block chain'
111
112
 
112
- const resolved = new Promise(resolve => resolve({ data: data }))
113
+ const resolved = new Promise(resolve => resolve({ data }))
113
114
  sandbox.stub(axios, 'get').returns(resolved)
114
115
 
115
116
  const result = await bchjs.RawTransactions.sendRawTransaction(
@@ -1,12 +1,14 @@
1
1
  // Public npm libraries
2
- const assert = require('assert')
3
- const Buffer = require('safe-buffer').Buffer
2
+ import assert from 'assert'
3
+ import { Buffer } from 'safe-buffer'
4
4
 
5
5
  // Mocks
6
- const fixtures = require('./fixtures/script.json')
6
+ import { createRequire } from 'module'
7
7
 
8
8
  // Unit under test (uut)
9
- const BCHJS = require('../../src/bch-js')
9
+ import BCHJS from '../../src/bch-js.js'
10
+ const require = createRequire(import.meta.url)
11
+ const fixtures = require('./fixtures/script.json')
10
12
  let bchjs
11
13
 
12
14
  describe('#Script', () => {
@@ -438,8 +440,8 @@ describe('#Script', () => {
438
440
  Buffer.from(fixture.hex, 'hex')
439
441
  ),
440
442
  {
441
- redeemScriptSig: redeemScriptSig,
442
- redeemScript: redeemScript
443
+ redeemScriptSig,
444
+ redeemScript
443
445
  }
444
446
  )
445
447
  })
@@ -1,10 +1,12 @@
1
- const assert = require('assert')
1
+ import assert from 'assert'
2
2
 
3
- const BCHJS = require('../../src/bch-js')
4
- // const SLP = require("../../src/slp/slp")
3
+ import BCHJS from '../../src/bch-js.js'
4
+
5
+ import { createRequire } from 'module'
6
+ // import SLP from "../../src/slp/slp.js"
5
7
  // const SLP = new slp({ restURL: "http://fakeurl.com/" })
6
8
  let slp
7
-
9
+ const require = createRequire(import.meta.url)
8
10
  const fixtures = require('./fixtures/slp/address.json')
9
11
  // const axios = require("axios")
10
12
  // const sinon = require("sinon")
@@ -1,8 +1,10 @@
1
- const assert = require('assert')
1
+ import assert from 'assert'
2
2
 
3
- const fixtures = require('./fixtures/slp/ecpair.json')
3
+ import { createRequire } from 'module'
4
4
 
5
- const BCHJS = require('../../src/bch-js')
5
+ import BCHJS from '../../src/bch-js.js'
6
+ const require = createRequire(import.meta.url)
7
+ const fixtures = require('./fixtures/slp/ecpair.json')
6
8
  let slp
7
9
 
8
10
  // const SLP = require("../../src/slp/slp")
@@ -2,15 +2,16 @@
2
2
  Unit tests for the TokenType1 library.
3
3
  */
4
4
 
5
- const assert = require('chai').assert
6
- const sinon = require('sinon')
7
- // const axios = require('axios')
5
+ import chai from 'chai'
6
+ import sinon from 'sinon'
7
+ // const SERVER = bchjs.restURL
8
+
9
+ import BCHJS from '../../src/bch-js.js'
10
+ const { assert } = chai
11
+ // import axios from 'axios'
8
12
 
9
13
  // Default to unit tests unless some other value for TEST is passed.
10
14
  if (!process.env.TEST) process.env.TEST = 'unit'
11
- // const SERVER = bchjs.restURL
12
-
13
- const BCHJS = require('../../src/bch-js')
14
15
  const bchjs = new BCHJS()
15
16
 
16
17
  // Mock data used for unit tests
@@ -2,15 +2,16 @@
2
2
  Unit tests for the TokenType1 library.
3
3
  */
4
4
 
5
- const assert = require('chai').assert
6
- const sinon = require('sinon')
7
- // const axios = require("axios")
5
+ import chai from 'chai'
6
+ import sinon from 'sinon'
7
+ // const SERVER = bchjs.restURL
8
+
9
+ import BCHJS from '../../src/bch-js.js'
10
+ const { assert } = chai
11
+ // import axios from "axios"
8
12
 
9
13
  // Default to unit tests unless some other value for TEST is passed.
10
14
  if (!process.env.TEST) process.env.TEST = 'unit'
11
- // const SERVER = bchjs.restURL
12
-
13
- const BCHJS = require('../../src/bch-js')
14
15
  const bchjs = new BCHJS()
15
16
 
16
17
  // Mock data used for unit tests
@@ -1,19 +1,20 @@
1
1
  // Public npm libraries
2
- const assert = require('chai').assert
3
- const sinon = require('sinon')
4
- const cloneDeep = require('lodash.clonedeep')
2
+ import chai from 'chai'
3
+ import sinon from 'sinon'
4
+ import cloneDeep from 'lodash.clonedeep'
5
5
 
6
6
  // Unit under test
7
- const SLP = require('../../src/slp/slp')
7
+ import SLP from '../../src/slp/slp.js'
8
+
9
+ // Mock data used for unit tests
10
+ import mockDataLib from './fixtures/slp/mock-utils.js'
11
+ const { assert } = chai
8
12
  let uut
9
13
  // let SERVER
10
14
 
11
15
  const REST_URL = process.env.RESTURL
12
16
  ? process.env.RESTURL
13
17
  : 'https://bchn.fullstack.cash/v5/'
14
-
15
- // Mock data used for unit tests
16
- const mockDataLib = require('./fixtures/slp/mock-utils')
17
18
  let mockData
18
19
 
19
20
  // Default to unit tests unless some other value for TEST is passed.
@@ -1,9 +1,12 @@
1
+ import { createRequire } from 'module'
2
+ import assert from 'assert'
3
+ import chai from 'chai'
4
+ import BCHJS from '../../src/bch-js.js'
5
+ import { Buffer } from 'safe-buffer'
6
+ const require = createRequire(import.meta.url)
1
7
  const fixtures = require('./fixtures/transaction-builder.json')
2
- const assert = require('assert')
3
- const assert2 = require('chai').assert
4
- const BCHJS = require('../../src/bch-js')
8
+ const { assert: assert2 } = chai
5
9
  const bchjs = new BCHJS()
6
- const Buffer = require('safe-buffer').Buffer
7
10
 
8
11
  describe('#TransactionBuilder', () => {
9
12
  describe('#hashTypes', () => {
@@ -3,11 +3,12 @@
3
3
  */
4
4
 
5
5
  // Public npm libraries
6
- const assert = require('chai').assert
7
- const sinon = require('sinon')
8
- // const cloneDeep = require('lodash.clonedeep')
6
+ import chai from 'chai'
7
+ import sinon from 'sinon'
8
+ // import cloneDeep from 'lodash.clonedeep'
9
9
 
10
- const BCHJS = require('../../src/bch-js')
10
+ import BCHJS from '../../src/bch-js.js'
11
+ const { assert } = chai
11
12
  const bchjs = new BCHJS()
12
13
 
13
14
  // const mockDataLib = require('./fixtures/transaction-mock.js')
package/test/unit/util.js CHANGED
@@ -1,39 +1,18 @@
1
- const assert = require('assert')
2
- const assert2 = require('chai').assert
3
- const axios = require('axios')
4
- const BCHJS = require('../../src/bch-js')
5
- const bchjs = new BCHJS()
6
- const sinon = require('sinon')
1
+ /*
2
+ Unit tests for util.js library.
3
+ */
7
4
 
8
- describe('#Util', () => {
9
- describe('#validateAddress', () => {
10
- let sandbox
11
- beforeEach(() => (sandbox = sinon.createSandbox()))
12
- afterEach(() => sandbox.restore())
13
-
14
- it('should validate address', done => {
15
- const data = {
16
- isvalid: true,
17
- address: 'bitcoincash:qpz7qtkuyhrsz4qmnnrvf8gz9zd0u9v7eqsewyk4w5',
18
- scriptPubKey: '76a91445e02edc25c701541b9cc6c49d02289afe159ec888ac',
19
- ismine: false,
20
- iswatchonly: false,
21
- isscript: false
22
- }
5
+ // Global npm libraries
6
+ import chai from 'chai'
23
7
 
24
- const resolved = new Promise(resolve => resolve({ data: data }))
25
- sandbox.stub(axios, 'get').returns(resolved)
8
+ // Local libraries
9
+ import BCHJS from '../../src/bch-js.js'
10
+ // import sinon from 'sinon'
11
+ const { assert: assert2 } = chai
26
12
 
27
- bchjs.Util.validateAddress(
28
- 'bitcoincash:qpz7qtkuyhrsz4qmnnrvf8gz9zd0u9v7eqsewyk4w5'
29
- )
30
- .then(result => {
31
- assert.deepStrictEqual(data, result)
32
- })
33
- .then(done, done)
34
- })
35
- })
13
+ const bchjs = new BCHJS()
36
14
 
15
+ describe('#Util', () => {
37
16
  describe('#floor8', () => {
38
17
  it('should floor a number to 8 decimals', () => {
39
18
  const num = 1.234567891111
@@ -2,13 +2,14 @@
2
2
  Unit tests for the utxo.js library.
3
3
  */
4
4
 
5
- const sinon = require('sinon')
6
- const assert = require('chai').assert
5
+ import sinon from 'sinon'
6
+ import chai from 'chai'
7
7
 
8
- const BCHJS = require('../../src/bch-js')
9
- const bchjs = new BCHJS()
8
+ import BCHJS from '../../src/bch-js.js'
10
9
 
11
- const mockData = require('./fixtures/utxo-mocks')
10
+ import mockData from './fixtures/utxo-mocks.js'
11
+ const { assert } = chai
12
+ const bchjs = new BCHJS()
12
13
 
13
14
  describe('#utxo', () => {
14
15
  let sandbox
@@ -0,0 +1,193 @@
1
+ import assert from 'assert'
2
+ import sinon from 'sinon'
3
+ import BCHJS from '../../src/bch-js.js'
4
+
5
+ describe('#X402 Integration', () => {
6
+ let sandbox
7
+
8
+ beforeEach(() => {
9
+ sandbox = sinon.createSandbox()
10
+ })
11
+
12
+ afterEach(() => {
13
+ sandbox.restore()
14
+ })
15
+
16
+ describe('#Constructor Configuration', () => {
17
+ it('should initialize without x402 by default', () => {
18
+ const bchjs = new BCHJS()
19
+
20
+ assert.strictEqual(bchjs.wif, '')
21
+ assert.strictEqual(bchjs.paymentAmountSats, 20000)
22
+ assert.strictEqual(bchjs.bchServerURL, 'https://free-bch.fullstack.cash')
23
+ })
24
+
25
+ it('should accept wif in config', () => {
26
+ const testWif = 'L1eYaneXDDXy8VDig4Arwe8wYHbhtsA5wuQvwsKwhaYeneoZuKG4'
27
+ const bchjs = new BCHJS({
28
+ wif: testWif
29
+ })
30
+
31
+ assert.strictEqual(bchjs.wif, testWif)
32
+ })
33
+
34
+ it('should accept paymentAmountSats in config', () => {
35
+ const bchjs = new BCHJS({
36
+ paymentAmountSats: 5000
37
+ })
38
+
39
+ assert.strictEqual(bchjs.paymentAmountSats, 5000)
40
+ })
41
+
42
+ it('should accept bchServerURL in config', () => {
43
+ const customUrl = 'http://localhost:5000'
44
+ const bchjs = new BCHJS({
45
+ bchServerURL: customUrl
46
+ })
47
+
48
+ assert.strictEqual(bchjs.bchServerURL, customUrl)
49
+ })
50
+
51
+ it('should use default values when config options are not provided', () => {
52
+ const bchjs = new BCHJS({
53
+ restURL: 'http://localhost:3000/v6/'
54
+ })
55
+
56
+ assert.strictEqual(bchjs.wif, '')
57
+ assert.strictEqual(bchjs.paymentAmountSats, 20000)
58
+ assert.strictEqual(bchjs.bchServerURL, 'https://free-bch.fullstack.cash')
59
+ })
60
+
61
+ it('should read WIF from BCHJSWIF environment variable', () => {
62
+ const testWif = 'L1eYaneXDDXy8VDig4Arwe8wYHbhtsA5wuQvwsKwhaYeneoZuKG4'
63
+ const originalEnv = process.env.BCHJSWIF
64
+ process.env.BCHJSWIF = testWif
65
+
66
+ try {
67
+ const bchjs = new BCHJS()
68
+ assert.strictEqual(bchjs.wif, testWif)
69
+ } finally {
70
+ // Restore original env value
71
+ if (originalEnv === undefined) {
72
+ delete process.env.BCHJSWIF
73
+ } else {
74
+ process.env.BCHJSWIF = originalEnv
75
+ }
76
+ }
77
+ })
78
+
79
+ it('should prefer config.wif over BCHJSWIF environment variable', () => {
80
+ const configWif = 'L1XHKhaBAfkr2FJQn3pTfCMxz652WYfmvKj8xDCHCEDV9tWGcbYj'
81
+ const envWif = 'L1eYaneXDDXy8VDig4Arwe8wYHbhtsA5wuQvwsKwhaYeneoZuKG4'
82
+ const originalEnv = process.env.BCHJSWIF
83
+ process.env.BCHJSWIF = envWif
84
+
85
+ try {
86
+ const bchjs = new BCHJS({ wif: configWif })
87
+ assert.strictEqual(bchjs.wif, configWif)
88
+ } finally {
89
+ // Restore original env value
90
+ if (originalEnv === undefined) {
91
+ delete process.env.BCHJSWIF
92
+ } else {
93
+ process.env.BCHJSWIF = originalEnv
94
+ }
95
+ }
96
+ })
97
+ })
98
+
99
+ describe('#x402 Helper Functions', () => {
100
+ it('should expose createSigner function', () => {
101
+ const bchjs = new BCHJS()
102
+
103
+ assert.strictEqual(typeof bchjs.x402.createSigner, 'function')
104
+ })
105
+
106
+ it('should expose withPaymentInterceptor function', () => {
107
+ const bchjs = new BCHJS()
108
+
109
+ assert.strictEqual(typeof bchjs.x402.withPaymentInterceptor, 'function')
110
+ })
111
+
112
+ it('should expose createPaymentHeader function', () => {
113
+ const bchjs = new BCHJS()
114
+
115
+ assert.strictEqual(typeof bchjs.x402.createPaymentHeader, 'function')
116
+ })
117
+
118
+ it('should expose selectPaymentRequirements function', () => {
119
+ const bchjs = new BCHJS()
120
+
121
+ assert.strictEqual(typeof bchjs.x402.selectPaymentRequirements, 'function')
122
+ })
123
+ })
124
+
125
+ describe('#Axios Instance', () => {
126
+ it('should have axios available in sub-modules', () => {
127
+ const bchjs = new BCHJS()
128
+
129
+ // Check that sub-modules have axios available (from their own import or config)
130
+ assert.ok(bchjs.Control.axios)
131
+ assert.ok(bchjs.Blockchain.axios)
132
+ assert.ok(bchjs.Mining.axios)
133
+ assert.ok(bchjs.Electrumx.axios)
134
+ assert.ok(bchjs.RawTransactions.axios)
135
+ assert.ok(bchjs.Price.axios)
136
+ })
137
+
138
+ it('should pass x402-wrapped axios instance when WIF is provided', () => {
139
+ const testWif = 'L1eYaneXDDXy8VDig4Arwe8wYHbhtsA5wuQvwsKwhaYeneoZuKG4'
140
+ const bchjs = new BCHJS({
141
+ wif: testWif
142
+ })
143
+
144
+ // All modules should have the same wrapped axios instance
145
+ assert.ok(bchjs.Control.axios)
146
+ assert.ok(bchjs.Blockchain.axios)
147
+ // When WIF is provided, the axios instances should be the same wrapped instance
148
+ assert.strictEqual(bchjs.Control.axios, bchjs.Blockchain.axios)
149
+ })
150
+ })
151
+
152
+ describe('#selectPaymentRequirements', () => {
153
+ it('should select BCH utxo payment requirements from accepts array', () => {
154
+ const bchjs = new BCHJS()
155
+
156
+ const accepts = [
157
+ {
158
+ scheme: 'exact',
159
+ network: 'base-sepolia',
160
+ maxAmountRequired: '10000'
161
+ },
162
+ {
163
+ scheme: 'utxo',
164
+ network: 'bch',
165
+ minAmountRequired: '1000',
166
+ payTo: 'bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d'
167
+ }
168
+ ]
169
+
170
+ const result = bchjs.x402.selectPaymentRequirements(accepts)
171
+
172
+ assert.strictEqual(result.scheme, 'utxo')
173
+ assert.strictEqual(result.network, 'bch')
174
+ assert.strictEqual(result.minAmountRequired, '1000')
175
+ })
176
+
177
+ it('should throw an error when no BCH requirements found', () => {
178
+ const bchjs = new BCHJS()
179
+
180
+ const accepts = [
181
+ {
182
+ scheme: 'exact',
183
+ network: 'base-sepolia',
184
+ maxAmountRequired: '10000'
185
+ }
186
+ ]
187
+
188
+ assert.throws(() => {
189
+ bchjs.x402.selectPaymentRequirements(accepts)
190
+ }, /No BCH payment requirements found/)
191
+ })
192
+ })
193
+ })
@@ -1,38 +0,0 @@
1
- /*
2
- Integration tests for the psf-slp-indexer.js library, specific to the eCash
3
- blockchain.
4
- */
5
-
6
- // Global npm libraries
7
- const assert = require('chai').assert
8
-
9
- // Local libraries
10
- const BCHJS = require('../../../../src/bch-js')
11
- let bchjs
12
-
13
- describe('#psf-slp-indexer', () => {
14
- beforeEach(async () => {
15
- // Introduce a delay so that the BVT doesn't trip the rate limits.
16
- if (process.env.IS_USING_FREE_TIER) await sleep(3000)
17
-
18
- bchjs = new BCHJS()
19
- })
20
-
21
- describe('#balance', () => {
22
- it('should get token balance for an ecash address', async () => {
23
- const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09'
24
-
25
- const result = await bchjs.PsfSlpIndexer.balance(addr)
26
- // console.log(`result: ${JSON.stringify(result, null, 2)}`)
27
-
28
- assert.property(result.balance, 'utxos')
29
- assert.property(result.balance, 'txs')
30
- assert.property(result.balance, 'balances')
31
- })
32
- })
33
- })
34
-
35
- // Promise-based sleep function
36
- function sleep (ms) {
37
- return new Promise(resolve => setTimeout(resolve, ms))
38
- }
@@ -1,73 +0,0 @@
1
- /*
2
- Integration tests for the bchjs. Only covers calls made to
3
- rest.bitcoin.com.
4
-
5
- TODO
6
- */
7
-
8
- const chai = require('chai')
9
- const assert = chai.assert
10
- const BCHJS = require('../../../../src/bch-js')
11
- const bchjs = new BCHJS({ restURL: process.env.RESTURL })
12
-
13
- // Inspect utility used for debugging.
14
- const util = require('util')
15
- util.inspect.defaultOptions = {
16
- showHidden: true,
17
- colors: true,
18
- depth: 3
19
- }
20
-
21
- describe('#rawtransaction', () => {
22
- beforeEach(async () => {
23
- if (process.env.IS_USING_FREE_TIER) await sleep(1500)
24
- })
25
-
26
- /*
27
- Testing sentRawTransaction isn't really possible with an integration test,
28
- as the endpoint really needs an e2e test to be properly tested. The tests
29
- below expect error messages returned from the server, but at least test
30
- that the server is responding on those endpoints, and responds consistently.
31
- */
32
- describe('sendRawTransaction', () => {
33
- it('should send a single transaction hex', async () => {
34
- try {
35
- const hex =
36
- '01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000'
37
-
38
- await bchjs.RawTransactions.sendRawTransaction(hex)
39
- // console.log(`result ${JSON.stringify(result, null, 2)}`)
40
-
41
- assert.equal(true, false, 'Unexpected result!')
42
- } catch (err) {
43
- // console.log(`err: ${util.inspect(err)}`)
44
-
45
- assert.hasAllKeys(err, ['error'])
46
- assert.include(err.error, 'bad-txns-inputs-missingorspent')
47
- // assert.include(err.error, 'Missing inputs')
48
- }
49
- })
50
-
51
- it('should send an array of tx hexes', async () => {
52
- try {
53
- const hexes = [
54
- '01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000',
55
- '01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000'
56
- ]
57
-
58
- const result = await bchjs.RawTransactions.sendRawTransaction(hexes)
59
- console.log(`result ${JSON.stringify(result, null, 2)}`)
60
- } catch (err) {
61
- // console.log(`err: ${util.inspect(err)}`)
62
-
63
- assert.hasAllKeys(err, ['error'])
64
- assert.include(err.error, 'bad-txns-inputs-missingorspent')
65
- // assert.include(err.error, 'Missing inputs')
66
- }
67
- })
68
- })
69
- })
70
-
71
- function sleep (ms) {
72
- return new Promise(resolve => setTimeout(resolve, ms))
73
- }
@@ -1,38 +0,0 @@
1
- /*
2
- Integration tests for the utxo.js library.
3
- */
4
-
5
- const assert = require('chai').assert
6
-
7
- const BCHJS = require('../../../../src/bch-js')
8
- const bchjs = new BCHJS()
9
-
10
- describe('#UTXO', () => {
11
- beforeEach(async () => {
12
- // sandbox = sinon.createSandbox()
13
-
14
- if (process.env.IS_USING_FREE_TIER) await sleep(1500)
15
- })
16
-
17
- describe('#get', () => {
18
- it('should get hydrated and filtered UTXOs for an address', async () => {
19
- const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09'
20
- // const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
21
-
22
- const result = await bchjs.Utxo.get(addr)
23
- // console.log(`result: ${JSON.stringify(result, null, 2)}`)
24
-
25
- // assert.isArray(result)
26
- assert.property(result, 'address')
27
- assert.property(result, 'bchUtxos')
28
- assert.property(result, 'nullUtxos')
29
- assert.property(result, 'slpUtxos')
30
- assert.isArray(result.bchUtxos)
31
- assert.isArray(result.nullUtxos)
32
- })
33
- })
34
- })
35
-
36
- function sleep (ms) {
37
- return new Promise(resolve => setTimeout(resolve, ms))
38
- }