aboutsummaryrefslogtreecommitdiff
path: root/client/cmd/get-proof-by-hash
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-proof-by-hash
parent9f7690327f8d74abdd86232546a154ab8408d174 (diff)
added additional basic client commands
Diffstat (limited to 'client/cmd/get-proof-by-hash')
-rw-r--r--client/cmd/get-proof-by-hash/main.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/client/cmd/get-proof-by-hash/main.go b/client/cmd/get-proof-by-hash/main.go
new file mode 100644
index 0000000..1f4f304
--- /dev/null
+++ b/client/cmd/get-proof-by-hash/main.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+
+ "encoding/base64"
+
+ "github.com/golang/glog"
+ "github.com/system-transparency/stfe/client"
+ "github.com/system-transparency/stfe/types"
+)
+
+var (
+ sthStr = flag.String("sth", "", "base64-encoded StItem of type StFormatSignedTreeHeadV1 (default: fetch new sth)")
+ leafHashStr = flag.String("leaf_hash", "", "base64-encoded leaf hash")
+)
+
+func main() {
+ flag.Parse()
+ defer glog.Flush()
+
+ client, err := client.NewClientFromFlags()
+ if err != nil {
+ glog.Errorf("NewClientFromFlags: %v", err)
+ return
+ }
+ leafHash, sth, err := newParamsFromFlags(client)
+ if err != nil {
+ glog.Errorf("NewRequestFromFlags: %v", err)
+ return
+ }
+
+ proof, err := client.GetProofByHash(context.Background(), leafHash, sth)
+ if err != nil {
+ glog.Errorf("GetProofByHash: %v", err)
+ return
+ }
+ serialized, err := types.Marshal(*proof)
+ if err != nil {
+ glog.Errorf("Marshal: %v", err)
+ }
+ fmt.Println("proof:", base64.StdEncoding.EncodeToString(serialized))
+}
+
+func newParamsFromFlags(client *client.Client) ([]byte, *types.StItem, error) {
+ serialized, err := base64.StdEncoding.DecodeString(*sthStr)
+ if err != nil {
+ return nil, nil, fmt.Errorf("sth: DecodeString: %v", err)
+ }
+ var item types.StItem
+ if err = types.Unmarshal(serialized, &item); err != nil {
+ return nil, nil, fmt.Errorf("sth: Unmarshal: %v", err)
+ } else if got, want := item.Format, types.StFormatSignedTreeHeadV1; got != want {
+ return nil, nil, fmt.Errorf("unexpected StItem format: %v", got)
+ }
+ leafHash, err := base64.StdEncoding.DecodeString(*leafHashStr)
+ if err != nil {
+ return nil, nil, fmt.Errorf("leaf_hash: DecodeString: %v", err)
+ } else if got, want := len(leafHash), 32; got != want {
+ return nil, nil, fmt.Errorf("leaf_hash: unexpected size: %v", got)
+ }
+ glog.V(3).Infof("created request parameters TreeSize(%d) and LeafHash(%s)", item.SignedTreeHeadV1.TreeHead.TreeSize, *leafHashStr)
+ return leafHash, &item, nil
+}