aboutsummaryrefslogtreecommitdiff
path: root/utils_state.go
blob: 89d2fc2752e05fa00bb1c36d182e4ff1da227ec1 (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
package main

import (
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"os"

	ct "github.com/google/certificate-transparency-go"
)

type treeHead struct {
	TreeSize int64             `json:"tree_size"`
	RootHash [sha256.Size]byte `json:root_hash"`
}

func readState(opts options, id []byte) (treeHead, error) {
	if _, err := os.Stat(fmt.Sprintf("%s/%x/%s", opts.logDirectory, id, opts.stateFile)); err != nil {
		return treeHead{0, sha256.Sum256(nil)}, nil
	}
	b, err := os.ReadFile(fmt.Sprintf("%s/%x/%s", opts.logDirectory, id, opts.stateFile))
	if err != nil {
		return treeHead{}, err
	}
	var th treeHead
	if err := json.Unmarshal(b, &th); err != nil {
		return treeHead{}, err
	}
	return th, nil
}

func readSnapshot(opts options, id []byte) (ct.SignedTreeHead, error) {
	b, err := os.ReadFile(fmt.Sprintf("%s/%x/%s", opts.logDirectory, id, opts.sthFile))
	if err != nil {
		return ct.SignedTreeHead{}, err
	}
	var sth ct.SignedTreeHead
	if err := json.Unmarshal(b, &sth); err != nil {
		return ct.SignedTreeHead{}, err
	}
	return sth, nil
}