certichain

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

commit c2a00254cff010e7c8e0679f36de439dfda66b35
parent b2b7f56b274d84ceedf9b88f3fdba9df5696c7c2
Author: Shivakumar <[email protected]>
Date:   Wed, 12 Aug 2026 01:31:27 +0530

chore: restructure and rename binary

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

Diffstat:
Rcmd/institution/anchor.go -> cmd/certi/anchor.go | 0
Acmd/certi/init.go | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acmd/certi/main.go | 47+++++++++++++++++++++++++++++++++++++++++++++++
Dcmd/institution/main.go | 143-------------------------------------------------------------------------------
4 files changed, 164 insertions(+), 143 deletions(-)

diff --git a/cmd/institution/anchor.go b/cmd/certi/anchor.go diff --git a/cmd/certi/init.go b/cmd/certi/init.go @@ -0,0 +1,117 @@ +// 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 main + +import ( + "bufio" + "flag" + "fmt" + "os" + "path/filepath" + "strings" + + "certichain/internal/crypto" +) + +const ( + configDir = ".certichain" + privFile = "private_key.pem" + pubFile = "public_key.pem" + + dirPremission = 0o700 + privPremission = 0o600 + pubPremission = 0o644 +) + +func runInit(args []string) { + fs := flag.NewFlagSet("init", flag.ExitOnError) + force := fs.Bool("force", false, "overwrite existing keys without prompting") + fs.Parse(args) + + home, err := os.UserHomeDir() + if err != nil { + fmt.Fprintln(os.Stderr, "resolving HOME path:", err) + os.Exit(1) + } + dir := filepath.Join(home, configDir) + + privPath := filepath.Join(dir, privFile) + pubPath := filepath.Join(dir, pubFile) + + if _, err := os.Stat(privPath); err == nil { + if !(*force) && !confirmOverwrite(privPath) { + fmt.Println("aborted - existing keys left untouched") + return + } + } + + if err := os.MkdirAll(dir, dirPremission); err != nil { + fmt.Fprintln(os.Stderr, "creating key directory:", err) + os.Exit(1) + } + + pub, priv, err := crypto.GenerateKeyPair() + if err != nil { + fmt.Fprintln(os.Stderr, "generating keypair:", err) + os.Exit(1) + } + + privPEM, err := crypto.PrivateKeyToPEM(priv) + if err != nil { + fmt.Fprintln(os.Stderr, "encoding private key:", err) + os.Exit(1) + } + pubPEM, err := crypto.PublicKeyToPEM(pub) + if err != nil { + fmt.Fprintln(os.Stderr, "encoding public key:", err) + os.Exit(1) + } + + if err := os.WriteFile(privPath, privPEM, privPremission); err != nil { + fmt.Fprintln(os.Stderr, "writing private key:", err) + os.Exit(1) + } + if err := os.WriteFile(pubPath, pubPEM, pubPremission); err != nil { + fmt.Fprintln(os.Stderr, "writing public key:", err) + os.Exit(1) + } + + did, err := crypto.NewDID(pub) + if err != nil { + fmt.Fprintln(os.Stderr, "deriving DID:", err) + os.Exit(1) + } + + fmt.Println("Institution initilized successfully!") + fmt.Println("\nDID:\n\t", did) + fmt.Println("Key:") + fmt.Println("\t Private:", privPath) + fmt.Println("\t Public :", pubPath) + fmt.Println("\nNext Step:") + fmt.Println("Register this DID with the network:") + fmt.Println("\n\t curl -X POST localhost:8080/v1/institutions \\") + fmt.Printf("\t -d '{\"public_key\":\"%x\"}'\n", []byte(pub)) +} + +func confirmOverwrite(path string) bool { + fmt.Printf("Keys already exists at: %s\n", path) + fmt.Println("Reinitlizing overwrites them - deletes the old DID forever.") + fmt.Print("Continue [y/N]: ") + + reader := bufio.NewReader(os.Stdin) + line, _ := reader.ReadString('\n') + line = strings.TrimSpace(strings.ToLower(line)) + return line == "y" || line == "yes" +} diff --git a/cmd/certi/main.go b/cmd/certi/main.go @@ -0,0 +1,47 @@ +// 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 main + +import ( + "fmt" + "os" +) + +func main() { + if len(os.Args) < 2 { + printUsage() + os.Exit(1) + } + + switch os.Args[1] { + case "init": + runInit(os.Args[2:]) + case "anchor": + runAnchor(os.Args[2:]) + default: + fmt.Fprintf(os.Stderr, "unknown command: %s\n", os.Args[1]) + os.Exit(1) + } +} + +func printUsage() { + fmt.Fprintln(os.Stdout, "usage: certi <command> [flags]") + fmt.Fprintln(os.Stdout, "commands:") + fmt.Fprintln(os.Stdout, "\tinit\tgenerate and store a new institution keypair") + fmt.Fprintln(os.Stdout, "\tanchor\tcreates a transaction from the merkleroot and anchors to blockchain") + fmt.Fprintln(os.Stdout, "flags:") + fmt.Fprintln(os.Stdout, "\t-force\t[init] overwrite existing key without prompting") + fmt.Fprintln(os.Stdout, "\t-url\t[anchor] default: localhost:8080, but can be set to other url") +} diff --git a/cmd/institution/main.go b/cmd/institution/main.go @@ -1,143 +0,0 @@ -// 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 main - -import ( - "bufio" - "certichain/internal/crypto" - "flag" - "fmt" - "os" - "path/filepath" - "strings" -) - -const ( - configDir = ".certichain" - privFile = "private_key.pem" - pubFile = "public_key.pem" - - dirPremission = 0o700 - privPremission = 0o600 - pubPremission = 0o644 -) - -func main() { - if len(os.Args) < 2 { - printUsage() - os.Exit(1) - } - - switch os.Args[1] { - case "init": - runInit(os.Args[2:]) - case "anchor": - runAnchor(os.Args[2:]) - default: - fmt.Fprintf(os.Stderr, "unknown command: %s\n", os.Args[1]) - os.Exit(1) - } -} - -func printUsage() { - fmt.Fprintln(os.Stdout, "usage: certi <command> [flags]") - fmt.Fprintln(os.Stdout, "commands:") - fmt.Fprintln(os.Stdout, "\tinit\tgenerate and store a new institution keypair") - fmt.Fprintln(os.Stdout, "\tanchor\tcreates a transaction from the merkleroot and anchors to blockchain") - fmt.Fprintln(os.Stdout, "flags:") - fmt.Fprintln(os.Stdout, "\t-force\t[init] overwrite existing key without prompting") - fmt.Fprintln(os.Stdout, "\t-url\t[anchor] default: localhost:8080, but can be set to other url") -} - -func runInit(args []string) { - fs := flag.NewFlagSet("init", flag.ExitOnError) - force := fs.Bool("force", false, "overwrite existing keys without prompting") - fs.Parse(args) - - home, err := os.UserHomeDir() - if err != nil { - fmt.Fprintln(os.Stderr, "resolving HOME path:", err) - os.Exit(1) - } - dir := filepath.Join(home, configDir) - - privPath := filepath.Join(dir, privFile) - pubPath := filepath.Join(dir, pubFile) - - if _, err := os.Stat(privPath); err == nil { - if !(*force) && !confirmOverwrite(privPath) { - fmt.Println("aborted - existing keys left untouched") - return - } - } - - if err := os.MkdirAll(dir, dirPremission); err != nil { - fmt.Fprintln(os.Stderr, "creating key directory:", err) - os.Exit(1) - } - - pub, priv, err := crypto.GenerateKeyPair() - if err != nil { - fmt.Fprintln(os.Stderr, "generating keypair:", err) - os.Exit(1) - } - - privPEM, err := crypto.PrivateKeyToPEM(priv) - if err != nil { - fmt.Fprintln(os.Stderr, "encoding private key:", err) - os.Exit(1) - } - pubPEM, err := crypto.PublicKeyToPEM(pub) - if err != nil { - fmt.Fprintln(os.Stderr, "encoding public key:", err) - os.Exit(1) - } - - if err := os.WriteFile(privPath, privPEM, privPremission); err != nil { - fmt.Fprintln(os.Stderr, "writing private key:", err) - os.Exit(1) - } - if err := os.WriteFile(pubPath, pubPEM, pubPremission); err != nil { - fmt.Fprintln(os.Stderr, "writing public key:", err) - os.Exit(1) - } - - did, err := crypto.NewDID(pub) - if err != nil { - fmt.Fprintln(os.Stderr, "deriving DID:", err) - os.Exit(1) - } - - fmt.Println("Institution initilized successfully!") - fmt.Println("\nDID:\n\t", did) - fmt.Println("Key:") - fmt.Println("\t Private:", privPath) - fmt.Println("\t Public :", pubPath) - fmt.Println("\nNext Step:") - fmt.Println("Register this DID with the network:") - fmt.Println("\n\t curl -X POST localhost:8080/v1/institutions \\") - fmt.Printf("\t -d '{\"public_key\":\"%x\"}'\n", []byte(pub)) -} - -func confirmOverwrite(path string) bool { - fmt.Printf("Keys already exists at: %s\n", path) - fmt.Println("Reinitlizing overwrites them - deletes the old DID forever.") - fmt.Print("Continue [y/N]: ") - - reader := bufio.NewReader(os.Stdin) - line, _ := reader.ReadString('\n') - line = strings.TrimSpace(strings.ToLower(line)) - return line == "y" || line == "yes" -}