You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB

  1. package types
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. crypto "github.com/tendermint/go-crypto"
  10. )
  11. func TestLoadValidator(t *testing.T) {
  12. assert, require := assert.New(t), require.New(t)
  13. // create some fixed values
  14. addrStr := "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456"
  15. pubStr := "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  16. privStr := "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  17. addrBytes, _ := hex.DecodeString(addrStr)
  18. pubBytes, _ := hex.DecodeString(pubStr)
  19. privBytes, _ := hex.DecodeString(privStr)
  20. // prepend type byte
  21. pubKey, err := crypto.PubKeyFromBytes(append([]byte{1}, pubBytes...))
  22. require.Nil(err, "%+v", err)
  23. privKey, err := crypto.PrivKeyFromBytes(append([]byte{1}, privBytes...))
  24. require.Nil(err, "%+v", err)
  25. serialized := fmt.Sprintf(`{
  26. "address": "%s",
  27. "pub_key": {
  28. "type": "ed25519",
  29. "data": "%s"
  30. },
  31. "priv_key": {
  32. "type": "ed25519",
  33. "data": "%s"
  34. },
  35. "last_height": 0,
  36. "last_round": 0,
  37. "last_step": 0,
  38. "last_signature": null
  39. }`, addrStr, pubStr, privStr)
  40. val := PrivValidator{}
  41. err = json.Unmarshal([]byte(serialized), &val)
  42. require.Nil(err, "%+v", err)
  43. // make sure the values match
  44. assert.EqualValues(addrBytes, val.Address)
  45. assert.EqualValues(pubKey, val.PubKey)
  46. assert.EqualValues(privKey, val.PrivKey)
  47. // export it and make sure it is the same
  48. out, err := json.Marshal(val)
  49. require.Nil(err, "%+v", err)
  50. assert.JSONEq(serialized, string(out))
  51. }