code.vegaprotocol.io/vega@v0.79.0/commands/announce_node.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"encoding/hex"
    20  
    21  	"code.vegaprotocol.io/vega/libs/crypto"
    22  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    23  )
    24  
    25  func CheckAnnounceNode(cmd *commandspb.AnnounceNode) error {
    26  	return checkAnnounceNode(cmd).ErrorOrNil()
    27  }
    28  
    29  func checkAnnounceNode(cmd *commandspb.AnnounceNode) Errors {
    30  	errs := NewErrors()
    31  
    32  	if cmd == nil {
    33  		return errs.FinalAddForProperty("announce_node", ErrIsRequired)
    34  	}
    35  
    36  	if len(cmd.VegaPubKey) == 0 {
    37  		errs.AddForProperty("announce_node.vega_pub_key", ErrIsRequired)
    38  	} else if !IsVegaPublicKey(cmd.VegaPubKey) {
    39  		errs.AddForProperty("announce_node.vega_pub_key", ErrShouldBeAValidVegaPublicKey)
    40  	}
    41  
    42  	if len(cmd.Id) == 0 {
    43  		errs.AddForProperty("announce_node.id", ErrIsRequired)
    44  	} else if !IsVegaPublicKey(cmd.Id) {
    45  		errs.AddForProperty("announce_node.id", ErrShouldBeAValidVegaPublicKey)
    46  	}
    47  
    48  	if len(cmd.EthereumAddress) == 0 {
    49  		errs.AddForProperty("announce_node.ethereum_address", ErrIsRequired)
    50  	} else if !crypto.EthereumIsValidAddress(cmd.EthereumAddress) {
    51  		errs.AddForProperty("announce_node.ethereum_address", ErrIsNotValidEthereumAddress)
    52  	}
    53  
    54  	if len(cmd.ChainPubKey) == 0 {
    55  		errs.AddForProperty("announce_node.chain_pub_key", ErrIsRequired)
    56  	}
    57  
    58  	if cmd.EthereumSignature == nil || len(cmd.EthereumSignature.Value) == 0 {
    59  		errs.AddForProperty("announce_node.ethereum_signature", ErrIsRequired)
    60  	} else {
    61  		_, err := hex.DecodeString(cmd.EthereumSignature.Value)
    62  		if err != nil {
    63  			errs.AddForProperty("announce_node.ethereum_signature.value", ErrShouldBeHexEncoded)
    64  		}
    65  	}
    66  
    67  	if cmd.VegaSignature == nil || len(cmd.VegaSignature.Value) == 0 {
    68  		errs.AddForProperty("announce_node.vega_signature", ErrIsRequired)
    69  	} else {
    70  		_, err := hex.DecodeString(cmd.VegaSignature.Value)
    71  		if err != nil {
    72  			errs.AddForProperty("announce_node.vega_signature.value", ErrShouldBeHexEncoded)
    73  		}
    74  	}
    75  
    76  	if len(cmd.SubmitterAddress) != 0 && !crypto.EthereumIsValidAddress(cmd.SubmitterAddress) {
    77  		errs.AddForProperty("announce_node.submitter_address", ErrIsNotValidEthereumAddress)
    78  	}
    79  
    80  	return errs
    81  }