This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle.go
129 lines (114 loc) · 4.15 KB
/
merkle.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Auto-generated by avdl-compiler v1.3.22 (https://github.com/keybase/node-avdl-compiler)
// Input file: avdl/keybase1/merkle.avdl
package keybase1
import (
"github.com/keybase/go-framed-msgpack-rpc/rpc"
context "golang.org/x/net/context"
)
type MerkleRootAndTime struct {
Root MerkleRootV2 `codec:"root" json:"root"`
UpdateTime Time `codec:"updateTime" json:"updateTime"`
FetchTime Time `codec:"fetchTime" json:"fetchTime"`
}
func (o MerkleRootAndTime) DeepCopy() MerkleRootAndTime {
return MerkleRootAndTime{
Root: o.Root.DeepCopy(),
UpdateTime: o.UpdateTime.DeepCopy(),
FetchTime: o.FetchTime.DeepCopy(),
}
}
type KBFSRootHash []byte
func (o KBFSRootHash) DeepCopy() KBFSRootHash {
return (func(x []byte) []byte {
if x == nil {
return nil
}
return append([]byte{}, x...)
})(o)
}
type KBFSRoot struct {
TreeID MerkleTreeID `codec:"treeID" json:"treeID"`
Root KBFSRootHash `codec:"root" json:"root"`
}
func (o KBFSRoot) DeepCopy() KBFSRoot {
return KBFSRoot{
TreeID: o.TreeID.DeepCopy(),
Root: o.Root.DeepCopy(),
}
}
type GetCurrentMerkleRootArg struct {
FreshnessMsec int `codec:"freshnessMsec" json:"freshnessMsec"`
}
type VerifyMerkleRootAndKBFSArg struct {
Root MerkleRootV2 `codec:"root" json:"root"`
ExpectedKBFSRoot KBFSRoot `codec:"expectedKBFSRoot" json:"expectedKBFSRoot"`
}
type MerkleInterface interface {
// GetCurrentMerkleRoot gets the current-most Merkle root from the keybase server.
// The caller can specify how stale a result can be with freshnessMsec.
// If 0 is specified, then any amount of staleness is OK. If -1 is specified, then
// we force a GET and a round-trip.
GetCurrentMerkleRoot(context.Context, int) (MerkleRootAndTime, error)
// VerifyMerkleRootAndKBFS checks that the given merkle root is indeed a valid
// root of the keybase server's Merkle tree, and that the given KBFS root
// is included in that global root.
VerifyMerkleRootAndKBFS(context.Context, VerifyMerkleRootAndKBFSArg) error
}
func MerkleProtocol(i MerkleInterface) rpc.Protocol {
return rpc.Protocol{
Name: "keybase.1.merkle",
Methods: map[string]rpc.ServeHandlerDescription{
"getCurrentMerkleRoot": {
MakeArg: func() interface{} {
ret := make([]GetCurrentMerkleRootArg, 1)
return &ret
},
Handler: func(ctx context.Context, args interface{}) (ret interface{}, err error) {
typedArgs, ok := args.(*[]GetCurrentMerkleRootArg)
if !ok {
err = rpc.NewTypeError((*[]GetCurrentMerkleRootArg)(nil), args)
return
}
ret, err = i.GetCurrentMerkleRoot(ctx, (*typedArgs)[0].FreshnessMsec)
return
},
MethodType: rpc.MethodCall,
},
"verifyMerkleRootAndKBFS": {
MakeArg: func() interface{} {
ret := make([]VerifyMerkleRootAndKBFSArg, 1)
return &ret
},
Handler: func(ctx context.Context, args interface{}) (ret interface{}, err error) {
typedArgs, ok := args.(*[]VerifyMerkleRootAndKBFSArg)
if !ok {
err = rpc.NewTypeError((*[]VerifyMerkleRootAndKBFSArg)(nil), args)
return
}
err = i.VerifyMerkleRootAndKBFS(ctx, (*typedArgs)[0])
return
},
MethodType: rpc.MethodCall,
},
},
}
}
type MerkleClient struct {
Cli rpc.GenericClient
}
// GetCurrentMerkleRoot gets the current-most Merkle root from the keybase server.
// The caller can specify how stale a result can be with freshnessMsec.
// If 0 is specified, then any amount of staleness is OK. If -1 is specified, then
// we force a GET and a round-trip.
func (c MerkleClient) GetCurrentMerkleRoot(ctx context.Context, freshnessMsec int) (res MerkleRootAndTime, err error) {
__arg := GetCurrentMerkleRootArg{FreshnessMsec: freshnessMsec}
err = c.Cli.Call(ctx, "keybase.1.merkle.getCurrentMerkleRoot", []interface{}{__arg}, &res)
return
}
// VerifyMerkleRootAndKBFS checks that the given merkle root is indeed a valid
// root of the keybase server's Merkle tree, and that the given KBFS root
// is included in that global root.
func (c MerkleClient) VerifyMerkleRootAndKBFS(ctx context.Context, __arg VerifyMerkleRootAndKBFSArg) (err error) {
err = c.Cli.Call(ctx, "keybase.1.merkle.verifyMerkleRootAndKBFS", []interface{}{__arg}, nil)
return
}