22 Commits
Author SHA1 Message Date
Chris Troutner cc78aad785 Merge pull request #22 from Permissionless-Software-Foundation/ct-unstable
fix(logs): Reducing log noise
2026-03-16 20:52:28 -07:00
Chris Troutner 91f99405c1 fix(logs): Reducing log noise 2026-03-16 20:51:33 -07:00
Chris Troutner a959416b10 Merge pull request #21 from Permissionless-Software-Foundation/ct-unstable
fix(Docker): Persisting winston logs
2026-03-16 20:08:46 -07:00
Chris Troutner 562a196998 fix(Docker): Persisting winston logs 2026-03-16 20:08:04 -07:00
Chris Troutner e006a7ad86 Merge pull request #20 from Permissionless-Software-Foundation/ct-unstable
fix(timouts): Adjusting network timeout settings
2026-03-12 12:46:21 -07:00
Chris Troutner f36cd2a8aa fix(timouts): Adjusting network timeout settings 2026-03-12 12:45:33 -07:00
Chris Troutner 8b2cf64f1b Using ports for new slp indexer 2026-02-08 13:27:19 -08:00
Chris Troutner 95ae06fada Merge pull request #19 from Permissionless-Software-Foundation/ct-unstable
Updating README
2026-02-08 13:14:00 -07:00
Chris Troutner 188dc939b3 Adding link to live API documentation 2026-02-08 13:13:19 -07:00
Chris Troutner 7d566beea3 Adding link to live API documentation 2026-02-08 13:12:41 -07:00
Chris Troutner 6c42f47e15 fix(README): Expanding README 2026-02-08 13:07:23 -07:00
Chris Troutner d19a9e64cd Adding stack image to README 2026-02-08 12:57:06 -07:00
Chris Troutner 7c96d72f7b Removing debug settings 2026-02-04 15:12:24 -07:00
Chris Troutner 7f1770ab70 Refining URL lookup 2026-02-04 15:06:55 -07:00
Chris Troutner b25f55ce6a Adding more debugging 2026-02-04 14:58:20 -07:00
Chris Troutner 6e32723b29 fix(getTransactionsBulk()): Adding bearer token debugging 2026-02-04 14:47:12 -07:00
Chris Troutner 3b08fc3b62 Merge pull request #18 from Permissionless-Software-Foundation/ct-unstable
fix(auth token): Passing auth token to internal bch-js
2026-02-04 14:32:04 -07:00
Chris Troutner 2c1f02d6c7 fix(auth token): Passing auth token to internal bch-js 2026-02-04 14:31:06 -07:00
Chris Troutner 6cf03ca0fe Merge pull request #17 from Permissionless-Software-Foundation/ct-unstable
fix(Fulcrum): Handling different URL and auth tokens
2026-02-04 14:10:58 -07:00
Chris Troutner 5ea7bc9c29 fix(Fulcrum): Handling different URL and auth tokens 2026-02-04 14:09:12 -07:00
Chris Troutner 39021aba46 Merge pull request #16 from Permissionless-Software-Foundation/ct-unstable
fix(node v22): Updating dependencies & testing node.js v22
2026-01-16 11:10:38 -07:00
Chris Troutner de34547951 fix(node v22): Updating dependencies & testing node.js v22 2026-01-16 11:09:54 -07:00
14 changed files with 403 additions and 84 deletions
+241 -24
View File
@@ -1,30 +1,247 @@
# psf-bch-api
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.
[![License](https://img.shields.io/npm/l/@psf/bch-js)](https://github.com/Permissionless-Software-Foundation/psf-bch-api/blob/master/LICENSE.md)
[![js-standard-style](https://img.shields.io/badge/javascript-standard%20code%20style-green.svg?style=flat-square)](https://github.com/feross/standard)
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.
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.
![psf-bch-api software stack](./bch-api-dependency-graph.png)
psf-bch-api depends on three pieces of back end infrastructure:
- **[BCHN Full Node](https://cashstack.info/docs/back-end/bchn-full-node)** - the base blockchain node that validates transactions and blocks.
- **[Fulcrum Indexer](https://cashstack.info/docs/back-end/fulcrum-indexer)** - an address indexer that tracks balances, transaction histories, and UTXOs.
- **[SLP Token Indexer](https://cashstack.info/docs/back-end/slp-indexer/slp-indexer-software)** - tracks all SLP tokens on the blockchain.
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).
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/).
## The .env File
All runtime configuration is driven by a `.env` file in the project root. An example is provided at `.env-example`. To get started:
`cp .env-example .env`
Then edit `.env` to match your environment. The file is organized into two sections:
### Infrastructure Setup
These variables tell psf-bch-api where to find the back end services it depends on:
- `RPC_BASEURL` - URL of the BCHN full node JSON-RPC interface. Default: `http://127.0.0.1:8332`
- `RPC_USERNAME` - RPC username for the full node.
- `RPC_PASSWORD` - RPC password for the full node.
- `FULCRUM_API` - URL of the Fulcrum indexer REST API.
- `SLP_INDEXER_API` - URL of the SLP Token Indexer REST API.
- `LOCAL_RESTURL` - The REST API URL used internally for wallet operations. Default: `http://127.0.0.1:5942/v6/`
### Access Control Settings
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.
- `PORT` - Port the server listens on. Default: `5942`
- `X402_ENABLED` - Enable x402-bch per-call payment middleware. Default: `true`
- `SERVER_BCH_ADDRESS` - BCH address that receives x402 payments. Default: `bitcoincash:qqsrke9lh257tqen99dkyy2emh4uty0vky9y0z0lsr`
- `FACILITATOR_URL` - URL of the x402-bch facilitator service. Default: `http://localhost:4345/facilitator`
- `X402_PRICE_SAT` - Price in satoshis charged per API call via x402. Default: `200`
- `USE_BASIC_AUTH` - Enable Bearer token authentication middleware. Default: `false`
- `BASIC_AUTH_TOKEN` - The expected Bearer token value.
## Access Control
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.
### 1. No Rate Limits (Open Access)
Set both access-control flags to `false`:
```
X402_ENABLED=false
USE_BASIC_AUTH=false
```
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).
### 2. Bearer Token Authentication
Set `USE_BASIC_AUTH=true` and `X402_ENABLED=false`:
```
X402_ENABLED=false
USE_BASIC_AUTH=true
BASIC_AUTH_TOKEN=my-secret-token
```
Every API request (except `/health` and `/`) must include an `Authorization` header with a valid Bearer token:
```
Authorization: Bearer my-secret-token
```
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.
### 3. x402-bch Per-Call Payments
Set `X402_ENABLED=true`:
```
X402_ENABLED=true
SERVER_BCH_ADDRESS=bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d
FACILITATOR_URL=http://localhost:4345/facilitator
X402_PRICE_SAT=200
```
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.
This is the right choice for **public, monetized APIs** where you want to charge per call.
#### Combined: x402 + Bearer Token
You can enable both at the same time:
```
X402_ENABLED=true
USE_BASIC_AUTH=true
BASIC_AUTH_TOKEN=my-secret-token
```
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.
## Development
This is a standard node.js project. To set up a development environment:
1. Clone the repository:
`git clone https://github.com/Permissionless-Software-Foundation/psf-bch-api && cd psf-bch-api`
2. Install dependencies:
`npm install`
3. Create your configuration file:
`cp .env-example .env`
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:
```
X402_ENABLED=false
USE_BASIC_AUTH=false
```
5. Start the server:
`npm start`
The server will start on port `5942` by default (or whatever you set in `PORT`). API documentation is available at `http://localhost:5942/`.
### Generating API Docs
The API reference documentation is generated by [apiDoc](https://apidocjs.com/) from inline annotations in the source code. To regenerate:
`npm run docs`
The output is written to the `docs/` directory and served by the running server at its root URL.
A live version can be found at [bch.fullstack.cash](https://bch.fullstack.cash/).
## Production (Docker)
A Docker setup is provided in the `production/docker/` directory for production deployments. The target OS is Ubuntu Linux.
1. Install [Docker and Docker Compose](https://docs.docker.com/engine/install/ubuntu/).
2. Navigate to the Docker directory:
`cd production/docker`
3. Create and configure the `.env` file. An example is provided:
`cp .env-example .env`
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:
```
RPC_BASEURL=http://172.17.0.1:8332
FULCRUM_API=http://172.17.0.1:3001/v1
SLP_INDEXER_API=http://172.17.0.1:5010
```
4. Build the Docker image:
`docker-compose build --no-cache`
5. Start the container:
`docker-compose up -d`
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.
To view logs:
`docker logs -f psf-bch-api`
To stop the container:
`docker-compose down`
A helper script `cleanup-images.sh` is provided to remove dangling Docker images after rebuilds.
## Testing
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).
### Unit Tests
Unit tests are located in `test/unit/` and cover adapters, controllers, and use cases. They do not require any running infrastructure. To run:
`npm test`
This will first lint the code with [Standard](https://standardjs.com/), then execute all unit tests with code coverage.
To generate an HTML coverage report:
`npm run coverage`
The report is written to the `coverage/` directory.
### Integration Tests
Integration tests are located in `test/integration/` and require the back end infrastructure (full node, Fulcrum, SLP indexer) to be running. To run:
`npm run test:integration`
Integration tests have a 25-second timeout per test to accommodate network calls.
## Configuration Reference
All configuration values are read from environment variables (via the `.env` file). The complete list:
- `PORT` - Server listen port. Default: `5942`
- `NODE_ENV` - Environment (`development` or `production`). Default: `development`
- `API_PREFIX` - URL prefix for all REST endpoints. Default: `/v6`
- `LOG_LEVEL` - Winston logging level. Default: `info`
- `RPC_BASEURL` - Full node JSON-RPC URL. Default: `http://127.0.0.1:8332`
- `RPC_USERNAME` - Full node RPC username.
- `RPC_PASSWORD` - Full node RPC password.
- `RPC_TIMEOUT_MS` - Full node RPC request timeout in ms. Default: `15000`
- `FULCRUM_API` - Fulcrum indexer REST API URL.
- `FULCRUM_TIMEOUT_MS` - Fulcrum API request timeout in ms. Default: `15000`
- `SLP_INDEXER_API` - SLP Token Indexer REST API URL.
- `SLP_INDEXER_TIMEOUT_MS` - SLP Indexer API request timeout in ms. Default: `15000`
- `LOCAL_RESTURL` - Internal REST URL for wallet operations. Default: `http://127.0.0.1:5942/v6/`
- `IPFS_GATEWAY` - IPFS gateway hostname. Default: `p2wdb-gateway-678.fullstack.cash`
- `X402_ENABLED` - Enable x402-bch payment middleware. Default: `true`
- `SERVER_BCH_ADDRESS` - BCH address for x402 payments. Default: `bitcoincash:qqsrke9lh257tqen99dkyy2emh4uty0vky9y0z0lsr`
- `FACILITATOR_URL` - x402-bch facilitator service URL. Default: `http://localhost:4345/facilitator`
- `X402_PRICE_SAT` - Satoshis charged per API call via x402. Default: `200`
- `USE_BASIC_AUTH` - Enable Bearer token authentication. Default: `false`
- `BASIC_AUTH_TOKEN` - Expected Bearer token value.
## License
[MIT](./LICENSE.md)
## x402-bch Payments
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.
### Configuration
Environment variables control the payment flow:
- `X402_ENABLED` — set to `false` (case-insensitive) to disable the middleware. Defaults to enabled.
- `SERVER_BCH_ADDRESS` — BCH cash address that receives funding transactions. Defaults to `bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d`.
- `FACILITATOR_URL` — Root URL of the facilitator service (e.g., `http://localhost:4345/facilitator`).
- `X402_PRICE_SAT` — Optional; override the satoshi price per call (defaults to `200`).
When `X402_ENABLED=false`, the server continues to operate without payment headers for local development or trusted deployments.
### Manual Verification
1. Start or point to an `x402-bch` facilitator service (the example facilitator listens at `http://localhost:4345/facilitator`).
2. Run the API server with the default configuration: `npm start`.
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.
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).
Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

+5
View File
@@ -246,6 +246,11 @@ class Server {
wlogger.info(`Server started on port ${this.config.port}`)
})
// Explicit timeout settings reduce stale keep-alive socket reuse races.
this.server.keepAliveTimeout = this.config.serverKeepAliveTimeoutMs
this.server.headersTimeout = this.config.serverHeadersTimeoutMs
this.server.requestTimeout = this.config.serverRequestTimeoutMs
this.server.on('error', (err) => {
console.error('Server error:', err)
wlogger.error('Server error:', err)
+21
View File
@@ -0,0 +1,21 @@
# 2026-03-16 Update Log
## Summary
Reduced noisy error logging for common SLP transaction misses in the `/v6/slp/txid` path.
## Changes Made
- Updated `src/use-cases/slp-use-cases.js` in `getTxid()`:
- Added a guard for expected missing-record errors (`404` + `Key not found in database`).
- Skips `wlogger.error()` for that specific, common case.
- Still rethrows the error so API response behavior is unchanged.
- Updated `src/controllers/rest-api/slp/controller.js` in `handleError()`:
- Added the same guard to suppress duplicate error-level logs for the same expected case.
- Keeps normal error logging for all other errors.
## Outcome
- The common "Key not found in database" case no longer pollutes error logs.
- Unexpected failures continue to be logged at error level.
- Client-facing status and error message behavior remains unchanged.
+47 -47
View File
@@ -9,12 +9,12 @@
"version": "7.0.0",
"license": "MIT",
"dependencies": {
"@psf/bch-js": "7.1.7",
"@psf/bch-js": "7.1.14",
"axios": "1.7.7",
"cors": "2.8.5",
"dotenv": "16.3.1",
"express": "5.1.0",
"minimal-slp-wallet": "7.1.2",
"minimal-slp-wallet": "7.1.5",
"psffpp": "1.2.1",
"slp-token-media": "1.2.10",
"winston": "3.11.0",
@@ -513,9 +513,9 @@
}
},
"node_modules/@eslint-community/eslint-utils": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.0.tgz",
"integrity": "sha512-ayVFHdtZ+hsq1t2Dy24wCmGXGe4q9Gu3smhLYALJrr473ZH27MsnSL+LKUlimp4BWJqMDMLmPpx/Q9R3OAlL4g==",
"version": "4.9.1",
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
"integrity": "sha512-phrYmNiYppR7znFEdqgfWHXR6NCkZEK7hwWDHZUjit/2/U0r6XvkDl0SYnoM51Hq7FhCGdLDT6zxCCOY1hexsQ==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -858,9 +858,9 @@
}
},
"node_modules/@psf/bch-js": {
"version": "7.1.7",
"resolved": "https://registry.npmjs.org/@psf/bch-js/-/bch-js-7.1.7.tgz",
"integrity": "sha512-qEgSyhtSJZ1QbxzxGWnN+DIgD8cq2MUTZ4vg2D5Bpskrm956QCAF32nDZnKVBHl0VTMnRRgoJ8yxBrUqDpYEHg==",
"version": "7.1.14",
"resolved": "https://registry.npmjs.org/@psf/bch-js/-/bch-js-7.1.14.tgz",
"integrity": "sha512-B/NXuxSoOHgMR0tHyY8qpXN3VDATeBhd6qhasJsiEjb22IGIjsXsdJ0oZR2DLnn/1UA+94+XJNyVz2UlcaqEpw==",
"license": "MIT",
"dependencies": {
"@chris.troutner/bip32-utils": "1.0.5",
@@ -886,7 +886,7 @@
"slp-mdm": "0.0.7",
"slp-parser": "0.0.4",
"wif": "2.0.6",
"x402-bch-axios": "2.1.1"
"x402-bch-axios": "2.2.1"
}
},
"node_modules/@psf/bch-js/node_modules/axios": {
@@ -1064,9 +1064,9 @@
"license": "MIT"
},
"node_modules/@types/node": {
"version": "25.0.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.3.tgz",
"integrity": "sha512-W609buLVRVmeW693xKfzHeIV6nJGGz98uCPfeXI1ELMLXVeKYZ9m15fAMSaUPBHYLGFsVRcMmSCksQOrZV9BYA==",
"version": "25.0.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.9.tgz",
"integrity": "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw==",
"license": "MIT",
"dependencies": {
"undici-types": "~7.16.0"
@@ -1715,9 +1715,9 @@
}
},
"node_modules/baseline-browser-mapping": {
"version": "2.9.11",
"resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.9.11.tgz",
"integrity": "sha512-Sg0xJUNDU1sJNGdfGWhVHX0kkZ+HWcvmVymJbj6NSgZZmW/8S9Y2HQ5euytnIgakgxN6papOAWiwDo1ctFDcoQ==",
"version": "2.9.15",
"resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.9.15.tgz",
"integrity": "sha512-kX8h7K2srmDyYnXRIppo4AH/wYgzWVCs+eKr3RusRSQ5PvRYoEFmR/I0PbdTjKFAoKqp5+kbxnNTFO9jOfSVJg==",
"license": "Apache-2.0",
"bin": {
"baseline-browser-mapping": "dist/cli.js"
@@ -2578,9 +2578,9 @@
"license": "MIT"
},
"node_modules/body-parser": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.1.tgz",
"integrity": "sha512-nfDwkulwiZYQIGwxdy0RUmowMhKcFVcYXUU7m4QlKYim1rUtg83xm2yjZ40QjDuc291AJjjeSc9b++AWHSgSHw==",
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.2.tgz",
"integrity": "sha512-oP5VkATKlNwcgvxi0vM0p/D3n2C3EReYVX+DNYs5TjZFn/oQt2j+4sVJtSMr18pdRr8wjTcBl6LoV+FUwzPmNA==",
"license": "MIT",
"dependencies": {
"bytes": "^3.1.2",
@@ -2589,7 +2589,7 @@
"http-errors": "^2.0.0",
"iconv-lite": "^0.7.0",
"on-finished": "^2.4.1",
"qs": "^6.14.0",
"qs": "^6.14.1",
"raw-body": "^3.0.1",
"type-is": "^2.0.1"
},
@@ -2602,9 +2602,9 @@
}
},
"node_modules/body-parser/node_modules/iconv-lite": {
"version": "0.7.1",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.1.tgz",
"integrity": "sha512-2Tth85cXwGFHfvRgZWszZSvdo+0Xsqmw8k8ZwxScfcBneNUraK+dxRxRm24nszx80Y0TVio8kKLt5sLE7ZCLlw==",
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.2.tgz",
"integrity": "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
@@ -2879,9 +2879,9 @@
}
},
"node_modules/caniuse-lite": {
"version": "1.0.30001762",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001762.tgz",
"integrity": "sha512-PxZwGNvH7Ak8WX5iXzoK1KPZttBXNPuaOvI2ZYU7NrlM+d9Ov+TUvlLOBNGzVXAntMSMMlJPd+jY6ovrVjSmUw==",
"version": "1.0.30001764",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001764.tgz",
"integrity": "sha512-9JGuzl2M+vPL+pz70gtMF9sHdMFbY9FJaQBi186cHKH3pSzDvzoUJUPV6fqiKIMyXbud9ZLg4F3Yza1vJ1+93g==",
"funding": [
{
"type": "opencollective",
@@ -4380,9 +4380,9 @@
}
},
"node_modules/esquery": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.6.0.tgz",
"integrity": "sha512-ca9pw9fomFcKPvFLXhBKUK90ZvGibiGOvRJNbjljY7s7uq/5YO4BOzcYtJqExdx99rF6aAcnRxHmcUHcz6sQsg==",
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.7.0.tgz",
"integrity": "sha512-Ap6G0WQwcU/LHsvLwON1fAQX9Zp0A2Y6Y/cJBl9r/JbW90Zyg4/zbG6zzKa2OTALELarYHmKu0GhpM5EO+7T0g==",
"dev": true,
"license": "BSD-3-Clause",
"dependencies": {
@@ -6475,13 +6475,13 @@
}
},
"node_modules/minimal-slp-wallet": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/minimal-slp-wallet/-/minimal-slp-wallet-7.1.2.tgz",
"integrity": "sha512-aSPrLf4Cl3P/AHrEcw2YTlzaoi2WG5rayZF3ibov6cv6RZj3v8vba3vbodXhL4bt91lQRg7d8FUrd4ks6DmfCg==",
"version": "7.1.5",
"resolved": "https://registry.npmjs.org/minimal-slp-wallet/-/minimal-slp-wallet-7.1.5.tgz",
"integrity": "sha512-dROdawFZJZLvyTRndJi987S+z0cOG9k6coyyTAfkaaQftP+UayjUyjKeQlJVrKAPgIzxhk6EoZKD6AtoGjwQrw==",
"license": "MIT",
"dependencies": {
"@chris.troutner/retry-queue": "1.0.11",
"@psf/bch-js": "7.1.7",
"@psf/bch-js": "7.1.14",
"bch-consumer": "1.6.2",
"bch-donation": "1.1.2",
"crypto-js": "4.0.0"
@@ -7859,9 +7859,9 @@
}
},
"node_modules/raw-body/node_modules/iconv-lite": {
"version": "0.7.1",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.1.tgz",
"integrity": "sha512-2Tth85cXwGFHfvRgZWszZSvdo+0Xsqmw8k8ZwxScfcBneNUraK+dxRxRm24nszx80Y0TVio8kKLt5sLE7ZCLlw==",
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.2.tgz",
"integrity": "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
@@ -9089,9 +9089,9 @@
}
},
"node_modules/terser": {
"version": "5.44.1",
"resolved": "https://registry.npmjs.org/terser/-/terser-5.44.1.tgz",
"integrity": "sha512-t/R3R/n0MSwnnazuPpPNVO60LX0SKL45pyl9YlvxIdkH0Of7D5qM2EVe+yASRIlY5pZ73nclYJfNANGWPwFDZw==",
"version": "5.46.0",
"resolved": "https://registry.npmjs.org/terser/-/terser-5.46.0.tgz",
"integrity": "sha512-jTwoImyr/QbOWFFso3YoU3ik0jBBDJ6JTOQiy/J2YxVJdZCc+5u7skhNwiOR3FQIygFqVUPHl7qbbxtjW2K3Qg==",
"license": "BSD-2-Clause",
"dependencies": {
"@jridgewell/source-map": "^0.3.3",
@@ -9597,9 +9597,9 @@
}
},
"node_modules/watchpack": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.5.0.tgz",
"integrity": "sha512-e6vZvY6xboSwLz2GD36c16+O/2Z6fKvIf4pOXptw2rY9MVwE/TXc6RGqxD3I3x0a28lwBY7DE+76uTPSsBrrCA==",
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.5.1.tgz",
"integrity": "sha512-Zn5uXdcFNIA1+1Ei5McRd+iRzfhENPCe7LeABkJtNulSxjma+l7ltNx55BWZkRlwRnpOgHqxnjyaDgJnNXnqzg==",
"license": "MIT",
"dependencies": {
"glob-to-regexp": "^0.4.1",
@@ -9869,9 +9869,9 @@
}
},
"node_modules/which-typed-array": {
"version": "1.1.19",
"resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.19.tgz",
"integrity": "sha512-rEvr90Bck4WZt9HHFC4DJMsjvu7x+r6bImz0/BrbWb7A2djJ8hnZMrWnHo9F8ssv0OMErasDhftrfROTyqSDrw==",
"version": "1.1.20",
"resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.20.tgz",
"integrity": "sha512-LYfpUkmqwl0h9A2HL09Mms427Q1RZWuOHsukfVcKRq9q95iQxdw0ix1JQrqbcDR9PH1QDwf5Qo8OZb5lksZ8Xg==",
"license": "MIT",
"dependencies": {
"available-typed-arrays": "^1.0.7",
@@ -10025,9 +10025,9 @@
"license": "ISC"
},
"node_modules/x402-bch-axios": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/x402-bch-axios/-/x402-bch-axios-2.1.1.tgz",
"integrity": "sha512-cRUWTpEjwoY7sDVSxS26lSoaxGQ0OxrggQjU+WqUo3aCj00p1TwNeLIxwX6AxX89y4dRPXfuC5Pm/9mxTg058w==",
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/x402-bch-axios/-/x402-bch-axios-2.2.1.tgz",
"integrity": "sha512-N23DpQRA746bqGDTCJYVQ8QL55PoxE6LTSNMFzx5sySsvnBem6H0hQLSf2N9e45O8xPVw3ulSvXncgTcVM8ikw==",
"license": "MIT",
"dependencies": {
"@chris.troutner/retry-queue": "1.0.11",
+2 -2
View File
@@ -15,12 +15,12 @@
"license": "MIT",
"description": "REST API proxy to Bitcoin Cash infrastructure",
"dependencies": {
"@psf/bch-js": "7.1.7",
"@psf/bch-js": "7.1.14",
"axios": "1.7.7",
"cors": "2.8.5",
"dotenv": "16.3.1",
"express": "5.1.0",
"minimal-slp-wallet": "7.1.2",
"minimal-slp-wallet": "7.1.5",
"psffpp": "1.2.1",
"slp-token-media": "1.2.10",
"winston": "3.11.0",
+1
View File
@@ -0,0 +1 @@
+1 -1
View File
@@ -9,7 +9,7 @@ RPC_PASSWORD=password
FULCRUM_API=http://172.17.0.1:3001/v1
# SLP Indexer
SLP_INDEXER_API=http://172.17.0.1:5010
SLP_INDEXER_API=http://172.17.0.1:5020
# REST API URL for wallet operations
LOCAL_RESTURL=http://172.17.0.1:5942/v6
+2
View File
@@ -17,4 +17,6 @@ services:
volumes:
#- ./start-rest2nostr.sh:/home/safeuser/REST2NOSTR/start-rest2nostr.sh
- ./.env:/home/safeuser/.env
- ../data:/home/safeuser/psf-bch-api/production/data
- ../data/logs:/home/safeuser/psf-bch-api/logs
restart: always
+5
View File
@@ -47,6 +47,11 @@ export default {
// Server port
port: parseInt(process.env.PORT, 10) || 5942,
// HTTP server connection lifecycle configuration.
serverKeepAliveTimeoutMs: Number(process.env.SERVER_KEEPALIVE_TIMEOUT_MS || 3000),
serverHeadersTimeoutMs: Number(process.env.SERVER_HEADERS_TIMEOUT_MS || 65000),
serverRequestTimeoutMs: Number(process.env.SERVER_REQUEST_TIMEOUT_MS || 120000),
// Environment
env: process.env.NODE_ENV || 'development',
+25 -2
View File
@@ -424,7 +424,20 @@ class FulcrumRESTController {
const cashAddr = this._validateAndConvertAddress(address)
const result = await this.fulcrumUseCases.getTransactions({ address: cashAddr, allTxs })
// Extract bearer token from request header if present
let bearerToken = null
if (req.headers && req.headers.authorization) {
const parts = req.headers.authorization.split(' ')
if (parts.length === 2 && parts[0] === 'Bearer') {
bearerToken = parts[1]
}
}
const result = await this.fulcrumUseCases.getTransactions({
address: cashAddr,
allTxs,
bearerToken
})
return res.status(200).json(result)
} catch (err) {
return this.handleError(err, res)
@@ -470,9 +483,19 @@ class FulcrumRESTController {
}
}
// Extract bearer token from request header if present
let bearerToken = null
if (req.headers && req.headers.authorization) {
const parts = req.headers.authorization.split(' ')
if (parts.length === 2 && parts[0] === 'Bearer') {
bearerToken = parts[1]
}
}
const result = await this.fulcrumUseCases.getTransactionsBulk({
addresses: validatedAddresses,
allTxs
allTxs,
bearerToken
})
return res.status(200).json(result)
} catch (err) {
+8 -1
View File
@@ -208,7 +208,14 @@ class SlpRESTController {
}
handleError (err, res) {
wlogger.error('Error in SlpRESTController:', err)
const isCommonMissingTxError =
err?.status === 404 &&
typeof err?.message === 'string' &&
err.message.includes('Key not found in database')
if (!isCommonMissingTxError) {
wlogger.error('Error in SlpRESTController:', err)
}
const status = err.status || 500
const message = err.message || 'Internal server error'
+37 -6
View File
@@ -6,9 +6,14 @@ import wlogger from '../adapters/wlogger.js'
import BCHJS from '@psf/bch-js'
import config from '../config/index.js'
// Use RESTURL (from test) or REST_URL (from psf-bch-api config) or fallback to config
const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
// Use BCHJSBEARERTOKEN (from test) or BASIC_AUTH_TOKEN (from psf-bch-api config) or fallback to config
const bearerToken = process.env.BCHJSBEARERTOKEN || process.env.BASIC_AUTH_TOKEN || config.basicAuth.token
const bchjs = new BCHJS({
restURL: config.restURL,
bearerToken: config.basicAuth.token
restURL,
bearerToken
})
class FulcrumUseCases {
@@ -101,13 +106,24 @@ class FulcrumUseCases {
}
}
async getTransactions ({ address, allTxs }) {
async getTransactions ({ address, allTxs, bearerToken = null }) {
try {
const response = await this.fulcrum.get(`electrumx/transactions/${address}`)
// Sort transactions in descending order, so that newest transactions are first.
if (response.transactions && Array.isArray(response.transactions)) {
response.transactions = await this.bchjs.Electrumx.sortAllTxs(response.transactions, 'DESCENDING')
// Use bearer token from request if provided, otherwise use the default bchjs instance
let bchjsInstance = this.bchjs
if (bearerToken) {
// Create a temporary bchjs instance with the bearer token from the request
const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
bchjsInstance = new BCHJS({
restURL,
bearerToken
})
}
response.transactions = await bchjsInstance.Electrumx.sortAllTxs(response.transactions, 'DESCENDING')
if (!allTxs) {
// Return only the first 100 transactions of the history.
@@ -122,16 +138,31 @@ class FulcrumUseCases {
}
}
async getTransactionsBulk ({ addresses, allTxs }) {
async getTransactionsBulk ({ addresses, allTxs, bearerToken = null }) {
try {
const response = await this.fulcrum.post('electrumx/transactions/', { addresses })
// Sort transactions in descending order for each address entry.
if (response.transactions && Array.isArray(response.transactions)) {
// Use bearer token from request if provided, otherwise use the default bchjs instance
let bchjsInstance = this.bchjs
// console.log('getTransactionsBulk() bearerToken: ', bearerToken)
if (bearerToken) {
// Create a temporary bchjs instance with the bearer token from the request
const restURL = config.restURL
// console.log('getTransactionsBulk() restURL: ', restURL)
bchjsInstance = new BCHJS({
restURL,
bearerToken
})
}
for (let i = 0; i < response.transactions.length; i++) {
const thisEntry = response.transactions[i]
if (thisEntry.transactions && Array.isArray(thisEntry.transactions)) {
thisEntry.transactions = await this.bchjs.Electrumx.sortAllTxs(thisEntry.transactions, 'DESCENDING')
thisEntry.transactions = await bchjsInstance.Electrumx.sortAllTxs(thisEntry.transactions, 'DESCENDING')
if (!allTxs && thisEntry.transactions.length > 100) {
// Extract only the first 100 transactions.
+8 -1
View File
@@ -98,7 +98,14 @@ class SlpUseCases {
try {
return await this.slpIndexer.post('slp/tx/', { txid })
} catch (err) {
wlogger.error('Error in SlpUseCases.getTxid()', err)
const isCommonMissingTxError =
err?.status === 404 &&
typeof err?.message === 'string' &&
err.message.includes('Key not found in database')
if (!isCommonMissingTxError) {
wlogger.error('Error in SlpUseCases.getTxid()', err)
}
throw err
}
}