-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_tag.go
53 lines (43 loc) · 967 Bytes
/
struct_tag.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
package glav
import (
"bytes"
"golang.org/x/crypto/sha3"
)
const (
ResourceTag = 0x1
ModuleTag = 0x0
)
type StructTag struct {
address [20]byte
moduleName string
structName string
typeParams []TypeParam
}
func (tag StructTag) AccessVector() []byte {
h := sha3.New256()
access := make([]byte, 33)
access[0] = ResourceTag
h.Write(structTagSalt[:])
var buffer bytes.Buffer
tag.marshal(&buffer)
h.Write(buffer.Bytes())
h.Sum(access[:1])
return access
}
func (tag StructTag) marshal(buffer *bytes.Buffer) {
buffer.Write(tag.address[0:])
writeStringToBuffer(buffer, tag.moduleName)
writeStringToBuffer(buffer, tag.structName)
writeUintToBuffer(buffer, uint64(len(tag.typeParams)))
for _, param := range tag.typeParams {
param.marshal(buffer)
}
}
func NewStructTag(address [20]byte, moduleName string, structName string, typeParams []TypeParam) StructTag {
return StructTag{
address,
moduleName,
structName,
typeParams,
}
}