-
Notifications
You must be signed in to change notification settings - Fork 170
/
identifier.go
168 lines (147 loc) · 4.81 KB
/
identifier.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package flow
import (
"encoding/hex"
"fmt"
"math/rand"
"reflect"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/fingerprint"
"github.com/onflow/flow-go/storage/merkle"
)
// Identifier represents a 32-byte unique identifier for an entity.
type Identifier [32]byte
// IdentifierFilter is a filter on identifiers.
type IdentifierFilter func(Identifier) bool
var (
// ZeroID is the lowest value in the 32-byte ID space.
ZeroID = Identifier{}
)
// HexStringToIdentifier converts a hex string to an identifier. The input
// must be 64 characters long and contain only valid hex characters.
func HexStringToIdentifier(hexString string) (Identifier, error) {
var identifier Identifier
i, err := hex.Decode(identifier[:], []byte(hexString))
if err != nil {
return identifier, err
}
if i != 32 {
return identifier, fmt.Errorf("malformed input, expected 32 bytes (64 characters), decoded %d", i)
}
return identifier, nil
}
func MustHexStringToIdentifier(hexString string) Identifier {
id, err := HexStringToIdentifier(hexString)
if err != nil {
panic(err)
}
return id
}
// String returns the hex string representation of the identifier.
func (id Identifier) String() string {
return hex.EncodeToString(id[:])
}
// Format handles formatting of id for different verbs. This is called when
// formatting an identifier with fmt.
func (id Identifier) Format(state fmt.State, verb rune) {
switch verb {
case 'x', 's', 'v':
_, _ = state.Write([]byte(id.String()))
default:
_, _ = state.Write([]byte(fmt.Sprintf("%%!%c(%s=%s)", verb, reflect.TypeOf(id), id)))
}
}
func (id Identifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
func (id *Identifier) UnmarshalText(text []byte) error {
var err error
*id, err = HexStringToIdentifier(string(text))
return err
}
func HashToID(hash []byte) Identifier {
var id Identifier
copy(id[:], hash)
return id
}
// MakeID creates an ID from a hash of encoded data. MakeID uses `model.Fingerprint() []byte` to get the byte
// representation of the entity, which uses RLP to encode the data. If the input defines its own canonical encoding by
// implementing Fingerprinter, it uses that instead. That allows removal of non-unique fields from structs or
// overwriting of the used encoder. We are using Fingerprint instead of the default encoding for two reasons: a) JSON
// (the default encoding) does not specify an order for the elements of arrays and objects, which could lead to
// different hashes depending on the JSON implementation and b) the Fingerprinter interface allows to exclude fields not
// needed in the pre-image of the hash that comprises the Identifier, which could be different from the encoding for
// sending entities in messages or for storing them.
func MakeID(entity interface{}) Identifier {
hasher := hash.NewSHA3_256()
hash := hasher.ComputeHash(fingerprint.Fingerprint(entity))
return HashToID(hash)
}
// PublicKeyToID creates an ID from a public key.
func PublicKeyToID(pub crypto.PublicKey) (Identifier, error) {
b := pub.Encode()
hasher := hash.NewSHA3_256()
hash := hasher.ComputeHash(b)
return HashToID(hash), nil
}
// GetIDs gets the IDs for a slice of entities.
func GetIDs(value interface{}) []Identifier {
v := reflect.ValueOf(value)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("non-slice value (%T)", value))
}
slice := make([]Identifier, 0, v.Len())
for i := 0; i < v.Len(); i++ {
entity, ok := v.Index(i).Interface().(Entity)
if !ok {
panic(fmt.Sprintf("slice contains non-entity (%T)", v.Index(i).Interface()))
}
slice = append(slice, entity.ID())
}
return slice
}
func MerkleRoot(ids ...Identifier) Identifier {
var root Identifier
tree := merkle.NewTree()
for _, id := range ids {
tree.Put(id[:], nil)
}
hash := tree.Hash()
copy(root[:], hash)
return root
}
func CheckMerkleRoot(root Identifier, ids ...Identifier) bool {
computed := MerkleRoot(ids...)
return root == computed
}
func ConcatSum(ids ...Identifier) Identifier {
var sum Identifier
hasher := hash.NewSHA3_256()
for _, id := range ids {
_, _ = hasher.Write(id[:])
}
hash := hasher.SumHash()
copy(sum[:], hash)
return sum
}
func CheckConcatSum(sum Identifier, fps ...Identifier) bool {
computed := ConcatSum(fps...)
return sum == computed
}
// Sample returns random sample of length 'size' of the ids
// [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
func Sample(size uint, ids ...Identifier) []Identifier {
n := uint(len(ids))
dup := make([]Identifier, 0, n)
dup = append(dup, ids...)
// if sample size is greater than total size, return all the elements
if n <= size {
return dup
}
for i := uint(0); i < size; i++ {
j := uint(rand.Intn(int(n - i)))
dup[i], dup[j+i] = dup[j+i], dup[i]
}
return dup[:size]
}