From ad18eda80428f6253c032b9a013d6b02e3410dc1 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Thu, 3 Mar 2022 12:09:04 +0100 Subject: [PATCH] Fix utxo rest API endpoint --- server/public.go | 7 ++++--- server/public_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/public.go b/server/public.go index 88666553..6e9a2117 100644 --- a/server/public.go +++ b/server/public.go @@ -1050,7 +1050,8 @@ func (s *PublicServer) apiXpub(r *http.Request, apiVersion int) (interface{}, er func (s *PublicServer) apiUtxo(r *http.Request, apiVersion int) (interface{}, error) { var utxo []api.Utxo var err error - if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { + if i := strings.LastIndex(r.URL.Path, "utxo/"); i > 0 { + desc := r.URL.Path[i+5:] onlyConfirmed := false c := r.URL.Query().Get("confirmed") if len(c) > 0 { @@ -1063,11 +1064,11 @@ func (s *PublicServer) apiUtxo(r *http.Request, apiVersion int) (interface{}, er if ec != nil { gap = 0 } - utxo, err = s.api.GetXpubUtxo(r.URL.Path[i+1:], onlyConfirmed, gap) + utxo, err = s.api.GetXpubUtxo(desc, onlyConfirmed, gap) if err == nil { s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub-utxo"}).Inc() } else { - utxo, err = s.api.GetAddressUtxo(r.URL.Path[i+1:], onlyConfirmed) + utxo, err = s.api.GetAddressUtxo(desc, onlyConfirmed) s.metrics.ExplorerViews.With(common.Labels{"action": "api-address-utxo"}).Inc() } if err == nil && apiVersion == apiV1 { diff --git a/server/public_test.go b/server/public_test.go index cb909251..849a2687 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -811,6 +811,15 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { `[{"txid":"3d90d15ed026dc45e19ffb52875ed18fa9e8012ad123d7f7212176e2b0ebdb71","vout":0,"value":"118641975500","height":225494,"confirmations":1,"address":"2N6utyMZfPNUb1Bk8oz7p2JqJrXkq83gegu","path":"m/49'/1'/33'/1/3"}]`, }, }, + { + name: "apiUtxo v2 xpub", + r: newGetRequest(ts.URL + "/api/v2/utxo/" + url.QueryEscape(dbtestdata.TaprootDescriptor)), + status: http.StatusOK, + contentType: "application/json; charset=utf-8", + body: []string{ + `[]`, + }, + }, { name: "apiBalanceHistory Addr2 v2", r: newGetRequest(ts.URL + "/api/v2/balancehistory/mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"),