-
Notifications
You must be signed in to change notification settings - Fork 351
/
ident.go
153 lines (127 loc) · 3.14 KB
/
ident.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ident
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"hash"
"sort"
)
type Identifiable interface {
Identity() []byte
}
type AddressProvider interface {
ContentAddress(entity Identifiable) string
}
type HexAddressProvider struct{}
func NewHexAddressProvider() *HexAddressProvider {
return &HexAddressProvider{}
}
func (*HexAddressProvider) ContentAddress(entity Identifiable) string {
return hex.EncodeToString(entity.Identity())
}
// IsContentAddress check if addr is valid content address or partial content address
func IsContentAddress(addr string) bool {
if len(addr) == 0 || len(addr) > sha256.Size*2 {
return false
}
_, err := hex.DecodeString(addr)
return err == nil
}
type AddressType uint8
const (
AddressTypeBytes AddressType = iota
AddressTypeString
AddressTypeInt64
AddressTypeStringSlice
AddressTypeStringMap
AddressTypeEmbeddedIdentifiable
)
// linter is angry because h can be an io.Writer, but we explicitly want a Hash here.
//
//nolint:interfacer
func marshalType(h hash.Hash, addressType AddressType) {
_, _ = h.Write([]byte{byte(addressType)})
}
func MarshalBytes(h hash.Hash, v []byte) {
marshalType(h, AddressTypeBytes)
MarshalInt64(h, int64(len(v)))
_, _ = h.Write(v)
}
func MarshalString(h hash.Hash, v string) {
marshalType(h, AddressTypeString)
MarshalInt64(h, int64(len(v)))
_, _ = h.Write([]byte(v))
}
func MarshalInt64(h hash.Hash, v int64) {
const int64Bytes = 8
marshalType(h, AddressTypeInt64)
_, _ = h.Write([]byte{int64Bytes})
bytes := make([]byte, int64Bytes)
binary.BigEndian.PutUint64(bytes, uint64(v))
_, _ = h.Write(bytes)
}
func MarshalStringSlice(h hash.Hash, v []string) {
marshalType(h, AddressTypeStringSlice)
MarshalInt64(h, int64(len(v)))
for _, item := range v {
MarshalString(h, item)
}
}
func MarshalStringMap(h hash.Hash, v map[string]string) {
marshalType(h, AddressTypeStringMap)
MarshalInt64(h, int64(len(v)))
keys := make([]string, len(v))
i := 0
for k := range v {
keys[i] = k
i++
}
sort.Strings(keys)
for _, k := range keys {
MarshalString(h, k)
MarshalString(h, v[k])
}
}
func MarshalIdentifiable(h hash.Hash, v Identifiable) {
marshalType(h, AddressTypeEmbeddedIdentifiable)
MarshalBytes(h, v.Identity())
}
type AddressWriter struct {
hash.Hash
}
func NewAddressWriter() *AddressWriter {
return &AddressWriter{Hash: sha256.New()}
}
func (b *AddressWriter) MarshalBytes(v []byte) *AddressWriter {
MarshalBytes(b, v)
return b
}
func (b *AddressWriter) MarshalString(v string) *AddressWriter {
MarshalString(b, v)
return b
}
func (b *AddressWriter) MarshalStringOpt(v string) *AddressWriter {
if len(v) > 0 {
MarshalString(b, v)
}
return b
}
func (b *AddressWriter) MarshalInt64(v int64) *AddressWriter {
MarshalInt64(b, v)
return b
}
func (b *AddressWriter) MarshalStringSlice(v []string) *AddressWriter {
MarshalStringSlice(b, v)
return b
}
func (b *AddressWriter) MarshalStringMap(v map[string]string) *AddressWriter {
MarshalStringMap(b, v)
return b
}
func (b *AddressWriter) MarshalIdentifiable(v Identifiable) *AddressWriter {
MarshalIdentifiable(b, v)
return b
}
func (b *AddressWriter) Identity() []byte {
return b.Sum(nil)
}