@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/README.md CHANGED
@@ -5,23 +5,21 @@
5
5
  [![License](https://img.shields.io/npm/l/@psf/bch-js)](https://github.com/Permissionless-Software-Foundation/bch-js/blob/master/LICENSE.md)
6
6
  [![js-standard-style](https://img.shields.io/badge/javascript-standard%20code%20style-green.svg?style=flat-square)](https://github.com/feross/standard) [![Join the chat at https://gitter.im/Permissionless-Software-Foundation/bch-js](https://badges.gitter.im/Permissionless-Software-Foundation/bch-js.svg)](https://gitter.im/Permissionless-Software-Foundation/bch-js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
7
 
8
- [bch-js](https://www.npmjs.com/package/@psf/bch-js) is a JavaScript npm library for creating web and mobile apps that can interact with the Bitcoin Cash (BCH) and eCash (XEC) blockchains. bch-js contains a toolbox of handy tools, and an easy API for talking with [bch-api REST API](https://github.com/Permissionless-Software-Foundation/bch-api). [FullStack.cash](https://fullstack.cash) offers paid cloud access to bch-api. You can run your own infrastructure by following documentation on [CashStack.info](https://cashstack.info).
8
+ [bch-js](https://www.npmjs.com/package/@psf/bch-js) is a JavaScript npm library for creating web and mobile apps that can interact with the Bitcoin Cash (BCH) blockchain. bch-js contains a toolbox of handy tools, and an easy API for talking with [psf-bch-api REST API](https://github.com/Permissionless-Software-Foundation/psf-bch-api). [FullStack.cash](https://fullstack.cash) offers paid cloud access to psf-bch-api. You can run your own infrastructure by following documentation on [CashStack.info](https://cashstack.info).
9
9
 
10
10
  ### Quick Start Videos:
11
11
 
12
- Here are two YouTube walk-through videos to help you get started:
12
+ YouTube walk-through videos to help you get started:
13
13
 
14
14
  - [Introduction to bch-js and the bch-js-examples repository](https://youtu.be/GD2i1ZUiyrk)
15
- - [Working with the FullStack.cash JWT token](https://youtu.be/GD2i1ZUiyrk)
16
15
 
17
16
  ### Quick Links
18
17
 
19
18
  - [npm Library](https://www.npmjs.com/package/@psf/bch-js)
20
19
  - [Documentation](https://bchjs.fullstack.cash/)
21
20
  - [Examples](https://github.com/Permissionless-Software-Foundation/bch-js-examples)
22
- - [bchn.fullstack.cash](https://bchn.fullstack.cash) - The REST API this library talks to by default.
21
+ - [x402-bch.fullstack.cash](https://x402-bch.fullstack.cash) - The REST API this library talks to by default.
23
22
  - [FullStack.cash](https://fullstack.cash) - cloud-based infrastructure for application developers.
24
- - [FullStack.cash Account](https://fullstack.cash/login) - Get your API key to unlock increased rate limits.
25
23
  - [Permissionless Software Foundation](https://psfoundation.cash) - The organization that maintains this library.
26
24
  - [CashStack.info](https://cashstack.info) - bch-js is part of the Cash Stack, a JavaScript framework for writing web 2 and web 3 business applications.
27
25
 
@@ -31,42 +29,26 @@ Here are two YouTube walk-through videos to help you get started:
31
29
 
32
30
  - Instantiate the library in your code:
33
31
 
34
- ```
35
- const BCHJS = require("@psf/bch-js")
32
+ ```javascript
33
+ import BCHJS from "@psf/bch-js"
36
34
  let bchjs = new BCHJS() // Defaults to BCHN network.
37
35
  ```
38
36
 
39
37
  This library is intended to be paired with
40
- the [bch-api](https://github.com/Permissionless-Software-Foundation/bch-api) REST API, and the infrastructure provided by [FullStack.cash](https://fullstack.cash). The `restURL` property can be changed to work with different Bitcoin Cash networks:
38
+ the [psf-bch-api](https://github.com/Permissionless-Software-Foundation/psf-bch-api) REST API, and the infrastructure provided by [FullStack.cash](https://fullstack.cash). The `restURL` property can be changed to work with different Bitcoin Cash networks:
41
39
 
42
- - BCHN Mainnet REST API server: https://bchn.fullstack.cash/v5/
43
- - ABC Mainnet REST API server: https://abc.fullstack.cash/v5/
40
+ - BCHN Mainnet REST API server: https://x402-bch.fullstack.cash/v7/
44
41
  - Check server status: https://metrics.fullstack.cash
45
42
 
46
- ### API Key (JWT Token)
47
-
48
- The [bch-api](https://github.com/Permissionless-Software-Foundation/bch-api) REST API hosted by [FullStack.cash](https://fullstack.cash) uses JWT tokens to pay for increased
49
- rate limits when interacting with the back end server. See [this article](https://cashstack.info) if you want to understand the system-as-a-whole. The JWT token can be fed to bch-js _implicitly_ or _explicitly_.
50
-
51
- - Implicitly: bch-js will detect your JWT token if you set the `BCHJSTOKEN` environment variable.
52
- - Explicitly: You can directly feed in the JWT token with the `apiToken` property when instantiating the library. Here is an example:
53
-
54
- ```
55
- const BCHJS = require("@psf/bch-js")
56
- let bchjs = new BCHJS({
57
- restURL: 'https://bchn.fullstack.cash/v5/',
58
- apiToken: 'eyJhbGciO...' // Your JWT token here.
59
- })
60
- ```
61
43
 
62
- ### Gatsby & Web Apps
44
+ ### Web Apps
63
45
 
64
46
  [minimal-slp-wallet](https://www.npmjs.com/package/minimal-slp-wallet) is a minimal wallet 'engine' that incorporates bch-js. It's compiled with Browserify for front end apps.
65
47
 
66
- [gatsby-theme-bch-wallet](https://github.com/Permissionless-Software-Foundation/gatsby-theme-bch-wallet) is a Gatsby Theme and [bch-wallet-starter](https://github.com/Permissionless-Software-Foundation/bch-wallet-starter) is a Gatsby Starter for building web wallets using minimal-slp-wallet.
67
-
68
48
  [This gist](https://gist.github.com/christroutner/6cb9d1b615f3f9363af79723157bc434) shows how to include minimal-slp-wallet into a basic web page without using a framework.
69
49
 
50
+ [bch-wallet-web3-spa](https://github.com/Permissionless-Software-Foundation/bch-wallet-web3-spa) is a React web app template using bch-js and minimal-slp-wallet.
51
+
70
52
  ## Features
71
53
 
72
54
  - [ECMAScript 2017 standard JavaScript](https://en.wikipedia.org/wiki/ECMAScript#8th_Edition_-_ECMAScript_2017) used instead of TypeScript. Works
@@ -0,0 +1,38 @@
1
+ #!/bin/bash
2
+
3
+ # Script to improve contrast in auto-generated API documentation
4
+ # The code examples in api-doc use grey-on-grey colors that are hard to read.
5
+ # This fixes that issue by improving text contrast for better readability.
6
+ # Run this script *after* running 'npm run docs'
7
+
8
+ FILE="./docs/assets/prism.css"
9
+
10
+ # Check if file exists
11
+ if [ ! -f "$FILE" ]; then
12
+ echo "Error: $FILE not found. Make sure to run 'npm run docs' first."
13
+ exit 1
14
+ fi
15
+
16
+ # Create a backup
17
+ #cp "$FILE" "$FILE.bak"
18
+
19
+ # Improve contrast:
20
+ # - Change default text color from #ccc to #e8e8e8 (brighter light gray)
21
+ # - Change comment color from #999 to #b5b5b5 (lighter for better visibility)
22
+ # - Change punctuation color from #ccc to #e8e8e8 (consistent with text)
23
+
24
+ # Replace default text color in code[class*="language-"] and pre[class*="language-"]
25
+ sed -i 's/color: #ccc;/color: #000000;/g' "$FILE"
26
+
27
+ # Replace comment color
28
+ sed -i 's/color: #999;/color: #b5b5b5;/g' "$FILE"
29
+
30
+
31
+ # Replace punctuation color (already handled by first sed, but explicit for clarity)
32
+ # The punctuation token also uses #ccc, so the first sed command handles it
33
+
34
+ echo "✓ Improved contrast in prism.css:"
35
+ echo " - Default text: #ccc → #e8e8e8"
36
+ echo " - Comments: #999 → #b5b5b5"
37
+ echo " - Punctuation: #ccc → #e8e8e8"
38
+
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@psf/bch-js",
3
- "version": "6.8.3",
3
+ "version": "7.0.1",
4
+ "type": "module",
4
5
  "description": "A JavaScript library for working with Bitcoin Cash, eCash, and SLP Tokens",
5
6
  "author": "Chris Troutner <chris.troutner@gmail.com>",
6
7
  "contributors": [
@@ -9,25 +10,16 @@
9
10
  ],
10
11
  "main": "src/bch-js",
11
12
  "scripts": {
12
- "test": "nyc mocha --trace-warnings --unhandled-rejections=strict --timeout 30000 test/unit/",
13
- "test:integration": "npm run test:integration:bchn",
14
- "test:integration:nft": "export RESTURL=https://bchn.fullstack.cash/v5/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 -g '#nft1' test/integration/chains/bchn/slp.js",
15
- "test:integration:abc": "export RESTURL=https://abc.fullstack.cash/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/abc/",
16
- "test:integration:bchn": "export RESTURL=https://bchn.fullstack.cash/v5/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
17
- "test:integration:bchn:slpdb": "export TESTSLP=1 && export RESTURL=https://bchn.fullstack.cash/v5/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
18
- "test:integration:local:abc": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/",
19
- "test:integration:local:bchn": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
20
- "test:integration:local:testnet": "RESTURL=http://localhost:4000/v5/ mocha --timeout 30000 test/integration/chains/testnet",
21
- "test:integration:decatur:bchn": "export RESTURL=http://192.168.2.129:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
22
- "test:integration:decatur:abc": "export RESTURL=http://192.168.2.142:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/",
23
- "test:integration:temp:bchn": "export RESTURL=http://157.90.174.219:3000/v5/ && mocha --timeout 30000 test/integration/",
24
- "test:temp": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 -g '#Encryption' test/integration/",
25
- "test:temp2": "mocha --timeout=30000 -g '#TransactionLib' test/unit/",
26
- "test:temp3": "export RESTURL=https://bchn.fullstack.cash/v5/ && mocha --timeout 30000 -g '#DSProof' test/integration/chains/",
27
- "test:temp4": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 -g '#decodeOpReturn' test/integration/",
28
- "coverage": "nyc report --reporter=text-lcov | coveralls",
29
- "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/",
30
- "docs": "./node_modules/.bin/apidoc -i src/ -o docs",
13
+ "test": "c8 mocha --trace-warnings --unhandled-rejections=strict --timeout 30000 test/unit/",
14
+ "test:integration": "npm run test:integration:local:noauth",
15
+ "test:integration:nft": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 -g '#nft1' test/integration/slp.js",
16
+ "test:integration:bchn": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 test/integration/",
17
+ "test:integration:local:noauth": "export RESTURL=http://localhost:5942/v6/ && mocha --timeout 30000 test/integration/",
18
+ "test:integration:local:auth": "export RESTURL=http://5.78.147.3:5942/v6/ && export BCHJSBEARERTOKEN=temp01 && mocha --timeout 30000 test/integration/",
19
+ "test:integration:decatur": "export RESTURL=http://192.168.2.127:5942/v6/ && mocha --timeout 30000 test/integration/",
20
+ "test:integration:x402": "export RESTURL=http://localhost:5942/v6/ && export BCHJSWIF=L1eYaneXDDXy8VDig4Arwe8wYHbhtsA5wuQvwsKwhaYeneoZuKG4 && mocha --timeout 30000 test/integration/",
21
+ "coverage": "nyc --reporter=html mocha --timeout 25000 test/unit/",
22
+ "docs": "./node_modules/.bin/apidoc -i src/ -o docs && ./fix-docs-contrast.sh",
31
23
  "lint": "standard --env mocha --fix"
32
24
  },
33
25
  "license": "MIT",
@@ -42,45 +34,39 @@
42
34
  "@psf/bitcoincash-ops": "2.0.0",
43
35
  "@psf/bitcoincashjs-lib": "4.0.3",
44
36
  "@psf/coininfo": "4.0.0",
45
- "axios": "1.12.2",
37
+ "axios": "1.13.2",
46
38
  "bc-bip68": "1.0.5",
47
- "bchaddrjs-slp": "0.2.5",
39
+ "bchaddrjs-slp": "0.2.14",
48
40
  "bigi": "1.4.2",
49
- "bignumber.js": "9.0.0",
50
- "bip-schnorr": "0.3.0",
51
- "bip38": "2.0.2",
52
- "bip39": "3.0.2",
41
+ "bignumber.js": "9.3.1",
42
+ "bip-schnorr": "0.6.7",
43
+ "bip38": "3.1.1",
44
+ "bip39": "3.1.0",
53
45
  "bip66": "1.1.5",
54
- "bitcoinjs-message": "2.0.0",
46
+ "bitcoinjs-message": "2.2.0",
55
47
  "bs58": "4.0.1",
56
48
  "ecashaddrjs": "1.0.7",
57
- "ini": "1.3.8",
58
- "randombytes": "2.0.6",
59
- "safe-buffer": "5.1.2",
60
- "satoshi-bitcoin": "1.0.4",
61
- "slp-mdm": "0.0.6",
49
+ "ini": "6.0.0",
50
+ "randombytes": "2.1.0",
51
+ "safe-buffer": "5.2.1",
52
+ "satoshi-bitcoin": "1.0.5",
53
+ "slp-mdm": "0.0.7",
62
54
  "slp-parser": "0.0.4",
63
- "wif": "2.0.6"
55
+ "wif": "2.0.6",
56
+ "x402-bch-axios": "1.1.1"
64
57
  },
65
58
  "devDependencies": {
66
- "apidoc": "0.50.5",
67
- "assert": "2.0.0",
59
+ "apidoc": "1.2.0",
60
+ "assert": "2.1.0",
61
+ "c8": "10.1.3",
68
62
  "chai": "4.1.2",
69
- "coveralls": "3.0.2",
70
- "eslint": "7.17.0",
71
- "eslint-config-prettier": "7.1.0",
72
- "eslint-config-standard": "16.0.3",
73
- "eslint-plugin-node": "11.1.0",
74
- "eslint-plugin-prettier": "3.3.1",
75
- "eslint-plugin-standard": "4.0.0",
76
63
  "husky": "4.3.8",
77
64
  "lodash.clonedeep": "4.5.0",
78
- "mocha": "9.2.1",
79
- "node-mocks-http": "1.7.0",
65
+ "mocha": "11.7.5",
80
66
  "nyc": "15.1.0",
81
- "semantic-release": "^19.0.2",
67
+ "semantic-release": "24.2.9",
82
68
  "sinon": "9.2.2",
83
- "standard": "^16.0.4"
69
+ "standard": "17.1.2"
84
70
  },
85
71
  "apidoc": {
86
72
  "title": "bch-js",
@@ -90,5 +76,10 @@
90
76
  "hooks": {
91
77
  "pre-commit": "npm run lint"
92
78
  }
79
+ },
80
+ "standard": {
81
+ "parserOptions": {
82
+ "ecmaVersion": "latest"
83
+ }
93
84
  }
94
85
  }
package/src/address.js CHANGED
@@ -1,7 +1,7 @@
1
- // const axios = require("axios")
2
- const Bitcoin = require('@psf/bitcoincashjs-lib')
3
- const cashaddr = require('ecashaddrjs')
4
- const coininfo = require('@psf/coininfo')
1
+ // import axios from "axios"
2
+ import Bitcoin from '@psf/bitcoincashjs-lib'
3
+ import cashaddr from 'ecashaddrjs'
4
+ import coininfo from '@psf/coininfo'
5
5
 
6
6
  class Address {
7
7
  constructor (config) {
@@ -10,22 +10,11 @@ class Address {
10
10
  else tmp.restURL = config.restURL
11
11
 
12
12
  this.restURL = tmp.restURL
13
- this.apiToken = tmp.apiToken
14
13
  this.authToken = config.authToken
15
14
 
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
- }
15
+ this.axiosOptions = {
16
+ headers: {
17
+ authorization: this.authToken
29
18
  }
30
19
  }
31
20
  }
@@ -312,28 +301,28 @@ class Address {
312
301
  return {
313
302
  prefix: 'bitcoincash',
314
303
  type: 'P2PKH',
315
- hash: hash,
304
+ hash,
316
305
  format: 'legacy'
317
306
  }
318
307
  case info.main.versions.scripthash:
319
308
  return {
320
309
  prefix: 'bitcoincash',
321
310
  type: 'P2SH',
322
- hash: hash,
311
+ hash,
323
312
  format: 'legacy'
324
313
  }
325
314
  case info.test.versions.public:
326
315
  return {
327
316
  prefix: 'bchtest',
328
317
  type: 'P2PKH',
329
- hash: hash,
318
+ hash,
330
319
  format: 'legacy'
331
320
  }
332
321
  case info.test.versions.scripthash:
333
322
  return {
334
323
  prefix: 'bchtest',
335
324
  type: 'P2SH',
336
- hash: hash,
325
+ hash,
337
326
  format: 'legacy'
338
327
  }
339
328
  default:
@@ -871,4 +860,4 @@ class Address {
871
860
  }
872
861
  }
873
862
 
874
- module.exports = Address
863
+ export default Address
package/src/bch-js.js CHANGED
@@ -7,36 +7,45 @@
7
7
  */
8
8
 
9
9
  // bch-api mainnet.
10
- const DEFAULT_REST_API = 'https://api.fullstack.cash/v5/'
11
10
  // const DEFAULT_REST_API = "http://localhost:3000/v5/"
12
11
 
12
+ // Global npm libraries
13
+ import axios from 'axios'
14
+ import {
15
+ createSigner,
16
+ withPaymentInterceptor,
17
+ createPaymentHeader,
18
+ selectPaymentRequirements
19
+ } from 'x402-bch-axios'
20
+
13
21
  // local deps
14
- const BitcoinCash = require('./bitcoincash')
15
- const Crypto = require('./crypto')
16
- const Util = require('./util')
17
- const Blockchain = require('./blockchain')
18
- const Control = require('./control')
19
- const Generating = require('./generating')
20
- const Mining = require('./mining')
21
- const RawTransactions = require('./raw-transactions')
22
- const Mnemonic = require('./mnemonic')
23
- const Address = require('./address')
24
- const HDNode = require('./hdnode')
25
- const TransactionBuilder = require('./transaction-builder')
26
- const ECPair = require('./ecpair')
27
- const Script = require('./script')
28
- const Price = require('./price')
29
- const Schnorr = require('./schnorr')
30
- const SLP = require('./slp/slp')
31
- const Encryption = require('./encryption')
32
- const Utxo = require('./utxo')
33
- const Transaction = require('./transaction')
34
- const DSProof = require('./dsproof')
35
- const Ecash = require('./ecash')
22
+ import BitcoinCash from './bitcoincash.js'
23
+ import Crypto from './crypto.js'
24
+ import Util from './util.js'
25
+ import Blockchain from './blockchain.js'
26
+ import Control from './control.js'
27
+ import Generating from './generating.js'
28
+ import Mining from './mining.js'
29
+ import RawTransactions from './raw-transactions.js'
30
+ import Mnemonic from './mnemonic.js'
31
+ import Address from './address.js'
32
+ import HDNode from './hdnode.js'
33
+ import TransactionBuilder from './transaction-builder.js'
34
+ import ECPair from './ecpair.js'
35
+ import Script from './script.js'
36
+ import Price from './price.js'
37
+ import Schnorr from './schnorr.js'
38
+ import SLP from './slp/slp.js'
39
+ import Encryption from './encryption.js'
40
+ import Utxo from './utxo.js'
41
+ import Transaction from './transaction.js'
42
+ import DSProof from './dsproof.js'
43
+ import Ecash from './ecash.js'
36
44
 
37
45
  // Indexers
38
- const Electrumx = require('./electrumx')
39
- const PsfSlpIndexer = require('./psf-slp-indexer')
46
+ import Electrumx from './electrumx.js'
47
+ import PsfSlpIndexer from './psf-slp-indexer.js'
48
+ const DEFAULT_REST_API = 'https://api.fullstack.cash/v6/'
40
49
 
41
50
  class BCHJS {
42
51
  constructor (config) {
@@ -47,39 +56,55 @@ class BCHJS {
47
56
  this.restURL = process.env.RESTURL
48
57
  } else this.restURL = DEFAULT_REST_API
49
58
 
50
- // Retrieve the apiToken
51
- this.apiToken = '' // default value.
52
- if (config && config.apiToken && config.apiToken !== '') {
53
- this.apiToken = config.apiToken
54
- } else if (process.env.BCHJSTOKEN && process.env.BCHJSTOKEN !== '') {
55
- this.apiToken = process.env.BCHJSTOKEN
59
+ // Retrieve the Bearer token for simple token authentication.
60
+ this.bearerToken = '' // default value.
61
+ if (config && config.bearerToken && config.bearerToken !== '') {
62
+ this.bearerToken = config.bearerToken
63
+ } else if (process.env.BCHJSBEARERTOKEN && process.env.BCHJSBEARERTOKEN !== '') {
64
+ this.bearerToken = process.env.BCHJSBEARERTOKEN
56
65
  }
57
66
 
58
- // Retrieve the Basic Authentication password.
59
- this.authPass = '' // default value.
60
- if (config && config.authPass && config.authPass !== '') {
61
- this.authPass = config.authPass
62
- } else if (process.env.BCHJSAUTHPASS && process.env.BCHJSAUTHPASS !== '') {
63
- this.authPass = process.env.BCHJSAUTHPASS
67
+ // Generate the authentication token for the authorization header.
68
+ this.authToken = ''
69
+ if (this.bearerToken) {
70
+ this.authToken = `Bearer ${this.bearerToken}`
64
71
  }
65
72
 
66
- // Generate a Basic Authentication token from an auth password
67
- this.authToken = ''
68
- if (this.authPass) {
69
- // console.log(`bch-js initialized with authPass: ${this.authPass}`)
70
- // Generate the header for Basic Authentication.
71
- const combined = `fullstackcash:${this.authPass}`
72
- const base64Credential = Buffer.from(combined).toString('base64')
73
- this.authToken = `Basic ${base64Credential}`
73
+ // x402 payment configuration
74
+ // If a WIF private key is provided, enable x402 automatic payment handling
75
+ this.wif = ''
76
+ if (config && config.wif && config.wif !== '') {
77
+ this.wif = config.wif
78
+ } else if (process.env.BCHJSWIF && process.env.BCHJSWIF !== '') {
79
+ this.wif = process.env.BCHJSWIF
74
80
  }
81
+ this.paymentAmountSats = (config && config.paymentAmountSats) || 2000 * 10
82
+ this.bchServerURL = (config && config.bchServerURL) || 'https://free-bch.fullstack.cash'
75
83
 
76
84
  const libConfig = {
77
85
  restURL: this.restURL,
78
- apiToken: this.apiToken,
79
86
  authToken: this.authToken
80
87
  }
81
88
 
82
- // console.log(`apiToken: ${this.apiToken}`)
89
+ // If WIF is provided, create an axios instance with x402 payment interceptor
90
+ // Otherwise, let sub-modules use their own axios import for backwards compatibility
91
+ if (this.wif) {
92
+ let axiosInstance = axios.create({
93
+ baseURL: this.restURL,
94
+ headers: {
95
+ authorization: this.authToken
96
+ }
97
+ })
98
+
99
+ const signer = createSigner(this.wif, this.paymentAmountSats)
100
+ axiosInstance = withPaymentInterceptor(
101
+ axiosInstance,
102
+ signer,
103
+ { bchServerURL: this.bchServerURL }
104
+ )
105
+
106
+ libConfig.axios = axiosInstance
107
+ }
83
108
 
84
109
  // ElectrumX indexer
85
110
  this.Electrumx = new Electrumx(libConfig)
@@ -117,7 +142,15 @@ class BCHJS {
117
142
  this.eCash = new Ecash()
118
143
 
119
144
  this.PsfSlpIndexer = new PsfSlpIndexer(libConfig)
145
+
146
+ // Expose x402 helper functions for advanced use cases
147
+ this.x402 = {
148
+ createSigner,
149
+ withPaymentInterceptor,
150
+ createPaymentHeader,
151
+ selectPaymentRequirements
152
+ }
120
153
  }
121
154
  }
122
155
 
123
- module.exports = BCHJS
156
+ export default BCHJS
@@ -1,13 +1,12 @@
1
- const Bitcoin = require('@psf/bitcoincashjs-lib')
2
- const sb = require('satoshi-bitcoin')
3
- const bitcoinMessage = require('bitcoinjs-message')
4
- const bs58 = require('bs58')
5
- const bip21 = require('@psf/bip21')
6
- const coininfo = require('@psf/coininfo')
7
- const bip38 = require('bip38')
8
- const wif = require('wif')
9
-
10
- const Buffer = require('safe-buffer').Buffer
1
+ import Bitcoin from '@psf/bitcoincashjs-lib'
2
+ import sb from 'satoshi-bitcoin'
3
+ import bitcoinMessage from 'bitcoinjs-message'
4
+ import bs58 from 'bs58'
5
+ import bip21 from '@psf/bip21'
6
+ import coininfo from '@psf/coininfo'
7
+ import bip38 from 'bip38'
8
+ import wif from 'wif'
9
+ import { Buffer } from 'safe-buffer'
11
10
 
12
11
  class BitcoinCash {
13
12
  constructor (address) {
@@ -519,4 +518,4 @@ class BitcoinCash {
519
518
  }
520
519
  }
521
520
 
522
- module.exports = BitcoinCash
521
+ export default BitcoinCash