aboutsummaryrefslogtreecommitdiff
path: root/util.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
commitc05c22ddbc771e7713849cae40f9d91bfafa0503 (patch)
treeb97d11ab2a914806e6f671f9aff1cab9767b2eab /util.go
parentc9b4b43654f0ff26207cc63449f13298cd3c56e8 (diff)
major refactor based on README.md and TODOs
Updated types, improved units tests, isolated most test data to have it in one place, renamed and created new files to improve readability, and fixed a bunch of minor TODOs.
Diffstat (limited to 'util.go')
-rw-r--r--util.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/util.go b/util.go
new file mode 100644
index 0000000..847c3f7
--- /dev/null
+++ b/util.go
@@ -0,0 +1,40 @@
+package stfe
+
+import (
+ "fmt"
+
+ "github.com/google/trillian"
+ ttypes "github.com/google/trillian/types"
+ "github.com/system-transparency/stfe/types"
+)
+
+func NewTreeHeadV1FromLogRoot(lr *ttypes.LogRootV1) *types.TreeHeadV1 {
+ return &types.TreeHeadV1{
+ Timestamp: uint64(lr.TimestampNanos / 1000 / 1000),
+ TreeSize: uint64(lr.TreeSize),
+ RootHash: types.NodeHash{
+ Data: lr.RootHash,
+ },
+ Extension: make([]byte, 0),
+ }
+}
+
+func NewNodePathFromHashPath(hashes [][]byte) []types.NodeHash {
+ path := make([]types.NodeHash, 0, len(hashes))
+ for _, hash := range hashes {
+ path = append(path, types.NodeHash{hash})
+ }
+ return path
+}
+
+func NewStItemListFromLeaves(leaves []*trillian.LogLeaf) (*types.StItemList, error) {
+ items := make([]types.StItem, 0, len(leaves))
+ for _, leaf := range leaves {
+ var item types.StItem
+ if err := types.Unmarshal(leaf.LeafValue, &item); err != nil {
+ return nil, fmt.Errorf("Unmarshal failed: %v", err)
+ }
+ items = append(items, item)
+ }
+ return &types.StItemList{items}, nil
+}