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.

180 lines
5.2 KiB

  1. package types
  2. import (
  3. "testing"
  4. acm "github.com/tendermint/tendermint/account"
  5. . "github.com/tendermint/tendermint/common"
  6. _ "github.com/tendermint/tendermint/config/tendermint_test"
  7. ptypes "github.com/tendermint/tendermint/permission/types"
  8. )
  9. var chainID string
  10. func init() {
  11. chainID = config.GetString("chain_id")
  12. }
  13. func TestSendTxSignable(t *testing.T) {
  14. sendTx := &SendTx{
  15. Inputs: []*TxInput{
  16. &TxInput{
  17. Address: []byte("input1"),
  18. Amount: 12345,
  19. Sequence: 67890,
  20. },
  21. &TxInput{
  22. Address: []byte("input2"),
  23. Amount: 111,
  24. Sequence: 222,
  25. },
  26. },
  27. Outputs: []*TxOutput{
  28. &TxOutput{
  29. Address: []byte("output1"),
  30. Amount: 333,
  31. },
  32. &TxOutput{
  33. Address: []byte("output2"),
  34. Amount: 444,
  35. },
  36. },
  37. }
  38. signBytes := acm.SignBytes(chainID, sendTx)
  39. signStr := string(signBytes)
  40. expected := Fmt(`{"chain_id":"%s","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  41. config.GetString("chain_id"))
  42. if signStr != expected {
  43. t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  44. }
  45. }
  46. func TestCallTxSignable(t *testing.T) {
  47. callTx := &CallTx{
  48. Input: &TxInput{
  49. Address: []byte("input1"),
  50. Amount: 12345,
  51. Sequence: 67890,
  52. },
  53. Address: []byte("contract1"),
  54. GasLimit: 111,
  55. Fee: 222,
  56. Data: []byte("data1"),
  57. }
  58. signBytes := acm.SignBytes(chainID, callTx)
  59. signStr := string(signBytes)
  60. expected := Fmt(`{"chain_id":"%s","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
  61. config.GetString("chain_id"))
  62. if signStr != expected {
  63. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  64. }
  65. }
  66. func TestNameTxSignable(t *testing.T) {
  67. nameTx := &NameTx{
  68. Input: &TxInput{
  69. Address: []byte("input1"),
  70. Amount: 12345,
  71. Sequence: 250,
  72. },
  73. Name: "google.com",
  74. Data: "secretly.not.google.com",
  75. Fee: 1000,
  76. }
  77. signBytes := acm.SignBytes(chainID, nameTx)
  78. signStr := string(signBytes)
  79. expected := Fmt(`{"chain_id":"%s","tx":[3,{"data":"secretly.not.google.com","fee":1000,"input":{"address":"696E70757431","amount":12345,"sequence":250},"name":"google.com"}]}`,
  80. config.GetString("chain_id"))
  81. if signStr != expected {
  82. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  83. }
  84. }
  85. func TestBondTxSignable(t *testing.T) {
  86. privKeyBytes := make([]byte, 64)
  87. var privKeyArray [64]byte
  88. copy(privKeyArray[:], privKeyBytes)
  89. privAccount := acm.GenPrivAccountFromPrivKeyBytes(&privKeyArray)
  90. bondTx := &BondTx{
  91. PubKey: privAccount.PubKey.(acm.PubKeyEd25519),
  92. Inputs: []*TxInput{
  93. &TxInput{
  94. Address: []byte("input1"),
  95. Amount: 12345,
  96. Sequence: 67890,
  97. },
  98. &TxInput{
  99. Address: []byte("input2"),
  100. Amount: 111,
  101. Sequence: 222,
  102. },
  103. },
  104. UnbondTo: []*TxOutput{
  105. &TxOutput{
  106. Address: []byte("output1"),
  107. Amount: 333,
  108. },
  109. &TxOutput{
  110. Address: []byte("output2"),
  111. Amount: 444,
  112. },
  113. },
  114. }
  115. signBytes := acm.SignBytes(chainID, bondTx)
  116. signStr := string(signBytes)
  117. expected := Fmt(`{"chain_id":"%s","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  118. config.GetString("chain_id"))
  119. if signStr != expected {
  120. t.Errorf("Unexpected sign string for BondTx. \nGot %s\nExpected %s", signStr, expected)
  121. }
  122. }
  123. func TestUnbondTxSignable(t *testing.T) {
  124. unbondTx := &UnbondTx{
  125. Address: []byte("address1"),
  126. Height: 111,
  127. }
  128. signBytes := acm.SignBytes(chainID, unbondTx)
  129. signStr := string(signBytes)
  130. expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`,
  131. config.GetString("chain_id"))
  132. if signStr != expected {
  133. t.Errorf("Got unexpected sign string for UnbondTx")
  134. }
  135. }
  136. func TestRebondTxSignable(t *testing.T) {
  137. rebondTx := &RebondTx{
  138. Address: []byte("address1"),
  139. Height: 111,
  140. }
  141. signBytes := acm.SignBytes(chainID, rebondTx)
  142. signStr := string(signBytes)
  143. expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`,
  144. config.GetString("chain_id"))
  145. if signStr != expected {
  146. t.Errorf("Got unexpected sign string for RebondTx")
  147. }
  148. }
  149. func TestPermissionsTxSignable(t *testing.T) {
  150. permsTx := &PermissionsTx{
  151. Input: &TxInput{
  152. Address: []byte("input1"),
  153. Amount: 12345,
  154. Sequence: 250,
  155. },
  156. PermArgs: &ptypes.SetBaseArgs{
  157. Address: []byte("address1"),
  158. Permission: 1,
  159. Value: true,
  160. },
  161. }
  162. signBytes := acm.SignBytes(chainID, permsTx)
  163. signStr := string(signBytes)
  164. expected := Fmt(`{"chain_id":"%s","tx":[32,{"args":"[2,{"address":"6164647265737331","permission":1,"value":true}]","input":{"address":"696E70757431","amount":12345,"sequence":250}}]}`,
  165. config.GetString("chain_id"))
  166. if signStr != expected {
  167. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  168. }
  169. }