@exodus/bitcoin-api 5.0.12 → 5.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,15 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [5.0.13](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoin-api@5.0.12...@exodus/bitcoin-api@5.0.13) (2026-11-03)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **bitcoin:** cache authenticated history inputs across scans ([#9174](https://github.com/ExodusMovement/assets/issues/9174)) ([b19d85b](https://github.com/ExodusMovement/assets/commit/b19d85b15f292bfccb023e5801320bc0f6e6f6cf))
12
+
13
+
14
+
6
15
  ## [5.0.12](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoin-api@5.0.11...@exodus/bitcoin-api@5.0.12) (2026-11-03)
7
16
 
8
17
  **Note:** Version bump only for package @exodus/bitcoin-api
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-api",
3
- "version": "5.0.12",
3
+ "version": "5.0.13",
4
4
  "description": "Bitcoin transaction and fee monitors, RPC with the blockchain node, other networking code.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -41,6 +41,7 @@
41
41
  "eventemitter3": "^5.0.4",
42
42
  "events": "^3.3.0",
43
43
  "lodash": "^4.17.21",
44
+ "lru-cache": "^6.0.0",
44
45
  "minimalistic-assert": "^1.0.1",
45
46
  "ms": "^2.1.1",
46
47
  "socket.io-client": "^2.1.1",
@@ -59,5 +60,5 @@
59
60
  "type": "git",
60
61
  "url": "git+https://github.com/ExodusMovement/assets.git"
61
62
  },
62
- "gitHead": "cc266df454f96bb8efd03909701476c54985e3d7"
63
+ "gitHead": "2f4cfccb065a86df38e9721dc0685da88df0cdfe"
63
64
  }
