verify.go (2294B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package handlers 16 17 import ( 18 "encoding/hex" 19 "net/http" 20 "time" 21 22 "certichain/api/dto" 23 "certichain/api/respond" 24 "certichain/internal/blockchain" 25 "certichain/internal/registry" 26 ) 27 28 // Verify returns a handler for GET /v1/verify/{tx_id}. 29 func Verify(bc *blockchain.Blockchain, reg *registry.Registry) http.HandlerFunc { 30 return func(w http.ResponseWriter, r *http.Request) { 31 txIDhash := r.PathValue("tx_id") 32 33 txID, err := hex.DecodeString(txIDhash) 34 if err != nil { 35 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid hex encoding") 36 return 37 } 38 39 tx, err := bc.GetTransaction(txID) 40 if err != nil { 41 writeStorageError(w, err, "transaction not found") 42 return 43 } 44 pubKey, err := reg.Get(tx.IssuerDID) 45 if err != nil { 46 writeStorageError(w, err, "issuer DID not found in registry") 47 return 48 } 49 50 block, err := bc.GetBlockByTxID(txID) 51 if err != nil { 52 writeStorageError(w, err, "transaction not found") 53 return 54 } 55 56 ok, err := tx.Verify(pubKey) 57 if err != nil { 58 respond.Error(w, http.StatusInternalServerError, "server_error", "internal server error") 59 return 60 } 61 62 if !ok { 63 respond.Error(w, http.StatusUnauthorized, "invalid_signature", "signature verification failed") 64 return 65 } 66 67 respond.JSON(w, http.StatusOK, &dto.VerifyResponse{ 68 TxID: hex.EncodeToString(tx.TxID), 69 IssuerDID: tx.IssuerDID, 70 IssuerPublicKey: hex.EncodeToString(pubKey), 71 MerkleRoot: hex.EncodeToString(tx.MerkleRoot), 72 BlockHash: block.Hash.String(), 73 BlockHeight: block.Header.Height, 74 BlockMerkleRoot: block.Header.MerkleRoot.String(), 75 Timestamp: time.Unix(tx.Timestamp, 0).Format(time.DateTime), 76 }) 77 } 78 }