certichain

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

commit fd830247a7ae277f7c49a5946542e84ff958b891
parent 52ec6099609daf2f567b39de592b12de10d38d0b
Author: Shivakumar <[email protected]>
Date:   Thu,  6 Aug 2026 00:36:57 +0530

feat(api): add POST and GET for transaction

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

Diffstat:
Mapi/dto/dto.go | 1+
Aapi/handlers/transactions.go | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/router.go | 2++
3 files changed, 166 insertions(+), 0 deletions(-)

diff --git a/api/dto/dto.go b/api/dto/dto.go @@ -36,6 +36,7 @@ type GetInstitutionResponse struct { // SubmitTransactionRequest is the request body for POST /v1/transactions. type SubmitTransactionRequest struct { + TxID string `json:"tx_id"` IssuerDID string `json:"issuer_did"` MerkleRoot string `json:"merkle_root"` Nonce string `json:"nonce"` diff --git a/api/handlers/transactions.go b/api/handlers/transactions.go @@ -0,0 +1,163 @@ +// 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" + "encoding/json" + "errors" + "net/http" + + "certichain/api/dto" + "certichain/internal/blockchain" + "certichain/internal/mempool" + "certichain/internal/registry" + "certichain/internal/storage" + "certichain/internal/transaction" +) + +// SubmitTransaction returns a handler for POST /v1/transactions. +func SubmitTransaction(mem *mempool.Mempool, reg *registry.Registry) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req dto.SubmitTransactionRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Malformed JSON body") + return + } + + txID, err := hex.DecodeString(req.TxID) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid TxID Hex Encoding") + return + } + nonce, err := hex.DecodeString(req.Nonce) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid nonce Hex Encoding") + return + } + merkleRoot, err := hex.DecodeString(req.MerkleRoot) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid MerkleRoot Hex Encoding") + return + } + signature, err := hex.DecodeString(req.Signature) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid Signature Hex Encoding") + return + } + + tx := &transaction.Transaction{ + TxID: txID, + Nonce: nonce, + MerkleRoot: merkleRoot, + IssuerDID: req.IssuerDID, + Signature: signature, + Timestamp: req.Timestamp, + } + + pubKey, err := reg.Get(req.IssuerDID) + if err != nil { + switch { + case errors.Is(err, storage.ErrNotFound): + writeError(w, http.StatusBadRequest, "unknown_issuer", "Issuer DID is not registered") + return + case errors.Is(err, registry.ErrInvalidDID): + writeError(w, http.StatusBadGateway, "invalid_body", "Malformed Issuer DID") + return + default: + writeError(w, http.StatusInternalServerError, "server_error", "Internal Server Error") + return + } + } + + valid, err := tx.Verify(pubKey) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", err.Error()) + return + } + if !valid { + writeError(w, http.StatusForbidden, "invalid_signature", "Signature verification failed") + return + } + + if err := mem.Add(tx); err != nil { + switch { + case errors.Is(err, mempool.ErrDuplicateTx): + writeError(w, http.StatusConflict, "duplicate_transaction", "transaction already submitted") + case errors.Is(err, mempool.ErrMempoolFull): + writeError(w, http.StatusServiceUnavailable, "mempool_full", "mempool is at capacity, try again later") + default: + writeError(w, http.StatusInternalServerError, "server_error", "Internal Server Error") + } + return + } + + writeJSON(w, http.StatusCreated, dto.SubmitTransactionResponse{TxID: req.TxID, Status: "pending"}) + } +} + +// GetTransaction returns a handler for GET /v1/transctions/{tx_id}. +func GetTransaction(mem *mempool.Mempool, bc *blockchain.Blockchain) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + txIDHex := r.PathValue("tx_id") + + txID, err := hex.DecodeString(txIDHex) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "invalid tx_id hex encoding") + return + } + + has := mem.Has([]byte(txID)) + + if has { + tx := mem.Get(txID) + writeJSON(w, http.StatusOK, &dto.GetTransactionResponse{ + TxID: txIDHex, + IssuerDID: tx.IssuerDID, + MerkleRoot: hex.EncodeToString(tx.MerkleRoot), + Timestamp: tx.Timestamp, + Status: "pending", + }) + return + } + + tx, err := bc.GetTransaction(txID) + if err != nil { + if errors.Is(err, storage.ErrNotFound) { + writeError(w, http.StatusNotFound, "not_found", "transaction not found") + return + } + writeError(w, http.StatusInternalServerError, "server_error", "internal server error") + return + } + + block, err := bc.GetBlockByTxID(txID) + if err != nil { + writeError(w, http.StatusInternalServerError, "server_error", "internal server error") + return + } + + blockHash := hex.EncodeToString(block.Hash[:]) + + writeJSON(w, http.StatusOK, &dto.GetTransactionResponse{ + TxID: txIDHex, + IssuerDID: tx.IssuerDID, + MerkleRoot: hex.EncodeToString(tx.MerkleRoot), + Timestamp: tx.Timestamp, + Status: "confirmed", + BlockHash: &blockHash, + }) + } +} diff --git a/api/router.go b/api/router.go @@ -27,6 +27,8 @@ func NewRouter(deps Deps) http.Handler { mux.HandleFunc("GET /healthz", handlers.Health()) mux.HandleFunc("POST /v1/institutions", handlers.RegisterInstitution(deps.Registry)) 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)) return mux }