-
Notifications
You must be signed in to change notification settings - Fork 36
/
metadata.go
37 lines (31 loc) · 1 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
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.NextBytes(1) // maybe option<metadata> ?
// 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")
}