-
Notifications
You must be signed in to change notification settings - Fork 107
/
context.go
59 lines (48 loc) · 1.55 KB
/
context.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
package address
import (
"encoding"
"errors"
"fmt"
"sync"
)
// ContextIdentifierMaxSize is the maximum size of a context's identifier string.
const ContextIdentifierMaxSize = 64
var (
// ErrMalformedContext is the error returned when an address context is malformed.
ErrMalformedContext = errors.New("address: malformed context")
registeredContexts sync.Map
_ encoding.BinaryMarshaler = Context{}
)
// Context is a domain separation context for addresses.
type Context struct {
// Identifier is the context's identifier string.
Identifier string
// Version is the context's version.
Version uint8
}
// MarshalBinary encodes a context into binary form.
func (c Context) MarshalBinary() (data []byte, err error) {
data = append([]byte(c.Identifier), c.Version)
return
}
// String returns a string representation of address' context.
func (c Context) String() string {
return fmt.Sprintf("Context(Identifier: '%s', Version: %d)", c.Identifier, c.Version)
}
// NewContext creates and registers a new context. This routine will panic if
// the context is malformed or is already registered.
func NewContext(identifier string, version uint8) Context {
// NOTE: We disallow identifier lengths of 0 to enforce strict domain separation.
l := len(identifier)
if l == 0 {
panic(ErrMalformedContext)
}
if l > ContextIdentifierMaxSize {
panic(ErrMalformedContext)
}
ctx := Context{identifier, version}
if _, loaded := registeredContexts.LoadOrStore(ctx, true); loaded {
panic(fmt.Sprintf("address: context %s is already registered", ctx))
}
return ctx
}