certichain

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

commit a2485daf60bc0e2ffd22bddfc221a84690815dd8
parent 6b5f27529a201363c95438f3f5c556b54a65f78c
Author: Shivakumar <[email protected]>
Date:   Fri,  7 Aug 2026 01:58:06 +0530

feat(api): add verify endpoint

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

Diffstat:
Aapi/handlers/verify.go | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/router.go | 1+
2 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/api/handlers/verify.go b/api/handlers/verify.go @@ -0,0 +1,77 @@ +// 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" + "net/http" + "time" + + "certichain/api/dto" + "certichain/internal/blockchain" + "certichain/internal/registry" +) + +// Verify returns a handler for GET /v1/verify/{tx_id}. +func Verify(bc *blockchain.Blockchain, reg *registry.Registry) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + txIDhash := r.PathValue("tx_id") + + txID, err := hex.DecodeString(txIDhash) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "invalid hex encoding") + return + } + + tx, err := bc.GetTransaction(txID) + if err != nil { + writeStorageError(w, err, "transaction not found") + return + } + pubKey, err := reg.Get(tx.IssuerDID) + if err != nil { + writeStorageError(w, err, "issuer DID not found in registry") + return + } + + block, err := bc.GetBlockByTxID(txID) + if err != nil { + writeStorageError(w, err, "transaction not found") + return + } + + ok, err := tx.Verify(pubKey) + if err != nil { + writeError(w, http.StatusInternalServerError, "server_error", "internal server error") + return + } + + if !ok { + writeError(w, http.StatusUnauthorized, "invalid_signature", "signature verification failed") + return + } + + writeJSON(w, http.StatusOK, &dto.VerifyResponse{ + TxID: hex.EncodeToString(tx.TxID), + IssuerDID: tx.IssuerDID, + IssuerPublicKey: hex.EncodeToString(pubKey), + MerkleRoot: hex.EncodeToString(tx.MerkleRoot), + BlockHash: block.Hash.String(), + BlockHeight: block.Header.Height, + BlockMerkleRoot: block.Header.MerkleRoot.String(), + Timestamp: time.Unix(tx.Timestamp, 0).Format(time.DateTime), + }) + } +} diff --git a/api/router.go b/api/router.go @@ -32,6 +32,7 @@ func NewRouter(deps Deps) http.Handler { 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)) + mux.HandleFunc("GET /v1/verify/{tx_id}", handlers.Verify(deps.Blockchain, deps.Registry)) return mux }