This project implements a simple blockchain with a peer-to-peer (P2P) network using Node.js. The blockchain is capable of adding new blocks and synchronizing the chain across multiple peers in the network. The project uses WebSockets for P2P communication and Express for the HTTP API.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- API Endpoints
- Project Structure
- Security Considerations
- License
- Blockchain: Basic implementation of a blockchain with block validation.
- P2P Network: Peer-to-peer network using WebSockets for communication.
- HTTP API: RESTful API for interacting with the blockchain.
- UPnP: Automatic port forwarding using UPnP.
- Environment Configuration: Easy configuration using environment variables.
- Node.js (v14 or higher)
- npm (v6 or higher)
- TypeScript (v5 or higher)
-
Clone the repository:
git clone https://github.com/SergioBonatto/cht.git cd cht
-
Install the dependencies:
npm install
-
Compile the TypeScript code:
npm run build
-
Create a
.env
file in the root directory based on the provided.env.example
:cp .env.example .env
-
Update the
.env
file with your configuration:P2P_PORT=6001 HTTP_PORT=3001 INITIAL_PEER=ws://<PEER_IP>:6001
-
Ensure you have the necessary SSL certificates and keys. You can use the provided examples as a reference:
- key.pem
- key_unencrypted.pem
- cert.pem
Start the server:
npm start
The HTTP server will run on the port specified in the .env
file (default: 3001), and the WebSocket server will run on the port specified in the .env
file (default: 6001).
- URL:
/blocks
- Method:
GET
- Description: Retrieves the current blockchain.
-
URL:
/mine
-
Method:
POST
-
Description: Mines a new block with the provided data and broadcasts it to the network.
-
Request Body:
{ "data": "Block data" }
.env
.env.example
cert.pem
cert.pem.example
key_unencrypted.pem
key.pem
key.pem.example
package.json
src/
├── blockchain/
│ ├── block.ts
│ └── blockchain.ts
├── main.ts
├── network/
│ ├── p2pNetwork.ts
│ └── upnp.ts
├── server/
│ ├── app.ts
│ ├── blockchainInstance.ts
│ ├── index.ts
│ ├── middleware.ts
│ ├── p2pNetworkInstance.ts
│ ├── routes.ts
│ └── utils.ts
src/blockchain/block.ts
: Defines theBlock
class.src/blockchain/blockchain.ts
: Defines theBlockchain
class.src/network/p2pNetwork.ts
: Defines theP2PNetwork
class.src/network/upnp.ts
: Handles UPnP port forwarding.src/server/server.ts
: Configures and starts the HTTP and WebSocket servers.src/index.ts
: Entry point of the application.
- Private Keys: Never commit private keys (
key.pem
,key_unencrypted.pem
) to a public repository. Use environment variables or secure storage solutions. - SSL/TLS: Ensure that SSL/TLS certificates are properly configured to secure communication between peers.
- Environment Variables: Use environment variables to manage sensitive configuration data.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to contribute to this project by submitting issues or pull requests. For major changes, please open an issue first to discuss what you would like to change.
Happy coding! 🚀