Example #1
0
// 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)
		}
	}
}
Example #2
0
func getOutputsHandler(req Request, gateway Gatewayer) Response {
	var addrs []string
	if err := req.DecodeParams(&addrs); err != nil {
		return makeErrorResponse(errCodeInvalidParams, errMsgInvalidParams)
	}

	if len(addrs) == 0 {
		return makeErrorResponse(errCodeInvalidParams, errMsgInvalidParams)
	}

	for i, a := range addrs {
		addrs[i] = strings.Trim(a, " ")
	}

	// validate those addresses
	for _, a := range addrs {
		if _, err := cipher.DecodeBase58Address(a); err != nil {
			return makeErrorResponse(errCodeInvalidParams, fmt.Sprintf("invalid address: %v", a))
		}
	}

	outs := gateway.GetUnspentOutputs(daemon.FbyAddresses(addrs))
	return makeSuccessResponse(req.ID, OutputsResult{outs})
}