-
Notifications
You must be signed in to change notification settings - Fork 212
/
nodeid.go
73 lines (57 loc) · 1.82 KB
/
nodeid.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
package types
import (
"encoding/hex"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/log"
)
// BytesToNodeID is a helper to copy buffer into NodeID struct.
func BytesToNodeID(buf []byte) (id NodeID) {
copy(id[:], buf)
return id
}
// NodeID contains a miner's public key.
type NodeID Hash32
const (
// NodeIDSize in bytes.
NodeIDSize = Hash32Length
)
// String returns a string representation of the NodeID, for logging purposes.
// It implements the Stringer interface.
func (id NodeID) String() string {
return hex.EncodeToString(id.Bytes())
}
// Bytes returns the byte representation of the Edwards public key.
func (id NodeID) Bytes() []byte {
return id[:]
}
// ShortString returns a the first 5 characters of the ID, for logging purposes.
func (id NodeID) ShortString() string {
return Shorten(id.String(), 5)
}
// Field returns a log field. Implements the LoggableField interface.
func (id NodeID) Field() log.Field { return log.Stringer("node_id", id) }
// EmptyNodeID is a canonical empty NodeID.
var EmptyNodeID NodeID
// EncodeScale implements scale codec interface.
func (id *NodeID) EncodeScale(e *scale.Encoder) (int, error) {
return scale.EncodeByteArray(e, id[:])
}
// DecodeScale implements scale codec interface.
func (id *NodeID) DecodeScale(d *scale.Decoder) (int, error) {
return scale.DecodeByteArray(d, id[:])
}
func (id *NodeID) MarshalText() ([]byte, error) {
return util.Base64Encode(id[:]), nil
}
func (id *NodeID) UnmarshalText(buf []byte) error {
return util.Base64Decode(id[:], buf)
}
// NodeIDsToHashes turns a list of NodeID into their Hash32 representation.
func NodeIDsToHashes(ids []NodeID) []Hash32 {
hashes := make([]Hash32, 0, len(ids))
for _, id := range ids {
hashes = append(hashes, Hash32(id))
}
return hashes
}