-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmetadata.go
62 lines (55 loc) · 1.9 KB
/
metadata.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
package scalecodec
import (
"errors"
"github.com/itering/scale.go/types"
"github.com/itering/scale.go/types/scaleBytes"
"github.com/itering/scale.go/utiles"
)
type MetadataDecoder struct {
types.ScaleDecoder
Version string `json:"version"`
Metadata types.MetadataStruct `json:"metadata"`
}
func (m *MetadataDecoder) Init(data []byte) {
sData := scaleBytes.ScaleBytes{Data: data}
m.ScaleDecoder.Init(sData, nil)
}
func (m *MetadataDecoder) Process() error {
magicBytes := m.NextBytes(4)
if string(magicBytes) != "meta" {
m.Data.Reset()
m.ProcessAndUpdateData("Option<Compact<u32>>") // Option<Compact<u32>>
magicBytes = m.NextBytes(4)
}
if string(magicBytes) == "meta" {
metadataVersion := utiles.U256(utiles.BytesToHex(m.Data.Data[m.Data.Offset : m.Data.Offset+1]))
m.Version = m.ProcessAndUpdateData("MetadataVersion").(string)
m.Metadata = m.ProcessAndUpdateData(m.Version).(types.MetadataStruct)
m.Metadata.MetadataVersion = int(metadataVersion.Int64())
return nil
}
return errors.New("not metadata")
}
func (m *MetadataDecoder) PortableRegistry() (map[int]types.SiType, error) {
magicBytes := m.NextBytes(4)
if string(magicBytes) != "meta" { // metadata version >= v15
m.Data.Reset()
m.ProcessAndUpdateData("Option<Compact<u32>>")
magicBytes = m.NextBytes(4)
}
if string(magicBytes) == "meta" {
metadataVersion := utiles.U256(utiles.BytesToHex(m.Data.Data[m.Data.Offset : m.Data.Offset+1]))
m.Version = m.ProcessAndUpdateData("MetadataVersion").(string)
if metadataVersion.Int64() < 14 {
return nil, errors.New("PortableRegistry not support, only metadata version >= v14")
}
portable := types.InitPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
var registry = make(map[int]types.SiType)
for index, item := range portable {
item.RemoveDocs()
registry[index] = item
}
return registry, nil
}
return nil, errors.New("not metadata")
}