func getAddrUxOuts(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } addr := r.FormValue("address") if addr == "" { wh.Error400(w, "address is empty") return } cipherAddr, err := cipher.DecodeBase58Address(addr) if err != nil { wh.Error400(w, err.Error()) return } uxs, err := gateway.GetAddrUxOuts(cipherAddr) if err != nil { wh.Error400(w, err.Error()) return } wh.SendOr404(w, uxs) } }
func walletSpendHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { sdst := r.FormValue("dst") if sdst == "" { Error400(w, "Missing destination address \"dst\"") return } dst, err := coin.DecodeBase58Address(sdst) if err != nil { Error400(w, "Invalid destination address") return } sfee := r.FormValue("fee") fee, err := strconv.ParseUint(sfee, 10, 64) if err != nil { Error400(w, "Invalid \"fee\" value") return } scoins := r.FormValue("coins") shours := r.FormValue("hours") coins, err := strconv.ParseUint(scoins, 10, 64) if err != nil { Error400(w, "Invalid \"coins\" value") return } hours, err := strconv.ParseUint(shours, 10, 64) if err != nil { Error400(w, "Invalid \"hours\" value") return } SendOr404(w, gateway.Spend(visor.NewBalance(coins, hours), fee, dst)) } }
func getUxOutByID(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } uxid := r.FormValue("uxid") if uxid == "" { wh.Error400(w, "uxid is empty") return } id, err := cipher.SHA256FromHex(uxid) if err != nil { wh.Error400(w, err.Error()) return } uxout, err := gateway.GetUxOutByID(id) if err != nil { wh.Error400(w, err.Error()) return } if uxout == nil { wh.Error404(w, "not found") return } wh.SendOr404(w, uxout) } }
// Loads/unloads wallets from the wallet directory func walletsReloadHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := gateway.ReloadWallets() if err != nil { Error500(w, err.(error).Error()) } } }
func connectionHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if addr := r.FormValue("addr"); addr == "" { Error404(w) } else { SendOr404(w, gateway.GetConnection(addr)) } } }
func walletTransactionResendHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { hash, err := coin.SHA256FromHex(r.FormValue("hash")) if err != nil { Error404(w) return } SendOr404(w, gateway.ResendTransaction(hash)) } }
func walletAddressTransactionsHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { saddr := r.FormValue("addr") addr, err := coin.DecodeBase58Address(saddr) if err != nil { Error404(w) return } SendOr404(w, gateway.GetAddressTransactions(addr)) } }
func blockchainBlockHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { sseq := r.FormValue("seq") seq, err := strconv.ParseUint(sseq, 10, 64) if err != nil { Error400(w, fmt.Sprintf("Invalid seq value \"%s\"", sseq)) return } SendOr404(w, gateway.GetBlock(seq)) } }
// Returns a wallet by ID if GET. Creates or updates a wallet if POST. func walletHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { SendOr404(w, gateway.GetWallet(wallet.WalletID(r.FormValue("id")))) } else if r.Method == "POST" { walletHandlerPOST(gateway, w, r) } else { Error405(w) } } }
// Saves all loaded wallets func walletsSaveHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { errs := gateway.SaveWallets().(map[wallet.WalletID]error) if len(errs) != 0 { err := "" for id, e := range errs { err += string(id) + ": " + e.Error() } Error500(w, err) } } }
// Create a wallet if no ID provided. Otherwise update an existing wallet. // Name the wallet with "name". func walletHandlerPOST(gateway *daemon.Gateway, w http.ResponseWriter, r *http.Request) { id := wallet.WalletID(r.FormValue("id")) name := r.FormValue("name") if id == "" { // Create wallet iw := gateway.CreateWallet() if iw != nil { w := iw.(wallet.Wallet) w.SetName(name) if err := gateway.SaveWallet(w.GetID()); err != nil { m := "Failed to save wallet after renaming: %v" logger.Critical(m, err) } } SendOr500(w, iw) } else { // Update wallet iw := gateway.GetWallet(id) if iw != nil { w := iw.(wallet.Wallet) w.SetName(name) if err := gateway.SaveWallet(w.GetID()); err != nil { m := "Failed to save wallet after renaming: %v" logger.Critical(m, err) } } SendOr404(w, iw) } }
func blockchainBlocksHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { sstart := r.FormValue("start") start, err := strconv.ParseUint(sstart, 10, 64) if err != nil { Error400(w, fmt.Sprintf("Invalid start value \"%s\"", sstart)) return } send := r.FormValue("end") end, err := strconv.ParseUint(send, 10, 64) if err != nil { Error400(w, fmt.Sprintf("Invalid end value \"%s\"", send)) return } SendOr404(w, gateway.GetBlocks(start, end)) } }
func walletBalanceHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { saddr := r.FormValue("addr") predicted := r.FormValue("predicted") var m interface{} if saddr == "" { m = gateway.GetTotalBalance(predicted != "") } else { addr, err := coin.DecodeBase58Address(saddr) if err != nil { Error400(w, "Invalid address") return } m = gateway.GetBalance(addr, predicted != "") } SendOr404(w, m) } }
func getBlocks(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } sstart := r.FormValue("start") start, err := strconv.ParseUint(sstart, 10, 64) if err != nil { wh.Error400(w, fmt.Sprintf("Invalid start value \"%s\"", sstart)) return } send := r.FormValue("end") end, err := strconv.ParseUint(send, 10, 64) if err != nil { wh.Error400(w, fmt.Sprintf("Invalid end value \"%s\"", send)) return } wh.SendOr404(w, gateway.GetBlocks(start, end)) } }
// get last N blocks func getLastBlocks(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { wh.Error405(w, "") return } num := r.FormValue("num") if num == "" { wh.Error400(w, "Param: num is empty") return } n, err := strconv.ParseUint(num, 10, 64) if err != nil { wh.Error400(w, err.Error()) return } wh.SendOr404(w, gateway.GetLastBlocks(n)) } }
// getOutputsHandler get utxos base on the filters in url params. // mode: GET // url: /outputs?addrs=[:addrs]&hashes=[:hashes] // if addrs and hashes are not specificed, return all unspent outputs. // if both addrs and hashes are specificed, then both those filters are need to be matched. // if only specify one filter, then return outputs match the filter. func getOutputsHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { var addrs []string var hashes []string trimSpace := func(vs []string) []string { for i := range vs { vs[i] = strings.TrimSpace(vs[i]) } return vs } addrStr := r.FormValue("addrs") if addrStr != "" { addrs = trimSpace(strings.Split(addrStr, ",")) } hashStr := r.FormValue("hashes") if hashStr != "" { hashes = trimSpace(strings.Split(hashStr, ",")) } filters := []daemon.OutputsFilter{} if len(addrs) > 0 { filters = append(filters, daemon.FbyAddresses(addrs)) } if len(hashes) > 0 { filters = append(filters, daemon.FbyHashes(hashes)) } outs := gateway.GetUnspentOutputs(filters...) wh.SendOr404(w, outs) } } }
func connectionsHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { SendOr404(w, gateway.GetConnections()) } }
func blockchainHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { SendOr404(w, gateway.GetBlockchainMetadata()) } }
func blockchainProgressHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { wh.SendOr404(w, gateway.GetBlockchainProgress()) } }
// Returns the wallet's balance, both confirmed and predicted. The predicted // balance is the confirmed balance minus the pending spends. func walletBalanceHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id := r.FormValue("id") SendOr404(w, gateway.GetWalletBalance(wallet.WalletID(id))) } }
// Returns all loaded wallets func walletsHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { SendOr404(w, gateway.GetWallets()) } }