code.vegaprotocol.io/vega@v0.79.0/wallet/tests/sc_sign_and_verify_message_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 tests_test 17 18 import ( 19 "encoding/base64" 20 "testing" 21 22 vgrand "code.vegaprotocol.io/vega/libs/rand" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestSignMessage(t *testing.T) { 28 // given 29 home := t.TempDir() 30 _, passphraseFilePath := NewPassphraseFile(t, home) 31 recoveryPhraseFilePath := NewFile(t, home, "recovery-phrase.txt", testRecoveryPhrase) 32 walletName := vgrand.RandomStr(5) 33 34 // when 35 importWalletResp, err := WalletImport(t, []string{ 36 "--home", home, 37 "--output", "json", 38 "--wallet", walletName, 39 "--passphrase-file", passphraseFilePath, 40 "--recovery-phrase-file", recoveryPhraseFilePath, 41 "--version", "2", 42 }) 43 44 // then 45 require.NoError(t, err) 46 AssertImportWallet(t, importWalletResp). 47 WithName(walletName). 48 LocatedUnder(home) 49 50 // given 51 message := []byte("Je ne connaƮtrai pas la peur car la peur tue l'esprit.") 52 encodedMessage := base64.StdEncoding.EncodeToString(message) 53 54 // when 55 signResp, err := SignMessage(t, []string{ 56 "--home", home, 57 "--output", "json", 58 "--wallet", walletName, 59 "--pubkey", importWalletResp.Key.PublicKey, 60 "--message", encodedMessage, 61 "--passphrase-file", passphraseFilePath, 62 }) 63 64 // then 65 require.NoError(t, err) 66 AssertSignMessage(t, signResp). 67 WithSignature("StH82RHxjQ3yTeaSN25b6sJwAyLiq1CDvPWf0X4KIf/WTIjkunkWKn1Gq9ntCoGBfBZIyNfpPtGx0TSZsSrbCA==") 68 69 // when 70 verifyResp, err := VerifyMessage(t, []string{ 71 "--home", home, 72 "--output", "json", 73 "--pubkey", importWalletResp.Key.PublicKey, 74 "--message", encodedMessage, 75 "--signature", signResp.Signature, 76 }) 77 78 // then 79 require.NoError(t, err) 80 AssertVerifyMessage(t, verifyResp).IsValid() 81 } 82 83 func TestSignMessageWithTaintedKey(t *testing.T) { 84 // given 85 home := t.TempDir() 86 _, passphraseFilePath := NewPassphraseFile(t, home) 87 recoveryPhraseFilePath := NewFile(t, home, "recovery-phrase.txt", testRecoveryPhrase) 88 walletName := vgrand.RandomStr(5) 89 90 // when 91 importWalletResp, err := WalletImport(t, []string{ 92 "--home", home, 93 "--output", "json", 94 "--wallet", walletName, 95 "--passphrase-file", passphraseFilePath, 96 "--recovery-phrase-file", recoveryPhraseFilePath, 97 "--version", "2", 98 }) 99 100 // then 101 require.NoError(t, err) 102 AssertImportWallet(t, importWalletResp). 103 WithName(walletName). 104 WithPublicKey("b5fd9d3c4ad553cb3196303b6e6df7f484cf7f5331a572a45031239fd71ad8a0"). 105 LocatedUnder(home) 106 107 // when 108 err = KeyTaint(t, []string{ 109 "--home", home, 110 "--output", "json", 111 "--wallet", walletName, 112 "--pubkey", importWalletResp.Key.PublicKey, 113 "--passphrase-file", passphraseFilePath, 114 }) 115 116 // then 117 require.NoError(t, err) 118 119 // given 120 message := []byte("Je ne connaƮtrai pas la peur car la peur tue l'esprit.") 121 encodedMessage := base64.StdEncoding.EncodeToString(message) 122 123 // when 124 signResp, err := SignMessage(t, []string{ 125 "--home", home, 126 "--output", "json", 127 "--wallet", walletName, 128 "--pubkey", importWalletResp.Key.PublicKey, 129 "--message", encodedMessage, 130 "--passphrase-file", passphraseFilePath, 131 }) 132 133 // then 134 require.EqualError(t, err, "could not sign the message: the public key is tainted") 135 require.Nil(t, signResp) 136 137 // when 138 err = KeyUntaint(t, []string{ 139 "--home", home, 140 "--output", "json", 141 "--wallet", walletName, 142 "--pubkey", importWalletResp.Key.PublicKey, 143 "--passphrase-file", passphraseFilePath, 144 }) 145 146 // then 147 require.NoError(t, err) 148 149 // when 150 signResp, err = SignMessage(t, []string{ 151 "--home", home, 152 "--output", "json", 153 "--wallet", walletName, 154 "--pubkey", importWalletResp.Key.PublicKey, 155 "--message", encodedMessage, 156 "--passphrase-file", passphraseFilePath, 157 }) 158 159 // then 160 require.NoError(t, err) 161 AssertSignMessage(t, signResp). 162 WithSignature("StH82RHxjQ3yTeaSN25b6sJwAyLiq1CDvPWf0X4KIf/WTIjkunkWKn1Gq9ntCoGBfBZIyNfpPtGx0TSZsSrbCA==") 163 }