/
name.go
121 lines (101 loc) · 2.14 KB
/
name.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
package chain
import "github.com/greymass/go-eosio/pkg/abi"
// Type representing an EOSIO name.
type Name uint64
// Create a new name from a string, e.g. "teamgreymass".
func NewName(s string) Name {
return Name(stringToName(s))
}
// Convenience for NewName.
func N(s string) Name {
return Name(stringToName(s))
}
// Returns string representation of the name.
func (n Name) String() string {
return nameToString(uint64(n))
}
// abi.Marshaler conformance
func (n Name) MarshalABI(e *abi.Encoder) error {
return e.WriteUint64(uint64(n))
}
// abi.Unmarshaler conformance
func (n *Name) UnmarshalABI(d *abi.Decoder) error {
v, err := d.ReadUint64()
if err == nil {
*n = Name(v)
}
return err
}
// encoding.TextMarshaler conformance
func (n Name) MarshalText() (text []byte, err error) {
return []byte(n.String()), nil
}
// encoding.TextUnmarshaler conformance
func (n *Name) UnmarshalText(text []byte) error {
new := NewName(string(text))
*n = new
return nil
}
// from https://github.com/eoscanada/eos-go/blob/21697b8969f6446181086db27ae7e7a302bf166d/name.go
var base32Alphabet = []byte(".12345abcdefghijklmnopqrstuvwxyz")
func nameToString(value uint64) string {
a := []byte{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}
tmp := value
i := uint32(0)
for ; i <= 12; i++ {
bit := 0x1f
if i == 0 {
bit = 0x0f
}
c := base32Alphabet[tmp&uint64(bit)]
a[12-i] = c
shift := uint(5)
if i == 0 {
shift = 4
}
tmp >>= shift
}
return trimRightDots(a)
}
func trimRightDots(bytes []byte) string {
trimUpTo := -1
for i := 12; i >= 0; i-- {
if bytes[i] == '.' {
trimUpTo = i
} else {
break
}
}
if trimUpTo == -1 {
return string(bytes)
}
return string(bytes[0:trimUpTo])
}
func stringToName(s string) uint64 {
var rv uint64 = 0
var i uint32
sLen := uint32(len(s))
for ; i <= 12; i++ {
var c uint64
if i < sLen {
c = uint64(charToSymbol(s[i]))
}
if i < 12 {
c &= 0x1f
c <<= 64 - 5*(i+1)
} else {
c &= 0x0f
}
rv |= c
}
return rv
}
func charToSymbol(c byte) byte {
if c >= 'a' && c <= 'z' {
return c - 'a' + 6
}
if c >= '1' && c <= '5' {
return c - '1' + 1
}
return 0
}