From 45713631cf78a842190f8c88e7a6733d899bbaf3 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sun, 18 Aug 2024 11:46:44 +0200 Subject: [PATCH] api: check for an output to be unspent. --- btc/explorer_api.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/btc/explorer_api.go b/btc/explorer_api.go index c36b902..0bed59f 100644 --- a/btc/explorer_api.go +++ b/btc/explorer_api.go @@ -156,10 +156,31 @@ func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) { for _, tx := range txs { for voutIdx, vout := range tx.Vout { if vout.ScriptPubkeyAddr == addr { - vout.Outspend = &Outspend{ + // We need to also make sure that the tx is not + // already spent before including it as unspent. + // + // NOTE: Somehow LND sometimes contructs + // channels with the same keyfamily base hence + // the same pubkey. Needs to be investigated on + // the LND side. + outSpend := &Outspend{ Txid: tx.TXID, Vin: voutIdx, } + url := fmt.Sprintf( + "%s/tx/%s/outspend/%d", a.BaseURL, + tx.TXID, voutIdx, + ) + err := fetchJSON(url, outSpend) + if err != nil { + return nil, err + } + + if outSpend.Spent { + continue + } + + vout.Outspend = outSpend outputs = append(outputs, vout) } }