certichain

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

anchor.go (3475B)


      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 	"bytes"
     19 	"crypto/ed25519"
     20 	"encoding/hex"
     21 	"encoding/json"
     22 	"flag"
     23 	"fmt"
     24 	"net/http"
     25 	"os"
     26 	"path"
     27 
     28 	"certichain/api/dto"
     29 	"certichain/internal/crypto"
     30 	"certichain/internal/transaction"
     31 )
     32 
     33 func runAnchor(args []string) {
     34 	fs := flag.NewFlagSet("anchor", flag.ExitOnError)
     35 	url := fs.String("url", "http://localhost:8080", "certichain node base url")
     36 	fs.Parse(args)
     37 
     38 	if fs.NArg() != 1 {
     39 		fmt.Fprintln(os.Stderr, "usage: certi anchor [flags] <merkle-root>")
     40 		os.Exit(1)
     41 	}
     42 
     43 	merkleRoot, err := hex.DecodeString(fs.Arg(0))
     44 	if err != nil {
     45 		fmt.Fprintln(os.Stderr, "decoding merkle root:", err)
     46 		os.Exit(1)
     47 	}
     48 
     49 	pub, priv, err := loadKeys()
     50 	if err != nil {
     51 		fmt.Fprintln(os.Stderr, "loading keys:", err)
     52 		fmt.Fprintln(os.Stderr, "hint: run 'certi init' first")
     53 		os.Exit(1)
     54 	}
     55 
     56 	did, err := crypto.NewDID(pub)
     57 	if err != nil {
     58 		fmt.Fprintln(os.Stderr, "deriving DID:", err)
     59 		os.Exit(1)
     60 	}
     61 
     62 	tx, err := transaction.New(did, merkleRoot)
     63 	if err != nil {
     64 		fmt.Fprintln(os.Stderr, "creating transaction:", err)
     65 		os.Exit(1)
     66 	}
     67 
     68 	if err := tx.Sign(priv); err != nil {
     69 		fmt.Fprintln(os.Stderr, "signing transaction:", err)
     70 		os.Exit(1)
     71 	}
     72 
     73 	payload := dto.SubmitTransactionRequest{
     74 		TxID:       hex.EncodeToString(tx.TxID),
     75 		IssuerDID:  tx.IssuerDID,
     76 		MerkleRoot: hex.EncodeToString(tx.MerkleRoot),
     77 		Nonce:      hex.EncodeToString(tx.Nonce),
     78 		Timestamp:  tx.Timestamp,
     79 		Signature:  hex.EncodeToString(tx.Signature),
     80 	}
     81 
     82 	body, err := json.Marshal(payload)
     83 	if err != nil {
     84 		fmt.Fprintln(os.Stderr, "encoding request:", err)
     85 		os.Exit(1)
     86 	}
     87 
     88 	txURL := *url + "/v1/transactions"
     89 	resp, err := http.Post(txURL, "application/json", bytes.NewReader(body))
     90 	if err != nil {
     91 		fmt.Fprintln(os.Stderr, "submitting transaction:", err)
     92 		os.Exit(1)
     93 	}
     94 	defer resp.Body.Close()
     95 
     96 	var respBody dto.SubmitTransactionResponse
     97 	if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
     98 		fmt.Fprintln(os.Stderr, "decoding response:", err)
     99 		os.Exit(1)
    100 	}
    101 
    102 	fmt.Println("status:\n\t", resp.Status)
    103 	fmt.Printf("response:\n\t{\n\t\t\"status\":\"%s\",\n\t\t\"tx_id\" :\"%s\"\n\t}\n", respBody.Status, respBody.TxID)
    104 }
    105 
    106 func loadKeys() (ed25519.PublicKey, ed25519.PrivateKey, error) {
    107 	home, err := os.UserHomeDir()
    108 	if err != nil {
    109 		return nil, nil, err
    110 	}
    111 	dir := path.Join(home, configDir)
    112 
    113 	privPEM, err := os.ReadFile(path.Join(dir, privFile))
    114 	if err != nil {
    115 		return nil, nil, fmt.Errorf("reading private key:%w", err)
    116 	}
    117 	pubPEM, err := os.ReadFile(path.Join(dir, pubFile))
    118 	if err != nil {
    119 		return nil, nil, fmt.Errorf("reading public key:%w", err)
    120 	}
    121 
    122 	priv, err := crypto.PrivateKeyFromPEM(privPEM)
    123 	if err != nil {
    124 		return nil, nil, fmt.Errorf("parsing private key:%w", err)
    125 	}
    126 	pub, err := crypto.PublicKeyFromPEM(pubPEM)
    127 	if err != nil {
    128 		return nil, nil, fmt.Errorf("parsing public key:%w", err)
    129 	}
    130 
    131 	return pub, priv, nil
    132 }