@@ -42,6 +42,7 @@ export class BitcoinMonitorScanner {
42
42
  #gapLimit
43
43
  #refreshGapLimit
44
44
  #addressBatchConcurrency
45
+ #loadTransaction
45
46
  constructor({
46
47
  asset,
47
48
  assetClientInterface,
@@ -83,6 +84,7 @@ export class BitcoinMonitorScanner {
83
84
  this.#gapLimit = gapLimit
84
85
  this.#refreshGapLimit = refreshGapLimit
85
86
  this.#addressBatchConcurrency = addressBatchConcurrency
87
+ this.#loadTransaction = createTransactionLoader((id) => insightClient.fetchRawTx(id))
86
88
  }
87
89
 
88
90
  async #getGapLimit({ assetConfig, refresh }) {
@@ -94,7 +96,7 @@ export class BitcoinMonitorScanner {
94
96
  const asset = this.#asset
95
97
  const assetClientInterface = this.#assetClientInterface
96
98
  const insightClient = this.#insightClient
97
- const loadTransaction = createTransactionLoader((id) => insightClient.fetchRawTx(id))
99
+ const loadTransaction = this.#loadTransaction
98
100
  const txFetchLimit = this.#txFetchLimitResolver({ assetName: asset.name, refresh })
99
101
  const assetName = asset.name
100
102
  const accountState = await assetClientInterface.getAccountState({ assetName, walletAccount })
@@ -797,12 +799,20 @@ export class BitcoinMonitorScanner {
797
799
  ) {
798
800
  continue
799
801
  }
800
- const verified = await verifyTransactionSignatures({
801
- txId: item.txid,
802
- ownedScripts,
803
- transactionData: await loadTransactionWithPrevouts({ txId: item.txid, loadTransaction }),
804
- })
805
- assert(verified === true, `Cannot authenticate outgoing transaction ${item.txid}`)
802
+ try {
803
+ const verified = await verifyTransactionSignatures({
804
+ txId: item.txid,
805
+ ownedScripts,
806
+ transactionData: await loadTransactionWithPrevouts({
807
+ txId: item.txid,
808
+ loadTransaction,
809
+ }),
810
+ })
811
+ assert(verified === true, `Cannot authenticate outgoing transaction ${item.txid}`)
812
+ } catch (error) {
813
+ loadTransaction.delete(item.txid)
814
+ throw error
815
+ }
806
816
  verifiedIds.add(item.txid)
807
817
  }
808
818
  }
@@ -1,12 +1,14 @@
1
- import { memoize } from '@exodus/basic-utils'
1
+ import { restrictConcurrency } from '@exodus/basic-utils'
2
2
  import { crypto, opcodes, payments, script, scriptClassify, Transaction } from '@exodus/bitcoinjs'
3
3
  import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
4
4
  import { ecdsaVerifyHashSync } from '@exodus/crypto/secp256k1'
5
+ import LRU from 'lru-cache'
5
6
  import assert from 'minimalistic-assert'
6
7
 
7
8
  import { verifyTaprootInput } from './verify-taproot-input.js'
8
9
 
9
10
  const CURVE_ORDER = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141')
11
+ const TRANSACTION_FETCH_CONCURRENCY = 4
10
12
 
11
13
  const classifyOutput = scriptClassify.outputFactory()
12
14
  const { P2PKH, P2SH, P2WPKH, P2WSH, P2TR } = scriptClassify.types
@@ -105,32 +107,85 @@ function verifyInput(tx, index, prevouts, requireAllOutputs) {
105
107
  return false
106
108
  }
107
109
 
108
- export function createTransactionLoader(fetchRawTx) {
109
- return memoize(async (id) => {
110
- const raw = await fetchRawTx(id)
111
- assert(typeof raw === 'string' && /^(?:[\da-f]{2})+$/i.test(raw), 'Missing raw transaction')
112
- const tx = Transaction.fromHex(raw)
113
- assert(tx.getId() === id, 'Raw transaction hash mismatch')
114
- return tx
110
+ export function createTransactionLoader(fetchRawTx, { maxBytes = 16 * 1024 * 1024 } = {}) {
111
+ const cache = new LRU({
112
+ max: maxBytes,
113
+ // Allow for per-entry overhead as well as the retained raw bytes.
114
+ length: (bytes) => Math.max(1024, bytes.length),
115
115
  })
116
+ const pending = new Map()
117
+ const fetchTransaction = restrictConcurrency(
118
+ async (id) => {
119
+ const raw = await fetchRawTx(id)
120
+ assert(typeof raw === 'string' && /^(?:[\da-f]{2})+$/i.test(raw), 'Missing raw transaction')
121
+ const bytes = Buffer.from(raw, 'hex')
122
+ assert(Transaction.fromBuffer(bytes).getId() === id, 'Raw transaction hash mismatch')
123
+ return bytes
124
+ },
125
+ { concurrency: TRANSACTION_FETCH_CONCURRENCY }
126
+ )
127
+ const loadTransaction = async (id) => {
128
+ let bytes = cache.get(id)
129
+ if (!bytes) {
130
+ let promise = pending.get(id)
131
+ if (!promise) {
132
+ promise = fetchTransaction(id)
133
+ .then((bytes) => {
134
+ if (pending.get(id) === promise) cache.set(id, bytes)
135
+ return bytes
136
+ })
137
+ .finally(() => {
138
+ if (pending.get(id) === promise) pending.delete(id)
139
+ })
140
+ pending.set(id, promise)
141
+ }
142
+ bytes = await promise
143
+ }
144
+ // The parser creates Buffer views. Copy first so callers cannot mutate the cache
145
+ // or another account's transaction, including when they share an in-flight fetch.
146
+ return Transaction.fromBuffer(Buffer.from(bytes))
147
+ }
148
+ // A txid does not commit to witness data. Failed authentication must allow
149
+ // the next scan to fetch a different witness for the same transaction.
150
+ loadTransaction.delete = (id) => {
151
+ cache.del(id)
152
+ pending.delete(id)
153
+ }
154
+ return loadTransaction
116
155
  }
117
156
 
118
157
  export async function loadTransactionWithPrevouts({ txId, loadTransaction }) {
119
158
  const tx = await loadTransaction(txId)
159
+ if (tx.isCoinbase()) return { tx, prevouts: [] }
160
+ const outpoints = new Set()
161
+ const inputIds = tx.ins.map((input) => {
162
+ const id = Buffer.from(input.hash).reverse().toString('hex')
163
+ const outpoint = `${id}:${input.index}`
164
+ assert(!outpoints.has(outpoint), 'Duplicate transaction input')
165
+ outpoints.add(outpoint)
166
+ return id
167
+ })
120
168
  const prevouts = []
121
- if (!tx.isCoinbase()) {
122
- const outpoints = new Set()
123
- for (const input of tx.ins) {
124
- const id = Buffer.from(input.hash).reverse().toString('hex')
125
- const outpoint = `${id}:${input.index}`
126
- assert(!outpoints.has(outpoint), 'Duplicate transaction input')
127
- outpoints.add(outpoint)
128
- const previous = await loadTransaction(id)
129
- const output = previous.outs[input.index]
130
- assert(output, 'Missing previous output')
131
- prevouts.push(output)
169
+ let nextIndex = 0
170
+ let stopped = false
171
+ const loadPrevouts = async () => {
172
+ while (!stopped && nextIndex < inputIds.length) {
173
+ const index = nextIndex++
174
+ try {
175
+ const previous = await loadTransaction(inputIds[index])
176
+ const output = previous.outs[tx.ins[index].index]
177
+ assert(output, 'Missing previous output')
178
+ prevouts[index] = output
179
+ } catch (error) {
180
+ // Leave already-started reads alone: another account may be using them.
181
+ stopped = true
182
+ throw error
183
+ }
132
184
  }
133
185
  }
186
+ await Promise.all(
187
+ Array.from({ length: Math.min(TRANSACTION_FETCH_CONCURRENCY, inputIds.length) }, loadPrevouts)
188
+ )
134
189
  return { tx, prevouts }
135
190
  }
136
191