-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmethod_config.go
88 lines (76 loc) · 2.19 KB
/
method_config.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
package c_polygonid
import (
"encoding/json"
"errors"
"hash/fnv"
"math"
"math/big"
core "github.com/iden3/go-iden3-core/v2"
)
type MethodConfig struct {
MethodName core.DIDMethod `json:"name"`
Blockchain core.Blockchain `json:"blockchain"`
NetworkID core.NetworkID `json:"network"`
NetworkFlag byte `json:"networkFlag"`
MethodByte byte `json:"methodByte"`
ChainID core.ChainID `json:"chainId"`
}
func (cfg *MethodConfig) UnmarshalJSON(in []byte) error {
var j struct {
MethodName *core.DIDMethod `json:"name"`
Blockchain *core.Blockchain `json:"blockchain"`
NetworkID *core.NetworkID `json:"network"`
NetworkFlag *jsonByte `json:"networkFlag"`
MethodByte *jsonByte `json:"methodByte"`
ChainID *jsonNumber `json:"chainId"`
}
err := json.Unmarshal(in, &j)
if err != nil {
return err
}
if j.MethodName == nil {
return errors.New("method name is empty")
}
cfg.MethodName = *j.MethodName
if j.Blockchain == nil {
return errors.New("blockchain is empty")
}
cfg.Blockchain = *j.Blockchain
if j.NetworkID == nil {
return errors.New("network ID is empty")
}
cfg.NetworkID = *j.NetworkID
if j.NetworkFlag == nil {
return errors.New("network flag is not set")
}
cfg.NetworkFlag = byte(*j.NetworkFlag)
if j.MethodByte == nil {
return errors.New("method byte is not set")
}
cfg.MethodByte = byte(*j.MethodByte)
if j.ChainID == nil {
return errors.New("chain ID is not set")
}
assertUnderlineTypeInt32(cfg.ChainID)
chainID := (*big.Int)(j.ChainID)
if !chainID.IsInt64() {
return errors.New("chain ID is not inside int32 bounds")
}
chainIDi := chainID.Int64()
if chainIDi > math.MaxInt32 || chainIDi < math.MinInt32 {
return errors.New("chain ID is not inside int32 bounds")
}
cfg.ChainID = core.ChainID(chainIDi)
return nil
}
// Hash generate a unique hash for the method config
func (cfg MethodConfig) Hash() uint64 {
h := fnv.New64a()
// errors are always nil
_, _ = h.Write([]byte(cfg.MethodName))
_, _ = h.Write([]byte(cfg.Blockchain))
_, _ = h.Write([]byte(cfg.NetworkID))
_, _ = h.Write([]byte{cfg.NetworkFlag, cfg.MethodByte})
_, _ = h.Write(chainIDToBytes(cfg.ChainID))
return h.Sum64()
}