certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

commit 6b5f27529a201363c95438f3f5c556b54a65f78c
parent 1e329ada33b5b584c3719eead43566a3cc77a878
Author: Shivakumar <[email protected]>
Date:   Fri,  7 Aug 2026 01:57:34 +0530

feat(api): add blocks endpoint

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Aapi/handlers/blocks.go | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/router.go | 3+++
2 files changed, 119 insertions(+), 0 deletions(-)

diff --git a/api/handlers/blocks.go b/api/handlers/blocks.go @@ -0,0 +1,116 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handlers + +import ( + "encoding/hex" + "errors" + "net/http" + "strconv" + "time" + + "certichain/api/dto" + "certichain/internal/blockchain" + "certichain/internal/storage" +) + +// GetBlock returns a handler for GET /v1/blocks/{hash} +func GetBlock(bc *blockchain.Blockchain) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + blockHash := r.PathValue("hash") + hash, err := hex.DecodeString(blockHash) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "invalid hex encoding") + return + } + + block, err := bc.GetBlock(hash) + if err != nil { + writeStorageError(w, err, "block not found") + return + } + writeBlock(w, block) + } +} + +// GetBlockByHeight returns a handler for GET /v1/blocks/{height}. +func GetBlockByHeight(bc *blockchain.Blockchain) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + blockHeight := r.PathValue("height") + + height, err := strconv.ParseUint(blockHeight, 10, 64) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "invalid height: must be non-negative integer") + return + } + + block, err := bc.GetBlockByHeight(uint64(height)) + if err != nil { + writeStorageError(w, err, "block not found") + return + } + writeBlock(w, block) + } +} + +// GetLatestBlock returns a handler for GET /v1/blocks/latest. +func GetLatestBlock(bc *blockchain.Blockchain) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + height := bc.Height() + block, err := bc.GetBlockByHeight(height) + if err != nil { + writeStorageError(w, err, "block not found") + return + } + writeBlock(w, block) + } +} + +// writeBlock is a helper function to eval block and all the transaction. +func writeBlock(w http.ResponseWriter, block *blockchain.Block) { + var txs []dto.GetTransactionResponse + blockhash := block.Hash.String() + blockTxs := block.Transactions + + for _, tx := range blockTxs { + txs = append(txs, dto.GetTransactionResponse{ + TxID: hex.EncodeToString(tx.TxID), + IssuerDID: tx.IssuerDID, + MerkleRoot: hex.EncodeToString(tx.MerkleRoot), + Timestamp: time.Unix(tx.Timestamp, 0).Format(time.DateTime), + Status: "confirmed", + BlockHash: &blockhash, + }) + } + + writeJSON(w, http.StatusOK, dto.BlockResponse{ + Height: block.Header.Height, + Hash: block.Hash.String(), + PrevHash: block.Header.PrevHash.String(), + MerkleRoot: block.Header.MerkleRoot.String(), + Timestamp: time.Unix(block.Header.TimeStamp, 0).Format(time.DateTime), + Transactions: txs, + }) + +} + +// writeStorageError checks for 404 or 500 error status code. +func writeStorageError(w http.ResponseWriter, err error, msg string) { + if errors.Is(err, storage.ErrNotFound) { + writeError(w, http.StatusNotFound, "not_found", msg) + return + } + writeError(w, http.StatusBadRequest, "invalid_body", "invalid hash hex encoding") +} diff --git a/api/router.go b/api/router.go @@ -29,6 +29,9 @@ func NewRouter(deps Deps) http.Handler { mux.HandleFunc("GET /v1/institutions/{did}", handlers.GetInstitution(deps.Registry)) mux.HandleFunc("POST /v1/transactions", handlers.SubmitTransaction(deps.Mempool, deps.Registry)) mux.HandleFunc("GET /v1/transactions/{tx_id}", handlers.GetTransaction(deps.Mempool, deps.Blockchain)) + mux.HandleFunc("GET /v1/blocks/{hash}", handlers.GetBlock(deps.Blockchain)) + mux.HandleFunc("GET /v1/blocks/height/{height}", handlers.GetBlockByHeight(deps.Blockchain)) + mux.HandleFunc("GET /v1/blocks/latest", handlers.GetLatestBlock(deps.Blockchain)) return mux }