aboutsummaryrefslogtreecommitdiff
path: root/client/cmd/get-consistency-proof/main.go
blob: bb8a7a6d29b816d6e6f5fec681681aa7a0fe87b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 (
	first  = flag.String("first", "", "base64-encoded sth")
	second = flag.String("second", "", "base64-encoded sth")
)

func main() {
	flag.Parse()
	defer glog.Flush()

	client, err := client.NewClientFromFlags()
	if err != nil {
		glog.Errorf("NewClientFromFlags: %v", err)
		return
	}
	sth1, sth2, err := newParamsFromFlags()
	if err != nil {
		glog.Errorf("NewRequestFromFlags: %v", err)
		return
	}

	proof, err := client.GetConsistencyProof(context.Background(), sth1, sth2)
	if err != nil {
		glog.Errorf("GetConsistencyProof: %v", err)
		return
	}
	serialized, err := types.Marshal(*proof)
	if err != nil {
		glog.Errorf("Marshal: %v", err)
		return
	}
	fmt.Println("proof:", base64.StdEncoding.EncodeToString(serialized))
}

func newParamsFromFlags() (*types.StItem, *types.StItem, error) {
	sth1, err := decodeSthStr(*first)
	if err != nil {
		return nil, nil, fmt.Errorf("first: decodeSthStr: %v", err)
	}
	sth2, err := decodeSthStr(*second)
	if err != nil {
		return nil, nil, fmt.Errorf("second: decodeSthStr: %v", err)
	}
	return sth1, sth2, nil
}

func decodeSthStr(sthStr string) (*types.StItem, error) {
	serialized, err := base64.StdEncoding.DecodeString(sthStr)
	if err != nil {
		return nil, fmt.Errorf("DecodeString: %v", err)
	}
	var item types.StItem
	if err = types.Unmarshal(serialized, &item); err != nil {
		return nil, fmt.Errorf("Unmarshal: %v", err)
	}
	return &item, nil
}