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.

57 lines
1.6 KiB

  1. package rpc
  2. import (
  3. "net/http"
  4. "github.com/tendermint/tendermint/account"
  5. "github.com/tendermint/tendermint/binary"
  6. blk "github.com/tendermint/tendermint/block"
  7. . "github.com/tendermint/tendermint/common"
  8. )
  9. func SignTxHandler(w http.ResponseWriter, r *http.Request) {
  10. txStr := GetParam(r, "tx")
  11. privAccountsStr := GetParam(r, "privAccounts")
  12. var err error
  13. var tx blk.Tx
  14. binary.ReadJSON(&tx, []byte(txStr), &err)
  15. if err != nil {
  16. WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid tx: %v", err))
  17. return
  18. }
  19. privAccounts := binary.ReadJSON([]*account.PrivAccount{}, []byte(privAccountsStr), &err).([]*account.PrivAccount)
  20. if err != nil {
  21. WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid privAccounts: %v", err))
  22. return
  23. }
  24. for i, privAccount := range privAccounts {
  25. if privAccount == nil || privAccount.PrivKey == nil {
  26. WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid (empty) privAccount @%v", i))
  27. return
  28. }
  29. }
  30. switch tx.(type) {
  31. case *blk.SendTx:
  32. sendTx := tx.(*blk.SendTx)
  33. for i, input := range sendTx.Inputs {
  34. input.PubKey = privAccounts[i].PubKey
  35. input.Signature = privAccounts[i].Sign(sendTx)
  36. }
  37. case *blk.BondTx:
  38. bondTx := tx.(*blk.BondTx)
  39. for i, input := range bondTx.Inputs {
  40. input.PubKey = privAccounts[i].PubKey
  41. input.Signature = privAccounts[i].Sign(bondTx)
  42. }
  43. case *blk.UnbondTx:
  44. unbondTx := tx.(*blk.UnbondTx)
  45. unbondTx.Signature = privAccounts[0].Sign(unbondTx).(account.SignatureEd25519)
  46. case *blk.RebondTx:
  47. rebondTx := tx.(*blk.RebondTx)
  48. rebondTx.Signature = privAccounts[0].Sign(rebondTx).(account.SignatureEd25519)
  49. }
  50. WriteAPIResponse(w, API_OK, struct{ blk.Tx }{tx})
  51. }