code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/message_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/base64" 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 TestSignMessageFlags(t *testing.T) { 32 t.Run("Valid flags succeeds", testSignMessageFlagsValidFlagsSucceeds) 33 t.Run("Missing wallet fails", testSignMessageFlagsMissingWalletFails) 34 t.Run("Missing public key fails", testSignMessageFlagsMissingPubKeyFails) 35 t.Run("Missing message fails", testSignMessageFlagsMissingMessageFails) 36 t.Run("Malformed message fails", testSignMessageFlagsMalformedMessageFails) 37 } 38 39 func testSignMessageFlagsValidFlagsSucceeds(t *testing.T) { 40 testDir := t.TempDir() 41 42 // given 43 expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir) 44 walletName := vgrand.RandomStr(10) 45 pubKey := vgrand.RandomStr(20) 46 encodedMessage := vgrand.RandomStr(20) 47 48 f := &cmd.SignMessageFlags{ 49 Wallet: walletName, 50 PubKey: pubKey, 51 Message: encodedMessage, 52 PassphraseFile: passphraseFilePath, 53 } 54 55 expectedReq := api.AdminSignMessageParams{ 56 Wallet: walletName, 57 PublicKey: pubKey, 58 EncodedMessage: encodedMessage, 59 } 60 61 // when 62 req, passphrase, err := f.Validate() 63 64 // then 65 require.NoError(t, err) 66 require.NotNil(t, req) 67 assert.Equal(t, expectedReq, req) 68 assert.Equal(t, expectedPassphrase, passphrase) 69 } 70 71 func testSignMessageFlagsMissingWalletFails(t *testing.T) { 72 testDir := t.TempDir() 73 74 // given 75 f := newSignMessageFlags(t, testDir) 76 f.Wallet = "" 77 78 // when 79 req, _, err := f.Validate() 80 81 // then 82 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet")) 83 assert.Empty(t, req) 84 } 85 86 func testSignMessageFlagsMissingPubKeyFails(t *testing.T) { 87 testDir := t.TempDir() 88 89 // given 90 f := newSignMessageFlags(t, testDir) 91 f.PubKey = "" 92 93 // when 94 req, _, err := f.Validate() 95 96 // then 97 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pubkey")) 98 assert.Empty(t, req) 99 } 100 101 func testSignMessageFlagsMissingMessageFails(t *testing.T) { 102 testDir := t.TempDir() 103 104 // given 105 f := newSignMessageFlags(t, testDir) 106 f.Message = "" 107 108 // when 109 req, _, err := f.Validate() 110 111 // then 112 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("message")) 113 assert.Empty(t, req) 114 } 115 116 func testSignMessageFlagsMalformedMessageFails(t *testing.T) { 117 testDir := t.TempDir() 118 119 // given 120 f := newSignMessageFlags(t, testDir) 121 f.Message = vgrand.RandomStr(5) 122 123 // when 124 req, _, err := f.Validate() 125 126 // then 127 assert.ErrorIs(t, err, flags.MustBase64EncodedError("message")) 128 assert.Empty(t, req) 129 } 130 131 func newSignMessageFlags(t *testing.T, testDir string) *cmd.SignMessageFlags { 132 t.Helper() 133 134 _, passphraseFilePath := NewPassphraseFile(t, testDir) 135 walletName := vgrand.RandomStr(10) 136 pubKey := vgrand.RandomStr(20) 137 decodedMessage := []byte(vgrand.RandomStr(20)) 138 139 return &cmd.SignMessageFlags{ 140 Wallet: walletName, 141 PubKey: pubKey, 142 Message: base64.StdEncoding.EncodeToString(decodedMessage), 143 PassphraseFile: passphraseFilePath, 144 } 145 }