transactions.go (5085B)
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 "encoding/json" 20 "errors" 21 "net/http" 22 "time" 23 24 "certichain/api/dto" 25 "certichain/api/respond" 26 "certichain/internal/blockchain" 27 "certichain/internal/mempool" 28 "certichain/internal/registry" 29 "certichain/internal/storage" 30 "certichain/internal/transaction" 31 ) 32 33 // SubmitTransaction returns a handler for POST /v1/transactions. 34 func SubmitTransaction(mem *mempool.Mempool, reg *registry.Registry) http.HandlerFunc { 35 return func(w http.ResponseWriter, r *http.Request) { 36 var req dto.SubmitTransactionRequest 37 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 38 respond.Error(w, http.StatusBadRequest, "invalid_body", "malformed JSON body") 39 return 40 } 41 42 txID, err := hex.DecodeString(req.TxID) 43 if err != nil { 44 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid TxID hex encoding") 45 return 46 } 47 nonce, err := hex.DecodeString(req.Nonce) 48 if err != nil { 49 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid nonce hex encoding") 50 return 51 } 52 merkleRoot, err := hex.DecodeString(req.MerkleRoot) 53 if err != nil { 54 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid merkleroot hex encoding") 55 return 56 } 57 signature, err := hex.DecodeString(req.Signature) 58 if err != nil { 59 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid signature hex encoding") 60 return 61 } 62 63 tx := &transaction.Transaction{ 64 TxID: txID, 65 Nonce: nonce, 66 MerkleRoot: merkleRoot, 67 IssuerDID: req.IssuerDID, 68 Signature: signature, 69 Timestamp: req.Timestamp, 70 } 71 72 pubKey, err := reg.Get(req.IssuerDID) 73 if err != nil { 74 switch { 75 case errors.Is(err, storage.ErrNotFound): 76 respond.Error(w, http.StatusBadRequest, "unknown_issuer", "issuer DID is not registered") 77 return 78 case errors.Is(err, registry.ErrInvalidDID): 79 respond.Error(w, http.StatusBadRequest, "invalid_body", "malformed issuer DID") 80 return 81 default: 82 respond.Error(w, http.StatusInternalServerError, "server_error", "internal server error") 83 return 84 } 85 } 86 87 valid, err := tx.Verify(pubKey) 88 if err != nil { 89 respond.Error(w, http.StatusBadRequest, "invalid_body", err.Error()) 90 return 91 } 92 if !valid { 93 respond.Error(w, http.StatusForbidden, "invalid_signature", "signature verification failed") 94 return 95 } 96 97 if err := mem.Add(tx); err != nil { 98 switch { 99 case errors.Is(err, mempool.ErrDuplicateTx): 100 respond.Error(w, http.StatusConflict, "duplicate_transaction", "transaction already submitted") 101 case errors.Is(err, mempool.ErrMempoolFull): 102 respond.Error(w, http.StatusServiceUnavailable, "mempool_full", "mempool is at capacity, try again later") 103 default: 104 respond.Error(w, http.StatusInternalServerError, "server_error", "internal server error") 105 } 106 return 107 } 108 109 respond.JSON(w, http.StatusCreated, dto.SubmitTransactionResponse{TxID: req.TxID, Status: "pending"}) 110 } 111 } 112 113 // GetTransaction returns a handler for GET /v1/transctions/{tx_id}. 114 func GetTransaction(mem *mempool.Mempool, bc *blockchain.Blockchain) http.HandlerFunc { 115 return func(w http.ResponseWriter, r *http.Request) { 116 txIDHex := r.PathValue("tx_id") 117 118 txID, err := hex.DecodeString(txIDHex) 119 if err != nil { 120 respond.Error(w, http.StatusBadRequest, "invalid_body", "invalid tx_id hex encoding") 121 return 122 } 123 124 if tx := mem.Get(txID); tx != nil { 125 respond.JSON(w, http.StatusOK, &dto.GetTransactionResponse{ 126 TxID: txIDHex, 127 IssuerDID: tx.IssuerDID, 128 MerkleRoot: hex.EncodeToString(tx.MerkleRoot), 129 Timestamp: time.Unix(tx.Timestamp, 0).Format(time.DateTime), 130 Status: "pending", 131 }) 132 return 133 } 134 135 tx, err := bc.GetTransaction(txID) 136 if err != nil { 137 if errors.Is(err, storage.ErrNotFound) { 138 respond.Error(w, http.StatusNotFound, "not_found", "transaction not found") 139 return 140 } 141 respond.Error(w, http.StatusInternalServerError, "server_error", "internal server error") 142 return 143 } 144 145 block, err := bc.GetBlockByTxID(txID) 146 if err != nil { 147 respond.Error(w, http.StatusInternalServerError, "server_error", "internal server error") 148 return 149 } 150 151 blockHash := hex.EncodeToString(block.Hash[:]) 152 153 respond.JSON(w, http.StatusOK, &dto.GetTransactionResponse{ 154 TxID: txIDHex, 155 IssuerDID: tx.IssuerDID, 156 MerkleRoot: hex.EncodeToString(tx.MerkleRoot), 157 Timestamp: time.Unix(tx.Timestamp, 0).Format(time.DateTime), 158 Status: "confirmed", 159 BlockHash: &blockHash, 160 }) 161 } 162 }