Skip to content

[bug]: /v1/transactions doesn't work when adding filter param account #9664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hanxianlong opened this issue Apr 1, 2025 · 4 comments
Open
Labels
bug Unintended code behaviour needs triage

Comments

@hanxianlong
Copy link

Background

I'm trying to filter the transactions that are only relevant to an account...

Your environment

  • lnd 0.18.5
  • Mac OS
  • bitcoind
  • any other relevant environment details

Steps to reproduce

Do this following steps in the polar environment:

step1 import an account into LND with /v2/wallet/accounts/import let's name this account A, and then create an p2tr address
step2: send 0.1 BTC to account A's p2tr address
step3: try to filter account A's transactions with https://lightning.engineering/api-docs/api/lnd/lightning/get-transactions/ and set the query parameters as : /v1/transactions?account=A&
and then send the request, we can get a reponse contains one transaction
step4: change the query paramter account to B、C、D or any other values, we can still get the same response with step3.

I invistigated into the source code, and found the in the 2411 line of the file wallet.go, which is in the btcsuit/btcwallet lib, the following method doesn't use accountName parameter at all.

`func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier,
accountName string, cancel <-chan struct{}) (*GetTransactionsResult, error) {

var start, end int32 = 0, -1

w.chainClientLock.Lock()
chainClient := w.chainClient
w.chainClientLock.Unlock()

// TODO: Fetching block heights by their hashes is inherently racy
// because not all block headers are saved but when they are for SPV the
// db can be queried directly without this.
if startBlock != nil {
	if startBlock.hash == nil {
		start = startBlock.height
	} else {
		if chainClient == nil {
			return nil, errors.New("no chain server client")
		}
		switch client := chainClient.(type) {
		case *chain.RPCClient:
			startHeader, err := client.GetBlockHeaderVerbose(
				startBlock.hash,
			)
			if err != nil {
				return nil, err
			}
			start = startHeader.Height
		case *chain.BitcoindClient:
			var err error
			start, err = client.GetBlockHeight(startBlock.hash)
			if err != nil {
				return nil, err
			}
		case *chain.NeutrinoClient:
			var err error
			start, err = client.GetBlockHeight(startBlock.hash)
			if err != nil {
				return nil, err
			}
		}
	}
}
if endBlock != nil {
	if endBlock.hash == nil {
		end = endBlock.height
	} else {
		if chainClient == nil {
			return nil, errors.New("no chain server client")
		}
		switch client := chainClient.(type) {
		case *chain.RPCClient:
			endHeader, err := client.GetBlockHeaderVerbose(
				endBlock.hash,
			)
			if err != nil {
				return nil, err
			}
			end = endHeader.Height
		case *chain.BitcoindClient:
			var err error
			start, err = client.GetBlockHeight(endBlock.hash)
			if err != nil {
				return nil, err
			}
		case *chain.NeutrinoClient:
			var err error
			end, err = client.GetBlockHeight(endBlock.hash)
			if err != nil {
				return nil, err
			}
		}
	}
}

var res GetTransactionsResult
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
	txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)

	rangeFn := func(details []wtxmgr.TxDetails) (bool, error) {
		// TODO: probably should make RangeTransactions not reuse the
		// details backing array memory.
		dets := make([]wtxmgr.TxDetails, len(details))
		copy(dets, details)
		details = dets

		txs := make([]TransactionSummary, 0, len(details))
		for i := range details {
			txs = append(txs, makeTxSummary(dbtx, w, &details[i]))
		}

		if details[0].Block.Height != -1 {
			blockHash := details[0].Block.Hash
			res.MinedTransactions = append(res.MinedTransactions, Block{
				Hash:         &blockHash,
				Height:       details[0].Block.Height,
				Timestamp:    details[0].Block.Time.Unix(),
				Transactions: txs,
			})
		} else {
			res.UnminedTransactions = txs
		}

		select {
		case <-cancel:
			return true, nil
		default:
			return false, nil
		}
	}

	return w.TxStore.RangeTransactions(txmgrNs, start, end, rangeFn)
})
return &res, err

}
`

Expected behaviour

transactions should filtered by the account.

Actual behaviour

account name filter dosn't work. Could you please give me some suggestions about how to solve this issue, or are there any other workarounds to achieve this requirement?
@guggero thanks :)

@hanxianlong hanxianlong added bug Unintended code behaviour needs triage labels Apr 1, 2025
@guggero
Copy link
Collaborator

guggero commented Apr 1, 2025

Yeah, looks like this was never implemented in the wallet. Don't know about a workaround other than filtering on the client side by matching against the addresses found by https://lightning.engineering/api-docs/api/lnd/wallet-kit/list-addresses/

@ViktorTigerstrom
Copy link
Collaborator

ViktorTigerstrom commented Apr 1, 2025

Just as additional info: I tried replicating this through lncli as well, but then realised that we haven't even added the account filter option to the lncli listchaintxns command, so that'd be an additional feature to add when fixing this issue.

There are also some additional params missing in the lncli version which are present in https://lightning.engineering/api-docs/api/lnd/lightning/get-transactions/#lnrpcgettransactionsrequest

@hanxianlong
Copy link
Author

Just as additional info: I tried replicating this through lncli as well, but then realised that we haven't even added the account filter option to the lncli listchaintxns command, so that'd be an additional feature to add when fixing this issue.

There are also some additional params missing in the lncli version which are present in https://lightning.engineering/api-docs/api/lnd/lightning/get-transactions/#lnrpcgettransactionsrequest

Yeah, I thought missing params was by design before :D

@hanxianlong
Copy link
Author

Yeah, looks like this was never implemented in the wallet. Don't know about a workaround other than filtering on the client side by matching against the addresses found by https://lightning.engineering/api-docs/api/lnd/wallet-kit/list-addresses/

I tried to investigate on the btcsuite/btcwallet source code and tried to fix this problem... however I'm not quite familiar with the code logic. Hope someone can fix this issue several versions later :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unintended code behaviour needs triage
Projects
None yet
Development

No branches or pull requests

3 participants