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.

73 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. )
  18. n, p := "foo", "bar"
  19. n2, p2 := "other", "thing"
  20. acct, err := cstore.Create(n, p, algo)
  21. require.Nil(err, "%+v", err)
  22. acct2, err := cstore.Create(n2, p2, algo)
  23. require.Nil(err, "%+v", err)
  24. cases := []struct {
  25. data string
  26. key keys.Info
  27. name, pass string
  28. }{
  29. {"first", acct, n, p},
  30. {"kehfkhefy8y", acct, n, p},
  31. {"second", acct2, n2, p2},
  32. }
  33. for _, tc := range cases {
  34. tx := New([]byte(tc.data))
  35. // unsigned version
  36. _, err = tx.Signers()
  37. assert.NotNil(err)
  38. orig, err := tx.TxBytes()
  39. require.Nil(err, "%+v", err)
  40. data := tx.SignBytes()
  41. assert.Equal(tc.data, string(data))
  42. // sign it
  43. err = cstore.Sign(tc.name, tc.pass, tx)
  44. require.Nil(err, "%+v", err)
  45. // but not twice
  46. err = cstore.Sign(tc.name, tc.pass, tx)
  47. require.NotNil(err)
  48. // make sure it is proper now
  49. sigs, err := tx.Signers()
  50. require.Nil(err, "%+v", err)
  51. if assert.Equal(1, len(sigs)) {
  52. // This must be refactored...
  53. assert.Equal(tc.key.PubKey, sigs[0])
  54. }
  55. // the tx bytes should change after this
  56. after, err := tx.TxBytes()
  57. require.Nil(err, "%+v", err)
  58. assert.NotEqual(orig, after, "%X != %X", orig, after)
  59. // sign bytes are the same
  60. data = tx.SignBytes()
  61. assert.Equal(tc.data, string(data))
  62. }
  63. }