-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
70 lines (59 loc) · 1.73 KB
/
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
package relay
import (
"encoding/hex"
"fmt"
"strings"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
lcptypes "github.com/datachainlab/lcp-go/light-clients/lcp/types"
"github.com/hyperledger-labs/yui-relayer/core"
)
var _ core.ProverConfig = (*ProverConfig)(nil)
var _ codectypes.UnpackInterfacesMessage = (*ProverConfig)(nil)
func (cfg *ProverConfig) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if cfg == nil {
return nil
}
if err := unpacker.UnpackAny(cfg.OriginProver, new(core.ProverConfig)); err != nil {
return err
}
return nil
}
func (pc ProverConfig) Build(chain core.Chain) (core.Prover, error) {
if err := pc.Validate(); err != nil {
return nil, err
}
prover, err := pc.OriginProver.GetCachedValue().(core.ProverConfig).Build(chain)
if err != nil {
return nil, err
}
return NewProver(pc, chain, prover)
}
func (pc ProverConfig) GetMrenclave() []byte {
mrenclave, err := decodeMrenclaveHex(pc.Mrenclave)
if err != nil {
panic(err)
}
return mrenclave
}
func (pc ProverConfig) Validate() error {
// origin prover config validation
if err := pc.OriginProver.GetCachedValue().(core.ProverConfig).Validate(); err != nil {
return fmt.Errorf("failed to validate the origin prover's config: %v", err)
}
// lcp prover config validation
mrenclave, err := decodeMrenclaveHex(pc.Mrenclave)
if err != nil {
return err
}
if l := len(mrenclave); l != lcptypes.MrenclaveSize {
return fmt.Errorf("MRENCLAVE length must be %v, but got %v", lcptypes.MrenclaveSize, l)
}
if pc.KeyExpiration == 0 {
return fmt.Errorf("KeyExpiration must be greater than 0")
}
return nil
}
func decodeMrenclaveHex(s string) ([]byte, error) {
s = strings.ToLower(strings.TrimPrefix(s, "0x"))
return hex.DecodeString(s)
}