code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/transaction_sign_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 cmd_test
    17  
    18  import (
    19  	"encoding/json"
    20  	"testing"
    21  
    22  	cmd "code.vegaprotocol.io/vega/cmd/vegawallet/commands"
    23  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    24  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    25  	"code.vegaprotocol.io/vega/wallet/api"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestSignCommandFlags(t *testing.T) {
    32  	t.Run("Valid flags succeeds", testSignCommandFlagsValidFlagsSucceeds)
    33  	t.Run("Missing wallet fails", testSignCommandFlagsMissingWalletFails)
    34  	t.Run("Missing chain ID fails", testSignCommandFlagsMissingChainIDFails)
    35  	t.Run("Missing public key fails", testSignCommandFlagsMissingPubKeyFails)
    36  	t.Run("Missing tx height fails", testSignCommandFlagsMissingTxBlockHeightFails)
    37  	t.Run("Missing tx height fails", testSignCommandFlagsMissingTxBlockHashFails)
    38  	t.Run("Missing pow difficulty fails", testSignCommandFlagsMissingPoWDifficultyFails)
    39  	t.Run("Missing request fails", testSignCommandFlagsMissingRequestFails)
    40  	t.Run("Network and PoW mutually exclusive", testSignCommandFlagsNetworkPoWMutuallyExclusive)
    41  }
    42  
    43  func testSignCommandFlagsValidFlagsSucceeds(t *testing.T) {
    44  	testDir := t.TempDir()
    45  
    46  	// given
    47  	expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir)
    48  	walletName := vgrand.RandomStr(10)
    49  	pubKey := vgrand.RandomStr(20)
    50  
    51  	f := &cmd.SignTransactionFlags{
    52  		Wallet:         walletName,
    53  		PubKey:         pubKey,
    54  		PassphraseFile: passphraseFilePath,
    55  		Network:        "fairground",
    56  		RawTransaction: `{"voteSubmission": {"proposalId": "ec066610abbd1736b69cadcb059b9efdfdd9e3e33560fc46b2b8b62764edf33f", "value": "VALUE_YES"}}`,
    57  	}
    58  
    59  	expectedTx := make(map[string]any)
    60  	assert.NoError(t, json.Unmarshal([]byte(f.RawTransaction), &expectedTx))
    61  
    62  	expectedReq := api.AdminSignTransactionParams{
    63  		Wallet:      walletName,
    64  		Network:     "fairground",
    65  		PublicKey:   pubKey,
    66  		Transaction: expectedTx,
    67  	}
    68  
    69  	// when
    70  	req, passphrase, err := f.Validate()
    71  
    72  	// then
    73  	require.NoError(t, err)
    74  	require.NotNil(t, req)
    75  
    76  	assert.Equal(t, expectedReq, req)
    77  	assert.Equal(t, expectedPassphrase, passphrase)
    78  }
    79  
    80  func testSignCommandFlagsMissingWalletFails(t *testing.T) {
    81  	testDir := t.TempDir()
    82  
    83  	// given
    84  	f := newSignCommandFlags(t, testDir)
    85  	f.Wallet = ""
    86  
    87  	// when
    88  	req, _, err := f.Validate()
    89  
    90  	// then
    91  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet"))
    92  	assert.Empty(t, req)
    93  }
    94  
    95  func testSignCommandFlagsMissingChainIDFails(t *testing.T) {
    96  	testDir := t.TempDir()
    97  
    98  	// given
    99  	f := newSignCommandFlags(t, testDir)
   100  	f.ChainID = ""
   101  
   102  	// when
   103  	req, _, err := f.Validate()
   104  
   105  	// then
   106  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("chain-id"))
   107  	assert.Empty(t, req)
   108  }
   109  
   110  func testSignCommandFlagsMissingPubKeyFails(t *testing.T) {
   111  	testDir := t.TempDir()
   112  
   113  	// given
   114  	f := newSignCommandFlags(t, testDir)
   115  	f.PubKey = ""
   116  
   117  	// when
   118  	req, _, err := f.Validate()
   119  
   120  	// then
   121  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pubkey"))
   122  	assert.Empty(t, req)
   123  }
   124  
   125  func testSignCommandFlagsMissingTxBlockHeightFails(t *testing.T) {
   126  	testDir := t.TempDir()
   127  
   128  	// given
   129  	f := newSignCommandFlags(t, testDir)
   130  	f.TxBlockHeight = 0
   131  
   132  	// when
   133  	req, _, err := f.Validate()
   134  
   135  	// then
   136  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("tx-height"))
   137  	assert.Empty(t, req)
   138  }
   139  
   140  func testSignCommandFlagsMissingTxBlockHashFails(t *testing.T) {
   141  	testDir := t.TempDir()
   142  
   143  	// given
   144  	f := newSignCommandFlags(t, testDir)
   145  	f.TxBlockHash = ""
   146  
   147  	// when
   148  	req, _, err := f.Validate()
   149  
   150  	// then
   151  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("tx-block-hash"))
   152  	assert.Empty(t, req)
   153  }
   154  
   155  func testSignCommandFlagsMissingPoWDifficultyFails(t *testing.T) {
   156  	testDir := t.TempDir()
   157  
   158  	// given
   159  	f := newSignCommandFlags(t, testDir)
   160  	f.PowDifficulty = 0
   161  
   162  	// when
   163  	req, _, err := f.Validate()
   164  
   165  	// then
   166  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pow-difficulty"))
   167  	assert.Empty(t, req)
   168  }
   169  
   170  func testSignCommandFlagsNetworkPoWMutuallyExclusive(t *testing.T) {
   171  	testDir := t.TempDir()
   172  
   173  	// given
   174  	f := newSignCommandFlags(t, testDir)
   175  	f.Network = "fairground"
   176  
   177  	// when
   178  	req, _, err := f.Validate()
   179  
   180  	// then
   181  	assert.ErrorIs(t, err, flags.MutuallyExclusiveError("network", "tx-height"))
   182  	assert.Empty(t, req)
   183  }
   184  
   185  func testSignCommandFlagsMissingRequestFails(t *testing.T) {
   186  	testDir := t.TempDir()
   187  
   188  	// given
   189  	f := newSignCommandFlags(t, testDir)
   190  	f.RawTransaction = ""
   191  
   192  	// when
   193  	req, _, err := f.Validate()
   194  
   195  	// then
   196  	assert.ErrorIs(t, err, flags.ArgMustBeSpecifiedError("transaction"))
   197  	assert.Empty(t, req)
   198  }
   199  
   200  func newSignCommandFlags(t *testing.T, testDir string) *cmd.SignTransactionFlags {
   201  	t.Helper()
   202  
   203  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
   204  	walletName := vgrand.RandomStr(10)
   205  	pubKey := vgrand.RandomStr(20)
   206  
   207  	return &cmd.SignTransactionFlags{
   208  		RawTransaction: `{"voteSubmission": {"proposalId": "some-id", "value": "VALUE_YES"}}`,
   209  		Wallet:         walletName,
   210  		PubKey:         pubKey,
   211  		TxBlockHeight:  150,
   212  		ChainID:        vgrand.RandomStr(5),
   213  		TxBlockHash:    "hashhash",
   214  		PowDifficulty:  12,
   215  		PassphraseFile: passphraseFilePath,
   216  	}
   217  }