2015-01-10 05:41:50 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
)
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Request: {}
|
|
|
|
|
|
|
|
type ResponseGenPrivAccount struct {
|
|
|
|
PrivAccount *account.PrivAccount
|
|
|
|
}
|
|
|
|
|
2015-01-10 05:41:50 -08:00
|
|
|
func GenPrivAccountHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
privAccount := account.GenPrivAccount()
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
WriteAPIResponse(w, API_OK, ResponseGenPrivAccount{privAccount})
|
2015-01-10 05:41:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
// Request: {"address": string}
|
|
|
|
|
|
|
|
type ResponseGetAccount struct {
|
|
|
|
Account *account.Account
|
|
|
|
}
|
|
|
|
|
2015-01-11 23:12:33 -08:00
|
|
|
func GetAccountHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
addressStr := GetParam(r, "address")
|
2015-01-10 05:41:50 -08:00
|
|
|
|
2015-01-11 23:12:33 -08:00
|
|
|
var address []byte
|
2015-01-10 05:41:50 -08:00
|
|
|
var err error
|
2015-01-11 23:12:33 -08:00
|
|
|
binary.ReadJSON(&address, []byte(addressStr), &err)
|
2015-01-10 05:41:50 -08:00
|
|
|
if err != nil {
|
2015-01-11 23:12:33 -08:00
|
|
|
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid address: %v", err))
|
2015-01-10 05:41:50 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-11 23:12:33 -08:00
|
|
|
state := consensusState.GetState()
|
|
|
|
account_ := state.GetAccount(address)
|
2015-01-10 05:41:50 -08:00
|
|
|
|
2015-01-13 21:03:01 -08:00
|
|
|
if account_ == nil {
|
|
|
|
WriteAPIResponse(w, API_OK, struct{}{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
WriteAPIResponse(w, API_OK, ResponseGetAccount{account_})
|
2015-01-10 05:41:50 -08:00
|
|
|
}
|
2015-01-11 14:27:46 -08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
// Request: {}
|
|
|
|
|
|
|
|
type ResponseListAccounts struct {
|
|
|
|
BlockHeight uint
|
|
|
|
Accounts []*account.Account
|
|
|
|
}
|
|
|
|
|
2015-01-11 14:27:46 -08:00
|
|
|
func ListAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
2015-01-11 18:21:17 -08:00
|
|
|
var blockHeight uint
|
|
|
|
var accounts []*account.Account
|
2015-01-11 14:27:46 -08:00
|
|
|
state := consensusState.GetState()
|
2015-01-11 18:21:17 -08:00
|
|
|
blockHeight = state.LastBlockHeight
|
2015-01-11 14:27:46 -08:00
|
|
|
state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
|
2015-01-11 18:21:17 -08:00
|
|
|
accounts = append(accounts, value.(*account.Account))
|
2015-01-11 14:27:46 -08:00
|
|
|
return false
|
|
|
|
})
|
2015-01-11 18:21:17 -08:00
|
|
|
|
2015-03-25 18:06:57 -07:00
|
|
|
WriteAPIResponse(w, API_OK, ResponseListAccounts{blockHeight, accounts})
|
2015-01-11 14:27:46 -08:00
|
|
|
}
|