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.

61 lines
1.5 KiB

  1. package rpc
  2. import (
  3. "net/http"
  4. "github.com/tendermint/tendermint/account"
  5. "github.com/tendermint/tendermint/binary"
  6. . "github.com/tendermint/tendermint/common"
  7. )
  8. func GenPrivAccountHandler(w http.ResponseWriter, r *http.Request) {
  9. privAccount := account.GenPrivAccount()
  10. WriteAPIResponse(w, API_OK, struct {
  11. PrivAccount *account.PrivAccount
  12. }{privAccount})
  13. }
  14. //-----------------------------------------------------------------------------
  15. func GetAccountHandler(w http.ResponseWriter, r *http.Request) {
  16. addressStr := GetParam(r, "address")
  17. var address []byte
  18. var err error
  19. binary.ReadJSON(&address, []byte(addressStr), &err)
  20. if err != nil {
  21. WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid address: %v", err))
  22. return
  23. }
  24. state := consensusState.GetState()
  25. account_ := state.GetAccount(address)
  26. if account_ == nil {
  27. WriteAPIResponse(w, API_OK, struct{}{})
  28. return
  29. }
  30. WriteAPIResponse(w, API_OK, struct {
  31. Account *account.Account
  32. }{account_})
  33. }
  34. //-----------------------------------------------------------------------------
  35. func ListAccountsHandler(w http.ResponseWriter, r *http.Request) {
  36. var blockHeight uint
  37. var accounts []*account.Account
  38. state := consensusState.GetState()
  39. blockHeight = state.LastBlockHeight
  40. state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
  41. accounts = append(accounts, value.(*account.Account))
  42. return false
  43. })
  44. WriteAPIResponse(w, API_OK, struct {
  45. BlockHeight uint
  46. Accounts []*account.Account
  47. }{blockHeight, accounts})
  48. }