code.vegaprotocol.io/vega@v0.79.0/core/datasource/common/data_signer_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 //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types 17 18 package common_test 19 20 import ( 21 "testing" 22 23 "code.vegaprotocol.io/vega/core/datasource/common" 24 errors "code.vegaprotocol.io/vega/core/datasource/errors" 25 datapb "code.vegaprotocol.io/vega/protos/vega/data/v1" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestSignerIsEmpty(t *testing.T) { 31 t.Run("empty singer", func(t *testing.T) { 32 s := &common.Signer{} 33 isEmpty := s.IsEmpty() 34 assert.True(t, isEmpty) 35 36 s = &common.Signer{ 37 Signer: &common.SignerETHAddress{}, 38 } 39 isEmpty = s.IsEmpty() 40 assert.True(t, isEmpty) 41 42 s = &common.Signer{ 43 Signer: &common.SignerETHAddress{ 44 ETHAddress: nil, 45 }, 46 } 47 isEmpty = s.IsEmpty() 48 assert.True(t, isEmpty) 49 50 s = &common.Signer{ 51 Signer: &common.SignerETHAddress{ 52 ETHAddress: &common.ETHAddress{ 53 Address: "", 54 }, 55 }, 56 } 57 s = &common.Signer{ 58 Signer: &common.SignerETHAddress{ 59 ETHAddress: nil, 60 }, 61 } 62 isEmpty = s.IsEmpty() 63 assert.True(t, isEmpty) 64 65 s = &common.Signer{ 66 Signer: &common.SignerPubKey{ 67 PubKey: nil, 68 }, 69 } 70 isEmpty = s.IsEmpty() 71 assert.True(t, isEmpty) 72 73 s = &common.Signer{ 74 Signer: &common.SignerPubKey{}, 75 } 76 isEmpty = s.IsEmpty() 77 assert.True(t, isEmpty) 78 79 s = &common.Signer{ 80 Signer: &common.SignerPubKey{ 81 PubKey: &common.PubKey{ 82 Key: "", 83 }, 84 }, 85 } 86 isEmpty = s.IsEmpty() 87 assert.True(t, isEmpty) 88 }) 89 } 90 91 func TestCreateSignerFromString(t *testing.T) { 92 signerString := "TESTSTRING" 93 signer := common.CreateSignerFromString(signerString, common.SignerTypePubKey) 94 assert.NotNil(t, signer) 95 assert.NotNil(t, signer.Signer) 96 // Implicitly test `GetSignerPubKey` 97 assert.IsType(t, &common.PubKey{}, signer.GetSignerPubKey()) 98 assert.Equal(t, "TESTSTRING", signer.GetSignerPubKey().Key) 99 100 signerString = "0xTESTSTRING" 101 signer = common.CreateSignerFromString(signerString, common.SignerTypeEthAddress) 102 assert.NotNil(t, signer) 103 assert.NotNil(t, signer.Signer) 104 // Implicitly test `GetSignerETHAddress` 105 assert.IsType(t, &common.ETHAddress{}, signer.GetSignerETHAddress()) 106 assert.Equal(t, "0xTESTSTRING", signer.GetSignerETHAddress().Address) 107 } 108 109 func TestSignersIntoProto(t *testing.T) { 110 signers := []*common.Signer{ 111 { 112 Signer: &common.SignerPubKey{ 113 PubKey: &common.PubKey{ 114 Key: "testsign", 115 }, 116 }, 117 }, 118 { 119 Signer: &common.SignerETHAddress{ 120 ETHAddress: &common.ETHAddress{ 121 Address: "0xtest-ethereum-address", 122 }, 123 }, 124 }, 125 { 126 Signer: &common.SignerETHAddress{ 127 ETHAddress: nil, 128 }, 129 }, 130 } 131 132 protoSigners := common.SignersIntoProto(signers) 133 assert.Equal(t, 3, len(protoSigners)) 134 assert.NotNil(t, protoSigners[0].GetPubKey()) 135 assert.IsType(t, &datapb.Signer_PubKey{}, protoSigners[0].Signer) 136 assert.IsType(t, &datapb.PubKey{}, protoSigners[0].GetPubKey()) 137 assert.Equal(t, "testsign", protoSigners[0].GetPubKey().Key) 138 assert.NotNil(t, protoSigners[1].GetEthAddress()) 139 assert.IsType(t, &datapb.Signer_EthAddress{}, protoSigners[1].Signer) 140 assert.IsType(t, &datapb.ETHAddress{}, protoSigners[1].GetEthAddress()) 141 assert.Equal(t, "0xtest-ethereum-address", protoSigners[1].GetEthAddress().Address) 142 assert.NotNil(t, protoSigners[2].GetEthAddress()) 143 assert.IsType(t, &datapb.Signer_EthAddress{}, protoSigners[1].Signer) 144 assert.IsType(t, &datapb.ETHAddress{}, protoSigners[0].GetEthAddress()) 145 assert.Equal(t, "", protoSigners[2].GetEthAddress().Address) 146 } 147 148 func TestSignersToStringList(t *testing.T) { 149 signers := []*common.Signer{ 150 { 151 Signer: &common.SignerPubKey{ 152 PubKey: &common.PubKey{ 153 Key: "testsign", 154 }, 155 }, 156 }, 157 { 158 Signer: &common.SignerETHAddress{ 159 ETHAddress: &common.ETHAddress{ 160 Address: "0xtest-ethereum-address", 161 }, 162 }, 163 }, 164 { 165 Signer: &common.SignerETHAddress{ 166 ETHAddress: nil, 167 }, 168 }, 169 } 170 171 list := common.SignersToStringList(signers) 172 assert.Equal( 173 t, 174 []string{ 175 "signerPubKey(pubKey(testsign))", 176 "signerETHAddress(ethAddress(0xtest-ethereum-address))", 177 "signerETHAddress(nil)", 178 }, 179 list, 180 ) 181 } 182 183 func TestSignersFromProto(t *testing.T) { 184 t.Run("empty signers list", func(t *testing.T) { 185 protoSigners := []*datapb.Signer{ 186 {}, 187 {}, 188 } 189 190 signers := common.SignersFromProto(protoSigners) 191 assert.Equal(t, 2, len(signers)) 192 for _, s := range signers { 193 assert.Nil(t, s.Signer) 194 assert.Nil(t, s.GetSignerPubKey()) 195 assert.Nil(t, s.GetSignerETHAddress()) 196 } 197 198 protoSigners = []*datapb.Signer{ 199 { 200 Signer: &datapb.Signer_PubKey{ 201 PubKey: &datapb.PubKey{}, 202 }, 203 }, 204 { 205 Signer: &datapb.Signer_EthAddress{ 206 EthAddress: &datapb.ETHAddress{}, 207 }, 208 }, 209 } 210 211 signers = common.SignersFromProto(protoSigners) 212 assert.Equal(t, 2, len(signers)) 213 for i, s := range signers { 214 assert.NotNil(t, s.Signer) 215 if i == 0 { 216 assert.NotNil(t, s.GetSignerPubKey()) 217 assert.Equal(t, "", s.GetSignerPubKey().Key) 218 } else { 219 assert.NotNil(t, s.GetSignerETHAddress()) 220 assert.Equal(t, "", s.GetSignerETHAddress().Address) 221 } 222 } 223 }) 224 225 t.Run("non-empty signers list", func(t *testing.T) { 226 protoSigners := []*datapb.Signer{ 227 { 228 Signer: &datapb.Signer_PubKey{ 229 PubKey: &datapb.PubKey{ 230 Key: "TESTSIGN", 231 }, 232 }, 233 }, 234 { 235 Signer: &datapb.Signer_EthAddress{ 236 EthAddress: &datapb.ETHAddress{ 237 Address: "0xtest-ethereum-address", 238 }, 239 }, 240 }, 241 { 242 Signer: &datapb.Signer_PubKey{ 243 PubKey: &datapb.PubKey{}, 244 }, 245 }, 246 { 247 Signer: &datapb.Signer_EthAddress{ 248 EthAddress: &datapb.ETHAddress{}, 249 }, 250 }, 251 } 252 253 signers := common.SignersFromProto(protoSigners) 254 assert.Equal(t, 4, len(signers)) 255 for i, s := range signers { 256 assert.NotNil(t, s.Signer) 257 if i == 0 { 258 assert.NotNil(t, s.GetSignerPubKey()) 259 assert.Equal(t, "TESTSIGN", s.GetSignerPubKey().Key) 260 } 261 if i == 1 { 262 assert.NotNil(t, s.GetSignerETHAddress()) 263 assert.Equal(t, "0xtest-ethereum-address", s.GetSignerETHAddress().Address) 264 } 265 266 if i == 2 { 267 assert.NotNil(t, s.GetSignerPubKey()) 268 assert.Equal(t, "", s.GetSignerPubKey().Key) 269 } 270 271 if i == 3 { 272 assert.NotNil(t, s.GetSignerETHAddress()) 273 assert.Equal(t, "", s.GetSignerETHAddress().Address) 274 } 275 } 276 }) 277 } 278 279 func TestSignerAsHex(t *testing.T) { 280 t.Run("empty signer", func(t *testing.T) { 281 signer := &common.Signer{} 282 283 hexSigner, err := common.SignerAsHex(signer) 284 assert.ErrorIs(t, errors.ErrSignerUnknownType, err) 285 assert.Nil(t, hexSigner) 286 287 signer = &common.Signer{Signer: &common.SignerPubKey{}} 288 289 hexSigner, err = common.SignerAsHex(signer) 290 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 291 assert.NotNil(t, hexSigner) 292 assert.NotNil(t, hexSigner.Signer) 293 assert.Nil(t, hexSigner.GetSignerPubKey()) 294 295 signer = &common.Signer{ 296 Signer: &common.SignerETHAddress{ 297 ETHAddress: &common.ETHAddress{}, 298 }, 299 } 300 301 hexSigner, err = common.SignerAsHex(signer) 302 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 303 assert.NotNil(t, hexSigner) 304 assert.Nil(t, hexSigner.Signer) 305 assert.Nil(t, hexSigner.GetSignerETHAddress()) 306 307 signer = &common.Signer{ 308 Signer: &common.SignerPubKey{ 309 PubKey: &common.PubKey{}, 310 }, 311 } 312 313 hexSigner, err = common.SignerAsHex(signer) 314 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 315 assert.NotNil(t, hexSigner) 316 assert.Nil(t, hexSigner.Signer) 317 assert.Nil(t, hexSigner.GetSignerPubKey()) 318 }) 319 320 t.Run("non-empty pubKey signer", func(t *testing.T) { 321 signer := &common.Signer{ 322 Signer: &common.SignerPubKey{ 323 PubKey: &common.PubKey{ 324 Key: "TESTSIGN", 325 }, 326 }, 327 } 328 329 hexSigner, err := common.SignerAsHex(signer) 330 assert.Nil(t, err) 331 assert.IsType(t, &common.Signer{}, hexSigner) 332 assert.NotNil(t, hexSigner.Signer) 333 assert.IsType(t, &common.SignerPubKey{}, hexSigner.Signer) 334 assert.NotNil(t, hexSigner.Signer.GetSignerType()) 335 assert.IsType(t, &common.PubKey{}, hexSigner.GetSignerPubKey()) 336 assert.Equal(t, "0x544553545349474e", hexSigner.GetSignerPubKey().Key) 337 }) 338 339 t.Run("non-empty ethAddress signer", func(t *testing.T) { 340 signer := &common.Signer{ 341 Signer: &common.SignerETHAddress{ 342 ETHAddress: &common.ETHAddress{ 343 Address: "0xtest-ethereum-address", 344 }, 345 }, 346 } 347 348 hexSigner, err := common.SignerAsHex(signer) 349 assert.Nil(t, err) 350 assert.IsType(t, &common.Signer{}, hexSigner) 351 assert.NotNil(t, hexSigner.Signer) 352 assert.IsType(t, &common.SignerETHAddress{}, hexSigner.Signer) 353 assert.NotNil(t, hexSigner.Signer.GetSignerType()) 354 assert.IsType(t, &common.ETHAddress{}, hexSigner.GetSignerETHAddress()) 355 assert.Equal(t, "0xtest-ethereum-address", hexSigner.GetSignerETHAddress().Address) 356 }) 357 } 358 359 func TestSignerAsString(t *testing.T) { 360 t.Run("empty signer", func(t *testing.T) { 361 signer := &common.Signer{} 362 signAsString, err := common.SignerAsString(signer) 363 assert.ErrorIs(t, errors.ErrSignerUnknownType, err) 364 assert.Nil(t, signAsString) 365 }) 366 367 t.Run("empty pubkey/eth address signer", func(t *testing.T) { 368 signer := &common.Signer{ 369 Signer: &common.SignerPubKey{ 370 PubKey: nil, 371 }, 372 } 373 374 signAsString, err := common.SignerAsString(signer) 375 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 376 assert.NotNil(t, signAsString) 377 assert.IsType(t, &common.Signer{}, signAsString) 378 assert.Nil(t, signAsString.Signer) 379 380 signer = &common.Signer{ 381 Signer: &common.SignerETHAddress{ 382 ETHAddress: nil, 383 }, 384 } 385 386 signAsString, err = common.SignerAsString(signer) 387 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 388 assert.NotNil(t, signAsString) 389 assert.IsType(t, &common.Signer{}, signAsString) 390 assert.Nil(t, signAsString.Signer) 391 }) 392 393 t.Run("non-empty pubkey signer", func(t *testing.T) { 394 signer := &common.Signer{ 395 Signer: &common.SignerPubKey{ 396 PubKey: &common.PubKey{ 397 Key: "testsign", 398 }, 399 }, 400 } 401 402 signAsString, err := common.SignerAsString(signer) 403 assert.ErrorIs(t, nil, err) 404 assert.NotNil(t, signAsString) 405 assert.IsType(t, &common.Signer{}, signAsString) 406 assert.NotNil(t, signAsString.Signer) 407 assert.NotNil(t, signAsString.GetSignerPubKey()) 408 assert.Equal(t, "testsign", signAsString.GetSignerPubKey().Key) 409 410 signer = &common.Signer{ 411 Signer: &common.SignerPubKey{ 412 PubKey: &common.PubKey{ 413 Key: "0x544553545349474e", 414 }, 415 }, 416 } 417 418 signAsString, err = common.SignerAsString(signer) 419 assert.ErrorIs(t, nil, err) 420 assert.NotNil(t, signAsString) 421 assert.IsType(t, &common.Signer{}, signAsString) 422 assert.NotNil(t, signAsString.Signer) 423 assert.NotNil(t, signAsString.GetSignerPubKey()) 424 assert.Equal(t, "TESTSIGN", signAsString.GetSignerPubKey().Key) 425 }) 426 427 t.Run("non-empty eth address signer", func(t *testing.T) { 428 signer := &common.Signer{ 429 Signer: &common.SignerETHAddress{ 430 ETHAddress: &common.ETHAddress{ 431 Address: "0x746573742d657468657265756d2d61646472657373", 432 }, 433 }, 434 } 435 436 signAsString, err := common.SignerAsString(signer) 437 assert.ErrorIs(t, nil, err) 438 assert.NotNil(t, signAsString) 439 assert.IsType(t, &common.Signer{}, signAsString) 440 assert.NotNil(t, signAsString.Signer) 441 assert.NotNil(t, signAsString.GetSignerETHAddress()) 442 assert.Equal(t, "test-ethereum-address", signAsString.GetSignerETHAddress().Address) 443 }) 444 } 445 446 func TestSignerSerialize(t *testing.T) { 447 t.Run("empty signer", func(t *testing.T) { 448 signer := &common.Signer{ 449 Signer: nil, 450 } 451 452 serialized, err := signer.Serialize() 453 assert.ErrorIs(t, errors.ErrSignerUnknownType, err) 454 assert.Nil(t, serialized) 455 }) 456 457 t.Run("empty pubKey signer", func(t *testing.T) { 458 signer := &common.Signer{ 459 Signer: &common.SignerPubKey{}, 460 } 461 462 serialized, err := signer.Serialize() 463 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 464 assert.Nil(t, serialized) 465 }) 466 467 t.Run("empty ethAddress signer", func(t *testing.T) { 468 signer := &common.Signer{ 469 Signer: &common.SignerETHAddress{}, 470 } 471 472 serialized, err := signer.Serialize() 473 assert.ErrorIs(t, errors.ErrSignerIsEmpty, err) 474 assert.Nil(t, serialized) 475 }) 476 477 t.Run("pubKey signer", func(t *testing.T) { 478 key := "TESTKEY" 479 signer := &common.Signer{ 480 Signer: &common.SignerPubKey{ 481 PubKey: &common.PubKey{ 482 Key: key, 483 }, 484 }, 485 } 486 487 // Test implicitly common.SignerPubKey.Serialize() 488 serialized, err := signer.Serialize() 489 assert.NoError(t, err) 490 assert.Equal(t, uint8(0x0), serialized[0]) 491 assert.Equal(t, key, string(serialized[1:])) 492 }) 493 494 t.Run("ethAddress signer", func(t *testing.T) { 495 address := "test-eth-address" 496 signer := &common.Signer{ 497 Signer: &common.SignerETHAddress{ 498 ETHAddress: &common.ETHAddress{ 499 Address: address, 500 }, 501 }, 502 } 503 504 // Tests implicitly common.SignerETHAddress.Serialize() 505 serialized, err := signer.Serialize() 506 assert.NoError(t, err) 507 assert.Equal(t, uint8(0x1), serialized[0]) 508 assert.Equal(t, address, string(serialized[1:])) 509 }) 510 } 511 512 func TestDeserializeSigner(t *testing.T) { 513 t.Run("empty content", func(t *testing.T) { 514 signer := common.DeserializeSigner([]byte{}) 515 assert.NotNil(t, signer) 516 assert.Nil(t, signer.Signer) 517 }) 518 519 t.Run("non-empty content with no indicative byte", func(t *testing.T) { 520 signer := common.DeserializeSigner([]byte{83, 84, 75, 69, 89}) 521 assert.NotNil(t, signer) 522 assert.Nil(t, signer.Signer) 523 }) 524 525 t.Run("non-empty pubKey with indicative byte", func(t *testing.T) { 526 // Implicitly test DeserializePubKey 527 signer := common.DeserializeSigner([]byte{0, 84, 69, 83, 84, 75, 69, 89}) 528 assert.NotNil(t, signer) 529 assert.NotNil(t, signer.Signer) 530 assert.IsType(t, &common.SignerPubKey{}, signer.Signer) 531 assert.NotNil(t, signer.GetSignerPubKey()) 532 assert.Equal(t, "TESTKEY", signer.GetSignerPubKey().Key) 533 }) 534 535 t.Run("non-empty ethAddress with indicative byte", func(t *testing.T) { 536 // Implicitly test DeserializeETHAddress 537 signer := common.DeserializeSigner([]byte{1, 116, 101, 115, 116, 45, 101, 116, 104, 45, 97, 100, 100, 114, 101, 115, 115}) 538 assert.NotNil(t, signer) 539 assert.NotNil(t, signer.Signer) 540 assert.IsType(t, &common.SignerETHAddress{}, signer.Signer) 541 assert.NotNil(t, signer.GetSignerETHAddress()) 542 assert.Equal(t, "0xtest-eth-address", signer.GetSignerETHAddress().Address) 543 }) 544 } 545 546 func TestNewSigner(t *testing.T) { 547 signer := common.NewSigner(common.SignerTypePubKey) 548 assert.NotNil(t, signer) 549 assert.NotNil(t, signer.Signer) 550 assert.IsType(t, &common.SignerPubKey{}, signer.Signer) 551 552 signer = common.NewSigner(common.SignerTypeEthAddress) 553 assert.NotNil(t, signer) 554 assert.NotNil(t, signer.Signer) 555 assert.IsType(t, &common.SignerETHAddress{}, signer.Signer) 556 557 signer = common.NewSigner(common.SignerType(0)) 558 assert.Nil(t, signer) 559 }