certichain

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

verify.go (1828B)


      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 main
     16 
     17 import (
     18 	"encoding/json"
     19 	"flag"
     20 	"fmt"
     21 	"net/http"
     22 	"os"
     23 
     24 	"certichain/api/dto"
     25 )
     26 
     27 func runVerify(args []string) {
     28 	fs := flag.NewFlagSet("verify", flag.ExitOnError)
     29 	url := fs.String("url", "http://localhost:8080", "certichainn node base URL")
     30 	fs.Parse(args)
     31 
     32 	if fs.NArg() != 1 {
     33 		fmt.Fprintln(os.Stderr, "usage: certi verify [flags] <tx-id>")
     34 		os.Exit(1)
     35 	}
     36 	txID := fs.Arg(0)
     37 
     38 	resp, err := http.Get(*url + "/v1/verify/" + txID)
     39 	if err != nil {
     40 		fmt.Fprintln(os.Stderr, "contacting node:", err)
     41 		os.Exit(1)
     42 	}
     43 	defer resp.Body.Close()
     44 
     45 	if resp.StatusCode != http.StatusOK {
     46 		var errResp dto.ErrorResponse
     47 		json.NewDecoder(resp.Body).Decode(&errResp)
     48 		fmt.Fprintf(os.Stderr, "verification failed: %s - %s", errResp.Error.Code, errResp.Error.Message)
     49 		os.Exit(1)
     50 	}
     51 
     52 	var verifyResp dto.VerifyResponse
     53 	if err := json.NewDecoder(resp.Body).Decode(&verifyResp); err != nil {
     54 		fmt.Fprintln(os.Stderr, "decoding response:", err)
     55 		os.Exit(1)
     56 	}
     57 
     58 	pretty, err := json.MarshalIndent(verifyResp, "", "    ")
     59 	if err != nil {
     60 		fmt.Fprintln(os.Stderr, "decoding response:", err)
     61 		os.Exit(1)
     62 	}
     63 	fmt.Fprint(os.Stdout, "Success: Valid Transaction\nResponse:\n")
     64 	fmt.Fprintln(os.Stdout, string(pretty))
     65 }