-
Notifications
You must be signed in to change notification settings - Fork 106
/
namespace.go
143 lines (111 loc) · 3.53 KB
/
namespace.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
package common
import (
"bytes"
"encoding"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
)
const (
// NamespaceSize is the size of a chain namespace identifier in bytes.
NamespaceSize = 32
// NamespaceHexSize is the size of the chain namespace identifier in string format.
NamespaceHexSize = NamespaceSize * 2
// NamespaceIDSize is the size of the identifier component of a namespace.
NamespaceIDSize = NamespaceSize - 8
NamespaceTest NamespaceFlag = 1 << 63
NamespaceKeyManager NamespaceFlag = 1 << 62
flagsReserved = ^(NamespaceTest | NamespaceKeyManager)
)
var (
// ErrMalformedNamespace is the error returned when a namespace
// identifier is malformed.
ErrMalformedNamespace = errors.New("malformed namespace")
_ encoding.BinaryMarshaler = (*Namespace)(nil)
_ encoding.BinaryUnmarshaler = (*Namespace)(nil)
)
// NamespaceFlag is a namespace flag.
type NamespaceFlag uint64
// Namespace is a chain namespace identifier.
type Namespace [NamespaceSize]byte
// MarshalBinary encodes a namespace identifier into binary form.
func (n *Namespace) MarshalBinary() (data []byte, err error) {
data = append([]byte{}, n[:]...)
return
}
// UnmarshalBinary decodes a binary marshaled namespace identifier.
func (n *Namespace) UnmarshalBinary(data []byte) error {
if len(data) != NamespaceSize {
return ErrMalformedNamespace
}
copy(n[:], data)
if !n.isValid() {
return ErrMalformedNamespace
}
return nil
}
// MarshalText encodes a namespace identifier into text form.
func (n Namespace) MarshalText() (data []byte, err error) {
return []byte(base64.StdEncoding.EncodeToString(n[:])), nil
}
// UnmarshalText decodes a text marshaled namespace identifier.
func (n *Namespace) UnmarshalText(text []byte) error {
b, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return err
}
return n.UnmarshalBinary(b)
}
// UnmarshalHex deserializes a hexadecimal text string into the given type.
func (n *Namespace) UnmarshalHex(text string) error {
b, err := hex.DecodeString(text)
if err != nil {
return err
}
return n.UnmarshalBinary(b)
}
// Equal compares vs another namespace for equality.
func (n *Namespace) Equal(cmp *Namespace) bool {
if cmp == nil {
return false
}
return bytes.Equal(n[:], cmp[:])
}
// String returns the string representation of a chain namespace identifier.
func (n Namespace) String() string {
return hex.EncodeToString(n[:])
}
// IsTest returns true iff the namespace is for a test runtime.
func (n Namespace) IsTest() bool {
return n.flags()&NamespaceTest != 0
}
// IsKeyManager returns true iff the namespace is for a key manager runtime.
func (n Namespace) IsKeyManager() bool {
return n.flags()&NamespaceKeyManager != 0
}
func (n Namespace) isValid() bool {
return n.flags()&flagsReserved == 0
}
func (n Namespace) flags() NamespaceFlag {
return NamespaceFlag(binary.BigEndian.Uint64(n[0:8]))
}
// NewNamespace returns a new namespace from it's component ID and flags.
func NewNamespace(id [NamespaceIDSize]byte, flags NamespaceFlag) (Namespace, error) {
var n Namespace
binary.BigEndian.PutUint64(n[0:8], uint64(flags))
copy(n[8:], id[:])
if !n.isValid() {
return n, ErrMalformedNamespace
}
return n, nil
}
// NewTestNamespaceFromSeed returns a test namespace from a seed and flags.
func NewTestNamespaceFromSeed(seed []byte, flags NamespaceFlag) Namespace {
h := hash.NewFromBytes(seed)
var rtID [NamespaceIDSize]byte
copy(rtID[:], h[:])
ns, _ := NewNamespace(rtID, NamespaceTest|flags)
return ns
}