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.

74 lines
1.7 KiB

  1. package tx
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. crypto "github.com/tendermint/go-crypto"
  7. keys "github.com/tendermint/go-crypto/keys"
  8. "github.com/tendermint/go-crypto/keys/cryptostore"
  9. "github.com/tendermint/go-crypto/keys/storage/memstorage"
  10. )
  11. func TestOneSig(t *testing.T) {
  12. assert, require := assert.New(t), require.New(t)
  13. algo := crypto.NameEd25519
  14. cstore := cryptostore.New(
  15. cryptostore.SecretBox,
  16. memstorage.New(),
  17. keys.MustLoadCodec("english"),
  18. )
  19. n, p := "foo", "bar"
  20. n2, p2 := "other", "thing"
  21. acct, _, err := cstore.Create(n, p, algo)
  22. require.Nil(err, "%+v", err)
  23. acct2, _, err := cstore.Create(n2, p2, algo)
  24. require.Nil(err, "%+v", err)
  25. cases := []struct {
  26. data string
  27. key keys.Info
  28. name, pass string
  29. }{
  30. {"first", acct, n, p},
  31. {"kehfkhefy8y", acct, n, p},
  32. {"second", acct2, n2, p2},
  33. }
  34. for _, tc := range cases {
  35. tx := New([]byte(tc.data))
  36. // unsigned version
  37. _, err = tx.Signers()
  38. assert.NotNil(err)
  39. orig, err := tx.TxBytes()
  40. require.Nil(err, "%+v", err)
  41. data := tx.SignBytes()
  42. assert.Equal(tc.data, string(data))
  43. // sign it
  44. err = cstore.Sign(tc.name, tc.pass, tx)
  45. require.Nil(err, "%+v", err)
  46. // but not twice
  47. err = cstore.Sign(tc.name, tc.pass, tx)
  48. require.NotNil(err)
  49. // make sure it is proper now
  50. sigs, err := tx.Signers()
  51. require.Nil(err, "%+v", err)
  52. if assert.Equal(1, len(sigs)) {
  53. // This must be refactored...
  54. assert.Equal(tc.key.PubKey, sigs[0])
  55. }
  56. // the tx bytes should change after this
  57. after, err := tx.TxBytes()
  58. require.Nil(err, "%+v", err)
  59. assert.NotEqual(orig, after, "%X != %X", orig, after)
  60. // sign bytes are the same
  61. data = tx.SignBytes()
  62. assert.Equal(tc.data, string(data))
  63. }
  64. }