aboutsummaryrefslogtreecommitdiff
path: root/client/cmd/get-entries/main.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-03-16 16:29:24 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-03-16 16:29:24 +0100
commit453a0c38516496052c5f570691c74516c8675e2d (patch)
treef56648b1073dc327b0cd1cc25e371cd5ebefa00d /client/cmd/get-entries/main.go
parent9f7690327f8d74abdd86232546a154ab8408d174 (diff)
added additional basic client commands
Diffstat (limited to 'client/cmd/get-entries/main.go')
-rw-r--r--client/cmd/get-entries/main.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/client/cmd/get-entries/main.go b/client/cmd/get-entries/main.go
new file mode 100644
index 0000000..f32fdbf
--- /dev/null
+++ b/client/cmd/get-entries/main.go
@@ -0,0 +1,83 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+
+ "encoding/base64"
+
+ "github.com/golang/glog"
+ "github.com/google/trillian/merkle/rfc6962"
+ "github.com/system-transparency/stfe/client"
+ "github.com/system-transparency/stfe/types"
+)
+
+var (
+ start = flag.Uint64("start", 0, "inclusive start index to download")
+ end = flag.Uint64("end", 0, "inclusive stop index to download")
+)
+
+func main() {
+ flag.Parse()
+ defer glog.Flush()
+
+ client, err := client.NewClientFromFlags()
+ if err != nil {
+ glog.Errorf("NewClientFromFlags: %v", err)
+ return
+ }
+ items, err := getRange(client, *start, *end)
+ if err != nil {
+ glog.Errorf("getRange: %v", err)
+ return
+ }
+ if err := printRange(items); err != nil {
+ glog.Errorf("printRange: %v", err)
+ return
+ }
+}
+
+func getRange(client *client.Client, start, end uint64) ([]*types.StItem, error) {
+ items := make([]*types.StItem, 0, end-start+1)
+ for len(items) != cap(items) {
+ rsp, err := client.GetEntries(context.Background(), start, end)
+ if err != nil {
+ return nil, fmt.Errorf("fetching entries failed: %v", err)
+ }
+ items = append(items, rsp...)
+ start += uint64(len(rsp))
+ }
+ return items, nil
+}
+
+func printRange(items []*types.StItem) error {
+ for i, item := range items {
+ var status string
+ msg, err := types.Marshal(item.SignedChecksumV1.Data)
+ if err != nil {
+ return fmt.Errorf("Marshal data failed: %v", err)
+ }
+ sig := item.SignedChecksumV1.Signature.Signature
+ namespace := &item.SignedChecksumV1.Signature.Namespace
+ if err := namespace.Verify(msg, sig); err != nil {
+ status = "unverified signature"
+ } else {
+ status = "verified signature"
+ }
+ serializedNamespace, err := types.Marshal(*namespace)
+ if err != nil {
+ return fmt.Errorf("Marshal namespace failed: %v", err)
+ }
+ serializedLeaf, err := types.Marshal(*item)
+ if err != nil {
+ return fmt.Errorf("Marshal item on index %d: %v", *start+uint64(i), err)
+ }
+ fmt.Printf("Index(%d) - %s\n", *start+uint64(i), status)
+ fmt.Printf("-> Namespace: %s\n", base64.StdEncoding.EncodeToString(serializedNamespace))
+ fmt.Printf("-> Identifier: %s\n", string(item.SignedChecksumV1.Data.Identifier))
+ fmt.Printf("-> Checksum: %s\n", base64.StdEncoding.EncodeToString(item.SignedChecksumV1.Data.Checksum))
+ fmt.Printf("-> Leaf hash: %s\n", base64.StdEncoding.EncodeToString(rfc6962.DefaultHasher.HashLeaf(serializedLeaf)))
+ }
+ return nil
+}