<p>You are asking a few different questions here, I'll try to help with each:</p>
<p><strong>Wallet filters:</strong> See <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" rel="nofollow noreferrer">BIP37</a> which describes SPV wallets in general. A <a href="https://en.wikipedia.org/wiki/Bloom_filter" rel="nofollow noreferrer">Bloom Filter</a> is generated by the SPV wallet and sent to full nodes on the network. The filter is a compact data structure that represents every public key the SPV wallet is interested in. The full node will check each transaction in blocks and the mempool against this filter and if there is a match, that transaction is sent back to the SPV wallet. BIP37 specifies all the data the filter is tested against but in summary, it includes transaction hashes, public keys, public key hashes, and script hashes.</p>
<p>You can see <a href="https://github.com/bcoin-org/bcoin/blob/2dece4fbe10fffa369e9dd0e8263eb57ba30dcac/lib/wallet/walletdb.js#L251-L293" rel="nofollow noreferrer">here in the bcoin wallet</a> (also JavaScript) where the wallet's public key hashes and watched outpoints are added to the bloom filter.</p>
<p>The <a href="https://bcoin.io/api-docs/?javascript#code-add-filter-code" rel="nofollow noreferrer">bcoin socket API</a> also has a method for adding address hashes to a bloom filter before it is sent to a full node.</p>
<p><strong>HD Wallet Addresses:</strong> To understand HD wallets it's important to read <a href="https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki" rel="nofollow noreferrer">BIP44</a> which describes the recovery process. During BIP44 wallet recovery, the program starts with a seed of entropy (derived from your 12- or 24-word phrase). It then generates account 0, receive and change addresses 0 through 20. Then, starting from a specified block height (just before the wallet was first generated in time) it iterates through the blockchain looking for TXs that match those addresses. Every time it finds a match, it generates another address (the 21st address, and so on...). In SPV mode, each new address would have to be added to the Bloom Filter and sent back to the full node. This actually can cause issues with the wallet sync, and <a href="https://github.com/breadwallet/breadwallet-core/blob/8eb05454df3e2d5cca248b4e24eeffa420c97e3a/bitcoin/BRPeer.c#L59-L85" rel="nofollow noreferrer">some blocks may need to be re-scanned.</a></p>
<p><strong>Notes:</strong></p>
<blockquote>
<p>I am using bitcoinjs-lib to create private key (WIF), addresses and sign the transactions. I would like to fetch all transactions (UTXOs) associated with a wallet.</p>
</blockquote>
<p>In general using a single private key and reusing a single address is considered bad practice. BIP44 is an excellent design for wallets that generate multiple keys and addresses.</p>
<p>If you really only have one address to scan for UTXO you can use an <a href="https://bcoin.io/api-docs/#get-tx-by-address" rel="nofollow noreferrer">address indexer like in bcoin</a> or <a href="https://github.com/bitcoin/bitcoin/pull/14053" rel="nofollow noreferrer">soon-to-be-merged to Bitcoin Core</a>. You may also have some success just using <a href="https://chainquery.com/bitcoin-cli/scantxoutset" rel="nofollow noreferrer">rpc scantxoutset</a> fo your single address.</p>

CC BY-SA 4.0
2020-06-16T14:33:27.810Z
2020-06-16T14:33:27.810Z
pinhead
<p>I am using <a href="https://github.com/bitcoinjs/bitcoinjs-lib" rel="nofollow noreferrer">bitcoinjs-lib</a> to create private key (WIF), addresses and sign the transactions.</p>

<p>I would like to fetch all transactions (UTXOs) associated with a wallet. I found these libs:  <a href="https://github.com/mappum/bitcoin-filter" rel="nofollow noreferrer">bitcoin-filter </a> and <a href="https://github.com/mappum/bitcoin-net" rel="nofollow noreferrer">bitcoin-net </a>.</p>

<p>But I didn't understand the code below:</p>

