-
Notifications
You must be signed in to change notification settings - Fork 379
/
map_client.go
98 lines (89 loc) · 3.4 KB
/
map_client.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"github.com/google/trillian"
"github.com/google/trillian/types"
"google.golang.org/grpc/status"
)
// MapClient represents a client for a given Trillian Map instance.
type MapClient struct {
*MapVerifier
MapID int64
Conn trillian.TrillianMapClient
}
// NewMapClientFromTree returns a verifying Map client for the specified tree.
func NewMapClientFromTree(client trillian.TrillianMapClient, config *trillian.Tree) (*MapClient, error) {
verifier, err := NewMapVerifierFromTree(config)
if err != nil {
return nil, err
}
return &MapClient{
MapVerifier: verifier,
MapID: config.TreeId,
Conn: client,
}, nil
}
// GetAndVerifyLatestMapRoot verifies and returns the latest map root.
func (c *MapClient) GetAndVerifyLatestMapRoot(ctx context.Context) (*types.MapRootV1, error) {
rootResp, err := c.Conn.GetSignedMapRoot(ctx, &trillian.GetSignedMapRootRequest{MapId: c.MapID})
if err != nil {
s := status.Convert(err)
return nil, status.Errorf(s.Code(), "GetSignedMapRoot(%v): %v", c.MapID, s.Message())
}
return c.VerifySignedMapRoot(rootResp.GetMapRoot())
}
// GetAndVerifyMapLeaves verifies and returns the requested map leaves.
// indexes may not contain duplicates.
func (c *MapClient) GetAndVerifyMapLeaves(ctx context.Context, indexes [][]byte) ([]*trillian.MapLeaf, error) {
getResp, err := c.Conn.GetLeaves(ctx, &trillian.GetMapLeavesRequest{
MapId: c.MapID,
Index: indexes,
})
if err != nil {
s := status.Convert(err)
return nil, status.Errorf(s.Code(), "map.GetLeaves(): %v", s.Message())
}
return c.VerifyMapLeavesResponse(indexes, -1, getResp)
}
// GetAndVerifyMapLeavesByRevision verifies and returns the requested map leaves at a specific revision.
// indexes may not contain duplicates.
func (c *MapClient) GetAndVerifyMapLeavesByRevision(ctx context.Context, revision int64, indexes [][]byte) ([]*trillian.MapLeaf, error) {
getResp, err := c.Conn.GetLeavesByRevision(ctx, &trillian.GetMapLeavesByRevisionRequest{
MapId: c.MapID,
Index: indexes,
Revision: revision,
})
if err != nil {
s := status.Convert(err)
return nil, status.Errorf(s.Code(), "map.GetLeaves(): %v", s.Message())
}
return c.VerifyMapLeavesResponse(indexes, revision, getResp)
}
// SetAndVerifyMapLeaves calls SetLeaves and verifies the signature of the returned map root.
func (c *MapClient) SetAndVerifyMapLeaves(ctx context.Context, leaves []*trillian.MapLeaf, metadata []byte) (*types.MapRootV1, error) {
// Set new leaf values.
req := &trillian.SetMapLeavesRequest{
MapId: c.MapID,
Leaves: leaves,
Metadata: metadata,
}
setResp, err := c.Conn.SetLeaves(ctx, req)
if err != nil {
s := status.Convert(err)
return nil, status.Errorf(s.Code(), "map.SetLeaves(MapId: %v): %v", c.MapID, s.Message())
}
return c.VerifySignedMapRoot(setResp.GetMapRoot())
}