This project demonstrates a simple implementation of a blockchain using Go. It simulates a decentralized ledger for tracking books and their transactions. The blockchain stores book transactions in blocks, where each block is cryptographically linked to its predecessor.
- Book Creation: Allows the creation of books and generates a unique book ID.
- Blockchain: Stores book transactions as blocks, with each block being validated.
- Genesis Block: The first block in the blockchain, used to initialize the chain.
- API Endpoints:
GET /
: Returns the entire blockchain.POST /
: Adds a new block to the blockchain.POST /new
: Creates a new book and returns its details.
- Go (version 1.18 or higher)
-
Clone the repository:
git clone https://github.com/adammast/bookstore-blockchain-go.git cd bookstore-blockchain-go
-
Install dependencies (if any):
go mod tidy
Run the application:
go run .
This will start a local server on http://localhost:3000
.
-
Create a new book:
POST /new
with JSON payload:{ "title": "The Go Programming Language", "author": "Alan A. A. Donovan", "publish_date": "2015-10-26", "isbn": "9780134190440" }
-
Add a new transaction (book purchase):
POST /
with JSON payload:{ "book_id": "book_id_123", "user": "John Doe", "purchase_date": "2025-03-16", "is_genesis": false }
-
Get blockchain:
GET /
to retrieve the current blockchain.
- gorilla/mux: A powerful URL router and dispatcher for Go, used to manage HTTP routes in the project.
- crypto/md5: Part of the Go standard library, used for generating MD5 hashes to create unique book IDs.
- crypto/sha256: Part of the Go standard library, used for generating SHA-256 hashes for block validation.
- encoding/json: Used for encoding and decoding JSON data, which allows communication with the API.
- encoding/hex: Used to encode and decode hexadecimal strings, specifically for generating book IDs and hash outputs.
- sync: Used for concurrency and synchronization.
- log: Used for logging messages to standard output and error output.
- net/http: Part of the Go standard library, used to create and manage the HTTP server and handle requests.
- fmt: Used for formatted I/O operations such as printing to the console.
- io: Provides basic I/O functionality, including writing and reading data to and from the HTTP response.
This project is licensed under the MIT License.