From 43f8927a1c2441ad076043c7ff6b456bfd68a016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Sun, 21 Nov 2021 11:39:15 +0100 Subject: [PATCH] added show command and command handling --- cmd/taler-auditor-offline-signing/main.go | 63 +++++++++++++++++------ 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/cmd/taler-auditor-offline-signing/main.go b/cmd/taler-auditor-offline-signing/main.go index d35bc18..78e2809 100644 --- a/cmd/taler-auditor-offline-signing/main.go +++ b/cmd/taler-auditor-offline-signing/main.go @@ -608,14 +608,24 @@ func Sign(input *Input, url string, pk ed25519.PrivateKey) ([]SignOperation, err } var ( + injson = flag.String("input", "-", "input json for signing") keyfile = flag.String("key", "auditor.key", "filename of EC25519 private key") url = flag.String("url", "https://auditor.codeblau.de/", "auditor url") ) func main() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n %s [Options] (show|sign)\nOptions:\n", os.Args[0], os.Args[0]) + flag.PrintDefaults() + } flag.Parse() + if len(flag.Args()) == 0 { + flag.Usage() + os.Exit(1) + } + if len(*keyfile) == 0 { log.Println("keyfile needed") return @@ -626,25 +636,48 @@ func main() { log.Printf("couldn't read keyfile: %v\n", e) return } + pk := ed25519.NewKeyFromSeed(k) + pub := (pk.Public()).(ed25519.PublicKey) + pe, _ := crockfordEncode(pub) - input := new(Input) - dec := json.NewDecoder(os.Stdin) - e = dec.Decode(input) - if e != nil { - log.Fatal(e) - } + switch flag.Arg(0) { + case "show": + fmt.Printf("public key: %s\n", string(pe)) + case "sign": + var dec *json.Decoder - output, err := Sign(input, *url, pk) - if err != nil { - log.Fatalf("error signing: %v", err) - } + input := new(Input) + if *injson == "-" { + dec = json.NewDecoder(os.Stdin) + } else { + f, e := os.Open(*injson) + if e != nil { + log.Fatal(e) + } + defer f.Close() - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - e = enc.Encode(output) - if e != nil { - log.Fatal(e) + dec = json.NewDecoder(f) + } + e = dec.Decode(input) + if e != nil { + log.Fatal(e) + } + + output, err := Sign(input, *url, pk) + if err != nil { + log.Fatalf("error signing: %v", err) + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + e = enc.Encode(output) + if e != nil { + log.Fatal(e) + } + default: + flag.Usage() + os.Exit(2) } }