Skip to content

Commit

Permalink
improve operator config validations
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jun 21, 2024
1 parent 79d6a1d commit f1f19bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
30 changes: 25 additions & 5 deletions relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,39 @@ func (pc ProverConfig) Validate() error {
}
if l := len(pc.Operators); l > 1 {
return fmt.Errorf("Operators: currently only one or zero(=permissionless) operator is supported, but got %v", l)
} else if l == 0 {
return nil
}
if len(pc.Operators) > 0 || pc.OperatorsEip712Params != nil {
if pc.OperatorSigner == nil {
return fmt.Errorf("OperatorSigner must be set if Operators or OperatorsEip712Params is set")
}

// ----- operators config validation -----

if pc.OperatorSigner == nil {
return fmt.Errorf("OperatorSigner must be set if Operators or OperatorsEip712Params is set")
}
if pc.OperatorsEip712Params != nil {
{
signerConfig, ok := pc.OperatorSigner.GetCachedValue().(signer.SignerConfig)
if !ok {
return fmt.Errorf("failed to cast OperatorSigner's config: %T", pc.OperatorSigner.GetCachedValue())
} else if err := signerConfig.Validate(); err != nil {
return fmt.Errorf("failed to validate the OperatorSigner's config: %v", err)
}
signer, err := signerConfig.Build()
if err != nil {
return fmt.Errorf("failed to build the OperatorSigner: %v", err)
}
addr, err := NewEIP712Signer(signer).GetSignerAddress()
if err != nil {
return fmt.Errorf("failed to get the OperatorSigner's address: %v", err)
}
op, err := decodeOperatorAddress(pc.Operators[0])
if err != nil {
return fmt.Errorf("failed to decode operator address: %v", err)
}
if addr != op {
return fmt.Errorf("OperatorSigner's address must be equal to the first operator's address: %v != %v", addr, op)
}
}
if pc.OperatorsEip712Params != nil {
switch params := pc.OperatorsEip712Params.(type) {
case *ProverConfig_OperatorsEip712EvmChainParams:
if params.OperatorsEip712EvmChainParams.ChainId == 0 {
Expand Down
17 changes: 12 additions & 5 deletions relay/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ func (pr *Prover) IsOperatorEnabled() bool {

func (pr *Prover) GetOperators() ([]common.Address, error) {
var operators []common.Address
for _, operator := range pr.config.Operators {
addrStr := strings.TrimPrefix(operator, "0x")
if len(addrStr) != 40 {
return nil, fmt.Errorf("invalid operator address length %v", len(addrStr))
for i, operator := range pr.config.Operators {
addr, err := decodeOperatorAddress(operator)
if err != nil {
return nil, fmt.Errorf("failed to decode operator address: index=%v, operator=%v %w", i, operator, err)
}
addr := common.HexToAddress(operator)
operators = append(operators, addr)
}
return operators, nil
Expand Down Expand Up @@ -144,3 +143,11 @@ func (s EIP712Signer) GetSignerAddress() (common.Address, error) {
}
return crypto.PubkeyToAddress(*pubKey), nil
}

func decodeOperatorAddress(s string) (common.Address, error) {
addrStr := strings.TrimPrefix(s, "0x")
if len(addrStr) != 40 {
return common.Address{}, fmt.Errorf("invalid operator address length %v", len(addrStr))
}
return common.HexToAddress(s), nil
}

0 comments on commit f1f19bc

Please sign in to comment.