From 83aa752dff227b775a8515fd46cb1c68591f6aad Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 26 Jun 2024 09:13:29 +0200 Subject: [PATCH] btc: filter address outputs, only return unspent --- btc/explorer_api.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/btc/explorer_api.go b/btc/explorer_api.go index b61038a..a22e942 100644 --- a/btc/explorer_api.go +++ b/btc/explorer_api.go @@ -165,7 +165,26 @@ func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) { } } - return outputs, nil + // Now filter those that are really unspent, because above we get all + // outputs that are sent to the address. + var unspent []*Vout + for idx, vout := range outputs { + url := fmt.Sprintf( + "%s/tx/%s/outspend/%d", a.BaseURL, vout.Outspend.Txid, + idx, + ) + outspend := Outspend{} + err := fetchJSON(url, &outspend) + if err != nil { + return nil, err + } + + if !outspend.Spent { + unspent = append(unspent, vout) + } + } + + return unspent, nil } func (a *ExplorerAPI) Address(outpoint string) (string, error) {