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.

77 lines
1.8 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 TestMultiSig(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. type signer struct {
  25. key keys.Info
  26. name, pass string
  27. }
  28. cases := []struct {
  29. data string
  30. signers []signer
  31. }{
  32. {"one", []signer{{acct, n, p}}},
  33. {"two", []signer{{acct2, n2, p2}}},
  34. {"both", []signer{{acct, n, p}, {acct2, n2, p2}}},
  35. }
  36. for _, tc := range cases {
  37. tx := NewMulti([]byte(tc.data))
  38. // unsigned version
  39. _, err = tx.Signers()
  40. assert.NotNil(err)
  41. orig, err := tx.TxBytes()
  42. require.Nil(err, "%+v", err)
  43. data := tx.SignBytes()
  44. assert.Equal(tc.data, string(data))
  45. // sign it
  46. for _, s := range tc.signers {
  47. err = cstore.Sign(s.name, s.pass, tx)
  48. require.Nil(err, "%+v", err)
  49. }
  50. // make sure it is proper now
  51. sigs, err := tx.Signers()
  52. require.Nil(err, "%+v", err)
  53. if assert.Equal(len(tc.signers), len(sigs)) {
  54. for i := range sigs {
  55. // This must be refactored...
  56. assert.Equal(tc.signers[i].key.PubKey, sigs[i])
  57. }
  58. }
  59. // the tx bytes should change after this
  60. after, err := tx.TxBytes()
  61. require.Nil(err, "%+v", err)
  62. assert.NotEqual(orig, after, "%X != %X", orig, after)
  63. // sign bytes are the same
  64. data = tx.SignBytes()
  65. assert.Equal(tc.data, string(data))
  66. }
  67. }