certichain

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

commit c7434dea4e315788662555a94e4b169303b6f993
parent 9e5186e177aa7d3292912e871ee46eb40fffb7fc
Author: Shivakumar <[email protected]>
Date:   Sun,  2 Aug 2026 03:36:55 +0530

feat(api): add institution endpoint

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

Diffstat:
Aapi/handlers/institution.go | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/router.go | 1+
2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/api/handlers/institution.go b/api/handlers/institution.go @@ -0,0 +1,71 @@ +// 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/registry" +) + +// writeJSON writes v as a JSON response with the given status code. +func writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + json.NewEncoder(w).Encode(v) +} + +// writeError writes a dto.ErrorResponse with the given status, code, and message. +func writeError(w http.ResponseWriter, status int, code, message string) { + writeJSON(w, status, dto.ErrorResponse{ + Error: dto.ErrorDetail{Code: code, Message: message}, + }) +} + +// RegisterInstitution returns a handler for POST /v1/institutions. +func RegisterInstitution(reg *registry.Registry) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req dto.RegisterInstitutionRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "malformed JSON body") + return + } + + pubKey, err := hex.DecodeString(req.PublicKey) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid hex Encoding") + return + } + + did, err := reg.Register(pubKey) + if err != nil { + switch { + case errors.Is(err, registry.ErrInvalidKey): + writeError(w, http.StatusBadRequest, "invalid_body", "Invalid Public Key") + case errors.Is(err, registry.ErrAlreadyExists): + writeError(w, http.StatusConflict, "already_exists", "Public Key already exists") + default: + writeError(w, http.StatusInternalServerError, "server_error", "Internal Server Error") + } + return + } + + writeJSON(w, http.StatusCreated, dto.RegisterInstitutionResponse{DID: did}) + } +} diff --git a/api/router.go b/api/router.go @@ -25,6 +25,7 @@ func NewRouter(deps Deps) http.Handler { mux := http.NewServeMux() mux.HandleFunc("GET /healthz", handlers.Health()) + mux.HandleFunc("POST /v1/institutions", handlers.RegisterInstitution(deps.Registry)) return mux }