<pre><code>var filter = new Filter(peers)
filter.add(new Buffer('818895f3dc2c178629d3d2d8fa3ec4a3f8179821', 'hex'))
filter.add(walletObject)
</code></pre>

<p>What parameters does filter receive ? Are they the addresses ? 
If so, how can I find all the used addresses of a HD wallet ?</p>

<spv><bitcoinjs><bip37-bloom-filter>
How to fetch all UTXO from a SPV wallet using bitcoin-filter.js (or other lib)?
le0
# How to fetch all UTXO from a SPV wallet using bitcoin-filter.js (or other lib)?

(Asked by: username_0 on 2020-06-16)

I am using [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib) to create private key (WIF), addresses and sign the transactions.

I would like to fetch all transactions (UTXOs) associated with a wallet. I found these libs: [bitcoin-filter](https://github.com/mappum/bitcoin-filter)  and [bitcoin-net](https://github.com/mappum/bitcoin-net) .

But I didn't understand the code below:

```
var filter = new Filter(peers)
filter.add(new Buffer('818895f3dc2c178629d3d2d8fa3ec4a3f8179821', 'hex'))
filter.add(walletObject)

```

What parameters does filter receive ? Are they the addresses ?
If so, how can I find all the used addresses of a HD wallet ?

**Tags:** spv, bitcoinjs, bip37-bloom-filter


---

## 1 answers

### Answer by username_1 on 2020-06-16. Score: 1 | Accepted answer

You are asking a few different questions here, I'll try to help with each:

**Wallet filters:** See [BIP37](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki) which describes SPV wallets in general. A [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) is generated by the SPV wallet and sent to full nodes on the network. The filter is a compact data structure that represents every public key the SPV wallet is interested in. The full node will check each transaction in blocks and the mempool against this filter and if there is a match, that transaction is sent back to the SPV wallet. BIP37 specifies all the data the filter is tested against but in summary, it includes transaction hashes, public keys, public key hashes, and script hashes.

You can see [here in the bcoin wallet](https://github.com/bcoin-org/bcoin/blob/2dece4fbe10fffa369e9dd0e8263eb57ba30dcac/lib/wallet/walletdb.js#L251-L293) (also JavaScript) where the wallet's public key hashes and watched outpoints are added to the bloom filter.

The [bcoin socket API](https://bcoin.io/api-docs/?javascript#code-add-filter-code) also has a method for adding address hashes to a bloom filter before it is sent to a full node.

**HD Wallet Addresses:** To understand HD wallets it's important to read [BIP44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) which describes the recovery process. During BIP44 wallet recovery, the program starts with a seed of entropy (derived from your 12- or 24-word phrase). It then generates account 0, receive and change addresses 0 through 20. Then, starting from a specified block height (just before the wallet was first generated in time) it iterates through the blockchain looking for TXs that match those addresses. Every time it finds a match, it generates another address (the 21st address, and so on...). In SPV mode, each new address would have to be added to the Bloom Filter and sent back to the full node. This actually can cause issues with the wallet sync, and [some blocks may need to be re-scanned.](https://github.com/breadwallet/breadwallet-core/blob/8eb05454df3e2d5cca248b4e24eeffa420c97e3a/bitcoin/BRPeer.c#L59-L85)

**Notes:**

> I am using bitcoinjs-lib to create private key (WIF), addresses and sign the transactions. I would like to fetch all transactions (UTXOs) associated with a wallet.

In general using a single private key and reusing a single address is considered bad practice. BIP44 is an excellent design for wallets that generate multiple keys and addresses.

If you really only have one address to scan for UTXO you can use an [address indexer like in bcoin](https://bcoin.io/api-docs/#get-tx-by-address) or [soon-to-be-merged to Bitcoin Core](https://github.com/bitcoin/bitcoin/pull/14053). You may also have some success just using [rpc scantxoutset](https://chainquery.com/bitcoin-cli/scantxoutset) fo your single address.


---

