-
Notifications
You must be signed in to change notification settings - Fork 5
/
genesis_builder.go
75 lines (58 loc) · 1.55 KB
/
genesis_builder.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
package aura
import (
"encoding/json"
"errors"
sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/primitives/types"
"github.com/vedhavyas/go-subkey"
)
var (
errAuthoritiesAlreadyInitialized = errors.New("Authorities are already initialized!")
errAuthoritiesExceedMaxAuthorities = errors.New("Initial authority set must be less than MaxAuthorities")
)
type GenesisConfig struct {
Authorities sc.Sequence[types.Sr25519PublicKey]
}
type genesisConfigJsonStruct struct {
AuraGenesisConfig struct {
Authorities []string `json:"authorities"`
} `json:"aura"`
}
func (gc *GenesisConfig) UnmarshalJSON(data []byte) error {
gcJson := genesisConfigJsonStruct{}
if err := json.Unmarshal(data, &gcJson); err != nil {
return err
}
addrExists := map[string]bool{}
for _, a := range gcJson.AuraGenesisConfig.Authorities {
if addrExists[a] {
continue
}
_, pubKeyBytes, err := subkey.SS58Decode(a)
if err != nil {
return err
}
pubKey, err := types.NewSr25519PublicKey(sc.BytesToSequenceU8(pubKeyBytes)...)
if err != nil {
return err
}
gc.Authorities = append(gc.Authorities, pubKey)
addrExists[a] = true
}
return nil
}
func (m Module) CreateDefaultConfig() ([]byte, error) {
gc := genesisConfigJsonStruct{}
gc.AuraGenesisConfig.Authorities = []string{}
return json.Marshal(gc)
}
func (m Module) BuildConfig(config []byte) error {
gc := GenesisConfig{}
if err := json.Unmarshal(config, &gc); err != nil {
return err
}
if len(gc.Authorities) == 0 {
return nil
}
return m.initializeAuthorities(gc.Authorities)
}