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.

127 lines
3.3 KiB

  1. package state
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/account"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. //----------------------------------------------------------------------------
  8. // SendTx interface for adding inputs/outputs and adding signatures
  9. func NewSendTx() *types.SendTx {
  10. return &types.SendTx{
  11. Inputs: []*types.TxInput{},
  12. Outputs: []*types.TxOutput{},
  13. }
  14. }
  15. func SendTxAddInput(st AccountGetter, tx *types.SendTx, pubkey account.PubKey, amt uint64) error {
  16. addr := pubkey.Address()
  17. acc := st.GetAccount(addr)
  18. if acc == nil {
  19. return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey)
  20. }
  21. tx.Inputs = append(tx.Inputs, &types.TxInput{
  22. Address: addr,
  23. Amount: amt,
  24. Sequence: uint(acc.Sequence) + 1,
  25. Signature: account.SignatureEd25519{},
  26. PubKey: pubkey,
  27. })
  28. return nil
  29. }
  30. func SendTxAddOutput(tx *types.SendTx, addr []byte, amt uint64) error {
  31. tx.Outputs = append(tx.Outputs, &types.TxOutput{
  32. Address: addr,
  33. Amount: amt,
  34. })
  35. return nil
  36. }
  37. func SignSendTx(tx *types.SendTx, i int, privAccount *account.PrivAccount) error {
  38. if i >= len(tx.Inputs) {
  39. return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
  40. }
  41. tx.Inputs[i].PubKey = privAccount.PubKey
  42. tx.Inputs[i].Signature = privAccount.Sign(tx)
  43. return nil
  44. }
  45. //----------------------------------------------------------------------------
  46. // CallTx interface for creating tx
  47. func NewCallTx(st AccountGetter, from account.PubKey, to, data []byte, amt, gasLimit, fee uint64) (*types.CallTx, error) {
  48. addr := from.Address()
  49. acc := st.GetAccount(addr)
  50. if acc == nil {
  51. return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from)
  52. }
  53. input := &types.TxInput{
  54. Address: addr,
  55. Amount: amt,
  56. Sequence: uint(acc.Sequence) + 1,
  57. Signature: account.SignatureEd25519{},
  58. PubKey: from,
  59. }
  60. return &types.CallTx{
  61. Input: input,
  62. Address: to,
  63. GasLimit: gasLimit,
  64. Fee: fee,
  65. Data: data,
  66. }, nil
  67. }
  68. func SignCallTx(tx *types.CallTx, privAccount *account.PrivAccount) {
  69. tx.Input.PubKey = privAccount.PubKey
  70. tx.Input.Signature = privAccount.Sign(tx)
  71. }
  72. //----------------------------------------------------------------------------
  73. // BondTx interface for adding inputs/outputs and adding signatures
  74. func NewBondTx() *types.BondTx {
  75. return &types.BondTx{
  76. Inputs: []*types.TxInput{},
  77. UnbondTo: []*types.TxOutput{},
  78. }
  79. }
  80. func BondTxAddInput(st AccountGetter, tx *types.BondTx, pubkey account.PubKey, amt uint64) error {
  81. addr := pubkey.Address()
  82. acc := st.GetAccount(addr)
  83. if acc == nil {
  84. return fmt.Errorf("Invalid address %X from pubkey %X", addr, pubkey)
  85. }
  86. tx.Inputs = append(tx.Inputs, &types.TxInput{
  87. Address: addr,
  88. Amount: amt,
  89. Sequence: uint(acc.Sequence) + 1,
  90. Signature: account.SignatureEd25519{},
  91. PubKey: pubkey,
  92. })
  93. return nil
  94. }
  95. func BondTxAddOutput(tx *types.BondTx, addr []byte, amt uint64) error {
  96. tx.UnbondTo = append(tx.UnbondTo, &types.TxOutput{
  97. Address: addr,
  98. Amount: amt,
  99. })
  100. return nil
  101. }
  102. func SignBondTx(tx *types.BondTx, i int, privAccount *account.PrivAccount) error {
  103. if i >= len(tx.Inputs) {
  104. return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
  105. }
  106. tx.Inputs[i].PubKey = privAccount.PubKey
  107. tx.Inputs[i].Signature = privAccount.Sign(tx)
  108. return nil
  109. }