You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81-32Lines changed: 81 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ The @bitcoinerlab/discovery library, written in TypeScript, provides a method fo
4
4
5
5
## Features
6
6
7
-
-**Descriptor-Based Data Retrieval:** Retrieves UTXOs, transaction history, and balances for various sources: ranged descriptors, accounts (comprising internal & external descriptors), and addresses (a descriptor specialized for a specific index).
7
+
-**Descriptor-Based Data Retrieval:** Retrieves transaction historyfor various sources, including: ranged descriptors, accounts (comprising internal & external descriptors), and addresses (a descriptor specialized for a specific index).
8
8
9
9
-**Transaction Status Filter:** Offers the ability to filter results by `TxStatus`: `ALL` (including transactions in the mempool), `CONFIRMED` (assuming one confirmation) and `IRREVERSIBLE` (for transactions with more than a user-defined number of confirmations).
10
10
@@ -42,8 +42,7 @@ To get started, follow the steps below:
42
42
host: 'electrum.blockstream.info',
43
43
port: 60002,
44
44
protocol: 'ssl', // 'ssl' and 'tcp' allowed
45
-
network: networks.testnet// Specify the server's network; defaults to 'mainnet' if not specified
46
-
45
+
network: networks.testnet// Specify the server's network; defaults to networks.bitcoin (mainnet)
47
46
});
48
47
```
49
48
@@ -52,62 +51,112 @@ To get started, follow the steps below:
52
51
Please refer to the [Explorer documentation](https://github.com/bitcoinerlab/explorer) for more details.
53
52
54
53
3.**Create the Discovery Class**:
55
-
After creating the explorer client instance, you can create the `Discovery` class, which you will use to query the Blockchain. The `Discovery` class is created using the `DiscoveryFactory` function, passing the previously created explorer instance.
54
+
After creating the explorer client instance, you can create the `Discovery` class, which you will use to query the Blockchain. The `Discovery` class is created using the [`DiscoveryFactory` function](https://bitcoinerlab.com/modules/discovery/api/functions/DiscoveryFactory.html), passing the previously created explorer instance.
// where 'explorer' corresponds to 'esploraExplorer' or 'electrumExplorer' above
61
60
awaitexplorer.connect();
62
61
const discovery =newDiscovery();
62
+
// Perform discovery operations...
63
63
awaitexplorer.close();
64
64
```
65
65
66
-
The `Discovery` constructor accepts an optional object with two properties that are crucial for managing the application's memory usage:
66
+
The [`Discovery` constructor](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#constructor), `new Discovery({ descriptorsCacheSize, outputsPerDescriptorCacheSize })`, accepts an optional object with two properties that are crucial for managing the application's memory usage:
67
67
68
-
-`expressionsCacheSize`: This property represents the cache size limit for descriptor expressions. The cache, implemented using memoizers, serves a dual purpose: it speeds up data queries by avoiding unnecessary recomputations, and it helps maintain immutability. Reaching the limit of the cache size may lead to a loss of immutability and the returned reference may change. This is not a critical issue, as the data is still correct, but it may trigger extra renders in the UI. The default value is 1000, and you can set it to 0 for unbounded caches.
69
-
-`indicesPerExpressionCacheSize`: This property represents the cache size limit for indices per expression, related to the number of addresses in ranged descriptor expressions. Similar to the `expressionsCacheSize`, reaching the limit of this cache size may lead to the same immutability challenges. The default value is 10000, and you can set it to 0 for unbounded caches.
68
+
-`descriptorsCacheSize`: This property represents the cache size limit for descriptor expressions. The cache, implemented using memoizers, serves a dual purpose: it speeds up data derivation by avoiding unnecessary recomputations, and it helps maintain immutability. Reaching the limit of the cache size may lead to a loss of immutability and the returned reference may change. This is not a critical issue, as the returned data is still correct, but it may trigger extra renders in the UI. The default value is `1000`, and you can set it to `0` for unbounded caches.
69
+
-`outputsPerDescriptorCacheSize`: This property represents the cache size limit for indices per expression, related to the number of addresses in ranged descriptor expressions. Similar to the `descriptorsCacheSize`, reaching the limit of this cache size may lead to the same immutability challenges. The default value is `10000`, and you can set it to `0` for unbounded caches.
70
70
71
-
It is important to note that the default values for `expressionsCacheSize` and `indicesPerExpressionCacheSize` should be sufficient for most projects. However, if you expect to work with a large number of descriptor expressions or addresses, you may need to adjust these values accordingly. Conversely, for projects that require minimal resources, you may consider reducing these values to conserve memory.
71
+
It's noteworthy that the default settings for `descriptorsCacheSize` and `outputsPerDescriptorCacheSize` are adequate for most use cases. Yet, for projects handling a large volume of descriptor expressions or addresses, increasing these limits may be necessary. On the flip side, if conserving memory is a priority, particularly for projects with minimal resource needs, consider lowering these values.
72
72
73
73
**Note**: The `connect` method must be run before starting any data queries to the blockchain, and the `close` method should be run after you have completed all necessary queries and no longer need to query the blockchain.
74
74
75
+
75
76
4.**Using the Discovery Methods**
76
-
77
-
Once you've instantiated the `Discovery` class, you can leverage its methods to interact with blockchain data.
78
77
79
-
For instance, if you want to fetch all the addresses from a ranged descriptor expression, execute:
78
+
Once you've instantiated the `Discovery` class, you have access to [a variety of methods](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#fetch) to fetch and derive blockchain data from *Bitcoin Output Descriptors*.
80
79
80
+
Descriptor expressions are a simple language used to describe collections of Bitcoin output scripts. They enable the `Discovery` class to fetch detailed blockchain information about specific outputs. For more comprehensive insights into descriptor expressions, refer to the [BitcoinerLab descriptors module](https://bitcoinerlab.com/modules/descriptors).
81
+
82
+
To initiate (or update) the data retrieval process for addresses associated with a descriptor, whether ranged or fixed, execute [`fetch`](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#fetch):
This method retrieves all associated outputs for a given descriptor. If the descriptor is ranged, you can also specify an index to target a specific output within that range. When dealing with multiple descriptors, use the `descriptors` parameter with an array of strings. See the [`fetch` API documentation](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#fetch) for detailed usage.
85
88
86
-
In this context, the term `expressions` can be a single string or an array of strings. These expressions represent [descriptor expressions](https://bitcoinerlab.com/modules/descriptors). If an expression is ranged, it will retrieve all the related `scriptPubKeys`. Subsequently, you can obtain the UTXOs and balance for that particular expression using the subsequent line.
87
-
88
-
Other beneficial methods include:
89
+
**Note**: To ensure accurate data computations, fetch descriptor data (using the query above) before employing methods like `getUtxos`, `getBalance`, or others described below. An error will alert you when attempting to derive data from descriptors that have not been previously fetched. This ensures you do not compute data based on incomplete information. If you are unsure whether a descriptor has been previously fetched or need to ensure that the data is up-to-date, use [`whenFetched`](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#whenFetched):
// The descriptor data is outdated and may need to be fetched again.
99
+
}
100
+
}
101
+
```
102
+
103
+
If fetch status is verified or known, proceed directly to the data derivation methods:
89
104
90
-
-**Getting the Next Index**:
91
-
If you're dealing with ranged descriptor expressions and want to determine the next available (unused) index, use:
105
+
-**Deriving UTXOs**:
106
+
Use [`getUtxos`](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#getUtxos) to derive all unspent transaction outputs (UTXOs) from the fetched data:
Thismethodisessentialpost-discovery. ForagivenUTXO, ityieldsallpossible`scriptPubKeys`andrelateddatathatcanconsumethespecifiedUTXO. It's worth noting that this method returns an array since multiple valid descriptor expressions might refer to the same output.
110
+
111
+
-**CalculatingBalance**:
112
+
Use [`getBalance`](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#getBalance) to calculate the total balance from the fetched data:
Thisfunction is particularly useful when crafting a transaction capable of expending the UTXO, especially when paired with the @bitcoinerlab/descriptors library.
const index = discovery.getNextIndex({ descriptor });
123
+
```
124
+
Seethe [`getNextIndex`APIdocumentation](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#getNextIndex) for detailed usage.
const history = discovery.getHistory({ descriptors });
138
+
```
139
+
Refertothe [`getHistory`API](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#getHistory) for the details.
140
+
141
+
- **FetchingStandardAccounts**:
142
+
The [`fetchStandardAccounts`](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html#fetchStandardAccounts) method is a helper that automates the common task of retrieving or updating standard accounts (pkh, sh(wpkh), wpkh) associated with a master node. This method saves developers time and eliminates repetitive coding tasks.
143
+
144
+
Efficientlyretrievewalletaccountswith:
106
145
```typescript
107
-
const history = discovery.getHistory({ expressions, network });
146
+
await discovery.fetchStandardAccounts({
147
+
masterNode,
148
+
gapLimit: 20, // The default gap limit
149
+
onAccountUsed: (account) => {
150
+
// Optional: Trigger app updates when an account with transactions is found.
151
+
},
152
+
onAccountChecking: (account) => {
153
+
// Optional: Implement app-specific logic when the check for an account begins.
154
+
}
155
+
});
108
156
```
157
+
Implementthe`onAccountUsed`and`onAccountChecking`callbacksasneededforyourapp's functionality, such as UI updates or logging.
Themethodslistedaboveareonlyapartofallthe`Discovery`class's functionality. For a complete overview of all available methods and their usage, refer to [the API documentation](https://bitcoinerlab.com/modules/discovery/api/classes/_Internal_.Discovery.html).
Copy file name to clipboardExpand all lines: package.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
"name": "@bitcoinerlab/discovery",
3
3
"description": "A TypeScript library for retrieving Bitcoin funds from ranged descriptors, leveraging @bitcoinerlab/explorer for standardized access to multiple blockchain explorers.",
0 commit comments