code.vegaprotocol.io/vega@v0.79.0/commands/ethereum_key_rotate_submission_test.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_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestSubmittingNonEthereumKeyRotateSubmissionCommandFails(t *testing.T) {
    29  	err := checkEthereumKeyRotateSubmission(nil)
    30  
    31  	assert.Contains(t, err.Get("ethereum_key_rotate_submission"), commands.ErrIsRequired)
    32  }
    33  
    34  func TestEthereumKeyRotateSubmissionSubmittingEmptyCommandFails(t *testing.T) {
    35  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{})
    36  
    37  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.new_address"), commands.ErrIsRequired)
    38  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.current_address"), commands.ErrIsRequired)
    39  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.target_block"), commands.ErrIsRequired)
    40  }
    41  
    42  func TestEthereumKeyRotateSubmissionMissingNewAddressFails(t *testing.T) {
    43  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
    44  		TargetBlock:    100,
    45  		CurrentAddress: "0xED816fd7a6e39bce5d2df6A756F3812da06960fC",
    46  		EthereumSignature: &commandspb.Signature{
    47  			Value: "deadbeef",
    48  			Algo:  "vega/ed25519",
    49  		},
    50  	})
    51  
    52  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.new_address"), commands.ErrIsRequired)
    53  }
    54  
    55  func TestEthereumKeyRotateSubmissionMissingCurrentAddressFails(t *testing.T) {
    56  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
    57  		NewAddress:  "0xE7d65d1A6CD6eCcfbE78A5Aea2f096Dc60C4C127",
    58  		TargetBlock: 100,
    59  		EthereumSignature: &commandspb.Signature{
    60  			Value: "deadbeef",
    61  			Algo:  "vega/ed25519",
    62  		},
    63  	})
    64  
    65  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.current_address"), commands.ErrIsRequired)
    66  }
    67  
    68  func TestEthereumKeyRotateSubmissionMissingTargetBlockFails(t *testing.T) {
    69  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
    70  		NewAddress:     "0xE7d65d1A6CD6eCcfbE78A5Aea2f096Dc60C4C127",
    71  		CurrentAddress: "0xED816fd7a6e39bce5d2df6A756F3812da06960fC",
    72  		EthereumSignature: &commandspb.Signature{
    73  			Value: "deadbeef",
    74  			Algo:  "vega/ed25519",
    75  		},
    76  	})
    77  
    78  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.target_block"), commands.ErrIsRequired)
    79  }
    80  
    81  func TestSubmittingNonEmptyEthereumKeyRotateSubmissionCommandSuccess(t *testing.T) {
    82  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
    83  		TargetBlock:    100,
    84  		NewAddress:     "0xE7d65d1A6CD6eCcfbE78A5Aea2f096Dc60C4C127",
    85  		CurrentAddress: "0xED816fd7a6e39bce5d2df6A756F3812da06960fC",
    86  		EthereumSignature: &commandspb.Signature{
    87  			Value: "deadbeef",
    88  			Algo:  "vega/ed25519",
    89  		},
    90  	})
    91  
    92  	assert.True(t, err.Empty())
    93  }
    94  
    95  func TestEthereumKeyRotateSubmissionInvalidEthereumAddresses(t *testing.T) {
    96  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
    97  		NewAddress:       "0xE7d65d1A6CD6eCc",
    98  		CurrentAddress:   "0xED816fd7a6e",
    99  		SubmitterAddress: "0xED816fd7a6e",
   100  		EthereumSignature: &commandspb.Signature{
   101  			Value: "deadbeef",
   102  			Algo:  "vega/ed25519",
   103  		},
   104  	})
   105  
   106  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.new_address"), commands.ErrIsNotValidEthereumAddress)
   107  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.current_address"), commands.ErrIsNotValidEthereumAddress)
   108  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.submitter_address"), commands.ErrIsNotValidEthereumAddress)
   109  }
   110  
   111  func TestSubmittingNonEmptyEthereumKeyRotateSubmissionWithoutSigFails(t *testing.T) {
   112  	err := checkEthereumKeyRotateSubmission(&commandspb.EthereumKeyRotateSubmission{
   113  		TargetBlock:    100,
   114  		NewAddress:     "0xE7d65d1A6CD6eCcfbE78A5Aea2f096Dc60C4C127",
   115  		CurrentAddress: "0xED816fd7a6e39bce5d2df6A756F3812da06960fC",
   116  	})
   117  
   118  	assert.Contains(t, err.Get("ethereum_key_rotate_submission.signature"), commands.ErrIsRequired)
   119  }
   120  
   121  func checkEthereumKeyRotateSubmission(cmd *commandspb.EthereumKeyRotateSubmission) commands.Errors {
   122  	err := commands.CheckEthereumKeyRotateSubmission(cmd)
   123  
   124  	var e commands.Errors
   125  	if ok := errors.As(err, &e); !ok {
   126  		return commands.NewErrors()
   127  	}
   128  	return e
   129  }