psf-bch-api 7.3.6 → 7.3.7

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.
package/README.md CHANGED
@@ -1,30 +1,247 @@
1
1
  # psf-bch-api
2
2
 
3
- This is a REST API for communicating with Bitcoin Cash infrastructure. It replaces [bch-api](https://github.com/Permissionless-Software-Foundation/bch-api), and it implements [x402-bch protocol](https://github.com/x402-bch/x402-bch) to handle payments to access the API.
3
+ [![License](https://img.shields.io/npm/l/@psf/bch-js)](https://github.com/Permissionless-Software-Foundation/psf-bch-api/blob/master/LICENSE.md)
4
+ [![js-standard-style](https://img.shields.io/badge/javascript-standard%20code%20style-green.svg?style=flat-square)](https://github.com/feross/standard)
4
5
 
5
- ## License
6
+ This is a REST API server for communicating with Bitcoin Cash (BCH) blockchain infrastructure. It is written in node.js JavaScript using the [Express.js](https://expressjs.com/) framework and follows the [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) design pattern. It replaces the legacy [bch-api](https://github.com/Permissionless-Software-Foundation/bch-api) and implements the [x402-bch protocol](https://github.com/x402-bch/x402-bch) for optional per-call payments.
6
7
 
7
- [MIT](./LICENSE.md)
8
+ psf-bch-api is the heart of the [Cash Stack](https://cashstack.info), a full software stack for building blockchain-based applications. It creates a single web2 REST API interface that abstracts away the complexity of the underlying blockchain infrastructure, so that application developers can interact with the blockchain through simple HTTP calls.
9
+
10
+ ![psf-bch-api software stack](./bch-api-dependency-graph.png)
11
+
12
+ psf-bch-api depends on three pieces of back end infrastructure:
13
+
14
+ - **[BCHN Full Node](https://cashstack.info/docs/back-end/bchn-full-node)** - the base blockchain node that validates transactions and blocks.
15
+ - **[Fulcrum Indexer](https://cashstack.info/docs/back-end/fulcrum-indexer)** - an address indexer that tracks balances, transaction histories, and UTXOs.
16
+ - **[SLP Token Indexer](https://cashstack.info/docs/back-end/slp-indexer/slp-indexer-software)** - tracks all SLP tokens on the blockchain.
17
+
18
+ Front-end applications interact with psf-bch-api through libraries such as [bch-js](https://github.com/Permissionless-Software-Foundation/bch-js) or [bch-consumer](https://www.npmjs.com/package/bch-consumer).
19
+
20
+ High-level documentation about the full Cash Stack is available at [CashStack.info](https://cashstack.info). Interactive API reference documentation is served by the running server at its root URL (e.g. `http://localhost:5942/`), and a live version can be found at [bch.fullstack.cash](https://bch.fullstack.cash/).
21
+
22
+ ## The .env File
23
+
24
+ All runtime configuration is driven by a `.env` file in the project root. An example is provided at `.env-example`. To get started:
25
+
26
+ `cp .env-example .env`
27
+
28
+ Then edit `.env` to match your environment. The file is organized into two sections:
29
+
30
+ ### Infrastructure Setup
31
+
32
+ These variables tell psf-bch-api where to find the back end services it depends on:
33
+
34
+ - `RPC_BASEURL` - URL of the BCHN full node JSON-RPC interface. Default: `http://127.0.0.1:8332`
35
+ - `RPC_USERNAME` - RPC username for the full node.
36
+ - `RPC_PASSWORD` - RPC password for the full node.
37
+ - `FULCRUM_API` - URL of the Fulcrum indexer REST API.
38
+ - `SLP_INDEXER_API` - URL of the SLP Token Indexer REST API.
39
+ - `LOCAL_RESTURL` - The REST API URL used internally for wallet operations. Default: `http://127.0.0.1:5942/v6/`
40
+
41
+ ### Access Control Settings
42
+
43
+ These variables control who can access the API and how they pay for it. The three access-control use cases are described in detail in the [Access Control](#access-control) section below.
44
+
45
+ - `PORT` - Port the server listens on. Default: `5942`
46
+ - `X402_ENABLED` - Enable x402-bch per-call payment middleware. Default: `true`
47
+ - `SERVER_BCH_ADDRESS` - BCH address that receives x402 payments. Default: `bitcoincash:qqsrke9lh257tqen99dkyy2emh4uty0vky9y0z0lsr`
48
+ - `FACILITATOR_URL` - URL of the x402-bch facilitator service. Default: `http://localhost:4345/facilitator`
49
+ - `X402_PRICE_SAT` - Price in satoshis charged per API call via x402. Default: `200`
50
+ - `USE_BASIC_AUTH` - Enable Bearer token authentication middleware. Default: `false`
51
+ - `BASIC_AUTH_TOKEN` - The expected Bearer token value.
52
+
53
+ ## Access Control
54
+
55
+ psf-bch-api supports three major access-control configurations. Which one you choose depends on your deployment scenario. The behavior is controlled entirely by the `X402_ENABLED` and `USE_BASIC_AUTH` environment variables.
56
+
57
+ ### 1. No Rate Limits (Open Access)
58
+
59
+ Set both access-control flags to `false`:
60
+
61
+ ```
62
+ X402_ENABLED=false
63
+ USE_BASIC_AUTH=false
64
+ ```
65
+
66
+ All API endpoints are publicly accessible without any authentication or payment. This is the simplest configuration, ideal for **local development** or **private, trusted networks** where access control is handled at the network level (e.g. behind a firewall or VPN).
67
+
68
+ ### 2. Bearer Token Authentication
69
+
70
+ Set `USE_BASIC_AUTH=true` and `X402_ENABLED=false`:
71
+
72
+ ```
73
+ X402_ENABLED=false
74
+ USE_BASIC_AUTH=true
75
+ BASIC_AUTH_TOKEN=my-secret-token
76
+ ```
77
+
78
+ Every API request (except `/health` and `/`) must include an `Authorization` header with a valid Bearer token:
79
+
80
+ ```
81
+ Authorization: Bearer my-secret-token
82
+ ```
83
+
84
+ Requests without a valid token receive an HTTP `401 Unauthorized` response. This is the best option when you want to **restrict access to a known set of users or services** (e.g. an organization's internal apps) without requiring cryptocurrency payments.
85
+
86
+ ### 3. x402-bch Per-Call Payments
87
+
88
+ Set `X402_ENABLED=true`:
89
+
90
+ ```
91
+ X402_ENABLED=true
92
+ SERVER_BCH_ADDRESS=bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d
93
+ FACILITATOR_URL=http://localhost:4345/facilitator
94
+ X402_PRICE_SAT=200
95
+ ```
96
+
97
+ Every API call under the `/v6` prefix requires a BCH micro-payment. When a request arrives without a valid `X-PAYMENT` header, the server responds with HTTP `402 Payment Required` and includes the payment details. Client libraries that support the x402-bch protocol (like [bch-js](https://github.com/Permissionless-Software-Foundation/bch-js)) can handle payments automatically.
98
+
99
+ This is the right choice for **public, monetized APIs** where you want to charge per call.
100
+
101
+ #### Combined: x402 + Bearer Token
102
+
103
+ You can enable both at the same time:
104
+
105
+ ```
106
+ X402_ENABLED=true
107
+ USE_BASIC_AUTH=true
108
+ BASIC_AUTH_TOKEN=my-secret-token
109
+ ```
110
+
111
+ In this mode, requests that present a valid Bearer token bypass the x402 payment requirement. All other requests must pay. This allows you to give **free access to trusted clients** (via the Bearer token) while still **monetizing public access** via x402.
112
+
113
+ ## Development
114
+
115
+ This is a standard node.js project. To set up a development environment:
116
+
117
+ 1. Clone the repository:
118
+
119
+ `git clone https://github.com/Permissionless-Software-Foundation/psf-bch-api && cd psf-bch-api`
120
+
121
+ 2. Install dependencies:
8
122
 
9
- ## x402-bch Payments
123
+ `npm install`
10
124
 
11
- All REST endpoints exposed under the `/v6` prefix are protected by the [`x402-bch-express`](https://www.npmjs.com/package/x402-bch-express) middleware. Each API call requires a BCH payment authorization for **200 satoshis**. The middleware advertises payment requirements via HTTP 402 responses and validates incoming `X-PAYMENT` headers with a configured Facilitator.
125
+ 3. Create your configuration file:
12
126
 
13
- ### Configuration
127
+ `cp .env-example .env`
14
128
 
15
- Environment variables control the payment flow:
129
+ 4. Edit `.env` to point to your back end infrastructure (full node, Fulcrum, SLP indexer). For local development you will likely want to disable access control:
16
130
 
17
- - `X402_ENABLED` — set to `false` (case-insensitive) to disable the middleware. Defaults to enabled.
18
- - `SERVER_BCH_ADDRESS` — BCH cash address that receives funding transactions. Defaults to `bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d`.
19
- - `FACILITATOR_URL` — Root URL of the facilitator service (e.g., `http://localhost:4345/facilitator`).
20
- - `X402_PRICE_SAT` — Optional; override the satoshi price per call (defaults to `200`).
131
+ ```
132
+ X402_ENABLED=false
133
+ USE_BASIC_AUTH=false
134
+ ```
21
135
 
22
- When `X402_ENABLED=false`, the server continues to operate without payment headers for local development or trusted deployments.
136
+ 5. Start the server:
23
137
 
24
- ### Manual Verification
138
+ `npm start`
25
139
 
26
- 1. Start or point to an `x402-bch` facilitator service (the example facilitator listens at `http://localhost:4345/facilitator`).
27
- 2. Run the API server with the default configuration: `npm start`.
28
- 3. Call a protected endpoint without an `X-PAYMENT` header, e.g. `curl -i http://localhost:5942/v6/full-node/control/getNetworkInfo`. The server will respond with HTTP `402` and include payment requirements.
29
- 4. Restart the server with `X402_ENABLED=false npm start` to confirm that the same request now bypasses the middleware (useful for local development without payments).
140
+ The server will start on port `5942` by default (or whatever you set in `PORT`). API documentation is available at `http://localhost:5942/`.
30
141
 
142
+ ### Generating API Docs
143
+
144
+ The API reference documentation is generated by [apiDoc](https://apidocjs.com/) from inline annotations in the source code. To regenerate:
145
+
146
+ `npm run docs`
147
+
148
+ The output is written to the `docs/` directory and served by the running server at its root URL.
149
+
150
+ A live version can be found at [bch.fullstack.cash](https://bch.fullstack.cash/).
151
+
152
+ ## Production (Docker)
153
+
154
+ A Docker setup is provided in the `production/docker/` directory for production deployments. The target OS is Ubuntu Linux.
155
+
156
+ 1. Install [Docker and Docker Compose](https://docs.docker.com/engine/install/ubuntu/).
157
+
158
+ 2. Navigate to the Docker directory:
159
+
160
+ `cd production/docker`
161
+
162
+ 3. Create and configure the `.env` file. An example is provided:
163
+
164
+ `cp .env-example .env`
165
+
166
+ Edit `.env` to match your production infrastructure. Note that inside a Docker container, `localhost` refers to the container itself. Use `172.17.0.1` (the default Docker bridge gateway) to reach services running on the host machine:
167
+
168
+ ```
169
+ RPC_BASEURL=http://172.17.0.1:8332
170
+ FULCRUM_API=http://172.17.0.1:3001/v1
171
+ SLP_INDEXER_API=http://172.17.0.1:5010
172
+ ```
173
+
174
+ 4. Build the Docker image:
175
+
176
+ `docker-compose build --no-cache`
177
+
178
+ 5. Start the container:
179
+
180
+ `docker-compose up -d`
181
+
182
+ The container maps host port `5942` to container port `5942`. The `.env` file is mounted into the container as a volume, so you can update configuration without rebuilding.
183
+
184
+ To view logs:
185
+
186
+ `docker logs -f psf-bch-api`
187
+
188
+ To stop the container:
189
+
190
+ `docker-compose down`
191
+
192
+ A helper script `cleanup-images.sh` is provided to remove dangling Docker images after rebuilds.
193
+
194
+ ## Testing
195
+
196
+ The project includes both unit tests and integration tests. Tests use [Mocha](https://mochajs.org/) as the test runner, [Chai](https://www.chaijs.com/) for assertions, and [Sinon](https://sinonjs.org/) for mocking. Code coverage is provided by [c8](https://github.com/bcoe/c8).
197
+
198
+ ### Unit Tests
199
+
200
+ Unit tests are located in `test/unit/` and cover adapters, controllers, and use cases. They do not require any running infrastructure. To run:
201
+
202
+ `npm test`
203
+
204
+ This will first lint the code with [Standard](https://standardjs.com/), then execute all unit tests with code coverage.
205
+
206
+ To generate an HTML coverage report:
207
+
208
+ `npm run coverage`
209
+
210
+ The report is written to the `coverage/` directory.
211
+
212
+ ### Integration Tests
213
+
214
+ Integration tests are located in `test/integration/` and require the back end infrastructure (full node, Fulcrum, SLP indexer) to be running. To run:
215
+
216
+ `npm run test:integration`
217
+
218
+ Integration tests have a 25-second timeout per test to accommodate network calls.
219
+
220
+ ## Configuration Reference
221
+
222
+ All configuration values are read from environment variables (via the `.env` file). The complete list:
223
+
224
+ - `PORT` - Server listen port. Default: `5942`
225
+ - `NODE_ENV` - Environment (`development` or `production`). Default: `development`
226
+ - `API_PREFIX` - URL prefix for all REST endpoints. Default: `/v6`
227
+ - `LOG_LEVEL` - Winston logging level. Default: `info`
228
+ - `RPC_BASEURL` - Full node JSON-RPC URL. Default: `http://127.0.0.1:8332`
229
+ - `RPC_USERNAME` - Full node RPC username.
230
+ - `RPC_PASSWORD` - Full node RPC password.
231
+ - `RPC_TIMEOUT_MS` - Full node RPC request timeout in ms. Default: `15000`
232
+ - `FULCRUM_API` - Fulcrum indexer REST API URL.
233
+ - `FULCRUM_TIMEOUT_MS` - Fulcrum API request timeout in ms. Default: `15000`
234
+ - `SLP_INDEXER_API` - SLP Token Indexer REST API URL.
235
+ - `SLP_INDEXER_TIMEOUT_MS` - SLP Indexer API request timeout in ms. Default: `15000`
236
+ - `LOCAL_RESTURL` - Internal REST URL for wallet operations. Default: `http://127.0.0.1:5942/v6/`
237
+ - `IPFS_GATEWAY` - IPFS gateway hostname. Default: `p2wdb-gateway-678.fullstack.cash`
238
+ - `X402_ENABLED` - Enable x402-bch payment middleware. Default: `true`
239
+ - `SERVER_BCH_ADDRESS` - BCH address for x402 payments. Default: `bitcoincash:qqsrke9lh257tqen99dkyy2emh4uty0vky9y0z0lsr`
240
+ - `FACILITATOR_URL` - x402-bch facilitator service URL. Default: `http://localhost:4345/facilitator`
241
+ - `X402_PRICE_SAT` - Satoshis charged per API call via x402. Default: `200`
242
+ - `USE_BASIC_AUTH` - Enable Bearer token authentication. Default: `false`
243
+ - `BASIC_AUTH_TOKEN` - Expected Bearer token value.
244
+
245
+ ## License
246
+
247
+ [MIT](./LICENSE.md)
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "psf-bch-api",
3
- "version": "7.3.6",
3
+ "version": "7.3.7",
4
4
  "main": "psf-bch-api.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -146,10 +146,13 @@ class FulcrumUseCases {
146
146
  if (response.transactions && Array.isArray(response.transactions)) {
147
147
  // Use bearer token from request if provided, otherwise use the default bchjs instance
148
148
  let bchjsInstance = this.bchjs
149
- console.log('getTransactionsBulk() bearerToken: ', bearerToken)
149
+
150
+ // console.log('getTransactionsBulk() bearerToken: ', bearerToken)
150
151
  if (bearerToken) {
151
152
  // Create a temporary bchjs instance with the bearer token from the request
152
- const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
153
+ const restURL = config.restURL
154
+
155
+ // console.log('getTransactionsBulk() restURL: ', restURL)
153
156
  bchjsInstance = new BCHJS({
154
157
  restURL,
155
158
  bearerToken