code.vegaprotocol.io/vega@v0.79.0/wallet/api/helpers_for_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 api_test 17 18 import ( 19 "context" 20 "encoding/json" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/libs/jsonrpc" 25 vgrand "code.vegaprotocol.io/vega/libs/rand" 26 "code.vegaprotocol.io/vega/wallet/api" 27 "code.vegaprotocol.io/vega/wallet/api/node" 28 "code.vegaprotocol.io/vega/wallet/network" 29 "code.vegaprotocol.io/vega/wallet/wallet" 30 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func assertInvalidParams(t *testing.T, errorDetails *jsonrpc.ErrorDetails, expectedErr error) { 36 t.Helper() 37 require.NotNil(t, errorDetails) 38 assert.Equal(t, jsonrpc.ErrorCodeInvalidParams, errorDetails.Code) 39 assert.Equal(t, "Invalid params", errorDetails.Message) 40 assert.Equal(t, expectedErr.Error(), errorDetails.Data) 41 } 42 43 func assertRequestNotPermittedError(t *testing.T, errorDetails *jsonrpc.ErrorDetails, expectedErr error) { 44 t.Helper() 45 require.NotNil(t, errorDetails) 46 assert.Equal(t, api.ErrorCodeRequestNotPermitted, errorDetails.Code) 47 assert.Equal(t, string(api.ApplicationErrorType), errorDetails.Message) 48 assert.Equal(t, expectedErr.Error(), errorDetails.Data) 49 } 50 51 func assertRequestInterruptionError(t *testing.T, errorDetails *jsonrpc.ErrorDetails) { 52 t.Helper() 53 require.NotNil(t, errorDetails) 54 assert.Equal(t, api.ErrorCodeRequestHasBeenInterrupted, errorDetails.Code) 55 assert.Equal(t, string(api.ServerErrorType), errorDetails.Message) 56 assert.Equal(t, api.ErrRequestInterrupted.Error(), errorDetails.Data) 57 } 58 59 func assertConnectionClosedError(t *testing.T, errorDetails *jsonrpc.ErrorDetails) { 60 t.Helper() 61 require.NotNil(t, errorDetails) 62 assert.Equal(t, api.ErrorCodeConnectionHasBeenClosed, errorDetails.Code) 63 assert.Equal(t, string(api.UserErrorType), errorDetails.Message) 64 assert.Equal(t, api.ErrUserCloseTheConnection.Error(), errorDetails.Data) 65 } 66 67 func assertInternalError(t *testing.T, errorDetails *jsonrpc.ErrorDetails, expectedErr error) { 68 t.Helper() 69 require.NotNil(t, errorDetails) 70 assert.Equal(t, jsonrpc.ErrorCodeInternalError, errorDetails.Code) 71 assert.Equal(t, string(api.InternalErrorType), errorDetails.Message) 72 assert.Equal(t, expectedErr.Error(), errorDetails.Data) 73 } 74 75 func assertNetworkError(t *testing.T, errorDetails *jsonrpc.ErrorDetails, expectedErr error) { 76 t.Helper() 77 require.NotNil(t, errorDetails) 78 assert.Equal(t, api.ErrorCodeNodeCommunicationFailed, errorDetails.Code) 79 assert.Equal(t, string(api.NetworkErrorType), errorDetails.Message) 80 assert.Equal(t, expectedErr.Error(), errorDetails.Data) 81 } 82 83 func assertUserRejectionError(t *testing.T, errorDetails *jsonrpc.ErrorDetails, expectedErr error) { 84 t.Helper() 85 require.NotNil(t, errorDetails) 86 assert.Equal(t, api.ErrorCodeRequestHasBeenRejected, errorDetails.Code) 87 assert.Equal(t, string(api.UserErrorType), errorDetails.Message) 88 assert.Equal(t, expectedErr.Error(), errorDetails.Data) 89 } 90 91 func assertApplicationCancellationError(t *testing.T, errorDetails *jsonrpc.ErrorDetails) { 92 t.Helper() 93 require.NotNil(t, errorDetails) 94 assert.Equal(t, api.ErrorCodeRequestHasBeenCancelledByApplication, errorDetails.Code) 95 assert.Equal(t, string(api.ApplicationErrorType), errorDetails.Message) 96 assert.Equal(t, api.ErrApplicationCancelledTheRequest.Error(), errorDetails.Data) 97 } 98 99 func clientContextForTest() (context.Context, string) { 100 traceID := vgrand.RandomStr(5) 101 ctx := context.WithValue(context.Background(), jsonrpc.TraceIDKey, traceID) 102 return ctx, traceID 103 } 104 105 func walletWithPerms(t *testing.T, hostname string, perms wallet.Permissions) wallet.Wallet { 106 t.Helper() 107 108 walletName := vgrand.RandomStr(5) 109 110 w, _, err := wallet.NewHDWallet(walletName) 111 if err != nil { 112 t.Fatalf("could not create wallet for test: %v", err) 113 } 114 115 if _, err = w.GenerateKeyPair(nil); err != nil { 116 t.Fatalf("could not generate a key on the wallet for test: %v", err) 117 } 118 119 if err := w.UpdatePermissions(hostname, perms); err != nil { 120 t.Fatalf("could not update permissions on wallet for test: %v", err) 121 } 122 123 return w 124 } 125 126 func walletWithKey(t *testing.T) (wallet.Wallet, wallet.KeyPair) { 127 t.Helper() 128 129 w, kps := walletWithKeys(t, 1) 130 131 return w, kps[0] 132 } 133 134 func walletWithKeys(t *testing.T, num int) (wallet.Wallet, []wallet.KeyPair) { 135 t.Helper() 136 137 walletName := vgrand.RandomStr(5) 138 139 w, _, err := wallet.NewHDWallet(walletName) 140 if err != nil { 141 t.Fatalf("could not create wallet for test: %v", err) 142 } 143 144 kps := make([]wallet.KeyPair, 0, num) 145 for i := 0; i < num; i++ { 146 kp, err := w.GenerateKeyPair(nil) 147 if err != nil { 148 t.Fatalf("could not generate keys on wallet for test: %v", err) 149 } 150 kps = append(kps, kp) 151 } 152 153 return w, kps 154 } 155 156 func newNetwork(t *testing.T) network.Network { 157 t.Helper() 158 159 return network.Network{ 160 Name: vgrand.RandomStr(5), 161 API: network.APIConfig{ 162 GRPC: network.HostConfig{ 163 Hosts: []string{ 164 "n01.localtest.vega.xyz:3007", 165 }, 166 }, 167 REST: network.HostConfig{ 168 Hosts: []string{ 169 "http://n01.localtest.vega.xyz:3097", 170 }, 171 }, 172 GraphQL: network.HostConfig{ 173 Hosts: []string{ 174 "http://n01.localtest.vega.xyz:3087", 175 }, 176 }, 177 }, 178 } 179 } 180 181 func generateKey(t *testing.T, w wallet.Wallet) wallet.KeyPair { 182 t.Helper() 183 184 kp, err := w.GenerateKeyPair(nil) 185 if err != nil { 186 t.Fatalf("could not generate key for test wallet: %v", err) 187 } 188 return kp 189 } 190 191 func unexpectedNodeSelectorCall(t *testing.T) api.NodeSelectorBuilder { 192 t.Helper() 193 194 return func(hosts []string, _ uint64, _ time.Duration) (node.Selector, error) { 195 t.Fatalf("node selector shouldn't be called") 196 return nil, nil 197 } 198 } 199 200 var ( 201 fakeTransaction = `{"voteSubmission":{"proposalId":"eb2d3902fdda9c3eb6e369f2235689b871c7322cf3ab284dde3e9dfc13863a17","value":"VALUE_YES"}}` 202 fakeMalformedTransaction = `{"voteSubmission":{"proposalId":"not real id","value":"VALUE_YES"}}` 203 ) 204 205 func transactionFromJSON(t *testing.T, JSON string) map[string]any { 206 t.Helper() 207 testTransaction := make(map[string]any) 208 assert.NoError(t, json.Unmarshal([]byte(JSON), &testTransaction)) 209 return testTransaction 210 } 211 212 func testTransaction(t *testing.T) map[string]any { 213 t.Helper() 214 return transactionFromJSON(t, fakeTransaction) 215 } 216 217 func testMalformedTransaction(t *testing.T) map[string]any { 218 t.Helper() 219 return transactionFromJSON(t, fakeMalformedTransaction) 220 }